Statistics
| Revision:

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

History | View | Annotate | Download (13 KB)

1 18530 nbrodin
/* 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 18808 amunoz
import java.awt.geom.AffineTransform;
44
import java.io.IOException;
45
46 18777 amunoz
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
47
import org.gvsig.georeferencing.process.geotransform.GeoTransformProcess;
48
import org.gvsig.raster.IProcessActions;
49
import org.gvsig.raster.RasterProcess;
50
import org.gvsig.raster.buffer.BufferFactory;
51
import org.gvsig.raster.buffer.RasterBuffer;
52 18959 amunoz
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
53 18777 amunoz
import org.gvsig.raster.buffer.RasterBufferInvalidException;
54 18858 nbrodin
import org.gvsig.raster.buffer.WriterBufferServer;
55 18808 amunoz
import org.gvsig.raster.dataset.GeoRasterWriter;
56
import org.gvsig.raster.dataset.IBuffer;
57 18777 amunoz
import org.gvsig.raster.dataset.IRasterDataSource;
58 18808 amunoz
import org.gvsig.raster.dataset.NotSupportedExtensionException;
59
import org.gvsig.raster.dataset.io.RasterDriverException;
60 18786 amunoz
import org.gvsig.raster.datastruct.GeoPoint;
61 18777 amunoz
import org.gvsig.raster.grid.Grid;
62
import org.gvsig.raster.grid.GridExtent;
63 18959 amunoz
import org.gvsig.raster.grid.GridInterpolated;
64 18786 amunoz
import org.gvsig.raster.grid.OutOfGridException;
65
import org.gvsig.raster.util.RasterToolsUtil;
66 18777 amunoz
67
import com.iver.andami.PluginServices;
68
69 18530 nbrodin
/**
70
 *  Clase que representa una proceso de georreferenciacion de un raster.
71
 *
72
 *  @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
73 18808 amunoz
 *         @version 10/2/2008
74 18530 nbrodin
 **/
75 18777 amunoz
public class GeoreferencingProcess extends RasterProcess implements IProcessActions{
76
77
        //Capa a georreferenciar
78 19591 nbrodin
        private FLyrRasterSE             rasterSE                        = null;
79 18777 amunoz
80
        //Grid resultante de georreferenciacion
81 19591 nbrodin
        private Grid                     imageGrid          = null;
82 18777 amunoz
83
        //Fichero de salida
84 19591 nbrodin
        private String                   filename           = null;
85 18777 amunoz
86
87 18786 amunoz
        // Lista puntos de control
88 19591 nbrodin
        private GeoPoint                 gpcs[]             = null;
89 18786 amunoz
90 18777 amunoz
        //Extend de imagen corregida
91 19591 nbrodin
        GridExtent                       newExtend          = null;
92 18777 amunoz
93
        // Metodo de resampleado utilizado
94 19591 nbrodin
        private int                      rMethod            = 0;
95 18777 amunoz
96
        //Indicador de progreso
97 19591 nbrodin
        private int                      percent            = 0;
98 18777 amunoz
99 18786 amunoz
        // Grid resultado
100 19591 nbrodin
        private Grid                     gridResult         = null;
101 18786 amunoz
102 19591 nbrodin
        WriterBufferServer               writerBufferServer = null;
103 18833 amunoz
104 19591 nbrodin
        private int                      orden              = 0;
105 18786 amunoz
106 19591 nbrodin
        private int[]                    bands              = null;
107 18849 amunoz
108 19621 nbrodin
        //Tama?o de celda en X si es pasada por el usuario
109
        private double                   xCellSize           = 0;
110
        //Tama?o de celda en Y si es pasada por el usuario
111
        private double                   yCellSize           = 0;
112 19591 nbrodin
113 18777 amunoz
        /** Metodo que recoge los parametros del proceso georreferenciacion de un raster
114
        * <LI>rasterSE: capa a georeferenciar</LI>
115
        * <LI>filename: path con el fichero de salida</LI>
116 18808 amunoz
        * <LI>method: metodo de resampleo </LI>
117 18777 amunoz
        */
118
        public void init() {
119 18808 amunoz
120 19591 nbrodin
                rasterSE = (FLyrRasterSE)getParam("fLayer");
121
                filename = (String)getParam("filename");
122
                rMethod = (int)getIntParam("method");
123 18786 amunoz
                gpcs = (GeoPoint[])getParam("gpcs");
124 18833 amunoz
                orden= (int)getIntParam("orden");
125 19591 nbrodin
                bands = new int[rasterSE.getBandCount()];
126 19621 nbrodin
                xCellSize = (double)getDoubleParam("xCellSize");
127
                yCellSize = (double)getDoubleParam("yCellSize");
128 19591 nbrodin
129 18849 amunoz
                for(int i=0; i<rasterSE.getBandCount(); i++)
130
                        bands[i]= i;
131 18808 amunoz
                // Inicializacion del grid correspondiente a la imagen a corregir
132 18777 amunoz
                IRasterDataSource dsetCopy = null;
133
                dsetCopy = rasterSE.getDataSource().newDataset();
134
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
135
                if (!RasterBuffer.loadInMemory(dsetCopy))
136
                        bufferFactory.setReadOnly(true);
137
                try {
138
                                imageGrid = new Grid(bufferFactory);
139
                }catch (RasterBufferInvalidException e) {
140
                        e.printStackTrace();
141
                }
142
        }
143
144 18808 amunoz
145 18777 amunoz
        public void process() throws InterruptedException {
146 18808 amunoz
147 18777 amunoz
                GeoTransformProcess transform = new GeoTransformProcess();
148
                transform.setActions(this);
149 18786 amunoz
                transform.addParam("gpcs",gpcs);
150 18833 amunoz
                transform.addParam("orden",new Integer(orden));
151 18834 amunoz
                transform.run();
152 18777 amunoz
153
                // Obtenida la transformacion la aplicamos a los puntos extremos de la imagen
154 20026 amunoz
                double p1[]=transform.getCoordMap(0,0);
155
                double p2[]=transform.getCoordMap(rasterSE.getPxWidth(),0);
156
                double p3[]=transform.getCoordMap(0,rasterSE.getPxHeight());
157
                double p4[]=transform.getCoordMap(rasterSE.getPxWidth(),rasterSE.getPxHeight());
158 20005 amunoz
159 18777 amunoz
160 20019 amunoz
                double xmin=Math.min(p1[0],p3[0]);
161
                double ymin=Math.min(p3[1],p4[1]);
162
                double xmax=Math.max(p2[0],p4[0]);
163
                double ymax=Math.max(p1[1],p2[1]);
164 18777 amunoz
165 19622 nbrodin
                if(xCellSize <= 1)
166 19621 nbrodin
                        xCellSize = (xmax - xmin) / (double)rasterSE.getPxWidth();
167 19622 nbrodin
                if(yCellSize <= 1)
168 19621 nbrodin
                        yCellSize = (ymax - ymin) / (double)rasterSE.getPxHeight();
169 20914 amunoz
170 19621 nbrodin
                newExtend= new GridExtent(xmin, ymin, xmax, ymax, xCellSize);
171 18786 amunoz
                int datatype= rasterSE.getBufferFactory().getRasterBuf().getDataType();
172 19298 amunoz
173 18786 amunoz
                try {
174 19591 nbrodin
                        gridResult = new Grid(newExtend, newExtend, datatype, bands);
175 18786 amunoz
                } catch (RasterBufferInvalidException e) {
176
                        RasterToolsUtil.messageBoxError("error_grid", this, e);
177
                }
178
179
                double minPointX=gridResult.getGridExtent().getMin().getX();
180 19168 dguerrero
                double maxPointY=gridResult.getGridExtent().getMax().getY();
181 19574 amunoz
                double cellsize=gridResult.getCellSize();
182
183 18959 amunoz
184
                GridInterpolated gridInterpolated=null;
185
                gridInterpolated = new GridInterpolated((RasterBuffer)imageGrid.getRasterBuf(),imageGrid.getGridExtent(),imageGrid.getGridExtent(),bands);
186
187
                // SE ESTABLECE EL METODO DE INTERPOLACION (por defecto vecino mas proximo)
188
                if(rMethod==GridInterpolated.INTERPOLATION_BicubicSpline)
189
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_BicubicSpline);
190
                else if(rMethod==GridInterpolated.INTERPOLATION_Bilinear)
191
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_Bilinear);
192
                else
193
                        gridInterpolated.setInterpolationMethod(GridInterpolated.INTERPOLATION_NearestNeighbour);
194
195 18786 amunoz
                double coord[]=null;
196
                try {
197
198 18812 amunoz
                        if(datatype==IBuffer.TYPE_BYTE)
199
                        {
200 18959 amunoz
                                byte values[]=new byte[bands.length];
201 19948 amunoz
                                int progress=0;
202 20005 amunoz
                                // OPTIMIZACION. Se esta recorriendo secuencialmente cada banda.
203 19948 amunoz
                                for(int band=0; band<bands.length;band++){
204
                                        gridResult.setBandToOperate(band);
205
                                        gridInterpolated.setBandToOperate(band);
206 20005 amunoz
                                        for(int row=0; row<gridResult.getLayerNY(); row++){
207 19948 amunoz
                                                progress++;
208 20005 amunoz
                                                for(int col=0; col<gridResult.getLayerNX();col++)
209 19948 amunoz
                                                        {
210
                                                                coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);
211
                                                                values[band] = (byte)gridInterpolated._getValueAt(coord[0],coord[1]);
212
                                                                gridResult.setCellValue(col,row,(byte)values[band]);
213
                                                        }
214 20005 amunoz
                                                percent=(int)( progress*100/(gridResult.getLayerNY()*bands.length));
215 19948 amunoz
                                        }
216 18786 amunoz
                                }
217 18812 amunoz
                        }
218
219
                        if(datatype==IBuffer.TYPE_SHORT)
220
                        {
221 18966 amunoz
                                short values[]=new short[bands.length];
222 19948 amunoz
                                int progress=0;
223 20005 amunoz
                                // OPTIMIZACION. Se esta recorriendo secuencialmente cada banda.
224 19948 amunoz
                                for(int band=0; band<bands.length;band++){
225
                                        gridResult.setBandToOperate(band);
226
                                        gridInterpolated.setBandToOperate(band);
227 20005 amunoz
                                        for(int row=0; row<gridResult.getLayerNY(); row++){
228 19948 amunoz
                                                progress++;
229 20005 amunoz
                                                for(int col=0; col<gridResult.getLayerNX();col++)
230
                                                        {
231
                                                                coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);
232
                                                                values[band] = (short)gridInterpolated._getValueAt(coord[0],coord[1]);
233
                                                                gridResult.setCellValue(col,row,(short)values[band]);
234
                                                        }
235
                                                percent=(int)( progress*100/(gridResult.getLayerNY()*bands.length));
236 19948 amunoz
                                        }
237 20005 amunoz
                                }
238 18812 amunoz
                        }
239
240
                        if(datatype==IBuffer.TYPE_INT)
241
                        {
242 18966 amunoz
                                int values[]=new int[bands.length];
243 19948 amunoz
                                int progress=0;
244 20005 amunoz
                                // OPTIMIZACION. Se esta recorriendo secuencialmente cada banda.
245 19948 amunoz
                                for(int band=0; band<bands.length;band++){
246
                                        gridResult.setBandToOperate(band);
247
                                        gridInterpolated.setBandToOperate(band);
248 20005 amunoz
                                        for(int row=0; row<gridResult.getLayerNY(); row++){
249 19948 amunoz
                                                progress++;
250 20005 amunoz
                                                for(int col=0; col<gridResult.getLayerNX();col++)
251
                                                        {
252
                                                                coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);
253
                                                                values[band] = (int)gridInterpolated._getValueAt(coord[0],coord[1]);
254
                                                                gridResult.setCellValue(col,row,(int)values[band]);
255
                                                        }
256
                                                percent=(int)( progress*100/(gridResult.getLayerNY()*bands.length));
257 19948 amunoz
                                        }
258 20005 amunoz
                                }
259 18812 amunoz
                        }
260
261
                        if(datatype==IBuffer.TYPE_FLOAT)
262 18966 amunoz
                        {
263
                                float values[]=new float[bands.length];
264 19948 amunoz
                                int progress=0;
265 20005 amunoz
                                // OPTIMIZACION. Se esta recorriendo secuencialmente cada banda.
266 19948 amunoz
                                for(int band=0; band<bands.length;band++){
267
                                        gridResult.setBandToOperate(band);
268
                                        gridInterpolated.setBandToOperate(band);
269 20005 amunoz
                                        for(int row=0; row<gridResult.getLayerNY(); row++){
270 19948 amunoz
                                                progress++;
271 20005 amunoz
                                                for(int col=0; col<gridResult.getLayerNX();col++)
272
                                                        {
273
                                                                coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);
274
                                                                values[band] = (float)gridInterpolated._getValueAt(coord[0],coord[1]);
275
                                                                gridResult.setCellValue(col,row,(float)values[band]);
276
                                                        }
277
                                                percent=(int)( progress*100/(gridResult.getLayerNY()*bands.length));
278 19948 amunoz
                                        }
279 20005 amunoz
                                }
280 18812 amunoz
                        }
281
282 18966 amunoz
                        if(datatype==IBuffer.TYPE_DOUBLE)
283 18812 amunoz
                        {
284 18966 amunoz
                                double values[]=new double[bands.length];
285 19948 amunoz
                                int progress=0;
286 20005 amunoz
                                // OPTIMIZACION. Se esta recorriendo secuencialmente cada banda.
287 19948 amunoz
                                for(int band=0; band<bands.length;band++){
288
                                        gridResult.setBandToOperate(band);
289
                                        gridInterpolated.setBandToOperate(band);
290 20005 amunoz
                                        for(int row=0; row<gridResult.getLayerNY(); row++){
291 19948 amunoz
                                                progress++;
292 20005 amunoz
                                                for(int col=0; col<gridResult.getLayerNX();col++)
293
                                                        {
294
                                                                coord=transform.getCoordPixel(col*cellsize+minPointX, maxPointY-row*cellsize);
295
                                                                values[band] = (double)gridInterpolated._getValueAt(coord[0],coord[1]);
296
                                                                gridResult.setCellValue(col,row,(double)values[band]);
297
                                                        }
298
                                                percent=(int)( progress*100/(gridResult.getLayerNY()*bands.length));
299 19948 amunoz
                                        }
300 20005 amunoz
                                }
301 18966 amunoz
                        }
302 18812 amunoz
303 18786 amunoz
                } catch (OutOfGridException e) {
304
                        e.printStackTrace();
305 18959 amunoz
                } catch (RasterBufferInvalidAccessException e) {
306
                        e.printStackTrace();
307
                } catch (RasterBufferInvalidException e) {
308
                        e.printStackTrace();
309 18786 amunoz
                }
310
311 18808 amunoz
                generateLayer();
312
                if(externalActions!=null)
313
                        externalActions.end(filename);
314
        }
315 18786 amunoz
316 18808 amunoz
317 19304 amunoz
        private void generateLayer(){
318
319 18808 amunoz
                GeoRasterWriter grw = null;
320
                IBuffer buffer= gridResult.getRasterBuf();
321 18812 amunoz
            writerBufferServer = new WriterBufferServer(buffer);
322 18808 amunoz
                AffineTransform aTransform = new AffineTransform(newExtend.getCellSize(),0.0,0.0,-newExtend.getCellSize(),newExtend.getMin().getX(),newExtend.getMax().getY());
323
                int endIndex =filename.lastIndexOf(".");
324
                if (endIndex < 0)
325
                        endIndex = filename.length();
326
                try {
327
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename,gridResult.getRasterBuf().getBandCount(),aTransform, gridResult.getRasterBuf().getWidth(),gridResult.getRasterBuf().getHeight(), gridResult.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(),null);
328
                        grw.dataWrite();
329
                        grw.setWkt(rasterSE.getWktProjection());
330
                        grw.writeClose();
331
                } catch (NotSupportedExtensionException e1) {
332
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1);
333
                } catch (RasterDriverException e1) {
334 19304 amunoz
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e1);
335 18808 amunoz
                } catch (IOException e) {
336 19304 amunoz
                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
337 18808 amunoz
                } catch (InterruptedException e) {
338
                        Thread.currentThread().interrupt();
339
                }
340 18777 amunoz
        }
341 18808 amunoz
342 19304 amunoz
343 18814 amunoz
        /*
344
         * (non-Javadoc)
345
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
346
         */
347 18777 amunoz
        public String getTitle() {
348
                return PluginServices.getText(this,"georreferenciacion_process");
349
        }
350 18814 amunoz
351
        /*
352
         * (non-Javadoc)
353
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
354
         */
355 18777 amunoz
        public int getPercent() {
356 18812 amunoz
                if(writerBufferServer==null)
357
                        return percent;
358
                else
359 18966 amunoz
                        return writerBufferServer.getPercent();
360 18777 amunoz
        }
361 18530 nbrodin
362 18814 amunoz
363 20914 amunoz
        /*
364
         * (non-Javadoc)
365
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
366
         */
367
        public String getLog() {
368
                return PluginServices.getText(this,"georreferencing_log_message");
369
        }
370
371 18966 amunoz
372 18777 amunoz
        public void interrupted() {
373 19304 amunoz
                // TODO Auto-generated method stub
374 18777 amunoz
        }
375 18530 nbrodin
376 18777 amunoz
        public void end(Object param) {
377
        }
378
379 18530 nbrodin
}