Statistics
| Revision:

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

History | View | Annotate | Download (8.26 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.layers;
20

    
21
import java.util.ArrayList;
22

    
23
import org.gvsig.raster.grid.GridTransparency;
24
import org.gvsig.raster.grid.filter.RasterFilterListManager;
25
import org.gvsig.raster.shared.IRasterProperties;
26
import org.gvsig.raster.util.TransparencyRange;
27

    
28
import com.iver.andami.PluginServices;
29
import com.iver.andami.ui.mdiManager.IWindow;
30
import com.iver.cit.gvsig.fmap.layers.XMLException;
31
import com.iver.cit.gvsig.project.documents.view.gui.IView;
32
import com.iver.utiles.XMLEntity;
33

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

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

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

    
192
        /* (non-Javadoc)
193
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#getFilters()
194
         */
195
        public ArrayList getFilters() {
196
                return this.filters;
197
        }
198
        
199
        
200
        /* (non-Javadoc)
201
         * @see com.iver.cit.gvsig.fmap.layers.StatusRasterInterface#applyStatus(com.iver.cit.gvsig.fmap.layers.RasterFileAdapter)
202
         */
203
        public void applyStatus(IRasterProperties layer) {
204
                                                                                
205
                //Eliminamos el fichero inicial y cargamos las bandas si hay para que se carguen 
206
                //en el orden correcto
207
                if(layer instanceof FLyrRasterSE) {
208
                        if(files.size() != 0){
209
                                //((FLyrRasterSE)layer).delFile((String)files.get(0));
210
                                for(int i = 1; i < files.size(); i++)
211
                                        ((FLyrRasterSE)layer).addFile((String)files.get(i));
212
                        }
213
                }
214
                
215
                //Asigna las bandas
216
                if(bandR == -1)
217
                        bandR = 0;
218
                if(bandG == -1)
219
                        bandG = 0;
220
                if(bandB == -1)
221
                        bandB = 0;
222
                int[] renderBands = new int[]{bandR, bandG, bandB};
223
                layer.getRender().setRenderBands(renderBands);
224
                                
225
                //Asigna la transparencia
226
                GridTransparency transp = layer.getRender().getLastTransparency(); 
227
                if(transparency != 255) { 
228
                        transp.setOpacity(transparency);
229
                        transp.activeTransparency();
230
                }
231
                
232
                //Rangos de transparencia
233
                if(ranges != null) {
234
                        transp.setTransparencyRangeList(ranges);
235
                        transp.activeTransparency();
236
                }
237
                
238
                //Filtros
239
                if(layer.getRenderFilterList() != null) {
240
                        RasterFilterListManager filterListManager = new RasterFilterListManager(layer.getRenderFilterList());
241
                        //if(layer instanceof FLyrRasterSE)
242
                                //filterListManager.createStackFromStrings(this.filters, ((FLyrRasterSE)layer).getGeoRasterMultiDataset().get.getSource().getFiles());
243
                        //else
244
                                filterListManager.createStackFromStrings(this.filters, new Integer(0));
245
                }
246
                
247
                //Refrescamos todas las vistas
248
                IWindow[] w = PluginServices.getMDIManager().getAllWindows();
249
                for (int i = 0; i < w.length; i++) {
250
                        if(w[i] != null && w[i] instanceof IView) 
251
                                ((IView)w[i]).getMapControl().getMapContext().invalidate();        
252
                }
253
        }
254
}