Statistics
| Revision:

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

History | View | Annotate | Download (12 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
        /** Metodo que recoge los parametros del proceso georreferenciacion de un raster
112
        * <LI>rasterSE: capa a georeferenciar</LI>
113
        * <LI>filename: path con el fichero de salida</LI>
114
        * <LI>method: metodo de resampleo </LI>
115
        */
116
        public void init() {
117
                
118
                rasterSE= (FLyrRasterSE)getParam("fLayer");
119
                filename= (String)getParam("filename");
120
                rMethod=(int)getIntParam("method");
121
                gpcs = (GeoPoint[])getParam("gpcs");
122
                orden= (int)getIntParam("orden");
123
                bands= new int[rasterSE.getBandCount()];
124
                for(int i=0; i<rasterSE.getBandCount(); i++)
125
                        bands[i]= i;
126
                // Inicializacion del grid correspondiente a la imagen a corregir
127
                IRasterDataSource dsetCopy = null; 
128
                dsetCopy = rasterSE.getDataSource().newDataset();
129
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
130
                if (!RasterBuffer.loadInMemory(dsetCopy))
131
                        bufferFactory.setReadOnly(true);        
132
                try {
133
                                imageGrid = new Grid(bufferFactory);        
134
                }catch (RasterBufferInvalidException e) {
135
                        e.printStackTrace();                        
136
                }
137
        }
138
        
139

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

    
288

    
289
        private void generateLayer(){        
290
        
291
                GeoRasterWriter grw = null;
292
                IBuffer buffer= gridResult.getRasterBuf();
293
            writerBufferServer = new WriterBufferServer(buffer);
294
                AffineTransform aTransform = new AffineTransform(newExtend.getCellSize(),0.0,0.0,-newExtend.getCellSize(),newExtend.getMin().getX(),newExtend.getMax().getY());
295
                int endIndex =filename.lastIndexOf(".");
296
                if (endIndex < 0)
297
                        endIndex = filename.length();
298
                try {
299
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename,gridResult.getRasterBuf().getBandCount(),aTransform, gridResult.getRasterBuf().getWidth(),gridResult.getRasterBuf().getHeight(), gridResult.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(),null);
300
                        grw.dataWrite();
301
                        grw.setWkt(rasterSE.getWktProjection());
302
                        grw.writeClose();
303
                } catch (NotSupportedExtensionException e1) {
304
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1);
305
                } catch (RasterDriverException e1) {
306
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e1);        
307
                } catch (IOException e) {
308
                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
309
                } catch (InterruptedException e) {
310
                        Thread.currentThread().interrupt();
311
                }
312
        }
313
                
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
318
         */
319
        public String getTitle() {
320
                return PluginServices.getText(this,"georreferenciacion_process");
321
        }
322
        
323
        /*
324
         * (non-Javadoc)
325
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
326
         */
327
        public int getPercent() {
328
                if(writerBufferServer==null)
329
                        return percent;
330
                else
331
                        return writerBufferServer.getPercent();
332
        }
333

    
334
        
335
        
336
        public void interrupted() {
337
                // TODO Auto-generated method stub        
338
        }
339

    
340
        public void end(Object param) {        
341
                transformData = (GeoTransformDataResult)param;        
342
        }
343

    
344
}