Statistics
| Revision:

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

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

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

    
258

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

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

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

    
314
        public void end(Object param) {        
315
                transformData = (GeoTransformDataResult)param;        
316
        }
317

    
318
}