Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / fmap / layers / StatusLayerRaster.java @ 2311

History | View | Annotate | Download (12.8 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.ColorInterpretation;
43
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
44
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50

    
51

    
52

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

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

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

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

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