Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / classification / ClassificationParallelepipedProcess.java @ 18829

History | View | Annotate | Download (9.15 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Iba?ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.remotesensing.classification;
42

    
43
import java.util.ArrayList;
44

    
45
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
46
import org.gvsig.raster.buffer.RasterBuffer;
47
import org.gvsig.raster.dataset.IBuffer;
48
import org.gvsig.raster.grid.GridException;
49
import org.gvsig.raster.grid.roi.ROI;
50

    
51
import com.iver.cit.gvsig.project.documents.view.gui.View;
52

    
53
/** ClassificationParallelepipedProcess implementa el m?todo de clasificaci?n de 
54
 * paralelepipedos o hipercubos.
55
 * 
56
 * @see ClassificationGeneralProcess
57
 * @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
58
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
59
 * @version 19/10/2007 
60
*/
61

    
62
public class ClassificationParallelepipedProcess extends ClassificationGeneralProcess{
63
        
64
        private int                                         bandCount                                 = 0;
65
        double                                                         varianza[][]                        = null;
66
        double                                                         medias[][]                                = null; 
67
        int                                                         defaultClass                        =0;
68
        double                                                         stevcoef                                =0;
69

    
70
        
71
        /**
72
        * M?todo que implementa el clasificador por paralelepipedos. 
73
        * 
74
        * @param  array de tipo byte con los valores del pixel en cada una de las bandas 
75
        * @return clase a la que pertenece el pixel
76
    */
77
        
78
        public int getPixelClassForTypeByte(byte[] pixelBandsValues) {
79
                for (int clase=0; clase<numClases; clase++)
80
                {
81
                        boolean inClass= true;
82
                        for(int i=0; i< bandCount; i++)
83
                                {
84
                                 if(!((medias[clase][i]-stevcoef*Math.sqrt(varianza[clase][i])<pixelBandsValues[i]) && 
85
                                                 (pixelBandsValues[i] <medias[clase][i]+stevcoef*Math.sqrt(varianza[clase][i]))))
86
                                 {
87
                                         inClass= false;
88
                                 }
89
                                }
90
                         if(inClass==true)
91
                                 return clase;
92
                }
93
                
94
                return defaultClass;
95
        } // Fin del metodo
96

    
97

    
98
        
99
        /**
100
        * M?todo que implementa el clasificador por paralelepipedos. 
101
        * 
102
        * @param  array de tipo short con los valores del pixel en cada una de las bandas 
103
        * @return primera clase a la que  pertenece el pixel.
104
    */
105
        
106
        public int getPixelClassForTypeShort(short[] pixelBandsValues) {
107
                for (int clase=0; clase<numClases; clase++)
108
                {
109
                        boolean inClass= true;
110
                        for(int i=0; i< bandCount; i++)
111
                                {
112
                                 if(!((medias[clase][i]-stevcoef*Math.sqrt(varianza[clase][i])<pixelBandsValues[i]) && 
113
                                                 (pixelBandsValues[i] <medias[clase][i]+stevcoef*Math.sqrt(varianza[clase][i]))))
114
                                 {
115
                                         inClass= false;
116
                                 }
117
                                }
118
                         if(inClass==true)
119
                                 return clase;
120
                }
121
                
122
                return defaultClass;
123
        }
124

    
125

    
126
        /**
127
        * M?todo que implementa el clasificador por paralelepipedos. 
128
        * 
129
        * @param  array de tipo int con los valores del pixel en cada una de las bandas 
130
        * @return primera clase a la que  pertenece el pixel.
131
    */
132
        public int getPixelClassForTypeInt(int[] pixelBandsValues) {
133
                for (int clase=0; clase<numClases; clase++)
134
                {
135
                        boolean inClass= true;
136
                        for(int i=0; i< bandCount; i++)
137
                                {
138
                                 if(!((medias[clase][i]-stevcoef*Math.sqrt(varianza[clase][i])<pixelBandsValues[i]) && 
139
                                                 (pixelBandsValues[i] <medias[clase][i]+stevcoef*Math.sqrt(varianza[clase][i]))))
140
                                 {
141
                                         inClass= false;
142
                                 }
143
                                }
144
                         if(inClass==true)
145
                                 return clase;
146
                }
147
                
148
                return defaultClass;
149
        }
150

    
151

    
152
        
153
        /**
154
        * M?todo que implementa el clasificador por paralelepipedos. 
155
        * 
156
        * @param  array de tipo float con los valores del pixel en cada una de las bandas 
157
        * @return primera clase a la que  pertenece el pixel.
158
    */
159
        public int getPixelClassForTypeFloat(float[] pixelBandsValues) {
160
                for (int clase=0; clase<numClases; clase++)
161
                {
162
                        boolean inClass= true;
163
                        for(int i=0; i< bandCount; i++)
164
                                {
165
                                 if(!((medias[clase][i]-stevcoef*Math.sqrt(varianza[clase][i])<pixelBandsValues[i]) && 
166
                                                 (pixelBandsValues[i] <medias[clase][i]+stevcoef*Math.sqrt(varianza[clase][i]))))
167
                                 {
168
                                         inClass= false;
169
                                 }
170
                                }
171
                         if(inClass==true)
172
                                 return clase;
173
                }
174
                
175
                return defaultClass;
176
        }
177

    
178

    
179
        /**
180
        * M?todo que implementa el clasificador por paralelepipedos. 
181
        * 
182
        * @param  array de tipo double con los valores del pixel en cada una de las bandas 
183
        * @return primera clase a la que  pertenece el pixel.
184
    */
185
        public int getPixelClassForTypeDouble(double[] pixelBandsValues) {
186
                for (int clase=0; clase<numClases; clase++)
187
                {
188
                        boolean inClass= true;
189
                        for(int i=0; i< bandCount; i++)
190
                                {
191
                                 if(!((medias[clase][i]-stevcoef*Math.sqrt(varianza[clase][i])<pixelBandsValues[i]) && 
192
                                                 (pixelBandsValues[i] <medias[clase][i]+stevcoef*Math.sqrt(varianza[clase][i]))))
193
                                 {
194
                                         inClass= false;
195
                                 }
196
                                }
197
                         if(inClass==true)
198
                                 return clase;
199
                }
200
                
201
                return defaultClass;
202
        }
203

    
204

    
205
        /** Metodo que recoge los parametros del proceso de clasificacion de 
206
        * por paralelepipedos
207
        * <LI>rasterSE: Capa de entrada para la clasificaci?n</LI>
208
        * <LI> rois: lista de rois</LI> 
209
        * <LI> bandList:bandas habilitadas </LI> 
210
        * <LI>view: vista sobre la que se carga la capa al acabar el proceso</LI>
211
        * <LI>filename: path con el fichero de salida</LI>
212
        * <LI>stevcoef: coeficiente que establece el de desviacion maxima</LI>
213
        */
214
        public void init() {
215
                
216
                rasterSE= (FLyrRasterSE)getParam("layer");
217
                rois = (ArrayList)getParam("rois");
218
                view=(View)getParam("view");
219
                filename= getStringParam("filename");
220
                bandList = (int[])getParam("bandList");
221
                stevcoef= ((Double)getParam("dev")).doubleValue();
222
                
223
                bandCount  = bandList.length;
224
                numClases = rois.size();
225
                defaultClass= numClases;
226
                medias= new double[numClases][bandCount];
227
                varianza= new double[numClases][bandCount];
228
                
229
                // Calculo de estadisticas de clases
230
                for (int clase=0; clase<numClases; clase++)
231
                        for (int i=0;i<bandCount;i++){
232
                                ((ROI)rois.get(clase)).setBandToOperate(bandList[i]);
233
                                        try {
234
                                                medias[clase][i]=((ROI)rois.get(clase)).getMeanValue();
235
                                                varianza[clase][i]=((ROI)rois.get(clase)).getVariance();
236
                                        } catch (GridException e) {
237
                                                e.printStackTrace();
238
                                        }
239
                        }
240
        }
241

    
242

    
243
        
244
        /** Proceso de clasificacion por paralelepipedos*/
245
        public void process() throws InterruptedException {
246
                
247
                setGrid();
248
                withdefaultClass=true;
249
                rasterResult= RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, inputGrid.getRasterBuf().getWidth(),
250
                                                                inputGrid.getRasterBuf().getHeight(), 1, true);
251
                int c=0;
252
                int iNY= inputGrid.getLayerNY();
253
                int iNX= inputGrid.getLayerNX();        
254
                
255
                bandCount  = inputGrid.getBandCount();
256
                int inputGridNX = inputGrid.getNX();
257
                int datType = inputGrid.getRasterBuf().getDataType();
258
                
259
                // Caso buffer tipo byte
260
                if (datType == RasterBuffer.TYPE_BYTE){
261
                        byte data[]= new byte[bandCount];
262
                        for(int i=0; i<iNY;i++){
263
                                for(int j=0; j<iNX;j++){
264
                                        inputGrid.getRasterBuf().getElemByte(i, j, data);
265
                                        c= getPixelClassForTypeByte(data);
266
                                        rasterResult.setElem(i, j, 0,(byte) c);
267
                                        
268
                                }
269
                                percent = i*100/inputGridNX;
270
                        }
271
                }
272
                // Caso buffer tipo short
273
                if (datType == RasterBuffer.TYPE_SHORT){
274
                        short data[]= new short[bandCount];
275
                        for(int i=0; i<iNY;i++){
276
                                for(int j=0; j<iNX;j++){
277
                                        inputGrid.getRasterBuf().getElemShort(i, j, data);
278
                                        c= getPixelClassForTypeShort(data);
279
                                        rasterResult.setElem(i, j, 0,(byte) c);
280
                                        
281
                                }
282
                                percent = i*100/inputGridNX;
283
                        }
284
                }
285
                
286
                // Caso buffer tipo int
287
                if (datType == RasterBuffer.TYPE_INT){
288
                        int data[]= new int[bandCount];
289
                        for(int i=0; i<iNY;i++){
290
                                for(int j=0; j<iNX;j++){
291
                                        inputGrid.getRasterBuf().getElemInt(i, j, data);
292
                                        c= getPixelClassForTypeInt(data);
293
                                        rasterResult.setElem(i, j, 0,(byte) c);
294
                                        
295
                                }
296
                                percent = i*100/inputGridNX;
297
                        }
298
                }
299
                
300
                // Caso buffer tipo float
301
                if (datType == RasterBuffer.TYPE_FLOAT){
302
                        float data[]= new float[bandCount];
303
                        for(int i=0; i<iNY;i++){
304
                                for(int j=0; j<iNX;j++){
305
                                        inputGrid.getRasterBuf().getElemFloat(i, j, data);
306
                                        c= getPixelClassForTypeFloat(data);
307
                                        rasterResult.setElem(i, j, 0,(byte) c);
308
                                        
309
                                }
310
                                percent = i*100/inputGridNX;
311
                        }
312
                }
313
                
314
                // Caso buffer tipo double
315
                if (datType == RasterBuffer.TYPE_DOUBLE){
316
                        double data[]= new double[bandCount];
317
                        for(int i=0; i<iNY;i++){
318
                                for(int j=0; j<iNX;j++){
319
                                        inputGrid.getRasterBuf().getElemDouble(i, j, data);
320
                                        c= getPixelClassForTypeDouble(data);
321
                                        rasterResult.setElem(i, j, 0,(byte) c);
322
                                        
323
                                }
324
                                percent = i*100/inputGridNX;
325
                        }
326
                }
327
                
328
                writeToFile();
329
        }
330

    
331
        
332
}