Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / process / GeoreferencingProcess.java @ 18814

History | View | Annotate | Download (10.5 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.RasterBufferInvalidException;
54
import org.gvsig.raster.dataset.GeoRasterWriter;
55
import org.gvsig.raster.dataset.IBuffer;
56
import org.gvsig.raster.dataset.IRasterDataSource;
57
import org.gvsig.raster.dataset.NotSupportedExtensionException;
58
import org.gvsig.raster.dataset.io.RasterDriverException;
59
import org.gvsig.raster.datastruct.GeoPoint;
60
import org.gvsig.raster.grid.Grid;
61
import org.gvsig.raster.grid.GridException;
62
import org.gvsig.raster.grid.GridExtent;
63
import org.gvsig.raster.grid.OutOfGridException;
64
import org.gvsig.raster.util.RasterToolsUtil;
65

    
66
import com.iver.andami.PluginServices;
67

    
68
/**
69
 *  Clase que representa una proceso de georreferenciacion de un raster.
70
 *  
71
 *  @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
72
 *         @version 10/2/2008
73
 **/
74
public class GeoreferencingProcess extends RasterProcess implements IProcessActions{
75
        
76
        //Capa a georreferenciar
77
        private FLyrRasterSE rasterSE                        = null;
78
        
79
        //Grid resultante de georreferenciacion
80
        private Grid imageGrid = null;
81
        
82
        //Fichero de salida
83
        private String filename = null;
84
        
85
        // resutado de la transformacion 
86
        private GeoTransformDataResult transformData=null;
87
        
88
        // Lista puntos de control
89
        private GeoPoint gpcs[] =null;
90
        
91
        //Extend de imagen corregida
92
        GridExtent newExtend=null;
93
        
94
        // Metodo de resampleado utilizado
95
        private int rMethod= 0;
96
        
97
        //Indicador de progreso
98
        private int percent=0;
99
        
100
        // Grid resultado
101
        private Grid gridResult=null;
102
        
103
        WriterBufferServer writerBufferServer=null;
104
        
105
        /** Metodo que recoge los parametros del proceso georreferenciacion de un raster
106
        * <LI>rasterSE: capa a georeferenciar</LI>
107
        * <LI>filename: path con el fichero de salida</LI>
108
        * <LI>method: metodo de resampleo </LI>
109
        */
110
        public void init() {
111
                
112
                rasterSE= (FLyrRasterSE)getParam("fLayer");
113
                filename= (String)getParam("filename");
114
                rMethod=(int)getIntParam("method");
115
                gpcs = (GeoPoint[])getParam("gpcs");
116
                
117
                // Inicializacion del grid correspondiente a la imagen a corregir
118
                IRasterDataSource dsetCopy = null; 
119
                dsetCopy = rasterSE.getDataSource().newDataset();
120
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
121
                if (!RasterBuffer.loadInMemory(dsetCopy))
122
                        bufferFactory.setReadOnly(true);        
123
                try {
124
                                imageGrid = new Grid(bufferFactory);        
125
                }catch (RasterBufferInvalidException e) {
126
                        e.printStackTrace();                        
127
                }
128
        }
129
        
130

    
131
        public void process() throws InterruptedException {
132
                
133
                //COMENTAR:         Podiamos ahorrarlo si le pasamos  ya un  geoTransformDataResult
134
                GeoTransformProcess transform = new GeoTransformProcess();
135
                transform.setActions(this);
136
                transform.addParam("gpcs",gpcs);
137
                transform.addParam("orden",new Integer(1));
138
                transform.run();                
139
                
140
                // Obtenida la transformacion la aplicamos a los puntos extremos de la imagen
141
                double p1[]=transformData.getCoordMap(imageGrid.getGridExtent().getLLX(),imageGrid.getGridExtent().getLLY());
142
                double p2[]=transformData.getCoordMap(imageGrid.getGridExtent().getULX(),imageGrid.getGridExtent().getULY());
143
                double p3[]=transformData.getCoordMap(imageGrid.getGridExtent().getURX(),imageGrid.getGridExtent().getURY());
144
                double p4[]=transformData.getCoordMap(imageGrid.getGridExtent().getLRX(),imageGrid.getGridExtent().getLRY());
145
        
146
                
147
                double xmin=Math.min( Math.min(p1[0],p2[0]),Math.min(p3[0],p4[0]));
148
                double ymin=Math.min( Math.min(p1[1],p2[1]),Math.min(p3[1],p4[1]));
149
                double xmax=Math.max( Math.max(p1[0],p2[0]),Math.max(p3[0],p4[0]));        
150
                double ymax=Math.max( Math.max(p1[1],p2[1]),Math.max(p3[1],p4[1]));
151
                
152
                // OJO CON EL TAMA?O DE CELDA: CONSULTAR PARA EJEMPLO DE JOSE ES 25
153
                newExtend= new GridExtent(xmin,ymin,xmax,ymax,25);
154
                int datatype= rasterSE.getBufferFactory().getRasterBuf().getDataType();
155
                int bands[]= rasterSE.getBufferFactory().getDrawableBands();
156
                
157
                try {
158
                        gridResult= new Grid(newExtend,newExtend,datatype,bands);
159
                } catch (RasterBufferInvalidException e) {
160
                        RasterToolsUtil.messageBoxError("error_grid", this, e);
161
                }
162
                
163
                double minPointX=gridResult.getGridExtent().getMin().getX();
164
                double minPointY=gridResult.getGridExtent().getMin().getY();
165
                double cellsizeX=gridResult.getCellSize();
166
                double cellsizeY=gridResult.getCellSize();
167
                double coord[]=null;
168
                try {
169
                
170
                        // Caso imagen tipo byte
171
                        if(datatype==IBuffer.TYPE_BYTE)
172
                        {
173
                                for(int fil=0; fil<gridResult.getLayerNX(); fil++){
174
                                        for(int col=0; col<gridResult.getLayerNY();col++)
175
                                        {
176
                                                coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY);        
177
                                                double value;
178
                                                value = imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]);
179
                                                gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(byte)value);
180
                                        }
181
                                        percent= (int)fil*100/gridResult.getLayerNX();        
182
                                }        
183
                        }
184
                        
185
                        // Caso imagen tipo short
186
                        if(datatype==IBuffer.TYPE_SHORT)
187
                        {
188
                                for(int fil=0; fil<gridResult.getLayerNX(); fil++){
189
                                        for(int col=0; col<gridResult.getLayerNY();col++)
190
                                        {
191
                                                coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY);        
192
                                                short value;
193
                                                value = imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]);
194
                                                gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(short)value);
195
                                        }
196
                                        percent= (int)fil*100/gridResult.getLayerNX();        
197
                                }        
198
                        }
199
                        
200
                        //        Caso imagen tipo int
201
                        if(datatype==IBuffer.TYPE_INT)
202
                        {
203
                                for(int fil=0; fil<gridResult.getLayerNX(); fil++){
204
                                        for(int col=0; col<gridResult.getLayerNY();col++)
205
                                        {
206
                                                coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY);        
207
                                                short value;
208
                                                value = imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]);
209
                                                gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(int)value);
210
                                        }
211
                                        percent= (int)fil*100/gridResult.getLayerNX();        
212
                                }        
213
                        }
214
                        
215
                        // Caso imagen tipo float
216
                        if(datatype==IBuffer.TYPE_FLOAT)
217
                        {
218
                                for(int fil=0; fil<gridResult.getLayerNX(); fil++){
219
                                        for(int col=0; col<gridResult.getLayerNY();col++)
220
                                        {
221
                                                coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY);        
222
                                                double value;
223
                                                value = imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]);
224
                                                gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(float)value);
225
                                        }
226
                                        percent= (int)fil*100/gridResult.getLayerNX();        
227
                                }        
228
                        }
229
                        
230
                        // Caso imagen tipo double
231
                        if(datatype==IBuffer.TYPE_FLOAT)
232
                        {
233
                                for(int fil=0; fil<gridResult.getLayerNX(); fil++){
234
                                        for(int col=0; col<gridResult.getLayerNY();col++)
235
                                        {
236
                                                coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY);        
237
                                                double value;
238
                                                value = imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]);
239
                                                gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(double)value);
240
                                        }
241
                                        percent= (int)fil*100/gridResult.getLayerNX();        
242
                                }        
243
                        }
244
                        
245
                } catch (GridException e) {
246
                                e.printStackTrace();        
247
                } catch (OutOfGridException e) {
248
                        e.printStackTrace();
249
                }
250
                
251
                generateLayer();
252
                if(externalActions!=null)
253
                        externalActions.end(filename);
254
        }
255

    
256

    
257
        private void generateLayer(){
258
                
259
                GeoRasterWriter grw = null;
260
                IBuffer buffer= gridResult.getRasterBuf();
261
            writerBufferServer = new WriterBufferServer(buffer);
262
                AffineTransform aTransform = new AffineTransform(newExtend.getCellSize(),0.0,0.0,-newExtend.getCellSize(),newExtend.getMin().getX(),newExtend.getMax().getY());
263
                int endIndex =filename.lastIndexOf(".");
264
                if (endIndex < 0)
265
                        endIndex = filename.length();
266
                try {
267
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename,gridResult.getRasterBuf().getBandCount(),aTransform, gridResult.getRasterBuf().getWidth(),gridResult.getRasterBuf().getHeight(), gridResult.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(),null);
268
                        grw.dataWrite();
269
                        grw.setWkt(rasterSE.getWktProjection());
270
                        grw.writeClose();
271
                } catch (NotSupportedExtensionException e1) {
272
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1);
273
                } catch (RasterDriverException e1) {
274
                        e1.printStackTrace();
275
                } catch (IOException e) {
276
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
277
                } catch (InterruptedException e) {
278
                        Thread.currentThread().interrupt();
279
                }
280
        }
281
                
282
        
283
        // Metodos para la interpolacion
284

    
285
        
286
        
287
        /*
288
         * (non-Javadoc)
289
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
290
         */
291
        public String getTitle() {
292
                return PluginServices.getText(this,"georreferenciacion_process");
293
        }
294
        
295
        /*
296
         * (non-Javadoc)
297
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
298
         */
299
        public int getPercent() {
300
                if(writerBufferServer==null)
301
                        return percent;
302
                else
303
                        return writerBufferServer.getPercent();
304
        }
305

    
306
        
307
        public void interrupted() {
308
                // TODO Auto-generated method stub
309
                
310
        }
311

    
312
        public void end(Object param) {        
313
                transformData = (GeoTransformDataResult)param;        
314
        }
315

    
316
}