Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / fmap / layers / StatusLayerRaster.java @ 752

History | View | Annotate | Download (13.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.fmap.layers;
23

    
24
import java.util.ArrayList;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.RasterLocator;
28
import org.gvsig.fmap.dal.coverage.RasterManager;
29
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
30
import org.gvsig.fmap.dal.coverage.datastruct.Params;
31
import org.gvsig.fmap.dal.coverage.datastruct.TransparencyRange;
32
import org.gvsig.fmap.dal.coverage.exception.FileNotFoundInListException;
33
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException;
34
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
35
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
36
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
37
import org.gvsig.fmap.dal.coverage.exception.OperationNotSupportedException;
38
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
39
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
40
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
41
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
42
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
43
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.dynobject.DynStruct;
46
import org.gvsig.tools.persistence.PersistenceManager;
47
import org.gvsig.tools.persistence.PersistentState;
48
import org.gvsig.tools.persistence.exception.PersistenceException;
49

    
50

    
51

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

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

    
174
        public void saveToState(PersistentState state) throws PersistenceException {
175
                state.set("transparency", this.transparency);
176
                state.set("bandR", this.bandR);
177
                state.set("bandG", this.bandG);
178
                state.set("bandB", this.bandB);
179
                state.set("alphaBandNumber", this.alphaBandNumber);
180
                state.set("loadClass",this.loadClass);
181
                state.set("layer", this.layer);
182
                state.set("ranges", this.ranges);
183
                state.set("files", this.files);
184
                state.set("filters",this.filters);
185
        }
186

    
187
        /* (non-Javadoc)
188
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getXMLEntity(com.iver.utiles.XMLEntity)
189
         */
190
        public void setStateProperties(boolean loadClass, DefaultFLyrRaster layer) throws PersistenceException {
191
                this.layer = layer;
192
                this.loadClass = loadClass;
193
        }
194
        
195
        public static void registerPersistence() {
196
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
197
                DynStruct definition = manager.addDefinition(StatusLayerRaster.class,
198
                                "StatusLayerRaster_Persistent",
199
                                "StatusLayerRaster Persistent definition (FIXME loadClass)", null, null);
200
                definition.addDynFieldInt("transparency");
201
                definition.addDynFieldInt("bandR").setMandatory(true);
202
                definition.addDynFieldInt("bandG").setMandatory(true);
203
                definition.addDynFieldInt("bandB").setMandatory(true);
204
                definition.addDynFieldInt("alphaBandNumber");
205
                definition.addDynFieldBoolean("loadClass");
206
                
207
                definition.addDynFieldObject("layer")
208
                        .setClassOfValue(DefaultFLyrRaster.class);
209
                
210
                definition.addDynFieldList("ranges")
211
                        .setClassOfItems(TransparencyRange.class);
212
                
213
                definition.addDynFieldList("files")
214
                        .setClassOfItems(String.class);
215

    
216
                definition.addDynFieldList("filters")
217
                        .setClassOfItems(String.class);
218
        }
219
        
220
        /* (non-Javadoc)
221
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getFilters()
222
         */
223
        public List<String> getFilters() {
224
                return this.filters;
225
        }
226
        
227
        
228
        /* (non-Javadoc)
229
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#applyStatus(com.iver.cit.gvsig.fmap.layers.RasterFileAdapter)
230
         */
231
        public void applyStatus(DefaultFLyrRaster layer) throws NotSupportedExtensionException, RasterDriverException, FilterTypeException, FileNotFoundInListException, OperationNotSupportedException, FilterManagerException, InvalidSourceException {
232
                                                                                
233
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
234
                //en el orden correcto
235
                if(layer instanceof DefaultFLyrRaster) {
236
                        if(files != null && files.size() != 0){
237
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
238
                                for (int i = 1; i < files.size(); i++) 
239
                                        ((DefaultFLyrRaster)layer).addFile((String)files.get(i));
240
                        }
241
                }
242
                
243
                //Asigna las bandas
244
                int[] renderBands = new int[]{bandR, bandG, bandB};
245
                if(layer.getRender() != null)
246
                        layer.getRender().setRenderBands(renderBands);
247
                                
248
                //Asigna la transparencia
249
                Transparency transp = layer.getRender().getLastTransparency(); 
250
                if(transp != null && transparency != 255) { 
251
                        transp.setOpacity(transparency);
252
                        transp.activeTransparency();
253
                }
254
                
255
                //Rangos de transparencia
256
                if(transp != null && ranges != null) {
257
                        transp.setTransparencyRangeList(ranges);
258
                        transp.activeTransparency();
259
                }
260
                
261
                //Banda de transparencia
262
                if(transp != null && alphaBandNumber != -1) {
263
                        transp.setTransparencyBand(alphaBandNumber);
264
                        transp.activeTransparency();
265
                }
266
                
267
                //Filtros
268
                if (layer.getRender().getFilterList() != null) {
269
                        layer.getRender().getFilterList().createFilterListFromStrings((ArrayList<String>)filters);
270
                        enhancedCompV10((ArrayList<String>)filters, layer);
271
                        //sortFilters(layer.getRenderFilterList());
272
                }
273
                
274
                //Refrescamos todas las vistas
275
                layer.getMapContext().invalidate();
276
                /*IWindow[] w = PluginServices.getMDIManager().getAllWindows();
277
                for (int i = 0; i < w.length; i++) {
278
                        if(w[i] != null && w[i] instanceof AbstractViewPanel) 
279
                                ((AbstractViewPanel)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
         * @throws FilterManagerException 
291
         */
292
        public static void enhancedCompV10(ArrayList<String> filterArguments, DefaultFLyrRaster layer) throws FilterTypeException, FilterManagerException {
293
                RasterFilterList filterList = layer.getRender().getFilterList();
294
                filterList.remove("enhanced_stretch");
295
                filterList.remove("tailTrim");
296
                
297
                /*boolean removed = false;
298
                RasterFilterList list = layer.getRender().getFilterList();
299
                for (int i = 0; i < list.lenght(); i++) {
300
                        RasterFilter f = list.get(i); 
301
                        if(f instanceof TailTrimFilter || f instanceof LinearEnhancementFilter) { 
302
                                list.remove(f.getName());
303
                                removed = true;
304
                        }
305
                }
306
                if(removed)
307
                        list.controlTypes();*/
308
                
309
                //Para compatibilidad de realce con proyectos antiguos
310
                for (int i = 0; i < filterArguments.size(); i++) { 
311
                        if(((String)filterArguments.get(i)).startsWith("filter.enhanced.active=true")) {
312
                                RasterFilterListManager enhancementManager = filterList.getManagerByID("EnhancementStretch");
313
                                Params params = filterList.createEmptyFilterParams();
314
                                params.setParam("stats", ((RasterDataStore)layer.getDataStore()).getStatistics());
315
                                params.setParam("remove", new Boolean(false));
316
                                params.setParam("renderBands", layer.getRender().getRenderBands());
317
                                params.setParam("stretchs", null);//coge el LinearStretchParams por defecto
318
                                enhancementManager.addFilter(params);
319
                        }
320
                }
321
        }
322
        
323
        /*
324
         * (non-Javadoc)
325
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getRenderBands()
326
         */
327
        public int[] getRenderBands() {
328
                return new int[]{bandR, bandG, bandB};
329
        }
330
        
331
        /*
332
         * (non-Javadoc)
333
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterList()
334
         */
335
        public RasterFilterList getFilterList() throws FilterTypeException {
336
                RasterManager rManager = RasterLocator.getManager();
337
                try {
338
                        RasterFilterList filterList = layer.getRender().getFilterList();
339
                        if(filterList != null)
340
                                filterList.createFilterListFromStrings((ArrayList<String>)filters);
341
                        else {
342
                                filterList = rManager.createEmptyFilterList(Buffer.TYPE_BYTE);
343
                        }
344
                        
345
                        return filterList;
346
                } catch (NullPointerException e) {
347
                        return null;
348
                }
349
        }
350
        
351
        /*
352
         * (non-Javadoc)
353
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getColorTable()
354
         */
355
        public ColorTable getColorTable() throws FilterTypeException {
356
                return layer.getRender().getColorTable();
357
        }
358
        
359
        /*
360
         * (non-Javadoc)
361
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterArguments()
362
         */
363
        public List<String> getFilterArguments() {
364
                return filters;
365
        }
366
        
367
        String nameClass = null;
368
        /**
369
         * Asigna el nombre de la clase que se ha leido desde el proyecto
370
         * @param nameClass
371
         */
372
        public void setNameClass(String nameClass) {
373
                this.nameClass = nameClass;                
374
        }
375
}