svn-gvsig-desktop / tags / v1_1_Build_1011 / libraries / libCq_CMS_praster / src / org / cresques / io / GdalFile.java @ 12904
History | View | Annotate | Download (24.1 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io; |
25 |
|
26 |
import java.awt.Image; |
27 |
import java.awt.geom.NoninvertibleTransformException; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.awt.geom.Rectangle2D; |
30 |
import java.awt.image.BufferedImage; |
31 |
|
32 |
import org.cresques.cts.ICoordTrans; |
33 |
import org.cresques.cts.IProjection; |
34 |
import org.cresques.io.data.BandList; |
35 |
import org.cresques.io.data.RasterBuf; |
36 |
import org.cresques.io.datastruct.Metadata; |
37 |
import org.cresques.io.exceptions.NotSupportedExtensionException; |
38 |
import org.cresques.io.exceptions.SupersamplingNotSupportedException; |
39 |
import org.cresques.px.Extent; |
40 |
import org.cresques.util.Utilities; |
41 |
|
42 |
import es.gva.cit.jgdal.GdalException; |
43 |
|
44 |
/**
|
45 |
* @author Luis W. Sevilla
|
46 |
*/
|
47 |
public class GdalFile extends GeoRasterFile { |
48 |
public final static int BAND_HEIGHT = 64; |
49 |
protected GdalNative file = null; |
50 |
/**
|
51 |
* Tama?o de pixel para las imagenes con fichero RMF. No podemos salvarlo en file porque es necesario conocer el
|
52 |
* tama?o de pixel asignado por rl .rmf y el tama?o de pixel real.
|
53 |
*/
|
54 |
private double pixelSizeX = 0D, pixelSizeY = 0D; |
55 |
|
56 |
private Extent v = null; |
57 |
|
58 |
public GdalFile(IProjection proj, String fName, boolean hdf){ |
59 |
super(proj, fName);
|
60 |
} |
61 |
|
62 |
public GdalFile(IProjection proj, String fName)throws NotSupportedExtensionException{ |
63 |
super(proj, fName);
|
64 |
extent = new Extent();
|
65 |
try {
|
66 |
file = new GdalNative(fName, this); |
67 |
load(); |
68 |
readGeoInfo(fName); |
69 |
bandCount = file.getRasterCount(); |
70 |
if ( bandCount > 2) { |
71 |
setBand(RED_BAND, 0);
|
72 |
setBand(GREEN_BAND, 1);
|
73 |
setBand(BLUE_BAND, 2);
|
74 |
} else
|
75 |
setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
|
76 |
} catch (GdalException e) {
|
77 |
throw new NotSupportedExtensionException("Extension not supported"); |
78 |
} catch(Exception e){ |
79 |
System.out.println("Error en GdalOpen"); |
80 |
e.printStackTrace(); |
81 |
file = null;
|
82 |
} |
83 |
|
84 |
//Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
85 |
setDataType(org.cresques.util.Utilities.getRasterBufTypeFromGdalType(file.getDataType())); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Obtenemos o calculamos el extent de la imagen.
|
90 |
*/
|
91 |
public GeoFile load() {
|
92 |
extent = new Extent(file.bBoxRot.minX, file.bBoxRot.minY, file.bBoxRot.maxX, file.bBoxRot.maxY);
|
93 |
requestExtent = new Extent(file.bBoxWithoutRot.minX, file.bBoxWithoutRot.minY, file.bBoxWithoutRot.maxX, file.bBoxWithoutRot.maxY);
|
94 |
return this; |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Cierra el fichero de imagen
|
99 |
*/
|
100 |
public void close() { |
101 |
try {
|
102 |
if(file != null){ |
103 |
file.close(); |
104 |
file = null;
|
105 |
} |
106 |
} catch (GdalException e) {
|
107 |
// TODO Auto-generated catch block
|
108 |
e.printStackTrace(); |
109 |
} |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Asigna a cada banda R,G o B una banda de la imagen
|
114 |
*/
|
115 |
public void setBand(int flag, int bandNr) { |
116 |
super.setBand(flag, bandNr);
|
117 |
if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1; |
118 |
if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1; |
119 |
if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1; |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
|
124 |
* de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
|
125 |
* ha de estar en coordenadas del fichero.
|
126 |
*/
|
127 |
public void setView(Extent e) { |
128 |
if(rmfExists){
|
129 |
|
130 |
Point2D.Double petInit = null, petEnd = null; |
131 |
try{
|
132 |
petInit = new Point2D.Double(e.minX(), e.maxY()); |
133 |
petEnd = new Point2D.Double(e.maxX(), e.minY()); |
134 |
transformRMF.inverseTransform(petInit, petInit); |
135 |
transformRMF.inverseTransform(petEnd, petEnd); |
136 |
transformTFW.transform(petInit, petInit); |
137 |
transformTFW.transform(petEnd, petEnd); |
138 |
}catch(NoninvertibleTransformException ex){} |
139 |
double h = file.bBoxWithoutRot.maxY - file.bBoxWithoutRot.minY;
|
140 |
if(!file.isGeoreferenced())
|
141 |
v = new Extent( petInit.getX(), h - petInit.getY(), petEnd.getX(), h - petEnd.getY());
|
142 |
else
|
143 |
v = new Extent( petInit.getX(), petInit.getY(), petEnd.getX(), petEnd.getY());
|
144 |
|
145 |
}else
|
146 |
v = new Extent(e.minX(), e.minY(), e.maxX(), e.maxY());
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* Calcula la transformaci?n que se produce sobre la vista cuando la imagen tiene un fichero .rmf
|
151 |
* asociado. En Gdal el origen de coordenadas en Y es el valor m?nimo y crece hasta el m?ximo. De la
|
152 |
* misma forma calcula la matriz de transformaci?n de la cabecera del fichero o del world file asociado
|
153 |
* @param originX Origen de la imagen en la coordenada X
|
154 |
* @param originY Origen de la imagen en la coordenada Y
|
155 |
*/
|
156 |
public void setExtentTransform(double originX, double originY, double psX, double psY) { |
157 |
transformRMF.setToTranslation(originX, originY); |
158 |
transformRMF.scale(psX, psY); |
159 |
|
160 |
if(file.trans != null){ |
161 |
transformTFW.setToTranslation(file.trans.adfgeotransform[0], file.trans.adfgeotransform[3]); |
162 |
transformTFW.scale(file.trans.adfgeotransform[1], file.trans.adfgeotransform[5]); |
163 |
} |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Obtiene extent de la vista actual
|
168 |
*/
|
169 |
public Extent getView() {
|
170 |
return v;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Obtiene la anchura del fichero
|
175 |
*/
|
176 |
public int getWidth() { |
177 |
return file.width;
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Obtiene la altura del fichero
|
182 |
*/
|
183 |
public int getHeight() { |
184 |
return file.height;
|
185 |
} |
186 |
|
187 |
/* (non-Javadoc)
|
188 |
* @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
|
189 |
*/
|
190 |
public void reProject(ICoordTrans rp) { |
191 |
// TODO Auto-generated method stub
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* Obtiene la orientaci?n de la imagen a partir del signo del tama?o de pixel para poder
|
196 |
* asignarlo en el setView. Esto es util para poder conocer como debe leerse la image,
|
197 |
* de abajo a arriba, de arriba a abajo, de izquierda a derecha o de derecha a izquierda.
|
198 |
* La posici?n habitual es la que el pixel size en X es positivo y en Y negativo leyendose
|
199 |
* en este caso las X de menor a mayor y las Y de mayor a menor. Los casos posibles son:
|
200 |
* <UL>
|
201 |
* <LI><B>X > 0; Y < 0;</B> {true, false}</LI>
|
202 |
* <LI><B>X > 0; Y > 0;</B> {true, true}</LI>
|
203 |
* <LI><B>X < 0; Y > 0;</B> {false, true}</LI>
|
204 |
* <LI><B>X < 0; Y < 0;</B> {false, false}</LI>
|
205 |
* </UL>
|
206 |
*
|
207 |
* @return
|
208 |
*/
|
209 |
private boolean[] getOrientation(){ |
210 |
boolean[] orientation = {true, false}; |
211 |
if(!rmfExists){
|
212 |
if(file.trans != null && file.trans.adfgeotransform != null && file.trans.adfgeotransform[5] > 0) |
213 |
orientation[1] = true; |
214 |
if(file.trans != null && file.trans.adfgeotransform != null && file.trans.adfgeotransform[1] < 0) |
215 |
orientation[0] = false; |
216 |
}else{
|
217 |
if(rmfTransform.getScaleY() > 0) |
218 |
orientation[1] = true; |
219 |
if(rmfTransform.getScaleX() < 0) |
220 |
orientation[0] = false; |
221 |
} |
222 |
return orientation;
|
223 |
} |
224 |
|
225 |
/* (non-Javadoc)
|
226 |
* @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
|
227 |
*/
|
228 |
public Image updateImage(int width, int height, ICoordTrans rp) { |
229 |
int line, pRGBArray[] = null; |
230 |
Image image = null; |
231 |
|
232 |
if (mustVerifySize()) {
|
233 |
// Work out the correct aspect for the setView call.
|
234 |
double dFileAspect = (double)v.width()/(double)v.height(); |
235 |
double dWindowAspect = (double)width /(double)height; |
236 |
|
237 |
if (dFileAspect > dWindowAspect) {
|
238 |
height =(int)((double)width/dFileAspect); |
239 |
} else {
|
240 |
width = (int)((double)height*dFileAspect); |
241 |
} |
242 |
} |
243 |
|
244 |
// Set the view
|
245 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height, getOrientation()); |
246 |
setStep(file.stepArrayX, file.stepArrayY); |
247 |
|
248 |
if(width<=0)width=1; |
249 |
if(height<=0)height=1; |
250 |
|
251 |
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
252 |
//image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
253 |
pRGBArray = new int[width/**BAND_HEIGHT*/]; |
254 |
try {
|
255 |
//int nLin = height % BAND_HEIGHT;
|
256 |
file.setAlpha(getAlpha()); |
257 |
setBand(RED_BAND, rBandNr); |
258 |
setBand(GREEN_BAND, gBandNr); |
259 |
setBand(BLUE_BAND, bBandNr); |
260 |
for (line=0; line < height; line++) { //+=BAND_HEIGHT) { |
261 |
//int bandH = Math.min(BAND_HEIGHT, height-line);
|
262 |
//file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
|
263 |
file.readLineRGBA(pRGBArray); |
264 |
setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width); |
265 |
} |
266 |
} catch (Exception e) { |
267 |
// TODO Auto-generated catch block
|
268 |
e.printStackTrace(); |
269 |
} |
270 |
|
271 |
return image;
|
272 |
} |
273 |
|
274 |
public RasterBuf getRaster(int width, int height, ICoordTrans rp) { |
275 |
int line;
|
276 |
RasterBuf raster = null;
|
277 |
|
278 |
if(mustVerifySize()){
|
279 |
// Work out the correct aspect for the setView call.
|
280 |
double dFileAspect = (double)v.width()/(double)v.height(); |
281 |
double dWindowAspect = (double)width /(double)height; |
282 |
|
283 |
if (dFileAspect > dWindowAspect) {
|
284 |
height =(int)((double)width/dFileAspect); |
285 |
} else {
|
286 |
width = (int)((double)height*dFileAspect); |
287 |
} |
288 |
} |
289 |
|
290 |
// Set the view
|
291 |
boolean[] orientation = getOrientation(); |
292 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
293 |
width, height, orientation); |
294 |
setStep(file.stepArrayX, file.stepArrayY); |
295 |
|
296 |
try {
|
297 |
//Esta funci?n se usa para la renderizaci?n, por eso se crean 4 bandas a pi?on fijo
|
298 |
raster = new RasterBuf(getDataType(), width, height, 4, true); |
299 |
|
300 |
file.setAlpha(getAlpha()); |
301 |
setBand(RED_BAND, rBandNr); |
302 |
setBand(GREEN_BAND, gBandNr); |
303 |
setBand(BLUE_BAND, bBandNr); |
304 |
|
305 |
switch(getDataType()){
|
306 |
case RasterBuf.TYPE_BYTE:
|
307 |
for (line = 0; line < height; line++) |
308 |
file.readLine(raster.getLineByte(line)); |
309 |
break;
|
310 |
case RasterBuf.TYPE_SHORT:;
|
311 |
for (line = 0; line < height; line++) |
312 |
file.readLine(raster.getLineShort(line)); |
313 |
break;
|
314 |
case RasterBuf.TYPE_INT:
|
315 |
for (line = 0; line < height; line++) |
316 |
file.readLine(raster.getLineInt(line)); |
317 |
break;
|
318 |
case RasterBuf.TYPE_FLOAT:
|
319 |
for (line = 0; line < height; line++) |
320 |
file.readLine(raster.getLineFloat(line)); |
321 |
break;
|
322 |
case RasterBuf.TYPE_DOUBLE:
|
323 |
for (line = 0; line < height; line++) |
324 |
file.readLine(raster.getLineDouble(line)); |
325 |
break;
|
326 |
case RasterBuf.TYPE_UNDEFINED:break; |
327 |
} |
328 |
|
329 |
} catch (Exception e) { |
330 |
e.printStackTrace(); |
331 |
} |
332 |
|
333 |
return raster;
|
334 |
} |
335 |
|
336 |
/**
|
337 |
* Asigna al objeto Image los valores con los dato de la imagen contenidos en el
|
338 |
* vector de enteros.
|
339 |
* @param image imagen con los datos actuales
|
340 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
341 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
342 |
* @param w Ancho de la imagen
|
343 |
* @param h Alto de la imagen
|
344 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
345 |
* @param offset desplazamiento
|
346 |
* @param scansize tama?o de imagen recorrida por cada p
|
347 |
*/
|
348 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
349 |
int offset, int scansize) { |
350 |
image.setRGB(startX, startY, w, h, rgbArray, offset, scansize); |
351 |
} |
352 |
|
353 |
/**
|
354 |
* Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
355 |
* con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
|
356 |
* que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
|
357 |
* banda correspondiente a flags es sustituida por los datos del vector.
|
358 |
* @param image imagen con los datos actuales
|
359 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
360 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
361 |
* @param w Ancho de la imagen
|
362 |
* @param h Alto de la imagen
|
363 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
364 |
* @param offset desplazamiento
|
365 |
* @param scansize tama?o de imagen recorrida por cada paso
|
366 |
* @param flags banda que se va a sustituir (Ctes de GeoRasterFile)
|
367 |
*/
|
368 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
369 |
int offset, int scansize, int flags) { |
370 |
int [] line = new int[rgbArray.length]; |
371 |
image.getRGB(startX, startY, w, h, line, offset, scansize); |
372 |
if (flags == GeoRasterFile.RED_BAND)
|
373 |
for (int i=0; i<line.length; i++) |
374 |
line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000); |
375 |
else if (flags == GeoRasterFile.GREEN_BAND) |
376 |
for (int i=0; i<line.length; i++) |
377 |
line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00); |
378 |
else if (flags == GeoRasterFile.BLUE_BAND) |
379 |
for (int i=0; i<line.length; i++) |
380 |
line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff); |
381 |
image.setRGB(startX, startY, w, h, line, offset, scansize); |
382 |
} |
383 |
|
384 |
/**
|
385 |
* Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
386 |
* con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
|
387 |
* que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
|
388 |
* banda correspondiente a flags es sustituida por los datos del vector.
|
389 |
* @param image imagen con los datos actuales
|
390 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
391 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
392 |
* @param w Ancho de la imagen
|
393 |
* @param h Alto de la imagen
|
394 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
395 |
* @param offset desplazamiento
|
396 |
* @param scansize tama?o de imagen recorrida por cada paso
|
397 |
* @param origBand Banda origen del GeoRasterFile
|
398 |
* @param destBandFlag banda que se va a sustituir (Ctes de GeoRasterFile)
|
399 |
*/
|
400 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
401 |
int offset, int scansize, int origBand, int destBandFlag) { |
402 |
int [] line = new int[rgbArray.length]; |
403 |
image.getRGB(startX, startY, w, h, line, offset, scansize); |
404 |
if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND) |
405 |
for (int i=0; i<line.length; i++) |
406 |
line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000); |
407 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND) |
408 |
for (int i=0; i<line.length; i++) |
409 |
line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00); |
410 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND) |
411 |
for (int i=0; i<line.length; i++) |
412 |
line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff); |
413 |
|
414 |
else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND) |
415 |
for (int i=0; i<line.length; i++) |
416 |
line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ; |
417 |
else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND) |
418 |
for (int i=0; i<line.length; i++) |
419 |
line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16); |
420 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND) |
421 |
for (int i=0; i<line.length; i++) |
422 |
line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8); |
423 |
|
424 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND) |
425 |
for (int i=0; i<line.length; i++) |
426 |
line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8); |
427 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND) |
428 |
for (int i=0; i<line.length; i++) |
429 |
line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16); |
430 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND) |
431 |
for (int i=0; i<line.length; i++) |
432 |
line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8); |
433 |
image.setRGB(startX, startY, w, h, line, offset, scansize); |
434 |
} |
435 |
|
436 |
private void showOnOpen() { |
437 |
// Report en la apertura (quitar)
|
438 |
System.out.println("Fichero GDAL '"+getName()+"' abierto."); |
439 |
System.out.println("Version = "+file.version); |
440 |
System.out.println(" Size = ("+file.width+","+file.height+")"); |
441 |
try {
|
442 |
System.out.println(" NumBands = ("+file.getRasterCount()+")"); |
443 |
} catch (GdalException e) {
|
444 |
// TODO Auto-generated catch block
|
445 |
e.printStackTrace(); |
446 |
} |
447 |
//file.pintaInfo();
|
448 |
file.pintaPaleta(); |
449 |
|
450 |
} |
451 |
|
452 |
/* (non-Javadoc)
|
453 |
* @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
|
454 |
*/
|
455 |
public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag)throws SupersamplingNotSupportedException{ |
456 |
int line, pRGBArray[] = null; |
457 |
|
458 |
if(mustVerifySize()){
|
459 |
// Work out the correct aspect for the setView call.
|
460 |
double dFileAspect = (double)v.width()/(double)v.height(); |
461 |
double dWindowAspect = (double)width /(double)height; |
462 |
|
463 |
if (dFileAspect > dWindowAspect) {
|
464 |
height =(int)((double)width/dFileAspect); |
465 |
} else {
|
466 |
width = (int)((double)height*dFileAspect); |
467 |
} |
468 |
} |
469 |
|
470 |
// Set the view
|
471 |
boolean[] orientation = getOrientation(); |
472 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
473 |
width, height, orientation); |
474 |
setStep(file.stepArrayX, file.stepArrayY); |
475 |
|
476 |
if(width<=0)width=1; |
477 |
if(height<=0)height=1; |
478 |
|
479 |
pRGBArray = new int[width]; |
480 |
try {
|
481 |
setBand(RED_BAND, rBandNr); |
482 |
setBand(GREEN_BAND, gBandNr); |
483 |
setBand(BLUE_BAND, bBandNr); |
484 |
file.setAlpha(getAlpha()); |
485 |
if(img!=null){ |
486 |
if(orientation[1]){ |
487 |
for (line=0; line < height; line++) { |
488 |
file.readLineRGBA(pRGBArray); |
489 |
setRGBLine((BufferedImage) img, 0, height - 1 - line, width, 1, pRGBArray, 0, width, origBand, destBandFlag); |
490 |
} |
491 |
}else{
|
492 |
for (line=0; line < height; line++) { |
493 |
file.readLineRGBA(pRGBArray); |
494 |
setRGBLine((BufferedImage) img, 0, line, width, 1, pRGBArray, 0, width, origBand, destBandFlag); |
495 |
} |
496 |
} |
497 |
return img;
|
498 |
}else{
|
499 |
Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
500 |
if(orientation[1]){ |
501 |
for (line=0; line < height; line++) { |
502 |
file.readLineRGBA(pRGBArray); |
503 |
setRGBLine((BufferedImage) image, 0, height - 1 - line, width, 1, pRGBArray, 0, width); |
504 |
} |
505 |
}else{
|
506 |
for (line=0; line < height; line++) { |
507 |
file.readLineRGBA(pRGBArray); |
508 |
setRGBLine((BufferedImage) image, 0, line, width, 1, pRGBArray, 0, width); |
509 |
} |
510 |
} |
511 |
return image;
|
512 |
} |
513 |
} catch (Exception e) { |
514 |
// TODO Auto-generated catch block
|
515 |
e.printStackTrace(); |
516 |
} |
517 |
|
518 |
return img;
|
519 |
} |
520 |
|
521 |
/* (non-Javadoc)
|
522 |
* @see org.cresques.io.GeoRasterFile#getData(int, int, int)
|
523 |
*/
|
524 |
public Object getData(int x, int y, int band) { |
525 |
if(file != null){ |
526 |
Object[] data = file.getData(x, y); |
527 |
return data[band];
|
528 |
} |
529 |
return null; |
530 |
} |
531 |
|
532 |
/**
|
533 |
* Devuelve los datos de una ventana solicitada
|
534 |
* @param ulX coordenada X superior izda.
|
535 |
* @param ulY coordenada Y superior derecha.
|
536 |
* @param sizeX tama?o en X de la ventana.
|
537 |
* @param sizeY tama?o en Y de la ventana.
|
538 |
* @param band Banda solicitada.
|
539 |
*/
|
540 |
public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){ |
541 |
|
542 |
return null; |
543 |
} |
544 |
|
545 |
public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) { |
546 |
Extent selectedExtent = new Extent(x, y, x + w, y - h);
|
547 |
setView(selectedExtent); |
548 |
|
549 |
int width = 0; |
550 |
int height = 0; |
551 |
if(file.trans != null){ |
552 |
width = (int)Math.abs(selectedExtent.width() / file.trans.adfgeotransform[1]);//(int)(selectedExtent.width() * file.width) / extent.width(); |
553 |
height = (int)Math.abs(selectedExtent.height() / file.trans.adfgeotransform[5]); |
554 |
}else{
|
555 |
width = (int)Math.abs(selectedExtent.width()); |
556 |
height = (int)Math.abs(selectedExtent.height()); |
557 |
} |
558 |
|
559 |
try {
|
560 |
file.readWindow(rasterBuf, bandList, x, y, width, height); |
561 |
} catch (Exception e) { |
562 |
e.printStackTrace(); |
563 |
} |
564 |
|
565 |
return rasterBuf;
|
566 |
} |
567 |
|
568 |
/*
|
569 |
* (non-Javadoc)
|
570 |
* @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
|
571 |
*/
|
572 |
public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, RasterBuf rasterBuf) { |
573 |
Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
|
574 |
setView(selectedExtent); |
575 |
|
576 |
double width = 0; |
577 |
double height = 0; |
578 |
if(getTransform() != null){ |
579 |
width = (double)(Math.abs(selectedExtent.width() / getTransform()[1]));//(int)(selectedExtent.width() * file.width) / extent.width(); |
580 |
height = (double)(Math.abs(selectedExtent.height() / getTransform()[5])); |
581 |
}else{
|
582 |
width = (double)Math.abs(selectedExtent.width()); |
583 |
height = (double)Math.abs(selectedExtent.height()); |
584 |
} |
585 |
|
586 |
try {
|
587 |
file.readWindow(rasterBuf, bandList, minX, maxY, maxX, minY, width, height, bufWidth, bufHeight); |
588 |
} catch (Exception e) { |
589 |
e.printStackTrace(); |
590 |
} |
591 |
|
592 |
return rasterBuf;
|
593 |
} |
594 |
|
595 |
public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf) { |
596 |
try {
|
597 |
setView( |
598 |
new Extent( Utilities.getMapRectFromPxRect(getExtent().toRectangle2D(), |
599 |
getWidth(), |
600 |
getHeight(), |
601 |
new Rectangle2D.Double(x, y, w, h))) |
602 |
); |
603 |
file.readWindow(rasterBuf, bandList, x, y, w, h); |
604 |
} catch (Exception e) { |
605 |
e.printStackTrace(); |
606 |
} |
607 |
return rasterBuf;
|
608 |
} |
609 |
|
610 |
public RasterBuf getWindowRasterWithNoData(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) { |
611 |
Extent selectedExtent = new Extent(x, y, x + w, y - h);
|
612 |
setView(selectedExtent); |
613 |
|
614 |
try {
|
615 |
file.readWindowWithNoData(rasterBuf, bandList, x, y, x + w, y - h, rasterBuf.getWidth(), rasterBuf.getHeight()); |
616 |
} catch (Exception e) { |
617 |
e.printStackTrace(); |
618 |
} |
619 |
|
620 |
return rasterBuf;
|
621 |
} |
622 |
|
623 |
/**
|
624 |
* Obtiene la zona (Norte / Sur)
|
625 |
* @return true si la zona es norte y false si es sur
|
626 |
*/
|
627 |
|
628 |
public boolean getZone(){ |
629 |
|
630 |
return false; |
631 |
} |
632 |
|
633 |
/**
|
634 |
*Devuelve el n?mero de zona UTM
|
635 |
*@return N?mero de zona
|
636 |
*/
|
637 |
|
638 |
public int getUTM(){ |
639 |
|
640 |
return 0; |
641 |
} |
642 |
|
643 |
/**
|
644 |
* Obtiene el sistema de coordenadas geograficas
|
645 |
* @return Sistema de coordenadas geogr?ficas
|
646 |
*/
|
647 |
public String getGeogCS(){ |
648 |
return new String(""); |
649 |
} |
650 |
|
651 |
/**
|
652 |
* Devuelve el tama?o de bloque
|
653 |
* @return Tama?o de bloque
|
654 |
*/
|
655 |
public int getBlockSize(){ |
656 |
if(file != null) |
657 |
return file.getBlockSize();
|
658 |
else
|
659 |
return 0; |
660 |
} |
661 |
|
662 |
/**
|
663 |
* Obtiene el objeto que contiene los metadatos
|
664 |
*/
|
665 |
public Metadata getMetadata() {
|
666 |
if(file != null) |
667 |
return file.getMetadataJavaObject();
|
668 |
else
|
669 |
return null; |
670 |
} |
671 |
|
672 |
/**
|
673 |
* Obtiene el flag que dice si la imagen est? o no georreferenciada
|
674 |
* @return true si est? georreferenciada y false si no lo est?.
|
675 |
*/
|
676 |
public boolean isGeoreferenced() { |
677 |
if(file != null) |
678 |
return file.isGeoreferenced();
|
679 |
else
|
680 |
return false; |
681 |
} |
682 |
|
683 |
/**
|
684 |
* Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
685 |
* el valor de esta variable cada vez que dibuja.
|
686 |
* @return true si se ha supersampleado y false si no se ha hecho.
|
687 |
*/
|
688 |
public boolean isSupersampling() { |
689 |
if(file != null) |
690 |
return file.isSupersampling;
|
691 |
else
|
692 |
return false; |
693 |
} |
694 |
|
695 |
/**
|
696 |
* Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
|
697 |
* un fichero tfw.
|
698 |
* <UL>
|
699 |
* <LI>[1]tama?o de pixel en X</LI>
|
700 |
* <LI>[2]rotaci?n en X</LI>
|
701 |
* <LI>[4]rotaci?n en Y</LI>
|
702 |
* <LI>[5]tama?o de pixel en Y</LI>
|
703 |
* <LI>[0]origen en X</LI>
|
704 |
* <LI>[3]origen en Y</LI>
|
705 |
* </UL>
|
706 |
* Este m?todo debe ser reimplementado por el driver si tiene esta informaci?n. En principio
|
707 |
* Gdal es capaz de proporcionarla de esta forma.
|
708 |
*
|
709 |
* En caso de que exista fichero .rmf asociado al raster pasaremos de la informaci?n de georreferenciaci?n
|
710 |
* del .tfw y devolveremos la que est? asociada al rmf
|
711 |
* @return vector de double con los elementos de la transformaci?n af?n.
|
712 |
*/
|
713 |
public double[] getTransform(){ |
714 |
if(file != null && file.trans != null && !this.rmfExists()) |
715 |
return file.trans.adfgeotransform;
|
716 |
else{
|
717 |
if(this.rmfExists){ |
718 |
double[] rmfGeoref = { rmfTransform.getTranslateX(), |
719 |
rmfTransform.getScaleX(), |
720 |
rmfTransform.getShearX(), |
721 |
rmfTransform.getTranslateY(), |
722 |
rmfTransform.getShearY(), |
723 |
rmfTransform.getScaleY()}; |
724 |
return rmfGeoref;
|
725 |
} |
726 |
return null; |
727 |
} |
728 |
|
729 |
} |
730 |
|
731 |
/*
|
732 |
* (non-Javadoc)
|
733 |
* @see org.gvsig.fmap.driver.GeoRasterFile#rasterToWorld(java.awt.geom.Point2D)
|
734 |
*/
|
735 |
public Point2D rasterToWorld(Point2D pt) { |
736 |
return file.rasterToWorld(pt);
|
737 |
} |
738 |
|
739 |
/*
|
740 |
* (non-Javadoc)
|
741 |
* @see org.gvsig.fmap.driver.GeoRasterFile#worldToRaster(java.awt.geom.Point2D)
|
742 |
*/
|
743 |
public Point2D worldToRaster(Point2D pt){ |
744 |
return file.worldToRaster(pt);
|
745 |
} |
746 |
|
747 |
public void readPalette(){ |
748 |
file.readPalette(); |
749 |
} |
750 |
|
751 |
} |
752 |
|
753 |
|
754 |
|