Statistics
| Revision:

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

History | View | Annotate | Download (16.8 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.grid.GridTransparency;
32
import org.gvsig.raster.grid.filter.FilterTypeException;
33
import org.gvsig.raster.grid.filter.RasterFilter;
34
import org.gvsig.raster.grid.filter.RasterFilterList;
35
import org.gvsig.raster.grid.filter.RasterFilterListManager;
36
import org.gvsig.raster.grid.filter.bands.ColorTableFilter;
37
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
38
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
39
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
40
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
41
import org.gvsig.raster.hierarchy.IRasterProperties;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynobject.DynStruct;
44
import org.gvsig.tools.persistence.PersistenceManager;
45
import org.gvsig.tools.persistence.PersistentState;
46
import org.gvsig.tools.persistence.exception.PersistenceException;
47

    
48

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

    
59
        public static String                         defaultClass    = "org.gvsig.fmap.raster.layers.StatusLayerRaster";
60
        
61
        //Valor de opacidad global de la imagen
62
        public int                                                transparency    = 255;
63
        
64
        //Rangos de transparencia
65
        public ArrayList                                ranges          = new ArrayList();
66
                
67
        //(Selecci?n de bandas)N?mero de banda  asignado al Rojo, verde y azul         
68
        public int                                                 bandR           = 0;
69
        public int                                                 bandG           = 1;
70
        public int                                                 bandB           = 2;
71
        
72
        //Ficheros cargados en un PxRaster 
73
        public ArrayList                                files           = new ArrayList();
74
        
75
        //Filtros para poder montar una nueva pila
76
        public ArrayList                                filters         = new ArrayList();
77
        private IRasterProperties       layer           = null;
78
        private int                     alphaBandNumber = -1;
79
        
80
        //Utilizado para la persistencia
81
        private boolean                                 loadClass                = false;
82
        
83
        /* (non-Javadoc)
84
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#setXMLEntity(com.iver.utiles.XMLEntity)
85
         */
86
        public void loadFromState(PersistentState state, IRasterProperties layer)throws PersistenceException {
87
//                this.layer = layer;
88
//                //RECUPERAR PROPIEDADES
89
//                
90
//                //Recuperamos las propiedades de los filtros
91
//                for(int i = 0; i < xml.getPropertyCount(); i++) {
92
//                        if(xml.getPropertyName(i).startsWith("filter."))        
93
//                                filters.add(xml.getPropertyName(i) + "=" + xml.getPropertyValue(i));
94
//                }
95
//                
96
//                //Rangos de transparencia
97
//                if (xml.contains("filter.transparency.active") && xml.getBooleanProperty("filter.transparency.active")) {
98
//                        int i = 0;
99
//                        String value = null;
100
//                        while(true) {
101
//                                if(xml.contains("filter.transparency.transparencyRange" + i)) {
102
//                                        value = xml.getStringProperty("filter.transparency.transparencyRange" + i);
103
//                                        int alpha = 0;
104
//                                        if(value.indexOf("@") != 0) {
105
//                                                try {
106
//                                                        alpha = Integer.parseInt(value.substring(value.indexOf("@") + 1, value.length()));
107
//                                                } catch (NumberFormatException e) {
108
//                                                        alpha = 0;
109
//                                                }
110
//                                                if(value.indexOf("@") != -1)
111
//                                                        value = value.substring(0, value.indexOf("@"));
112
//                                        }
113
//                                        TransparencyRange range = new TransparencyRange(value);
114
//                                        if(alpha != 0)
115
//                                                range.setAlpha(alpha);
116
//                                        ranges.add(range);
117
//                                } else 
118
//                                        break;
119
//                                i ++;
120
//                        }
121
//                }
122
//                
123
//                if(xml.contains("filter.transparency.bandnumber")) 
124
//                        alphaBandNumber = xml.getIntProperty("filter.transparency.bandnumber");
125
//                
126
//                if (xml.contains("raster.opacityLevel")) {
127
//                        transparency = xml.getIntProperty("raster.opacityLevel");
128
//                        //Esto soluciona un problema de compatibilidad entre branch v10 y HEAD. Eliminar en futuras versiones
129
//                        if(nameClass != null && nameClass.compareTo("com.iver.cit.gvsig.fmap.layers.StatusLayerRaster") == 0)
130
//                                transparency = 255 - transparency;
131
//                }
132
//                                
133
//                if (xml.contains("raster.bandR")) 
134
//                        bandR = xml.getIntProperty("raster.bandR");
135
//                
136
//                if (xml.contains("raster.bandG")) 
137
//                        bandG = xml.getIntProperty("raster.bandG");
138
//                
139
//                if (xml.contains("raster.bandB")) 
140
//                        bandB = xml.getIntProperty("raster.bandB");
141
//                
142
//        
143
//                int cont = 0;
144
//                while(true && cont < 50) {
145
//                        if (xml.contains("raster.file" + cont)) {
146
//                                files.add(xml.getStringProperty("raster.file" + cont));
147
//                                cont++;
148
//                        }else 
149
//                                break;
150
//                }
151
        }        
152
        
153
        
154
        public void loadFromState(PersistentState state)
155
                        throws PersistenceException {
156
                //Recuperamos las propiedades de los filtros
157
                filters = new ArrayList(state.getList("raster_filters"));
158
//                for(int i = 0; i < xml.getPropertyCount(); i++) {
159
//                        if(xml.getPropertyName(i).startsWith("filter."))        
160
//                                filters.add(xml.getPropertyName(i) + "=" + xml.getPropertyValue(i));
161
//                }                
162
                
163
                
164
                
165
                if(state.hasValue("filter_transparency_bandnumber")) 
166
                        alphaBandNumber = state.getInt("filter_transparency_bandnumber");
167
                
168
                if (state.hasValue("raster_opacityLevel")) {
169
                        transparency = state.getInt("raster_opacityLevel");
170
                        //Esto soluciona un problema de compatibilidad entre branch v10 y HEAD. Eliminar en futuras versiones
171
                        if(nameClass != null && nameClass.compareTo("com.iver.cit.gvsig.fmap.layers.StatusLayerRaster") == 0)
172
                                transparency = 255 - transparency;
173
                }
174
                                
175
                if (state.hasValue("raster_bandR")) 
176
                        bandR = state.getInt("raster_bandR");
177
                
178
                if (state.hasValue("raster.bandG")) 
179
                        bandG = state.getInt("raster_bandG");
180
                
181
                if (state.hasValue("raster_bandB")) 
182
                        bandB = state.getInt("raster_bandB");
183
                
184
        
185
                int cont = 0;
186
                while(true && cont < 50) {
187
                        if (state.hasValue("raster_file" + cont)) {
188
                                files.add(state.getString("raster_file" + cont));
189
                                cont++;
190
                        }else 
191
                                break;
192
                }
193
                
194
        }
195

    
196
        public void saveToState(PersistentState state) throws PersistenceException {
197
                if(loadClass)
198
                        state.set("raster_class", StatusLayerRaster.defaultClass);
199
                
200
                //Opacidad
201
                GridTransparency transp = layer.getRenderTransparency();
202
                if(transp != null) {
203
                        state.set("transp", transp);                
204
                }
205
                
206
                //Posici?n de visualizado de bandas
207
                state.set("raster_bandR", layer.getRenderBands()[0]);
208
                state.set("raster_bandG", layer.getRenderBands()[1]);
209
                state.set("raster_bandB", layer.getRenderBands()[2]);
210
                
211
                //Ficheros
212
                if(files != null && layer.getFileCount() != 0) {
213
                        ArrayList files = new ArrayList();
214
                        for(int i = 0; i < layer.getFileCount(); i++){
215
                                files.add(layer.getFileName()[i]);
216
                        }
217
                        state.set("raster_file", files);
218
                }
219
                
220
                //Salvamos la lista de filtros aplicada en la renderizaci?n.
221
                //Si la lista es null (esto puede ocurrir cuando se abre un 
222
                //proyecto que tiene WCS y no se abre la vista de este) entonces hay que leer los filtros
223
                //que van a salvarse a disco directamente de la variable filters que es la que se ha cargado
224
                //al hacer el setXMLEntity.
225
                
226
                
227
                //Filtros
228
                RasterFilterListManager filterListManager = null;
229
                ArrayList l = null;
230
                if(layer.getRenderFilterList() != null) {
231
                        filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
232
                        l = filterListManager.getStringsFromFilterList();
233
                        if(l == null /*|| l.size() == 0*/)
234
                                l = filters;
235
                }else
236
                        l = filters;
237
                                         
238
//                for(int i = 0; i < l.size(); i++){
239
//                        state.set(getElem((String)l.get(i)), getValue((String)l.get(i)));
240
//                }
241
                state.set("raster_filters", l);
242
                
243
        }
244

    
245
        /* (non-Javadoc)
246
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getXMLEntity(com.iver.utiles.XMLEntity)
247
         */
248
        public void setStateProperties(boolean loadClass, IRasterProperties layer) throws PersistenceException {
249
                this.layer = layer;
250
                this.loadClass = loadClass;
251
        }
252
        
253
        public static void registerPersistent() {
254
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
255
                DynStruct definition = manager.addDefinition(
256
                                StatusLayerRaster.class,
257
                                "StatusLayerRaster_Persistent",
258
                                "StatusLayerRaster Persistent definition",
259
                                null, 
260
                                null
261
                );
262
                definition.addDynFieldString("raster_class");
263
                definition.addDynFieldInt("raster_opacityLevel");
264
                definition.addDynFieldBoolean("filter_transparency_active");
265
                definition.addDynFieldInt("filter_transparency_transparencyRange");
266
                definition.addDynFieldInt("filter_transparency_bandnumber");
267
                
268
                definition.addDynFieldInt("raster_bandR").setMandatory(true);
269
                definition.addDynFieldInt("raster_bandG").setMandatory(true);
270
                definition.addDynFieldInt("raster_bandB").setMandatory(true);
271
                
272
                definition.addDynFieldList("raster_file");
273
                
274
                definition.addDynFieldList("raster_filters");
275
        }
276
        
277
        /**
278
         * Obtiene el valor de una cadena de la forma elemento=valor
279
         * @param cadena 
280
         * @return
281
         */
282
        private String getValue(String cadena) {
283
                if(cadena!=null)
284
                        return cadena.substring(cadena.indexOf("=") + 1, cadena.length());
285
                else 
286
                        return null;
287
        }
288
        
289
        /**
290
         * Obtiene el elemento de una cadena de la forma elemento=valor
291
         * @param cadena 
292
         * @return
293
         */
294
        private String getElem(String cadena) {
295
                if(cadena != null)
296
                        return cadena.substring(0, cadena.indexOf("="));
297
                else 
298
                        return null;
299
        }
300

    
301
        /* (non-Javadoc)
302
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getFilters()
303
         */
304
        public ArrayList getFilters() {
305
                return this.filters;
306
        }
307
        
308
        
309
        /* (non-Javadoc)
310
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#applyStatus(com.iver.cit.gvsig.fmap.layers.RasterFileAdapter)
311
         */
312
        public void applyStatus(FLyrRasterSE layer) throws NotSupportedExtensionException, RasterDriverException, FilterTypeException {
313
                                                                                
314
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
315
                //en el orden correcto
316
                if(layer instanceof FLyrRasterSE) {
317
                        if(files != null && files.size() != 0){
318
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
319
                                for (int i = 1; i < files.size(); i++) 
320
                                        ((FLyrRasterSE)layer).addFile((String)files.get(i));
321
                        }
322
                }
323
                
324
                //Asigna las bandas
325
                int[] renderBands = new int[]{bandR, bandG, bandB};
326
                if(layer.getRender() != null)
327
                        layer.getRender().setRenderBands(renderBands);
328
                                
329
                //Asigna la transparencia
330
                GridTransparency transp = layer.getRender().getLastTransparency(); 
331
                if(transp != null && transparency != 255) { 
332
                        transp.setOpacity(transparency);
333
                        transp.activeTransparency();
334
                }
335
                
336
                //Rangos de transparencia
337
                if(transp != null && ranges != null) {
338
                        transp.setTransparencyRangeList(ranges);
339
                        transp.activeTransparency();
340
                }
341
                
342
                //Banda de transparencia
343
                if(transp != null && alphaBandNumber != -1) {
344
                        transp.setTransparencyBand(alphaBandNumber);
345
                        transp.activeTransparency();
346
                }
347
                
348
                //Filtros
349
                if (layer.getRenderFilterList() != null) {
350
                        RasterFilterListManager filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
351
                        filterListManager.createFilterListFromStrings(filters);
352
                        enhancedCompV10(filters, layer, filterListManager);
353
                        //sortFilters(layer.getRenderFilterList());
354
                }
355
                
356
                //Refrescamos todas las vistas
357
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
358
                for (int i = 0; i < w.length; i++) {
359
                        if(w[i] != null && w[i] instanceof IView) 
360
                                ((IView)w[i]).getMapControl().getMapContext().invalidate();        
361
                }
362
        }
363
        
364
        /**
365
         * M?todo para mantener la compatibilidad con la v10 del realce. El realce de la v10 no
366
         * aporta suficiente informaci?n por lo que se a?ade un filtro de realce generico v1.9
367
         * @param filterArguments
368
         * @param layer
369
         * @param filterListManager
370
         * @throws FilterTypeException
371
         */
372
        public static void enhancedCompV10(ArrayList<String> filterArguments, FLyrRasterSE layer, RasterFilterListManager filterListManager) throws FilterTypeException {
373
                boolean removed = false;
374
                RasterFilterList list = layer.getRenderFilterList();
375
                for (int i = 0; i < list.lenght(); i++) {
376
                        RasterFilter f = list.get(i); 
377
                        if(f instanceof TailTrimFilter || f instanceof LinearEnhancementFilter) { 
378
                                list.remove(f.getName());
379
                                removed = true;
380
                        }
381
                }
382
                if(removed)
383
                        list.controlTypes();
384
                
385
                //Para compatibilidad de realce con proyectos antiguos
386
                for (int i = 0; i < filterArguments.size(); i++) { 
387
                        if(((String)filterArguments.get(i)).startsWith("filter.enhanced.active=true")) {
388
                                EnhancementStretchListManager elm = new EnhancementStretchListManager(filterListManager);
389
                                try {
390
                                        elm.addEnhancedStretchFilter(LinearStretchParams.createStandardParam(layer.getRenderBands(), 0.0, layer.getDataSource().getStatistics(), false), 
391
                                                                                                layer.getDataSource().getStatistics(), 
392
                                                                                                layer.getRender().getRenderBands(), 
393
                                                                                                false);
394
                                } catch (FileNotOpenException e) {
395
                                        //No podemos aplicar el filtro
396
                                } catch (RasterDriverException e) {
397
                                        //No podemos aplicar el filtro
398
                                }
399
                        }
400
                }
401
        }
402
        
403
        /**
404
         * Al cargar filtros desde un proyecto salvado con la V10 los filtros pueden estar desordenados.
405
         * Colocamos los m?s importantes en su posici?n adecuada.
406
         * @throws FilterTypeException 
407
         *
408
         */
409
        private void sortFilters(RasterFilterList list) throws FilterTypeException {
410
                for (int i = 0; i < list.lenght(); i++) {
411
                        RasterFilter f = list.get(i); 
412
                        if(f instanceof TailTrimFilter) {
413
                                list.remove(f.getName());
414
                                list.add(f, 0);
415
                        }
416
                }
417
                
418
                for (int i = 0; i < list.lenght(); i++) {
419
                        RasterFilter f = list.get(i);
420
                        if(f instanceof LinearEnhancementFilter && list.get(0) instanceof TailTrimFilter) {
421
                                list.remove(f.getName());
422
                                list.add(f, 1);
423
                        } else if(f instanceof LinearEnhancementFilter) {
424
                                list.remove(f.getName());
425
                                list.add(f, 0);
426
                        }
427
                }
428
                list.controlTypes();
429
        }
430
        
431
        /*
432
         * (non-Javadoc)
433
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getRenderBands()
434
         */
435
        public int[] getRenderBands() {
436
                return new int[]{bandR, bandG, bandB};
437
        }
438
        
439
        /*
440
         * (non-Javadoc)
441
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterList()
442
         */
443
        public RasterFilterList getFilterList() throws FilterTypeException {
444
                try {
445
                        RasterFilterListManager filterListManager = null;
446
                        if(layer.getRenderFilterList() != null)
447
                                filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
448
                        else {
449
                                RasterFilterList fl = new RasterFilterList();
450
                                fl.setInitDataType(IBuffer.TYPE_BYTE);
451
                                filterListManager = new RasterFilterListManager(fl);
452
                        }
453
                        filterListManager.createFilterListFromStrings(filters);
454
                        return filterListManager.getFilterList();
455
                } catch (NullPointerException e) {
456
                        return null;
457
                }
458
        }
459
        
460
        /*
461
         * (non-Javadoc)
462
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getColorTable()
463
         */
464
        public ColorTable getColorTable() throws FilterTypeException {
465
                RasterFilterList fl = getFilterList();
466
                for (int i = 0; i < fl.lenght(); i++) {
467
                        if(fl.get(i) instanceof ColorTableFilter) 
468
                                return (ColorTable) fl.get(i).getParam("colorTable");
469
                }
470
                return null;
471
        }
472
        
473
        /*
474
         * (non-Javadoc)
475
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getTransparency()
476
         */
477
        public GridTransparency getTransparency() {                
478
                //Asigna la transparencia
479
                GridTransparency transp = new GridTransparency();
480
                if(transparency != 255) { 
481
                        transp.setOpacity(transparency);
482
                        transp.activeTransparency();
483
                }
484
                
485
                //Rangos de transparencia
486
                if(ranges != null) {
487
                        transp.setTransparencyRangeList(ranges);
488
                        transp.activeTransparency();
489
                }
490
                
491
                //Banda de transparencia
492
                if(transp != null && alphaBandNumber != -1) {
493
                        transp.setTransparencyBand(alphaBandNumber);
494
                        transp.activeTransparency();
495
                }
496
                
497
                return (transp.isTransparencyActive()) ? transp : null;
498
        }
499
        
500
        /*
501
         * (non-Javadoc)
502
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterArguments()
503
         */
504
        public ArrayList getFilterArguments() {
505
                return filters;
506
        }
507
        
508
        String nameClass = null;
509
        /**
510
         * Asigna el nombre de la clase que se ha leido desde el proyecto
511
         * @param nameClass
512
         */
513
        public void setNameClass(String nameClass) {
514
                this.nameClass = nameClass;                
515
        }
516
}