Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / layers / StatusLayerRaster.java @ 29722

History | View | Annotate | Download (14.1 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.andami.PluginServices;
24
import org.gvsig.andami.ui.mdiManager.IWindow;
25
import org.gvsig.app.project.documents.view.gui.IView;
26
import org.gvsig.raster.dataset.FileNotOpenException;
27
import org.gvsig.raster.dataset.IBuffer;
28
import org.gvsig.raster.dataset.NotSupportedExtensionException;
29
import org.gvsig.raster.dataset.RasterDriverException;
30
import org.gvsig.raster.datastruct.ColorTable;
31
import org.gvsig.raster.datastruct.TransparencyRange;
32
import org.gvsig.raster.grid.GridTransparency;
33
import org.gvsig.raster.grid.filter.FilterTypeException;
34
import org.gvsig.raster.grid.filter.RasterFilter;
35
import org.gvsig.raster.grid.filter.RasterFilterList;
36
import org.gvsig.raster.grid.filter.RasterFilterListManager;
37
import org.gvsig.raster.grid.filter.bands.ColorTableFilter;
38
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
39
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
40
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
41
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
42
import org.gvsig.raster.hierarchy.IRasterProperties;
43
import org.gvsig.utils.XMLEntity;
44
import org.gvsig.utils.XMLException;
45

    
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
                
77
        /* (non-Javadoc)
78
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#setXMLEntity(com.iver.utiles.XMLEntity)
79
         */
80
        public void setXMLEntity(XMLEntity xml, IRasterProperties layer)throws XMLException {
81
                this.layer = layer;
82
                //RECUPERAR PROPIEDADES
83
                
84
                //Recuperamos las propiedades de los filtros
85
                for(int i = 0; i < xml.getPropertyCount(); i++) {
86
                        if(xml.getPropertyName(i).startsWith("filter."))        
87
                                filters.add(xml.getPropertyName(i) + "=" + xml.getPropertyValue(i));
88
                }
89
                
90
                //Rangos de transparencia
91
                if (xml.contains("filter.transparency.active") && xml.getBooleanProperty("filter.transparency.active")) {
92
                        int i = 0;
93
                        String value = null;
94
                        while(true) {
95
                                if(xml.contains("filter.transparency.transparencyRange" + i)) {
96
                                        value = xml.getStringProperty("filter.transparency.transparencyRange" + i);
97
                                        int alpha = 0;
98
                                        if(value.indexOf("@") != 0) {
99
                                                try {
100
                                                        alpha = Integer.parseInt(value.substring(value.indexOf("@") + 1, value.length()));
101
                                                } catch (NumberFormatException e) {
102
                                                        alpha = 0;
103
                                                }
104
                                                if(value.indexOf("@") != -1)
105
                                                        value = value.substring(0, value.indexOf("@"));
106
                                        }
107
                                        TransparencyRange range = new TransparencyRange(value);
108
                                        if(alpha != 0)
109
                                                range.setAlpha(alpha);
110
                                        ranges.add(range);
111
                                } else 
112
                                        break;
113
                                i ++;
114
                        }
115
                }
116
                
117
                if (xml.contains("raster.opacityLevel")) {
118
                        transparency = xml.getIntProperty("raster.opacityLevel");
119
                        //Esto soluciona un problema de compatibilidad entre branch v10 y HEAD. Eliminar en futuras versiones
120
                        if(nameClass != null && nameClass.compareTo("com.iver.cit.gvsig.fmap.layers.StatusLayerRaster") == 0)
121
                                transparency = 255 - transparency;
122
                }
123
                                
124
                if (xml.contains("raster.bandR")) 
125
                        bandR = xml.getIntProperty("raster.bandR");
126
                
127
                if (xml.contains("raster.bandG")) 
128
                        bandG = xml.getIntProperty("raster.bandG");
129
                
130
                if (xml.contains("raster.bandB")) 
131
                        bandB = xml.getIntProperty("raster.bandB");
132
                
133
        
134
                int cont = 0;
135
                while(true && cont < 50) {
136
                        if (xml.contains("raster.file" + cont)) {
137
                                files.add(xml.getStringProperty("raster.file" + cont));
138
                                cont++;
139
                        }else 
140
                                break;
141
                }
142
        }
143
        
144

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

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