Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / principalcomponents / PCImageProcess.java @ 20872

History | View | Annotate | Download (12.6 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.principalcomponents;
42

    
43
import java.io.IOException;
44

    
45
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
46
import org.gvsig.raster.RasterProcess;
47
import org.gvsig.raster.buffer.RasterBuffer;
48
import org.gvsig.raster.buffer.WriterBufferServer;
49
import org.gvsig.raster.dataset.GeoRasterWriter;
50
import org.gvsig.raster.dataset.IBuffer;
51
import org.gvsig.raster.dataset.NotSupportedExtensionException;
52
import org.gvsig.raster.dataset.io.RasterDriverException;
53
import org.gvsig.raster.grid.Grid;
54
import org.gvsig.raster.grid.GridException;
55
import org.gvsig.raster.grid.GridExtent;
56
import org.gvsig.raster.util.RasterNotLoadException;
57
import org.gvsig.raster.util.RasterToolsUtil;
58

    
59
import Jama.Matrix;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.project.documents.view.gui.View;
63

    
64
/**
65
 *        PCImageProcess es la clase que implementa el proceso de contrucci?n de la imagen resultante del
66
 *        analisis de componentes principales a partir de los componentes seleccionados.
67
 *
68
 *        @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
69
 *        @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
70
 *        @version 19/10/2007 
71
 */
72
public class PCImageProcess extends RasterProcess{
73
        
74
        private double[]                         autovalues                         = null;
75
        private boolean[]                         selectedPCs                 = null;
76
        private String                                 layerName                         = null;
77
        private Grid                                 inputGrid                        = null;
78
        private Matrix                                 autoVectorMatrix        = null;
79
        private RasterBuffer                rasterResult                = null;
80
        private int                                 percent                           = 0;
81
        private WriterBufferServer  writerBufferServer;
82
        private View                                 view                                =null;
83
        private FLyrRasterSE                raster                                =null;
84
        
85

    
86
        /**
87
         * Connstructor
88
         */
89
        public PCImageProcess() {
90
        }
91
        
92
        
93
        /**
94
         * @return buffer resultante tras la transformacion
95
         */
96
        public RasterBuffer getBufferResult(){
97
                return rasterResult;        
98
        }
99
        
100
        /*
101
         * (non-Javadoc)
102
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
103
         */
104
        public String getLabel() {
105
                return  PluginServices.getText(this,"procesando");
106
        }
107

    
108
        /*
109
         * (non-Javadoc)
110
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
111
         */
112
        public String getLog() {
113
                if (writerBufferServer==null)
114
                        return PluginServices.getText(this,"generando_pcs")+"...";
115
                else
116
                        return PluginServices.getText(this,"escribiendo_resultado")+"...";
117
        }
118

    
119
        /*
120
         * (non-Javadoc)
121
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
122
         */
123
        public int getPercent() {
124
                if(writerBufferServer==null)
125
                        return percent;
126
                return writerBufferServer.getPercent();
127
        }
128

    
129
        
130
        /*
131
         * (non-Javadoc)
132
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
133
         */
134
        public String getTitle() {
135
                return PluginServices.getText(this,"principal_components");
136
        }
137

    
138
        /**
139
         * Par?metros obligatorios al proceso:
140
         * <UL>
141
         * <LI>raster: raster con la capa original</LI>
142
         * <LI>grid: grid solo con las bandas seleccionadas </LI>
143
         * <LI>autovalues: vector de autovalores </LI>
144
         * <LI>autoVectorMatrix: matriz de autovectores  </LI>
145
         * <LI>selectedPCs:  componentes seleccionados </LI>
146
         * <LI>layerName: nombre fichero de salida </LI>
147
         * <LI>view:vista sobre la que se carga la capa resultante </LI>
148
         * </UL> 
149
         */
150
        public void init() {
151
                raster= (FLyrRasterSE)getLayerParam("layer");
152
                inputGrid= (Grid)getParam("grid");
153
                autovalues= (double[])getParam("autovalores");
154
                autoVectorMatrix= (Matrix) getParam("autovectores");
155
                selectedPCs= (boolean[]) getParam("compselected");
156
                layerName= getStringParam("filename");
157
                view= (View)getParam("view");
158
        }
159

    
160

    
161
        public void process() throws InterruptedException {
162
                
163
                        //        ResultExtent de la primera banda del array 
164
                        GridExtent layerExtent = null;
165
                        layerExtent= inputGrid.getGridExtent();
166
                
167
                        // Se determina el numero de bandas de la imagen resultante, en funcion de la seleccion.
168
                        int numbandas=0;
169
                        for (int i=0;i<selectedPCs.length;i++)
170
                                if (selectedPCs[i])numbandas++;
171
                
172
                        // Correspondencia entre entre componete seleccionado y banda
173
                        int banda[] = new int[selectedPCs.length];
174
                        int band=0;
175
                        for (int i=0;i<selectedPCs.length;i++)
176
                                {
177
                                        if (selectedPCs[i]){ banda[i]=band; band++;}
178
                                        else banda[i]=-1;
179
                                }
180
                
181
                        // ResultOrden: Orden correcto de autovalores (de mayor a menor)
182
                        int resultOrden[]= new int[autovalues.length];
183
                        int cont = autovalues.length-1;
184
                        int noseleccionados=0;
185
                        for(int i=0;i<autovalues.length;i++){
186
                                if (banda[i]!=-1){
187
                                        resultOrden[i]=cont;
188
                                        cont--;
189
                                        }
190
                                        else noseleccionados++;
191
                        }
192
                
193
                        for(int i=0;i<autovalues.length;i++){
194
                                resultOrden[i]=resultOrden[i]- noseleccionados;
195
                        }
196
                
197
                        int bandas[]=new int [resultOrden.length -noseleccionados];        
198
                        for(int i=0; i< bandas.length;i++)
199
                                bandas[i]=i;
200
                
201
                        // Construccion del buffer para escritura. 
202
                        rasterResult=RasterBuffer.getBuffer(IBuffer.TYPE_DOUBLE, inputGrid.getLayerNX(), inputGrid.getLayerNY(), bandas.length, true);
203
                
204
                        double valor=0;
205
                
206
                        //                BUFFER TYPE BYTE
207
                        if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_BYTE){
208
                                // Construccion de la imagen en funcion de los componentes seleccionados.
209
                                for(int i=0; i<layerExtent.getNX();i++ ) {
210
                                        for (int j=0; j<layerExtent.getNY();j++){
211
                                                for (int k=0; k<inputGrid.getRasterBuf().getBandCount(); k++){
212
                                                        if (selectedPCs[k]){
213
                                                                for(int s=0;s<inputGrid.getRasterBuf().getBandCount();s++)
214
                                                                {   
215
                                                                        if (selectedPCs[s]){
216
                                                                        
217
                                                                                inputGrid.setBandToOperate(s);
218
                                                                                        try {
219
                                                                                                valor+=(double)inputGrid.getCellValueAsByte(i, j)*autoVectorMatrix.get(s,resultOrden[k]);
220
                                                                                        } catch (GridException e) {
221
                                                                                                RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e);
222
                                                                                        }
223
                                                                        }        
224
                                                                        
225
                                                                }
226
                                                                rasterResult.setElem(j, i, banda[k],(float) valor);
227
                                                                valor=0;
228
                                                        }
229
                                                }
230
                                        }
231
                                        percent = i*100/layerExtent.getNX();
232
                                }
233
                  
234
                        } // Fin caso RasterBuffer TypeByte 
235
                
236
        
237
                        //                BUFFER TYPE SHORT
238
                        if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_SHORT){
239
                        // Construccion de la imagen en funcion de los componentes seleccionados.
240
                                for(int i=0; i<layerExtent.getNX();i++ ) {
241
                                                for (int j=0; j<layerExtent.getNY();j++){
242
                                                        for (int k=0; k<inputGrid.getRasterBuf().getBandCount(); k++){
243
                                                                if (selectedPCs[k]){
244
                                                                        for(int s=0;s<inputGrid.getRasterBuf().getBandCount();s++)
245
                                                                        {   
246
                                                                                if (selectedPCs[s]){
247
                                                                                
248
                                                                                        inputGrid.setBandToOperate(s);
249
                                                                                                try {
250
                                                                                                        valor+=(double)inputGrid.getCellValueAsShort(i, j)*autoVectorMatrix.get(s,resultOrden[k]);
251
                                                                                                } catch (GridException e) {
252
                                                                                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e);
253
                                                                                                }
254
                                                                                }        
255
                                                                        }
256
                                                                        rasterResult.setElem(j, i, banda[k],valor);
257
                                                                        valor=0;        
258
                                                                }
259
                                                        }
260
                                                }
261
                                                percent = i*100/layerExtent.getNX();
262
                                        }
263
                          
264
                                } // Fin caso RasterBuffer TypeShort  
265
                        
266
                
267
                        //                BUFFER TYPE INT
268
                        if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_INT){
269
                        // Construccion de la imagen en funcion de los componentes seleccionados.
270
                                for(int i=0; i<layerExtent.getNX();i++ ) {
271
                                                for (int j=0; j<layerExtent.getNY();j++){
272
                                                        for (int k=0; k<inputGrid.getRasterBuf().getBandCount(); k++){
273
                                                                if (selectedPCs[k]){
274
                                                                        for(int s=0;s<inputGrid.getRasterBuf().getBandCount();s++)
275
                                                                        {   
276
                                                                                if (selectedPCs[s]){
277
                                                                                
278
                                                                                                inputGrid.setBandToOperate(s);
279
                                                                                                try {
280
                                                                                                        valor+=(double)inputGrid.getCellValueAsInt(i, j)*autoVectorMatrix.get(s,resultOrden[k]);
281
                                                                                                } catch (GridException e) {
282
                                                                                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e);
283
                                                                                                }                
284
                                                                                }        
285
                                                                        }
286
                                                                        rasterResult.setElem(j, i, banda[k],(float) valor);
287
                                                                        valor=0;        
288
                                                                }
289
                                                        }
290
                                                }
291
                                                percent = i*100/layerExtent.getNX();
292
                                        }
293
                                } // Fin caso RasterBuffer TypeInt 
294
                                        
295
                
296
                        //                BUFFER TYPE FLOAT
297
                        if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_FLOAT){
298
                                // Construccion de la imagen en funcion de los componentes seleccionados.
299
                                        for(int i=0; i<layerExtent.getNX();i++ ) {
300
                                                for (int j=0; j<layerExtent.getNY();j++){
301
                                                        for (int k=0; k<inputGrid.getRasterBuf().getBandCount(); k++){
302
                                                                if (selectedPCs[k]){
303
                                                                        for(int s=0;s<inputGrid.getRasterBuf().getBandCount();s++)
304
                                                                        {   
305
                                                                                if (selectedPCs[s]){
306
                                                                                
307
                                                                                                inputGrid.setBandToOperate(s);
308
                                                                                                try {
309
                                                                                                        valor+=(double)inputGrid.getCellValueAsFloat(i, j)*autoVectorMatrix.get(s,resultOrden[k]);
310
                                                                                                } catch (GridException e) {
311
                                                                                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e);
312
                                                                                                }
313
                                                                                }        
314
                                                                        }
315
                                                                        rasterResult.setElem(j, i,banda[k], (float)valor);
316
                                                                        valor=0;
317
                                                                        }
318
                                                        }
319
                                                }
320
                                                percent = i*100/layerExtent.getNX();
321
                                }
322
                          
323
                        } // Fin caso RasterBuffer TypeFloat
324
                
325
                
326
                        //                BUFFER TYPE DOUBLE
327
                        if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_DOUBLE){
328
                        // Construccion de la imagen en funcion de los componentes seleccionados.
329
                                for(int i=0; i<layerExtent.getNX();i++ ) {
330
                                                for (int j=0; j<layerExtent.getNY();j++){
331
                                                        for (int k=0; k<inputGrid.getRasterBuf().getBandCount(); k++){
332
                                                                if (selectedPCs[k]){
333
                                                                        for(int s=0;s<inputGrid.getRasterBuf().getBandCount();s++)
334
                                                                        {   
335
                                                                                if (selectedPCs[s]){
336
                                                                                
337
                                                                                                inputGrid.setBandToOperate(s);
338
                                                                                                try {
339
                                                                                                        valor+=inputGrid.getCellValueAsDouble(i, j)*autoVectorMatrix.get(s,resultOrden[k]);
340
                                                                                                } catch (GridException e) {
341
                                                                                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e);
342
                                                                                                }
343
                                                                                }        
344
                                                                        }
345
                                                                        rasterResult.setElem(j, i,banda[k], (float)valor);
346
                                                                        valor=0;        
347
                                                                }
348
                                                        }
349
                                                }
350
                                                percent = i*100/layerExtent.getNX();
351
                                }
352
                          
353
                        } // Fin caso RasterBuffer TypeDouble 
354
                
355
                        // escritutra a fichero
356
                        writeToFile();                
357
        }
358
        
359
        
360
        /**
361
         * Escritura del resultado en disco.
362
         */
363
        public void writeToFile(){
364
                try{
365
                        // Escritura de los datos a fichero temporal
366
                        String fileName=layerName;
367
                        int endIndex = fileName.lastIndexOf(".");
368
                        if (endIndex < 0)
369
                                 endIndex = fileName.length();
370
                        GeoRasterWriter grw = null;
371
                        writerBufferServer = new WriterBufferServer(rasterResult);
372
                        grw = GeoRasterWriter.getWriter(writerBufferServer, layerName, rasterResult.getBandCount(),raster.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(layerName).getParams(), null);
373
                        grw.dataWrite();
374
                        grw.setWkt(raster.getWktProjection());
375
                        grw.writeClose();
376
                        rasterResult.free();
377
                        String viewName = PluginServices.getMDIManager().getWindowInfo(view).getTitle();
378
                        RasterToolsUtil.loadLayer(viewName, layerName, null);
379
        
380
                } catch (NotSupportedExtensionException e) {
381
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
382
                } catch (IOException e) {
383
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
384
                } catch (InterruptedException e) {
385
                                Thread.currentThread().interrupt();
386
                } catch (RasterNotLoadException e) {
387
                                RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
388
                } catch (RasterDriverException e) {
389
                        e.printStackTrace();
390
                }
391

    
392
        }
393
        
394
}
395

    
396
        
397