Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / process / GeoreferencingProcess.java @ 19621

History | View | Annotate | Download (12.8 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.georeferencing.process;
42

    
43
import java.awt.geom.AffineTransform;
44
import java.io.IOException;
45

    
46
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
47
import org.gvsig.georeferencing.process.geotransform.GeoTransformDataResult;
48
import org.gvsig.georeferencing.process.geotransform.GeoTransformProcess;
49
import org.gvsig.raster.IProcessActions;
50
import org.gvsig.raster.RasterProcess;
51
import org.gvsig.raster.buffer.BufferFactory;
52
import org.gvsig.raster.buffer.RasterBuffer;
53
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
54
import org.gvsig.raster.buffer.RasterBufferInvalidException;
55
import org.gvsig.raster.buffer.WriterBufferServer;
56
import org.gvsig.raster.dataset.GeoRasterWriter;
57
import org.gvsig.raster.dataset.IBuffer;
58
import org.gvsig.raster.dataset.IRasterDataSource;
59
import org.gvsig.raster.dataset.NotSupportedExtensionException;
60
import org.gvsig.raster.dataset.io.RasterDriverException;
61
import org.gvsig.raster.datastruct.GeoPoint;
62
import org.gvsig.raster.grid.Grid;
63
import org.gvsig.raster.grid.GridExtent;
64
import org.gvsig.raster.grid.GridInterpolated;
65
import org.gvsig.raster.grid.OutOfGridException;
66
import org.gvsig.raster.util.RasterToolsUtil;
67

    
68
import com.iver.andami.PluginServices;
69

    
70
/**
71
 *  Clase que representa una proceso de georreferenciacion de un raster.
72
 *  
73
 *  @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
74
 *         @version 10/2/2008
75
 **/
76
public class GeoreferencingProcess extends RasterProcess implements IProcessActions{
77
        
78
        //Capa a georreferenciar
79
        private FLyrRasterSE             rasterSE                        = null;
80
        
81
        //Grid resultante de georreferenciacion
82
        private Grid                     imageGrid          = null;
83
        
84
        //Fichero de salida
85
        private String                   filename           = null;
86
        
87
        // resutado de la transformacion 
88
        private GeoTransformDataResult   transformData      = null;
89
        
90
        // Lista puntos de control
91
        private GeoPoint                 gpcs[]             = null;
92
        
93
        //Extend de imagen corregida
94
        GridExtent                       newExtend          = null;
95
        
96
        // Metodo de resampleado utilizado
97
        private int                      rMethod            = 0;
98
        
99
        //Indicador de progreso
100
        private int                      percent            = 0;
101
        
102
        // Grid resultado
103
        private Grid                     gridResult         = null;
104
        
105
        WriterBufferServer               writerBufferServer = null;
106

    
107
        private int                      orden              = 0;
108
        
109
        private int[]                    bands              = null;
110
        
111
        //Tama?o de celda en X si es pasada por el usuario
112
        private double                   xCellSize           = 0;
113
        //Tama?o de celda en Y si es pasada por el usuario
114
        private double                   yCellSize           = 0;
115
        
116
        /** Metodo que recoge los parametros del proceso georreferenciacion de un raster
117
        * <LI>rasterSE: capa a georeferenciar</LI>
118
        * <LI>filename: path con el fichero de salida</LI>
119
        * <LI>method: metodo de resampleo </LI>
120
        */
121
        public void init() {
122
                
123
                rasterSE = (FLyrRasterSE)getParam("fLayer");
124
                filename = (String)getParam("filename");
125
                rMethod = (int)getIntParam("method");
126
                gpcs = (GeoPoint[])getParam("gpcs");
127
                orden= (int)getIntParam("orden");
128
                bands = new int[rasterSE.getBandCount()];
129
                xCellSize = (double)getDoubleParam("xCellSize");
130
                yCellSize = (double)getDoubleParam("yCellSize");
131
                
132
                for(int i=0; i<rasterSE.getBandCount(); i++)
133
                        bands[i]= i;
134
                // Inicializacion del grid correspondiente a la imagen a corregir
135
                IRasterDataSource dsetCopy = null; 
136
                dsetCopy = rasterSE.getDataSource().newDataset();
137
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
138
                if (!RasterBuffer.loadInMemory(dsetCopy))
139
                        bufferFactory.setReadOnly(true);        
140
                try {
141
                                imageGrid = new Grid(bufferFactory);        
142
                }catch (RasterBufferInvalidException e) {
143
                        e.printStackTrace();                        
144
                }
145
        }
146
        
147

    
148
        public void process() throws InterruptedException {
149
                
150
                GeoTransformProcess transform = new GeoTransformProcess();
151
                transform.setActions(this);
152
                transform.addParam("gpcs",gpcs);
153
                transform.addParam("orden",new Integer(orden));
154
                transform.run();                
155
                
156
                // Obtenida la transformacion la aplicamos a los puntos extremos de la imagen
157
                double p1[]=transformData.getCoordMap(imageGrid.getGridExtent().getLLX(),imageGrid.getGridExtent().getLLY());
158
                double p2[]=transformData.getCoordMap(imageGrid.getGridExtent().getULX(),imageGrid.getGridExtent().getULY());
159
                double p3[]=transformData.getCoordMap(imageGrid.getGridExtent().getURX(),imageGrid.getGridExtent().getURY());
160
                double p4[]=transformData.getCoordMap(imageGrid.getGridExtent().getLRX(),imageGrid.getGridExtent().getLRY());
161
                
162
                double xmin=Math.min( Math.min(p1[0],p2[0]),Math.min(p3[0],p4[0]));
163
                double ymin=Math.min( Math.min(p1[1],p2[1]),Math.min(p3[1],p4[1]));
164
                double xmax=Math.max( Math.max(p1[0],p2[0]),Math.max(p3[0],p4[0]));        
165
                double ymax=Math.max( Math.max(p1[1],p2[1]),Math.max(p3[1],p4[1]));
166
                
167
                // OJO CON EL TAMA?O DE CELDA: CONSULTAR PARA EJEMPLO DE JOSE ES 25
168
                if(xCellSize <= 0)
169
                        xCellSize = (xmax - xmin) / (double)rasterSE.getPxWidth();
170
                if(yCellSize <= 0)
171
                        yCellSize = (ymax - ymin) / (double)rasterSE.getPxHeight();
172
                        
173
                newExtend= new GridExtent(xmin, ymin, xmax, ymax, xCellSize);
174
                int datatype= rasterSE.getBufferFactory().getRasterBuf().getDataType();
175
                
176
                try {
177
                        gridResult = new Grid(newExtend, newExtend, datatype, bands);
178
                } catch (RasterBufferInvalidException e) {
179
                        RasterToolsUtil.messageBoxError("error_grid", this, e);
180
                }
181
                
182
                double minPointX=gridResult.getGridExtent().getMin().getX();
183
                double maxPointY=gridResult.getGridExtent().getMax().getY();
184
                double cellsize=gridResult.getCellSize();
185
        
186
                
187
                GridInterpolated gridInterpolated=null;
188
                gridInterpolated = new GridInterpolated((RasterBuffer)imageGrid.getRasterBuf(),imageGrid.getGridExtent(),imageGrid.getGridExtent(),bands);
189
                
190
                // SE ESTABLECE EL METODO DE INTERPOLACION (por defecto vecino mas proximo)
191
                if(rMethod==GridInterpolated.INTERPOLATION_BicubicSpline)
192
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_BicubicSpline);
193
                else if(rMethod==GridInterpolated.INTERPOLATION_Bilinear)
194
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_Bilinear);
195
                else
196
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_NearestNeighbour);
197
                
198
                double coord[]=null;
199
                try {
200
                
201
                        if(datatype==IBuffer.TYPE_BYTE)
202
                        {
203
                                byte values[]=new byte[bands.length];
204
                                for(int col=0; col<gridResult.getLayerNX(); col++){
205
                                        for(int row=0; row<gridResult.getLayerNY();row++)
206
                                                for(int band=0; band<bands.length;band++)
207
                                                {
208
                                                        coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);        
209
                                                        gridInterpolated.setBandToOperate(band);
210
                                                        values[band] = (byte)gridInterpolated._getValueAt(coord[0],coord[1]);
211
                                                        gridResult.setBandToOperate(band);
212
                                                        gridResult.setCellValue(col,row,(byte)values[band]);
213
                                                }
214
                                                percent= (int)col*100/gridResult.getLayerNX();        
215
                                }        
216
                        }
217
                        
218
                        if(datatype==IBuffer.TYPE_SHORT)
219
                        {
220
                                short values[]=new short[bands.length];
221
                                for(int col=0; col<gridResult.getLayerNX(); col++){
222
                                        for(int row=0; row<gridResult.getLayerNY();row++)
223
                                                for(int band=0; band<bands.length;band++)
224
                                                {
225
                                                        coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);        
226
                                                        gridInterpolated.setBandToOperate(band);
227
                                                        values[band] = (short)gridInterpolated._getValueAt(coord[0],coord[1]);
228
                                                        gridResult.setBandToOperate(band);
229
                                                        gridResult.setCellValue(col,row,(short)values[band]);
230
                                                }
231
                                                percent= (int)col*100/gridResult.getLayerNX();        
232
                                }        
233
                        }
234
                        
235
                        if(datatype==IBuffer.TYPE_INT)
236
                        {
237
                                int values[]=new int[bands.length];
238
                                for(int col=0; col<gridResult.getLayerNX(); col++){
239
                                        for(int row=0; row<gridResult.getLayerNY();row++)
240
                                                for(int band=0; band<bands.length;band++)
241
                                                {
242
                                                        coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);        
243
                                                        gridInterpolated.setBandToOperate(band);
244
                                                        values[band] = (int) gridInterpolated._getValueAt(coord[0],coord[1]);
245
                                                        gridResult.setBandToOperate(band);
246
                                                        gridResult.setCellValue(col,row,(int)values[band]);
247
                                                }
248
                                                percent= (int)col*100/gridResult.getLayerNX();        
249
                                }        
250
                        }
251
                        
252
                        if(datatype==IBuffer.TYPE_FLOAT)
253
                        {        
254
                                float values[]=new float[bands.length];
255
                                for(int col=0; col<gridResult.getLayerNX(); col++){
256
                                        for(int row=0; row<gridResult.getLayerNY();row++)
257
                                                for(int band=0; band<bands.length;band++)
258
                                                {
259
                                                        coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);        
260
                                                        gridInterpolated.setBandToOperate(band);
261
                                                        values[band] = (float) gridInterpolated._getValueAt(coord[0],coord[1]);
262
                                                        gridResult.setBandToOperate(band);
263
                                                        gridResult.setCellValue(col,row,(float)values[band]);
264
                                                }
265
                                                percent= (int)col*100/gridResult.getLayerNX();        
266
                                }        
267
                        }
268
                        
269
                        if(datatype==IBuffer.TYPE_DOUBLE)
270
                        {
271
                                double values[]=new double[bands.length];
272
                                for(int col=0; col<gridResult.getLayerNX(); col++){
273
                                        for(int row=0; row<gridResult.getLayerNY();row++)
274
                                                for(int band=0; band<bands.length;band++)
275
                                                {
276
                                                        coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);        
277
                                                        gridInterpolated.setBandToOperate(band);
278
                                                        values[band] = gridInterpolated._getValueAt(coord[0],coord[1]);
279
                                                        gridResult.setBandToOperate(band);
280
                                                        gridResult.setCellValue(col,row,(double)values[band]);
281
                                                }
282
                                                percent= (int)col*100/gridResult.getLayerNX();        
283
                                }        
284
                        }
285
                        
286
                } catch (OutOfGridException e) {
287
                        e.printStackTrace();
288
                } catch (RasterBufferInvalidAccessException e) {
289
                        // TODO Auto-generated catch block
290
                        e.printStackTrace();
291
                } catch (RasterBufferInvalidException e) {
292
                        // TODO Auto-generated catch block
293
                        e.printStackTrace();
294
                }
295
                
296
                generateLayer();
297
                if(externalActions!=null)
298
                        externalActions.end(filename);
299
        }
300

    
301

    
302
        private void generateLayer(){        
303
        
304
                GeoRasterWriter grw = null;
305
                IBuffer buffer= gridResult.getRasterBuf();
306
            writerBufferServer = new WriterBufferServer(buffer);
307
                AffineTransform aTransform = new AffineTransform(newExtend.getCellSize(),0.0,0.0,-newExtend.getCellSize(),newExtend.getMin().getX(),newExtend.getMax().getY());
308
                int endIndex =filename.lastIndexOf(".");
309
                if (endIndex < 0)
310
                        endIndex = filename.length();
311
                try {
312
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename,gridResult.getRasterBuf().getBandCount(),aTransform, gridResult.getRasterBuf().getWidth(),gridResult.getRasterBuf().getHeight(), gridResult.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(),null);
313
                        grw.dataWrite();
314
                        grw.setWkt(rasterSE.getWktProjection());
315
                        grw.writeClose();
316
                } catch (NotSupportedExtensionException e1) {
317
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1);
318
                } catch (RasterDriverException e1) {
319
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e1);        
320
                } catch (IOException e) {
321
                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
322
                } catch (InterruptedException e) {
323
                        Thread.currentThread().interrupt();
324
                }
325
        }
326
                
327

    
328
        /*
329
         * (non-Javadoc)
330
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
331
         */
332
        public String getTitle() {
333
                return PluginServices.getText(this,"georreferenciacion_process");
334
        }
335
        
336
        /*
337
         * (non-Javadoc)
338
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
339
         */
340
        public int getPercent() {
341
                if(writerBufferServer==null)
342
                        return percent;
343
                else
344
                        return writerBufferServer.getPercent();
345
        }
346

    
347
        
348
        
349
        public void interrupted() {
350
                // TODO Auto-generated method stub        
351
        }
352

    
353
        public void end(Object param) {        
354
                transformData = (GeoTransformDataResult)param;        
355
        }
356

    
357
}