Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / layers / StatusLayerRaster.java @ 39078

History | View | Annotate | Download (14.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.fmap.raster.layers;
20

    
21
import java.util.ArrayList;
22

    
23
import org.gvsig.raster.dataset.FileNotOpenException;
24
import org.gvsig.raster.dataset.IBuffer;
25
import org.gvsig.raster.dataset.NotSupportedExtensionException;
26
import org.gvsig.raster.dataset.io.RasterDriverException;
27
import org.gvsig.raster.datastruct.ColorTable;
28
import org.gvsig.raster.datastruct.TransparencyRange;
29
import org.gvsig.raster.grid.GridTransparency;
30
import org.gvsig.raster.grid.filter.FilterTypeException;
31
import org.gvsig.raster.grid.filter.RasterFilter;
32
import org.gvsig.raster.grid.filter.RasterFilterList;
33
import org.gvsig.raster.grid.filter.RasterFilterListManager;
34
import org.gvsig.raster.grid.filter.bands.ColorTableFilter;
35
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
36
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
37
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
38
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
39
import org.gvsig.raster.hierarchy.IRasterProperties;
40

    
41
import com.iver.andami.PluginServices;
42
import com.iver.andami.ui.mdiManager.IWindow;
43
import com.iver.cit.gvsig.fmap.layers.XMLException;
44
import com.iver.cit.gvsig.project.documents.view.gui.IView;
45
import com.iver.utiles.XMLEntity;
46

    
47
/**
48
 * Esta clase almacena el estado de un raster en cuanto a las caracteristicas
49
 * de opacidad, bandas y filtros. Estas caracter?sticas pueden ser salvadas a 
50
 * un xml y recuperadas por la capa a trav?s de las funciones setXMLEntity y 
51
 * getXMLEntity
52
 * 
53
 * @author Nacho Brodin (nachobrodin@gmail.com)
54
 */
55
public class StatusLayerRaster implements IStatusRaster {
56

    
57
        public static String                         defaultClass    = "org.gvsig.fmap.raster.layers.StatusLayerRaster";
58
        
59
        //Valor de opacidad global de la imagen
60
        public int                                                transparency    = 255;
61
        
62
        //Rangos de transparencia
63
        public ArrayList                                ranges          = new ArrayList();
64
                
65
        //(Selecci?n de bandas)N?mero de banda  asignado al Rojo, verde y azul         
66
        public int                                                 bandR           = 0;
67
        public int                                                 bandG           = 1;
68
        public int                                                 bandB           = 2;
69
        
70
        //Ficheros cargados en un PxRaster 
71
        public ArrayList                                files           = new ArrayList();
72
        
73
        //Filtros para poder montar una nueva pila
74
        public ArrayList                                filters         = new ArrayList();
75
        private IRasterProperties       layer           = null;
76
        private int                     alphaBandNumber = -1;
77
                
78
        /* (non-Javadoc)
79
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#setXMLEntity(com.iver.utiles.XMLEntity)
80
         */
81
        public void setXMLEntity(XMLEntity xml, IRasterProperties layer)throws XMLException {
82
                this.layer = layer;
83
                //RECUPERAR PROPIEDADES
84
                
85
                //Recuperamos las propiedades de los filtros
86
                for(int i = 0; i < xml.getPropertyCount(); i++) {
87
                        if(xml.getPropertyName(i).startsWith("filter."))        
88
                                filters.add(xml.getPropertyName(i) + "=" + xml.getPropertyValue(i));
89
                }
90
                
91
                //Rangos de transparencia
92
                if (xml.contains("filter.transparency.active") && xml.getBooleanProperty("filter.transparency.active")) {
93
                        int i = 0;
94
                        String value = null;
95
                        while(true) {
96
                                if(xml.contains("filter.transparency.transparencyRange" + i)) {
97
                                        value = xml.getStringProperty("filter.transparency.transparencyRange" + i);
98
                                        int alpha = 0;
99
                                        if(value.indexOf("@") != 0) {
100
                                                try {
101
                                                        alpha = Integer.parseInt(value.substring(value.indexOf("@") + 1, value.length()));
102
                                                } catch (NumberFormatException e) {
103
                                                        alpha = 0;
104
                                                }
105
                                                if(value.indexOf("@") != -1)
106
                                                        value = value.substring(0, value.indexOf("@"));
107
                                        }
108
                                        TransparencyRange range = new TransparencyRange(value);
109
                                        if(alpha != 0)
110
                                                range.setAlpha(alpha);
111
                                        ranges.add(range);
112
                                } else
113
                                        break;
114
                                i ++;
115
                        }
116
                        
117
                        if(xml.contains("filter.transparency.bandnumber")) 
118
                                alphaBandNumber = xml.getIntProperty("filter.transparency.bandnumber");
119
                }
120
                
121
                if (xml.contains("raster.opacityLevel")) {
122
                        transparency = xml.getIntProperty("raster.opacityLevel");
123
                        //Esto soluciona un problema de compatibilidad entre branch v10 y HEAD. Eliminar en futuras versiones
124
                        if(nameClass != null && nameClass.compareTo("com.iver.cit.gvsig.fmap.layers.StatusLayerRaster") == 0)
125
                                transparency = 255 - transparency;
126
                }
127
                                
128
                if (xml.contains("raster.bandR")) 
129
                        bandR = xml.getIntProperty("raster.bandR");
130
                
131
                if (xml.contains("raster.bandG")) 
132
                        bandG = xml.getIntProperty("raster.bandG");
133
                
134
                if (xml.contains("raster.bandB")) 
135
                        bandB = xml.getIntProperty("raster.bandB");
136
                
137
        
138
                int cont = 0;
139
                while(true && cont < 50) {
140
                        if (xml.contains("raster.file" + cont)) {
141
                                files.add(xml.getStringProperty("raster.file" + cont));
142
                                cont++;
143
                        }else 
144
                                break;
145
                }
146
        }
147
        
148

    
149
        /* (non-Javadoc)
150
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getXMLEntity(com.iver.utiles.XMLEntity)
151
         */
152
        public void getXMLEntity(XMLEntity xml, boolean loadClass, IRasterProperties layer) throws XMLException {
153
                this.layer = layer;
154
                //SALVAR PROPIEDADES
155
                
156
                if(loadClass)
157
                        xml.putProperty("raster.class", StatusLayerRaster.defaultClass);
158
                
159
                //Opacidad
160
                GridTransparency transp = layer.getRenderTransparency();
161
                if(transp != null) {
162
                        if(transp.getOpacity() != 255)
163
                                xml.putProperty("raster.opacityLevel", "" + transp.getOpacity());
164
                        
165
                        if(transp.getTransparencyRange().size() > 0 || transp.getAlphaBandNumber() != -1)
166
                                xml.putProperty("filter.transparency.active", "true");
167
                        
168
                        //Rangos de transparencia
169
                        if(transp.getTransparencyRange().size() > 0) {
170
                                for (int i = 0; i < transp.getTransparencyRange().size(); i++) 
171
                                        xml.putProperty("filter.transparency.transparencyRange" + i, "" + ((TransparencyRange)transp.getTransparencyRange().get(i)).getStrEntry() + "@" + ((TransparencyRange)transp.getTransparencyRange().get(i)).getAlpha());
172
                        }
173
                        
174
                        if(transp.getAlphaBandNumber() != -1) 
175
                                xml.putProperty("filter.transparency.bandnumber", transp.getAlphaBandNumber());
176
                }
177
                
178
                //Posici?n de visualizado de bandas
179
                xml.putProperty("raster.bandR", "" + layer.getRenderBands()[0]);
180
                xml.putProperty("raster.bandG", "" + layer.getRenderBands()[1]);
181
                xml.putProperty("raster.bandB", "" + layer.getRenderBands()[2]);
182
                
183
                //Ficheros
184
                if(files != null && layer.getFileCount() != 0) {
185
                        for(int i = 0; i < layer.getFileCount(); i++)
186
                                xml.putProperty("raster.file" + i, "" + layer.getFileName()[i]);
187
                }
188
                
189
                //Salvamos la lista de filtros aplicada en la renderizaci?n.
190
                //Si la lista es null (esto puede ocurrir cuando se abre un 
191
                //proyecto que tiene WCS y no se abre la vista de este) entonces hay que leer los filtros
192
                //que van a salvarse a disco directamente de la variable filters que es la que se ha cargado
193
                //al hacer el setXMLEntity.
194
                
195
                
196
                //Filtros
197
                RasterFilterListManager filterListManager = null;
198
                ArrayList l = null;
199
                if(layer.getRenderFilterList() != null) {
200
                        filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
201
                        l = filterListManager.getStringsFromFilterList();
202
                        if(l == null /*|| l.size() == 0*/)
203
                                l = filters;
204
                }else
205
                        l = filters;
206
                                         
207
                for(int i = 0; i < l.size(); i++)
208
                        xml.putProperty(getElem((String)l.get(i)), getValue((String)l.get(i)));
209
        }
210
        
211
        /**
212
         * Obtiene el valor de una cadena de la forma elemento=valor
213
         * @param cadena 
214
         * @return
215
         */
216
        private String getValue(String cadena) {
217
                if(cadena!=null)
218
                        return cadena.substring(cadena.indexOf("=") + 1, cadena.length());
219
                else 
220
                        return null;
221
        }
222
        
223
        /**
224
         * Obtiene el elemento de una cadena de la forma elemento=valor
225
         * @param cadena 
226
         * @return
227
         */
228
        private String getElem(String cadena) {
229
                if(cadena != null)
230
                        return cadena.substring(0, cadena.indexOf("="));
231
                else 
232
                        return null;
233
        }
234

    
235
        /* (non-Javadoc)
236
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getFilters()
237
         */
238
        public ArrayList getFilters() {
239
                return this.filters;
240
        }
241
        
242
        
243
        /* (non-Javadoc)
244
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#applyStatus(com.iver.cit.gvsig.fmap.layers.RasterFileAdapter)
245
         */
246
        public void applyStatus(FLyrRasterSE layer) throws NotSupportedExtensionException, RasterDriverException, FilterTypeException {
247
                                                                                
248
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
249
                //en el orden correcto
250
                if(layer instanceof FLyrRasterSE) {
251
                        if(files != null && files.size() != 0){
252
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
253
                                for (int i = 1; i < files.size(); i++) 
254
                                        ((FLyrRasterSE)layer).addFile((String)files.get(i));
255
                        }
256
                }
257
                
258
                //Asigna las bandas
259
                int[] renderBands = new int[]{bandR, bandG, bandB};
260
                if(layer.getRender() != null)
261
                        layer.getRender().setRenderBands(renderBands);
262
                                
263
                //Asigna la transparencia
264
                GridTransparency transp = layer.getRender().getLastTransparency(); 
265
                if(transp != null && transparency != 255) { 
266
                        transp.setOpacity(transparency);
267
                        transp.activeTransparency();
268
                }
269
                
270
                //Rangos de transparencia
271
                if(transp != null && ranges != null) {
272
                        transp.setTransparencyRangeList(ranges);
273
                        transp.activeTransparency();
274
                }
275
                
276
                //Banda de transparencia
277
                if(transp != null && alphaBandNumber != -1) {
278
                        transp.setTransparencyBand(alphaBandNumber);
279
                        transp.activeTransparency();
280
                }
281
                
282
                //Filtros
283
                if (layer.getRenderFilterList() != null) {
284
                        RasterFilterListManager filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
285
                        filterListManager.createFilterListFromStrings(filters);
286
                        enhancedCompV10(filters, layer, filterListManager);
287
                        //sortFilters(layer.getRenderFilterList());
288
                }
289
                
290
                //Refrescamos todas las vistas
291
//                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
292
//                for (int i = 0; i < w.length; i++) {
293
//                        if(w[i] != null && w[i] instanceof IView) 
294
//                                ((IView)w[i]).getMapControl().getMapContext().invalidate();        
295
//                }
296
        }
297
        
298
        /**
299
         * M?todo para mantener la compatibilidad con la v10 del realce. El realce de la v10 no
300
         * aporta suficiente informaci?n por lo que se a?ade un filtro de realce generico v1.9
301
         * @param filterArguments
302
         * @param layer
303
         * @param filterListManager
304
         * @throws FilterTypeException
305
         */
306
        public static void enhancedCompV10(ArrayList<String> filterArguments, FLyrRasterSE layer, RasterFilterListManager filterListManager) throws FilterTypeException {
307
                boolean removed = false;
308
                RasterFilterList list = layer.getRenderFilterList();
309
                for (int i = 0; i < list.lenght(); i++) {
310
                        RasterFilter f = list.get(i); 
311
                        if(f instanceof TailTrimFilter || f instanceof LinearEnhancementFilter) { 
312
                                list.remove(f.getName());
313
                                removed = true;
314
                        }
315
                }
316
                if(removed)
317
                        list.controlTypes();
318
                
319
                //Para compatibilidad de realce con proyectos antiguos
320
                for (int i = 0; i < filterArguments.size(); i++) { 
321
                        if(((String)filterArguments.get(i)).startsWith("filter.enhanced.active=true")) {
322
                                EnhancementStretchListManager elm = new EnhancementStretchListManager(filterListManager);
323
                                try {
324
                                        elm.addEnhancedStretchFilter(LinearStretchParams.createStandardParam(layer.getRenderBands(), 0.0, layer.getDataSource().getStatistics(), false), 
325
                                                                                                layer.getDataSource().getStatistics(), 
326
                                                                                                layer.getRender().getRenderBands(), 
327
                                                                                                false);
328
                                } catch (FileNotOpenException e) {
329
                                        //No podemos aplicar el filtro
330
                                } catch (RasterDriverException e) {
331
                                        //No podemos aplicar el filtro
332
                                }
333
                        }
334
                }
335
        }
336
        
337
        /**
338
         * Al cargar filtros desde un proyecto salvado con la V10 los filtros pueden estar desordenados.
339
         * Colocamos los m?s importantes en su posici?n adecuada.
340
         * @throws FilterTypeException 
341
         *
342
         */
343
        private void sortFilters(RasterFilterList list) throws FilterTypeException {
344
                for (int i = 0; i < list.lenght(); i++) {
345
                        RasterFilter f = list.get(i); 
346
                        if(f instanceof TailTrimFilter) {
347
                                list.remove(f.getName());
348
                                list.add(f, 0);
349
                        }
350
                }
351
                
352
                for (int i = 0; i < list.lenght(); i++) {
353
                        RasterFilter f = list.get(i);
354
                        if(f instanceof LinearEnhancementFilter && list.get(0) instanceof TailTrimFilter) {
355
                                list.remove(f.getName());
356
                                list.add(f, 1);
357
                        } else if(f instanceof LinearEnhancementFilter) {
358
                                list.remove(f.getName());
359
                                list.add(f, 0);
360
                        }
361
                }
362
                list.controlTypes();
363
        }
364
        
365
        /*
366
         * (non-Javadoc)
367
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getRenderBands()
368
         */
369
        public int[] getRenderBands() {
370
                return new int[]{bandR, bandG, bandB};
371
        }
372
        
373
        /*
374
         * (non-Javadoc)
375
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterList()
376
         */
377
        public RasterFilterList getFilterList() throws FilterTypeException {
378
                try {
379
                        RasterFilterListManager filterListManager = null;
380
                        if(layer.getRenderFilterList() != null)
381
                                filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
382
                        else {
383
                                RasterFilterList fl = new RasterFilterList();
384
                                fl.setInitDataType(IBuffer.TYPE_BYTE);
385
                                filterListManager = new RasterFilterListManager(fl);
386
                        }
387
                        filterListManager.createFilterListFromStrings(filters);
388
                        return filterListManager.getFilterList();
389
                } catch (NullPointerException e) {
390
                        return null;
391
                }
392
        }
393
        
394
        /*
395
         * (non-Javadoc)
396
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getColorTable()
397
         */
398
        public ColorTable getColorTable() throws FilterTypeException {
399
                RasterFilterList fl = getFilterList();
400
                for (int i = 0; i < fl.lenght(); i++) {
401
                        if(fl.get(i) instanceof ColorTableFilter) 
402
                                return (ColorTable) fl.get(i).getParam("colorTable");
403
                }
404
                return null;
405
        }
406
        
407
        /*
408
         * (non-Javadoc)
409
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getTransparency()
410
         */
411
        public GridTransparency getTransparency() {                
412
                //Asigna la transparencia
413
                GridTransparency transp = new GridTransparency();
414
                if(transparency != 255) { 
415
                        transp.setOpacity(transparency);
416
                        transp.activeTransparency();
417
                }
418
                
419
                //Rangos de transparencia
420
                if(ranges != null) {
421
                        transp.setTransparencyRangeList(ranges);
422
                        transp.activeTransparency();
423
                }
424
                
425
                //Banda de transparencia
426
                if(transp != null && alphaBandNumber != -1) {
427
                        transp.setTransparencyBand(alphaBandNumber);
428
                        transp.activeTransparency();
429
                }
430
                
431
                return (transp.isTransparencyActive()) ? transp : null;
432
        }
433
        
434
        /*
435
         * (non-Javadoc)
436
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterArguments()
437
         */
438
        public ArrayList getFilterArguments() {
439
                return filters;
440
        }
441
        
442
        String nameClass = null;
443
        /**
444
         * Asigna el nombre de la clase que se ha leido desde el proyecto
445
         * @param nameClass
446
         */
447
        public void setNameClass(String nameClass) {
448
                this.nameClass = nameClass;                
449
        }
450
}