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 | 8026 | nacho | /*
|
---|---|---|---|
2 | * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
||
3 | *
|
||
4 | 12539 | jmvivo | * Copyright (C) 2004-5.
|
5 | *
|
||
6 | 8026 | nacho | * 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 | 12539 | jmvivo | *
|
22 | 8026 | nacho | * 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 | 12543 | maquerol | |
58 | public GdalFile(IProjection proj, String fName, boolean hdf){ |
||
59 | super(proj, fName);
|
||
60 | } |
||
61 | |||
62 | 8026 | nacho | 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 | 12539 | jmvivo | bandCount = file.getRasterCount(); |
70 | 8026 | nacho | 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 | 12539 | jmvivo | |
84 | 8026 | nacho | //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
85 | setDataType(org.cresques.util.Utilities.getRasterBufTypeFromGdalType(file.getDataType())); |
||
86 | } |
||
87 | 12539 | jmvivo | |
88 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
97 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
112 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
122 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | public void setView(Extent e) { |
128 | 8026 | nacho | if(rmfExists){
|
129 | 12539 | jmvivo | |
130 | 8026 | nacho | 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 | 8631 | nacho | transformRMF.inverseTransform(petInit, petInit); |
135 | transformRMF.inverseTransform(petEnd, petEnd); |
||
136 | transformTFW.transform(petInit, petInit); |
||
137 | transformTFW.transform(petEnd, petEnd); |
||
138 | 8026 | nacho | }catch(NoninvertibleTransformException ex){} |
139 | 8631 | nacho | double h = file.bBoxWithoutRot.maxY - file.bBoxWithoutRot.minY;
|
140 | if(!file.isGeoreferenced())
|
||
141 | 12539 | jmvivo | v = new Extent( petInit.getX(), h - petInit.getY(), petEnd.getX(), h - petEnd.getY());
|
142 | 8631 | nacho | else
|
143 | v = new Extent( petInit.getX(), petInit.getY(), petEnd.getX(), petEnd.getY());
|
||
144 | 12539 | jmvivo | |
145 | 8026 | nacho | }else
|
146 | 12539 | jmvivo | v = new Extent(e.minX(), e.minY(), e.maxX(), e.maxY());
|
147 | 8026 | nacho | } |
148 | 12539 | jmvivo | |
149 | 8631 | nacho | /**
|
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 | 12539 | jmvivo | public void setExtentTransform(double originX, double originY, double psX, double psY) { |
157 | 8631 | nacho | transformRMF.setToTranslation(originX, originY); |
158 | transformRMF.scale(psX, psY); |
||
159 | 12539 | jmvivo | |
160 | if(file.trans != null){ |
||
161 | 8631 | nacho | transformTFW.setToTranslation(file.trans.adfgeotransform[0], file.trans.adfgeotransform[3]); |
162 | transformTFW.scale(file.trans.adfgeotransform[1], file.trans.adfgeotransform[5]); |
||
163 | } |
||
164 | } |
||
165 | 12539 | jmvivo | |
166 | 8026 | nacho | /**
|
167 | * Obtiene extent de la vista actual
|
||
168 | */
|
||
169 | 12539 | jmvivo | public Extent getView() {
|
170 | return v;
|
||
171 | 8026 | nacho | } |
172 | 12539 | jmvivo | |
173 | 8026 | nacho | /**
|
174 | * Obtiene la anchura del fichero
|
||
175 | */
|
||
176 | 12539 | jmvivo | public int getWidth() { |
177 | return file.width;
|
||
178 | 8026 | nacho | } |
179 | 12539 | jmvivo | |
180 | 8026 | nacho | /**
|
181 | * Obtiene la altura del fichero
|
||
182 | */
|
||
183 | 12539 | jmvivo | public int getHeight() { |
184 | 8026 | nacho | 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 | 12539 | jmvivo | |
194 | 8631 | nacho | /**
|
195 | * Obtiene la orientaci?n de la imagen a partir del signo del tama?o de pixel para poder
|
||
196 | 12539 | jmvivo | * 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 | 8631 | nacho | * 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 | 12539 | jmvivo | *
|
207 | 8631 | nacho | * @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 | 12539 | jmvivo | |
225 | 8026 | nacho | /* (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 | 12539 | jmvivo | |
232 | 8026 | nacho | 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 | 12539 | jmvivo | |
237 | 8026 | nacho | if (dFileAspect > dWindowAspect) {
|
238 | height =(int)((double)width/dFileAspect); |
||
239 | } else {
|
||
240 | width = (int)((double)height*dFileAspect); |
||
241 | } |
||
242 | } |
||
243 | 12539 | jmvivo | |
244 | 8026 | nacho | // Set the view
|
245 | 8631 | nacho | file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height, getOrientation()); |
246 | 8026 | nacho | setStep(file.stepArrayX, file.stepArrayY); |
247 | 12539 | jmvivo | |
248 | 8026 | nacho | if(width<=0)width=1; |
249 | if(height<=0)height=1; |
||
250 | 12539 | jmvivo | |
251 | 8026 | nacho | 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 | 12539 | jmvivo | |
271 | 8026 | nacho | return image;
|
272 | } |
||
273 | 12539 | jmvivo | |
274 | 8026 | nacho | public RasterBuf getRaster(int width, int height, ICoordTrans rp) { |
275 | int line;
|
||
276 | RasterBuf raster = null;
|
||
277 | 12539 | jmvivo | |
278 | 8026 | nacho | 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 | 12539 | jmvivo | |
283 | 8026 | nacho | if (dFileAspect > dWindowAspect) {
|
284 | height =(int)((double)width/dFileAspect); |
||
285 | } else {
|
||
286 | width = (int)((double)height*dFileAspect); |
||
287 | } |
||
288 | } |
||
289 | 12539 | jmvivo | |
290 | 8026 | nacho | // Set the view
|
291 | 8631 | nacho | boolean[] orientation = getOrientation(); |
292 | file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
||
293 | width, height, orientation); |
||
294 | 8026 | nacho | setStep(file.stepArrayX, file.stepArrayY); |
295 | 12539 | jmvivo | |
296 | 8026 | nacho | 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 | 12539 | jmvivo | |
300 | 8026 | nacho | file.setAlpha(getAlpha()); |
301 | setBand(RED_BAND, rBandNr); |
||
302 | setBand(GREEN_BAND, gBandNr); |
||
303 | setBand(BLUE_BAND, bBandNr); |
||
304 | 12539 | jmvivo | |
305 | 8026 | nacho | switch(getDataType()){
|
306 | 12539 | jmvivo | case RasterBuf.TYPE_BYTE:
|
307 | for (line = 0; line < height; line++) |
||
308 | 8026 | nacho | file.readLine(raster.getLineByte(line)); |
309 | break;
|
||
310 | 12539 | jmvivo | case RasterBuf.TYPE_SHORT:;
|
311 | for (line = 0; line < height; line++) |
||
312 | 8026 | nacho | file.readLine(raster.getLineShort(line)); |
313 | break;
|
||
314 | case RasterBuf.TYPE_INT:
|
||
315 | 12539 | jmvivo | for (line = 0; line < height; line++) |
316 | 8026 | nacho | file.readLine(raster.getLineInt(line)); |
317 | break;
|
||
318 | case RasterBuf.TYPE_FLOAT:
|
||
319 | 12539 | jmvivo | for (line = 0; line < height; line++) |
320 | 8026 | nacho | file.readLine(raster.getLineFloat(line)); |
321 | break;
|
||
322 | case RasterBuf.TYPE_DOUBLE:
|
||
323 | 12539 | jmvivo | for (line = 0; line < height; line++) |
324 | 8026 | nacho | file.readLine(raster.getLineDouble(line)); |
325 | break;
|
||
326 | case RasterBuf.TYPE_UNDEFINED:break; |
||
327 | } |
||
328 | 12539 | jmvivo | |
329 | 8026 | nacho | } catch (Exception e) { |
330 | e.printStackTrace(); |
||
331 | } |
||
332 | 12539 | jmvivo | |
333 | 8026 | nacho | return raster;
|
334 | } |
||
335 | 12539 | jmvivo | |
336 | 8026 | nacho | /**
|
337 | 12539 | jmvivo | * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
|
338 | 8026 | nacho | * 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 | 12539 | jmvivo | protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
349 | 8026 | nacho | int offset, int scansize) { |
350 | image.setRGB(startX, startY, w, h, rgbArray, offset, scansize); |
||
351 | } |
||
352 | 12539 | jmvivo | |
353 | 8026 | nacho | /**
|
354 | 12539 | jmvivo | * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
355 | 8026 | nacho | * 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 | 12539 | jmvivo | protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
369 | 8026 | nacho | int offset, int scansize, int flags) { |
370 | 12539 | jmvivo | int [] line = new int[rgbArray.length]; |
371 | 8026 | nacho | 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 | 12539 | jmvivo | |
384 | 8026 | nacho | /**
|
385 | 12539 | jmvivo | * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
386 | 8026 | nacho | * 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 | 12539 | jmvivo | protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
401 | 8026 | nacho | int offset, int scansize, int origBand, int destBandFlag) { |
402 | 12539 | jmvivo | int [] line = new int[rgbArray.length]; |
403 | 8026 | nacho | 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 | 12539 | jmvivo | |
414 | 8026 | nacho | 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 | 12539 | jmvivo | |
424 | 8026 | nacho | 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 | 12539 | jmvivo | |
436 | 8026 | nacho | 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 | 12539 | jmvivo | |
458 | 8026 | nacho | 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 | 12539 | jmvivo | |
463 | 8026 | nacho | if (dFileAspect > dWindowAspect) {
|
464 | height =(int)((double)width/dFileAspect); |
||
465 | } else {
|
||
466 | width = (int)((double)height*dFileAspect); |
||
467 | } |
||
468 | } |
||
469 | |||
470 | 8631 | nacho | // Set the view
|
471 | boolean[] orientation = getOrientation(); |
||
472 | file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
||
473 | width, height, orientation); |
||
474 | 8026 | nacho | setStep(file.stepArrayX, file.stepArrayY); |
475 | |||
476 | if(width<=0)width=1; |
||
477 | if(height<=0)height=1; |
||
478 | 12539 | jmvivo | |
479 | 8026 | nacho | 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 | 8631 | nacho | 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 | 12539 | jmvivo | } |
491 | 8631 | nacho | }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 | 8026 | nacho | } |
497 | return img;
|
||
498 | }else{
|
||
499 | Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
||
500 | 8631 | nacho | 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 | 12539 | jmvivo | } |
505 | 8631 | nacho | }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 | 8026 | nacho | return image;
|
512 | } |
||
513 | } catch (Exception e) { |
||
514 | // TODO Auto-generated catch block
|
||
515 | e.printStackTrace(); |
||
516 | } |
||
517 | 12539 | jmvivo | |
518 | 8026 | nacho | return img;
|
519 | } |
||
520 | 12539 | jmvivo | |
521 | 8026 | nacho | /* (non-Javadoc)
|
522 | * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
|
||
523 | */
|
||
524 | public Object getData(int x, int y, int band) { |
||
525 | 10645 | nacho | if(file != null){ |
526 | Object[] data = file.getData(x, y); |
||
527 | return data[band];
|
||
528 | } |
||
529 | 8026 | nacho | return null; |
530 | } |
||
531 | 12539 | jmvivo | |
532 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
542 | 8026 | nacho | return null; |
543 | } |
||
544 | 12539 | jmvivo | |
545 | public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) { |
||
546 | 8026 | nacho | Extent selectedExtent = new Extent(x, y, x + w, y - h);
|
547 | setView(selectedExtent); |
||
548 | 12539 | jmvivo | |
549 | 8026 | nacho | 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 | 12539 | jmvivo | |
559 | 8026 | nacho | try {
|
560 | file.readWindow(rasterBuf, bandList, x, y, width, height); |
||
561 | } catch (Exception e) { |
||
562 | e.printStackTrace(); |
||
563 | } |
||
564 | 12539 | jmvivo | |
565 | 8026 | nacho | return rasterBuf;
|
566 | } |
||
567 | 12539 | jmvivo | |
568 | 10645 | nacho | /*
|
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 | 12539 | jmvivo | public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, RasterBuf rasterBuf) { |
573 | 10645 | nacho | Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
|
574 | setView(selectedExtent); |
||
575 | 12539 | jmvivo | |
576 | 10645 | nacho | 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 | 12539 | jmvivo | |
586 | 10645 | nacho | try {
|
587 | file.readWindow(rasterBuf, bandList, minX, maxY, maxX, minY, width, height, bufWidth, bufHeight); |
||
588 | } catch (Exception e) { |
||
589 | e.printStackTrace(); |
||
590 | } |
||
591 | 12539 | jmvivo | |
592 | 10645 | nacho | return rasterBuf;
|
593 | } |
||
594 | 12539 | jmvivo | |
595 | 8026 | nacho | public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf) { |
596 | try {
|
||
597 | setView( |
||
598 | 12539 | jmvivo | new Extent( Utilities.getMapRectFromPxRect(getExtent().toRectangle2D(), |
599 | getWidth(), |
||
600 | 8026 | nacho | 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 | 12539 | jmvivo | |
610 | 10645 | nacho | 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 | 12539 | jmvivo | |
614 | 10645 | nacho | 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 | 12539 | jmvivo | |
620 | 10645 | nacho | return rasterBuf;
|
621 | } |
||
622 | 12539 | jmvivo | |
623 | 8026 | nacho | /**
|
624 | * Obtiene la zona (Norte / Sur)
|
||
625 | * @return true si la zona es norte y false si es sur
|
||
626 | */
|
||
627 | 12539 | jmvivo | |
628 | 8026 | nacho | public boolean getZone(){ |
629 | 12539 | jmvivo | |
630 | 8026 | nacho | return false; |
631 | } |
||
632 | 12539 | jmvivo | |
633 | 8026 | nacho | /**
|
634 | *Devuelve el n?mero de zona UTM
|
||
635 | 12539 | jmvivo | *@return N?mero de zona
|
636 | 8026 | nacho | */
|
637 | 12539 | jmvivo | |
638 | 8026 | nacho | public int getUTM(){ |
639 | 12539 | jmvivo | |
640 | return 0; |
||
641 | 8026 | nacho | } |
642 | 12539 | jmvivo | |
643 | 8026 | nacho | /**
|
644 | * Obtiene el sistema de coordenadas geograficas
|
||
645 | * @return Sistema de coordenadas geogr?ficas
|
||
646 | */
|
||
647 | public String getGeogCS(){ |
||
648 | 12539 | jmvivo | return new String(""); |
649 | 8026 | nacho | } |
650 | 12539 | jmvivo | |
651 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
662 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | |
672 | 8026 | nacho | /**
|
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 | 12539 | jmvivo | else
|
680 | 8026 | nacho | return false; |
681 | } |
||
682 | 12539 | jmvivo | |
683 | 8026 | nacho | /**
|
684 | * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
||
685 | 12539 | jmvivo | * el valor de esta variable cada vez que dibuja.
|
686 | 8026 | nacho | * @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 | 12539 | jmvivo | else
|
692 | 8026 | nacho | return false; |
693 | } |
||
694 | 12539 | jmvivo | |
695 | 8026 | nacho | /**
|
696 | * Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
|
||
697 | * un fichero tfw.
|
||
698 | 12539 | jmvivo | * <UL>
|
699 | 8026 | nacho | * <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 | 12539 | jmvivo | *
|
709 | 8631 | nacho | * 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 | 8026 | nacho | * @return vector de double con los elementos de la transformaci?n af?n.
|
712 | */
|
||
713 | public double[] getTransform(){ |
||
714 | 8631 | nacho | if(file != null && file.trans != null && !this.rmfExists()) |
715 | 8026 | nacho | return file.trans.adfgeotransform;
|
716 | 8631 | nacho | else{
|
717 | if(this.rmfExists){ |
||
718 | 12539 | jmvivo | double[] rmfGeoref = { rmfTransform.getTranslateX(), |
719 | 8631 | nacho | rmfTransform.getScaleX(), |
720 | 12539 | jmvivo | rmfTransform.getShearX(), |
721 | 8631 | nacho | rmfTransform.getTranslateY(), |
722 | rmfTransform.getShearY(), |
||
723 | rmfTransform.getScaleY()}; |
||
724 | return rmfGeoref;
|
||
725 | } |
||
726 | return null; |
||
727 | } |
||
728 | 12539 | jmvivo | |
729 | 8026 | nacho | } |
730 | 10645 | nacho | |
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 | 12539 | jmvivo | |
739 | 10645 | nacho | /*
|
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 | 12539 | jmvivo | |
747 | 10645 | nacho | public void readPalette(){ |
748 | file.readPalette(); |
||
749 | } |
||
750 | 12543 | maquerol | |
751 | 8026 | nacho | } |
752 | |||
753 |