Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / layers / StatusLayerRaster.java @ 13916

History | View | Annotate | Download (11 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.raster.dataset.IBuffer;
24
import org.gvsig.raster.dataset.NotSupportedExtensionException;
25
import org.gvsig.raster.dataset.RasterDriverException;
26
import org.gvsig.raster.datastruct.TransparencyRange;
27
import org.gvsig.raster.grid.GridTransparency;
28
import org.gvsig.raster.grid.filter.RasterFilterList;
29
import org.gvsig.raster.grid.filter.RasterFilterListManager;
30
import org.gvsig.raster.hierarchy.IRasterProperties;
31

    
32
import com.iver.andami.PluginServices;
33
import com.iver.andami.ui.mdiManager.IWindow;
34
import com.iver.cit.gvsig.fmap.layers.XMLException;
35
import com.iver.cit.gvsig.project.documents.view.gui.IView;
36
import com.iver.utiles.XMLEntity;
37

    
38
/**
39
 * Esta clase almacena el estado de un raster en cuanto a las caracteristicas
40
 * de opacidad, bandas y filtros. Estas caracter?sticas pueden ser salvadas a 
41
 * un xml y recuperadas por la capa a trav?s de las funciones setXMLEntity y 
42
 * getXMLEntity
43
 * 
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class StatusLayerRaster implements IStatusRaster {
47

    
48
        public static String                         defaultClass = "org.gvsig.fmap.raster.layers.StatusLayerRaster";
49
        
50
        //Valor de opacidad global de la imagen
51
        public int                                                transparency = 255;
52
        
53
        //Rangos de transparencia
54
        public ArrayList                                ranges = new ArrayList();
55
                
56
        //(Selecci?n de bandas)N?mero de banda  asignado al Rojo, verde y azul         
57
        public int                                                 bandR = 0;
58
        public int                                                 bandG = 1;
59
        public int                                                 bandB = 2;
60
        
61
        //Ficheros cargados en un PxRaster 
62
        public ArrayList                                files = new ArrayList();
63
        
64
        //Filtros para poder montar una nueva pila
65
        public ArrayList                                filters = new ArrayList();
66
        private IRasterProperties       layer = null;
67
                
68
        /* (non-Javadoc)
69
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#setXMLEntity(com.iver.utiles.XMLEntity)
70
         */
71
        public void setXMLEntity(XMLEntity xml, IRasterProperties layer)throws XMLException {
72
                this.layer = layer;
73
                //RECUPERAR PROPIEDADES
74
                
75
                //Recuperamos las propiedades de los filtros
76
                for(int i = 0; i < xml.getPropertyCount(); i++) {
77
                        if(xml.getPropertyName(i).startsWith("filter."))        
78
                                filters.add(xml.getPropertyName(i) + "=" + xml.getPropertyValue(i));
79
                }
80
                
81
                //Rangos de transparencia
82
                if (xml.contains("filter.transparency.active") && xml.getBooleanProperty("filter.transparency.active")) {
83
                        int i = 0;
84
                        String value = null;
85
                        while(true) {
86
                                if(xml.contains("filter.transparency.transparencyRange" + i)) {
87
                                        value = xml.getStringProperty("filter.transparency.transparencyRange" + i);
88
                                        int alpha = 0;
89
                                        if(value.indexOf("@") != 0) {
90
                                                try {
91
                                                        alpha = Integer.parseInt(value.substring(value.indexOf("@") + 1, value.length()));
92
                                                } catch (NumberFormatException e) {
93
                                                        alpha = 0;
94
                                                }
95
                                                if(value.indexOf("@") != -1)
96
                                                        value = value.substring(0, value.indexOf("@"));
97
                                        }
98
                                        TransparencyRange range = new TransparencyRange(value);
99
                                        if(alpha != 0)
100
                                                range.setAlpha(alpha);
101
                                        ranges.add(range);
102
                                } else 
103
                                        break;
104
                                i ++;
105
                        }
106
                }
107
                
108
                if (xml.contains("raster.opacityLevel"))
109
                        transparency = xml.getIntProperty("raster.opacityLevel");
110
                                
111
                if (xml.contains("raster.bandR")) 
112
                        bandR = xml.getIntProperty("raster.bandR");
113
                
114
                if (xml.contains("raster.bandG")) 
115
                        bandG = xml.getIntProperty("raster.bandG");
116
                
117
                if (xml.contains("raster.bandB")) 
118
                        bandB = xml.getIntProperty("raster.bandB");
119
                
120
        
121
                int cont = 0;
122
                while(true && cont < 50) {
123
                        if (xml.contains("raster.file" + cont)) {
124
                                files.add(xml.getStringProperty("raster.file" + cont));
125
                                cont++;
126
                        }else 
127
                                break;
128
                }
129
        }
130
        
131

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

    
213
        /* (non-Javadoc)
214
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getFilters()
215
         */
216
        public ArrayList getFilters() {
217
                return this.filters;
218
        }
219
        
220
        
221
        /* (non-Javadoc)
222
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#applyStatus(com.iver.cit.gvsig.fmap.layers.RasterFileAdapter)
223
         */
224
        public void applyStatus(IRasterProperties layer) {
225
                                                                                
226
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
227
                //en el orden correcto
228
                if(layer instanceof FLyrRasterSE) {
229
                        if(files != null && files.size() != 0){
230
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
231
                                for (int i = 1; i < files.size(); i++) {
232
                                        try {
233
                                                ((FLyrRasterSE)layer).addFile((String)files.get(i));
234
                                        } catch (NotSupportedExtensionException e) {
235
                                                e.printStackTrace();
236
                                                //No a?adimos ese fichero pero continuamos con los dem?s
237
                                        } catch (RasterDriverException e) {
238
                                                e.printStackTrace();
239
                                                //No a?adimos ese fichero pero continuamos con los dem?s
240
                                        }
241
                                }
242
                        }
243
                }
244
                
245
                //Asigna las bandas
246
                int[] renderBands = new int[]{bandR, bandG, bandB};
247
                if(layer.getRender() != null)
248
                        layer.getRender().setRenderBands(renderBands);
249
                                
250
                //Asigna la transparencia
251
                GridTransparency transp = layer.getRender().getLastTransparency(); 
252
                if(transparency != 255) { 
253
                        transp.setOpacity(transparency);
254
                        transp.activeTransparency();
255
                }
256
                
257
                //Rangos de transparencia
258
                if(transp != null && ranges != null) {
259
                        transp.setTransparencyRangeList(ranges);
260
                        transp.activeTransparency();
261
                }
262
                
263
                //Filtros
264
                if (layer.getRenderFilterList() != null) {
265
                        RasterFilterListManager filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
266
                        filterListManager.createFilterListFromStrings(filters);
267
                }
268
                
269
                //Refrescamos todas las vistas
270
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
271
                for (int i = 0; i < w.length; i++) {
272
                        if(w[i] != null && w[i] instanceof IView) 
273
                                ((IView)w[i]).getMapControl().getMapContext().invalidate();        
274
                }
275
        }
276
        
277
        /*
278
         * (non-Javadoc)
279
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getRenderBands()
280
         */
281
        public int[] getRenderBands() {
282
                return new int[]{bandR, bandG, bandB};
283
        }
284
        
285
        /*
286
         * (non-Javadoc)
287
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterList()
288
         */
289
        public RasterFilterList getFilterList() {
290
                try {
291
                        RasterFilterListManager filterListManager = null;
292
                        if(layer.getRenderFilterList() != null)
293
                                filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
294
                        else {
295
                                RasterFilterList fl = new RasterFilterList();
296
                                fl.setInitDataType(IBuffer.TYPE_BYTE);
297
                                filterListManager = new RasterFilterListManager(fl);
298
                        }
299
                        filterListManager.createFilterListFromStrings(filters);
300
                        return filterListManager.getFilterList();
301
                } catch (NullPointerException e) {
302
                        return null;
303
                }
304
        }
305
        
306
        /*
307
         * (non-Javadoc)
308
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getTransparency()
309
         */
310
        public GridTransparency getTransparency() {
311
                //Esto soluciona un problema de compatibilidad entre branch v10 y HEAD. Eliminar en futuras versiones
312
                if(nameClass != null && nameClass.compareTo("com.iver.cit.gvsig.fmap.layers.StatusLayerRaster") == 0)
313
                        transparency = 255 - transparency;
314
                
315
                //Asigna la transparencia
316
                GridTransparency transp = new GridTransparency();
317
                if(transparency != 255) { 
318
                        transp.setOpacity(transparency);
319
                        transp.activeTransparency();
320
                }
321
                
322
                //Rangos de transparencia
323
                if(ranges != null) {
324
                        transp.setTransparencyRangeList(ranges);
325
                        transp.activeTransparency();
326
                }
327
                return (transp.isTransparencyActive()) ? transp : null;
328
        }
329
        
330
        /*
331
         * (non-Javadoc)
332
         * @see org.gvsig.fmap.raster.layers.IStatusRaster#getFilterArguments()
333
         */
334
        public ArrayList getFilterArguments() {
335
                return filters;
336
        }
337
        
338
        String nameClass = null;
339
        /**
340
         * Asigna el nombre de la clase que se ha leido desde el proyecto
341
         * @param nameClass
342
         */
343
        public void setNameClass(String nameClass) {
344
                this.nameClass = nameClass;                
345
        }
346
}