root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / process / GeoreferencingProcess.java @ 18849
History | View | Annotate | Download (13.9 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 |
private int[] bands=null; |
108 |
|
109 |
public static final int NEAREST_NEIGHBOR_INTERPOLATION = 0; |
110 |
public static final int BILINEAL_INTERPOLATION = 1; |
111 |
public static final int CUBIC_INTERPOLATION = 2; |
112 |
|
113 |
/** 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 |
* <LI>method: metodo de resampleo </LI>
|
117 |
*/
|
118 |
public void init() { |
119 |
|
120 |
rasterSE= (FLyrRasterSE)getParam("fLayer");
|
121 |
filename= (String)getParam("filename"); |
122 |
rMethod=(int)getIntParam("method"); |
123 |
gpcs = (GeoPoint[])getParam("gpcs"); |
124 |
orden= (int)getIntParam("orden"); |
125 |
bands= new int[rasterSE.getBandCount()]; |
126 |
for(int i=0; i<rasterSE.getBandCount(); i++) |
127 |
bands[i]= i; |
128 |
// Inicializacion del grid correspondiente a la imagen a corregir
|
129 |
IRasterDataSource dsetCopy = null;
|
130 |
dsetCopy = rasterSE.getDataSource().newDataset(); |
131 |
BufferFactory bufferFactory = new BufferFactory(dsetCopy);
|
132 |
if (!RasterBuffer.loadInMemory(dsetCopy))
|
133 |
bufferFactory.setReadOnly(true);
|
134 |
try {
|
135 |
imageGrid = new Grid(bufferFactory);
|
136 |
}catch (RasterBufferInvalidException e) {
|
137 |
e.printStackTrace(); |
138 |
} |
139 |
} |
140 |
|
141 |
|
142 |
public void process() throws InterruptedException { |
143 |
|
144 |
//COMENTAR: Podiamos ahorrarlo si le pasamos ya un geoTransformDataResult
|
145 |
GeoTransformProcess transform = new GeoTransformProcess();
|
146 |
transform.setActions(this);
|
147 |
transform.addParam("gpcs",gpcs);
|
148 |
transform.addParam("orden",new Integer(orden)); |
149 |
transform.run(); |
150 |
|
151 |
// Obtenida la transformacion la aplicamos a los puntos extremos de la imagen
|
152 |
double p1[]=transformData.getCoordMap(imageGrid.getGridExtent().getLLX(),imageGrid.getGridExtent().getLLY()); |
153 |
double p2[]=transformData.getCoordMap(imageGrid.getGridExtent().getULX(),imageGrid.getGridExtent().getULY()); |
154 |
double p3[]=transformData.getCoordMap(imageGrid.getGridExtent().getURX(),imageGrid.getGridExtent().getURY()); |
155 |
double p4[]=transformData.getCoordMap(imageGrid.getGridExtent().getLRX(),imageGrid.getGridExtent().getLRY()); |
156 |
|
157 |
|
158 |
double xmin=Math.min( Math.min(p1[0],p2[0]),Math.min(p3[0],p4[0])); |
159 |
double ymin=Math.min( Math.min(p1[1],p2[1]),Math.min(p3[1],p4[1])); |
160 |
double xmax=Math.max( Math.max(p1[0],p2[0]),Math.max(p3[0],p4[0])); |
161 |
double ymax=Math.max( Math.max(p1[1],p2[1]),Math.max(p3[1],p4[1])); |
162 |
|
163 |
// OJO CON EL TAMA?O DE CELDA: CONSULTAR PARA EJEMPLO DE JOSE ES 25
|
164 |
newExtend= new GridExtent(xmin,ymin,xmax,ymax,25); |
165 |
int datatype= rasterSE.getBufferFactory().getRasterBuf().getDataType();
|
166 |
try {
|
167 |
gridResult= new Grid(newExtend,newExtend,datatype,bands);
|
168 |
} catch (RasterBufferInvalidException e) {
|
169 |
RasterToolsUtil.messageBoxError("error_grid", this, e); |
170 |
} |
171 |
|
172 |
double minPointX=gridResult.getGridExtent().getMin().getX();
|
173 |
double minPointY=gridResult.getGridExtent().getMin().getY();
|
174 |
double cellsizeX=gridResult.getCellSize();
|
175 |
double cellsizeY=gridResult.getCellSize();
|
176 |
double coord[]=null; |
177 |
try {
|
178 |
|
179 |
// Caso imagen tipo byte. Usando metodo de vecino mas proximo
|
180 |
if(datatype==IBuffer.TYPE_BYTE)
|
181 |
{ |
182 |
for(int fil=0; fil<gridResult.getLayerNX(); fil++){ |
183 |
for(int col=0; col<gridResult.getLayerNY();col++) |
184 |
for(int band=0; band<bands.length;band++) |
185 |
{ |
186 |
coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY); |
187 |
double values[]=null; |
188 |
if(rMethod==NEAREST_NEIGHBOR_INTERPOLATION)
|
189 |
values = getNearestNeighborValue(coord[0],coord[1]);// imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]); |
190 |
gridResult.setBandToOperate(band); |
191 |
gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(byte)values[band]); |
192 |
} |
193 |
percent= (int)fil*100/gridResult.getLayerNX(); |
194 |
} |
195 |
} |
196 |
|
197 |
// Caso imagen tipo short
|
198 |
if(datatype==IBuffer.TYPE_SHORT)
|
199 |
{ |
200 |
for(int fil=0; fil<gridResult.getLayerNX(); fil++){ |
201 |
for(int col=0; col<gridResult.getLayerNY();col++) |
202 |
for(int band=0; band<bands.length;band++) |
203 |
{ |
204 |
coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY); |
205 |
double values[]=null; |
206 |
if(rMethod==NEAREST_NEIGHBOR_INTERPOLATION)
|
207 |
values = getNearestNeighborValue(coord[0],coord[1]);// imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]); |
208 |
gridResult.setBandToOperate(band); |
209 |
gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(short)values[band]); |
210 |
} |
211 |
percent= (int)fil*100/gridResult.getLayerNX(); |
212 |
} |
213 |
} |
214 |
|
215 |
// Caso imagen tipo int
|
216 |
if(datatype==IBuffer.TYPE_INT)
|
217 |
{ |
218 |
for(int fil=0; fil<gridResult.getLayerNX(); fil++){ |
219 |
for(int col=0; col<gridResult.getLayerNY();col++) |
220 |
for(int band=0; band<bands.length;band++) |
221 |
{ |
222 |
coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY); |
223 |
double values[]=null; |
224 |
if(rMethod==NEAREST_NEIGHBOR_INTERPOLATION)
|
225 |
values = getNearestNeighborValue(coord[0],coord[1]);// imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]); |
226 |
gridResult.setBandToOperate(band); |
227 |
gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(int)values[band]); |
228 |
} |
229 |
percent= (int)fil*100/gridResult.getLayerNX(); |
230 |
} |
231 |
} |
232 |
|
233 |
// Caso imagen tipo float
|
234 |
if(datatype==IBuffer.TYPE_FLOAT)
|
235 |
{ |
236 |
for(int fil=0; fil<gridResult.getLayerNX(); fil++){ |
237 |
for(int col=0; col<gridResult.getLayerNY();col++) |
238 |
for(int band=0; band<bands.length;band++) |
239 |
{ |
240 |
coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY); |
241 |
double values[]=null; |
242 |
if(rMethod==NEAREST_NEIGHBOR_INTERPOLATION)
|
243 |
values = getNearestNeighborValue(coord[0],coord[1]);// imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]); |
244 |
gridResult.setBandToOperate(band); |
245 |
gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(float)values[band]); |
246 |
} |
247 |
percent= (int)fil*100/gridResult.getLayerNX(); |
248 |
} |
249 |
} |
250 |
|
251 |
// Caso imagen tipo double
|
252 |
if(datatype==IBuffer.TYPE_FLOAT)
|
253 |
{ |
254 |
for(int fil=0; fil<gridResult.getLayerNX(); fil++){ |
255 |
for(int col=0; col<gridResult.getLayerNY();col++) |
256 |
for(int band=0; band<bands.length;band++) |
257 |
{ |
258 |
coord=transform.getCoordPixel(fil*cellsizeX+minPointX, col*cellsizeY+minPointY); |
259 |
double values[]=null; |
260 |
if(rMethod==NEAREST_NEIGHBOR_INTERPOLATION)
|
261 |
values = getNearestNeighborValue(coord[0],coord[1]);// imageGrid.getCellValueAsByte((int)coord[0],(int)coord[1]); |
262 |
gridResult.setBandToOperate(band); |
263 |
gridResult.setCellValue(fil,gridResult.getLayerNY()-col-1,(double)values[band]); |
264 |
} |
265 |
percent= (int)fil*100/gridResult.getLayerNX(); |
266 |
} |
267 |
} |
268 |
|
269 |
} catch (OutOfGridException e) {
|
270 |
e.printStackTrace(); |
271 |
} |
272 |
|
273 |
generateLayer(); |
274 |
if(externalActions!=null) |
275 |
externalActions.end(filename); |
276 |
} |
277 |
|
278 |
|
279 |
private void generateLayer(){ |
280 |
|
281 |
GeoRasterWriter grw = null;
|
282 |
IBuffer buffer= gridResult.getRasterBuf(); |
283 |
writerBufferServer = new WriterBufferServer(buffer);
|
284 |
AffineTransform aTransform = new AffineTransform(newExtend.getCellSize(),0.0,0.0,-newExtend.getCellSize(),newExtend.getMin().getX(),newExtend.getMax().getY()); |
285 |
int endIndex =filename.lastIndexOf("."); |
286 |
if (endIndex < 0) |
287 |
endIndex = filename.length(); |
288 |
try {
|
289 |
grw = GeoRasterWriter.getWriter(writerBufferServer, filename,gridResult.getRasterBuf().getBandCount(),aTransform, gridResult.getRasterBuf().getWidth(),gridResult.getRasterBuf().getHeight(), gridResult.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(),null);
|
290 |
grw.dataWrite(); |
291 |
grw.setWkt(rasterSE.getWktProjection()); |
292 |
grw.writeClose(); |
293 |
} catch (NotSupportedExtensionException e1) {
|
294 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1); |
295 |
} catch (RasterDriverException e1) {
|
296 |
e1.printStackTrace(); |
297 |
} catch (IOException e) { |
298 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
299 |
} catch (InterruptedException e) { |
300 |
Thread.currentThread().interrupt();
|
301 |
} |
302 |
} |
303 |
|
304 |
|
305 |
/**
|
306 |
* Metodo que devuelve el resultado de la interpolacion por vecino mas proximo.
|
307 |
* @param y coordenada y
|
308 |
* @param x coordenada x
|
309 |
* @return array con los valores ya interpolados para las coordenadas x e y de
|
310 |
* */
|
311 |
public double [] getNearestNeighborValue(double x, double y){ |
312 |
double bandsvalue[]=new double[bands.length]; |
313 |
|
314 |
// Caso buffer tipo byte
|
315 |
if(imageGrid.getRasterBuf().getDataType()==IBuffer.TYPE_BYTE){
|
316 |
for(int band=0; band<bands.length;band++) |
317 |
{ |
318 |
try {
|
319 |
imageGrid.setBandToOperate(band); |
320 |
bandsvalue[band]=imageGrid.getCellValueAsByte((int)Math.rint(x),(int)Math.rint(y)); |
321 |
} catch (GridException e) {
|
322 |
e.printStackTrace(); |
323 |
} |
324 |
} |
325 |
} |
326 |
// Caso buffer tipo short
|
327 |
if(imageGrid.getRasterBuf().getDataType()==IBuffer.TYPE_SHORT){
|
328 |
for(int band=0; band<bands.length;band++) |
329 |
{ |
330 |
try {
|
331 |
imageGrid.setBandToOperate(band); |
332 |
bandsvalue[band]=imageGrid.getCellValueAsShort((int)Math.rint(x),(int)Math.rint(y)); |
333 |
} catch (GridException e) {
|
334 |
e.printStackTrace(); |
335 |
} |
336 |
} |
337 |
} |
338 |
// Caso buffer tipo int
|
339 |
if(imageGrid.getRasterBuf().getDataType()==IBuffer.TYPE_INT){
|
340 |
for(int band=0; band<bands.length;band++) |
341 |
{ |
342 |
try {
|
343 |
imageGrid.setBandToOperate(band); |
344 |
bandsvalue[band]=imageGrid.getCellValueAsInt((int)Math.rint(x),(int)Math.rint(y)); |
345 |
} catch (GridException e) {
|
346 |
e.printStackTrace(); |
347 |
} |
348 |
} |
349 |
} |
350 |
// Caso buffer tipo float
|
351 |
if(imageGrid.getRasterBuf().getDataType()==IBuffer.TYPE_FLOAT){
|
352 |
for(int band=0; band<bands.length;band++) |
353 |
{ |
354 |
try {
|
355 |
imageGrid.setBandToOperate(band); |
356 |
bandsvalue[band]=imageGrid.getCellValueAsFloat((int)Math.rint(x),(int)Math.rint(y)); |
357 |
} catch (GridException e) {
|
358 |
e.printStackTrace(); |
359 |
} |
360 |
} |
361 |
} |
362 |
// Caso buffer tipo double
|
363 |
if(imageGrid.getRasterBuf().getDataType()==IBuffer.TYPE_DOUBLE){
|
364 |
for(int band=0; band<bands.length;band++) |
365 |
{ |
366 |
try {
|
367 |
imageGrid.setBandToOperate(band); |
368 |
bandsvalue[band]=imageGrid.getCellValueAsFloat((int)Math.rint(x),(int)Math.rint(y)); |
369 |
} catch (GridException e) {
|
370 |
e.printStackTrace(); |
371 |
} |
372 |
} |
373 |
} |
374 |
|
375 |
return bandsvalue;
|
376 |
} |
377 |
|
378 |
|
379 |
/*
|
380 |
* (non-Javadoc)
|
381 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
382 |
*/
|
383 |
public String getTitle() { |
384 |
return PluginServices.getText(this,"georreferenciacion_process"); |
385 |
} |
386 |
|
387 |
/*
|
388 |
* (non-Javadoc)
|
389 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
390 |
*/
|
391 |
public int getPercent() { |
392 |
if(writerBufferServer==null) |
393 |
return percent;
|
394 |
else
|
395 |
return 0;//writerBufferServer.getPercent(); |
396 |
} |
397 |
|
398 |
|
399 |
public void interrupted() { |
400 |
// TODO Auto-generated method stub
|
401 |
|
402 |
} |
403 |
|
404 |
public void end(Object param) { |
405 |
transformData = (GeoTransformDataResult)param; |
406 |
} |
407 |
|
408 |
} |