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 @ 2438

History | View | Annotate | Download (12.7 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.datastruct.Params;
29
import org.gvsig.fmap.dal.coverage.datastruct.TransparencyRange;
30
import org.gvsig.fmap.dal.coverage.exception.FileNotFoundInListException;
31
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException;
32
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
33
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
34
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
35
import org.gvsig.fmap.dal.coverage.exception.OperationNotSupportedException;
36
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
37
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
38
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
39
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
40
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
41
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
42
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48

    
49

    
50

    
51
/**
52
 * Esta clase almacena el estado de un raster en cuanto a las caracteristicas
53
 * de opacidad, bandas y filtros. Estas caracter?sticas pueden ser salvadas a 
54
 * un xml y recuperadas por la capa a trav?s de las funciones setXMLEntity y 
55
 * getXMLEntity
56
 * 
57
 * @deprecated
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
        public void loadFromState(PersistentState state, DefaultFLyrRaster 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
        @SuppressWarnings("unchecked")
155
        public void loadFromState(PersistentState state)
156
                        throws PersistenceException {
157
                this.transparency = state.getInt("transparency");
158
                this.bandR = state.getInt("bandR");
159
                this.bandG = state.getInt("bandG");
160
                this.bandB = state.getInt("bandB");
161
                this.alphaBandNumber = state.getInt("alphaBandNumber");
162
                this.loadClass = state.getBoolean("loadClass");
163
                
164
                this.layer = (DefaultFLyrRaster)state.get("layer");
165
                
166
                this.ranges = new ArrayList(state.getList("ranges"));
167
                this.files = new ArrayList(state.getList("files"));
168
                this.filters = new ArrayList(state.getList("filters"));
169
        }
170

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

    
184
        public void setStateProperties(boolean loadClass, DefaultFLyrRaster layer) throws PersistenceException {
185
                this.layer = layer;
186
                this.loadClass = loadClass;
187
        }
188
        
189
        public static void registerPersistence() {
190
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
191
                DynStruct definition = manager.addDefinition(StatusLayerRaster.class,
192
                                "StatusLayerRaster_Persistent",
193
                                "StatusLayerRaster Persistent definition (FIXME loadClass)", null, null);
194
                definition.addDynFieldInt("transparency");
195
                definition.addDynFieldInt("bandR").setMandatory(true);
196
                definition.addDynFieldInt("bandG").setMandatory(true);
197
                definition.addDynFieldInt("bandB").setMandatory(true);
198
                definition.addDynFieldInt("alphaBandNumber");
199
                definition.addDynFieldBoolean("loadClass");
200
                
201
                definition.addDynFieldObject("layer")
202
                        .setClassOfValue(DefaultFLyrRaster.class);
203
                
204
                definition.addDynFieldList("ranges")
205
                        .setClassOfItems(TransparencyRange.class);
206
                
207
                definition.addDynFieldList("files")
208
                        .setClassOfItems(String.class);
209

    
210
                definition.addDynFieldList("filters")
211
                        .setClassOfItems(String.class);
212
        }
213
        
214
        public List<String> getFilters() {
215
                return this.filters;
216
        }
217
        
218
        public void applyStatus(DefaultFLyrRaster layer) throws NotSupportedExtensionException, RasterDriverException, FilterTypeException, FileNotFoundInListException, OperationNotSupportedException, FilterManagerException, InvalidSourceException {
219
                                                                                
220
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
221
                //en el orden correcto
222
                if(layer instanceof DefaultFLyrRaster) {
223
                        if(files != null && files.size() != 0){
224
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
225
                                for (int i = 1; i < files.size(); i++) 
226
                                        ((DefaultFLyrRaster)layer).addFile((String)files.get(i));
227
                        }
228
                }
229
                
230
                //Asigna las bandas
231
                String[] valuesCI = new String[layer.getDataStore().getBandCount()];
232
                valuesCI[bandR] = ColorInterpretation.RED_BAND;
233
                valuesCI[bandG] = ColorInterpretation.GREEN_BAND;
234
                valuesCI[bandB] = ColorInterpretation.BLUE_BAND;
235
                ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(valuesCI);
236
                if(layer.getRender() != null)
237
                        layer.getRender().setRenderColorInterpretation(ci);
238
                                
239
                //Asigna la transparencia
240
                Transparency transp = layer.getRender().getRenderingTransparency(); 
241
                if(transp != null && transparency != 255) { 
242
                        transp.setOpacity(transparency);
243
                        transp.activeTransparency();
244
                }
245
                
246
                //Rangos de transparencia
247
                if(transp != null && ranges != null) {
248
                        transp.setTransparencyRangeList(ranges);
249
                        transp.activeTransparency();
250
                }
251
                
252
                //Banda de transparencia
253
                if(transp != null && alphaBandNumber != -1) {
254
                        transp.setTransparencyBand(alphaBandNumber);
255
                        transp.activeTransparency();
256
                }
257
                
258
                //Filtros
259
                /*if (layer.getRender().getFilterList() != null) {
260
                        layer.getRender().getFilterList().createFilterListFromStrings((ArrayList<String>)filters);
261
                        enhancedCompV10((ArrayList<String>)filters, layer);
262
                        //sortFilters(layer.getRenderFilterList());
263
                }*/
264
                
265
                //Refrescamos todas las vistas
266
                layer.getMapContext().invalidate();
267
                /*IWindow[] w = PluginServices.getMDIManager().getAllWindows();
268
                for (int i = 0; i < w.length; i++) {
269
                        if(w[i] != null && w[i] instanceof AbstractViewPanel) 
270
                                ((AbstractViewPanel)w[i]).getMapControl().getMapContext().invalidate();        
271
                }*/
272
        }
273
        
274
        /**
275
         * M?todo para mantener la compatibilidad con la v10 del realce. El realce de la v10 no
276
         * aporta suficiente informaci?n por lo que se a?ade un filtro de realce generico v1.9
277
         * @param filterArguments
278
         * @param layer
279
         * @param filterListManager
280
         * @throws FilterTypeException
281
         * @throws FilterManagerException 
282
         */
283
        public static void enhancedCompV10(ArrayList<String> filterArguments, DefaultFLyrRaster layer) throws FilterTypeException, FilterManagerException {
284
                RasterFilterList filterList = layer.getRender().getFilterList();
285
                filterList.remove("enhanced_stretch");
286
                filterList.remove("tailTrim");
287
                
288
                /*boolean removed = false;
289
                RasterFilterList list = layer.getRender().getFilterList();
290
                for (int i = 0; i < list.lenght(); i++) {
291
                        RasterFilter f = list.get(i); 
292
                        if(f instanceof TailTrimFilter || f instanceof LinearEnhancementFilter) { 
293
                                list.remove(f.getName());
294
                                removed = true;
295
                        }
296
                }
297
                if(removed)
298
                        list.controlTypes();*/
299
                
300
                //Para compatibilidad de realce con proyectos antiguos
301
                for (int i = 0; i < filterArguments.size(); i++) { 
302
                        if(((String)filterArguments.get(i)).startsWith("filter.enhanced.active=true")) {
303
                                RasterFilterListManager enhancementManager = filterList.getManagerByID("EnhancementStretch");
304
                                Params params = filterList.createEmptyFilterParams();
305
                                params.setParam("stats", ((RasterDataStore)layer.getDataStore()).getStatistics());
306
                                params.setParam("remove", new Boolean(false));
307
                                params.setParam("renderBands", layer.getRender().getRenderColorInterpretation().buildRenderBands());
308
                                params.setParam("stretchs", null);//coge el LinearStretchParams por defecto
309
                                enhancementManager.addFilter(params);
310
                        }
311
                }
312
        }
313
        
314
        public int[] getRenderBands() {
315
                return new int[]{bandR, bandG, bandB};
316
        }
317
        
318
        public RasterFilterList getFilterList() throws FilterTypeException {
319
                /*RasterManager rManager = RasterLocator.getManager();
320
                try {
321
                        RasterFilterList filterList = layer.getRender().getFilterList();
322
                        if(filterList != null)
323
                                filterList.createFilterListFromStrings((ArrayList<String>)filters);
324
                        else {
325
                                filterList = rManager.createEmptyFilterList(Buffer.TYPE_BYTE);
326
                        }
327
                        
328
                        return filterList;
329
                } catch (NullPointerException e) {
330
                        return null;
331
                }*/
332
                return null;
333
        }
334
        
335
        public ColorTable getColorTable() throws FilterTypeException {
336
                return layer.getRender().getColorTable();
337
        }
338
        
339
        public List<String> getFilterArguments() {
340
                return filters;
341
        }
342
        
343
        String nameClass = null;
344
        /**
345
         * Asigna el nombre de la clase que se ha leido desde el proyecto
346
         * @param nameClass
347
         */
348
        public void setNameClass(String nameClass) {
349
                this.nameClass = nameClass;                
350
        }
351
}