root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / GdalNative.java @ 37962
History | View | Annotate | Download (45.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. 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 |
package org.gvsig.raster.dataset.io; |
20 |
|
21 |
import java.awt.geom.AffineTransform; |
22 |
import java.awt.geom.NoninvertibleTransformException; |
23 |
import java.awt.geom.Point2D; |
24 |
import java.io.IOException; |
25 |
|
26 |
import org.gvsig.raster.RasterLibrary; |
27 |
import org.gvsig.raster.dataset.BandList; |
28 |
import org.gvsig.raster.dataset.IBuffer; |
29 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
30 |
import org.gvsig.raster.dataset.properties.DatasetMetadata; |
31 |
import org.gvsig.raster.datastruct.ColorTable; |
32 |
import org.gvsig.raster.datastruct.Extent; |
33 |
import org.gvsig.raster.datastruct.Transparency; |
34 |
import org.gvsig.raster.process.RasterTask; |
35 |
import org.gvsig.raster.process.RasterTaskQueue; |
36 |
import org.gvsig.raster.util.RasterUtilities; |
37 |
|
38 |
import es.gva.cit.jgdal.Gdal; |
39 |
import es.gva.cit.jgdal.GdalBuffer; |
40 |
import es.gva.cit.jgdal.GdalException; |
41 |
import es.gva.cit.jgdal.GdalRasterBand; |
42 |
import es.gva.cit.jgdal.GeoTransform; |
43 |
/**
|
44 |
* Soporte 'nativo' para ficheros desde GDAL.
|
45 |
*
|
46 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
47 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
48 |
*/
|
49 |
public class GdalNative extends Gdal { |
50 |
private String fileName = null; |
51 |
/**
|
52 |
* Nombre corto del driver de gdal
|
53 |
*/
|
54 |
private String shortName = ""; |
55 |
public GeoTransform trans = null; |
56 |
|
57 |
public int width = 0, height = 0; |
58 |
public double originX = 0D, originY = 0D; |
59 |
public String version = ""; |
60 |
protected int rBandNr = 1, gBandNr = 2, bBandNr = 3, aBandNr = 4; |
61 |
private int[] dataType = null; |
62 |
/**
|
63 |
* Metadatos leidos de la imagen
|
64 |
*/
|
65 |
protected DatasetMetadata metadata = null; |
66 |
protected boolean georeferenced = true; |
67 |
|
68 |
/**
|
69 |
* Vectores que contiene los desplazamientos de un pixel cuando hay supersampling.
|
70 |
* , es decir el n?mero de pixels de pantalla que tiene un pixel de imagen. Como todos
|
71 |
* los pixeles no tienen el mismo ancho y alto ha de meterse en un array y no puede ser
|
72 |
* una variable. Adem?s hay que tener en cuenta que el primer y ?ltimo pixel son de
|
73 |
* distinto tama?o que el resto.
|
74 |
*/
|
75 |
public int[] stepArrayX = null, stepArrayY = null; |
76 |
protected GdalRasterBand[] gdalBands = null; |
77 |
private double lastReadLine = -1; |
78 |
private int currentFullWidth = -1; |
79 |
private int currentFullHeight = -1; |
80 |
private int currentViewWidth = -1; |
81 |
private int currentViewHeight = -1; |
82 |
private double currentViewX = 0D; |
83 |
private double viewportScaleX = 0D; |
84 |
private double viewportScaleY = 0D; |
85 |
//private double wcWidth = 0D;
|
86 |
private double stepX = 0D; |
87 |
private double stepY = 0D; |
88 |
public boolean isSupersampling = false; |
89 |
/**
|
90 |
* Estado de transparencia del raster.
|
91 |
*/
|
92 |
protected Transparency fileTransparency = null; |
93 |
protected ColorTable palette = null; |
94 |
protected DatasetColorInterpretation colorInterpr = null; |
95 |
protected AffineTransform ownTransformation = null; |
96 |
protected AffineTransform externalTransformation = new AffineTransform(); |
97 |
|
98 |
/**
|
99 |
* Overview usada en el ?ltimo setView
|
100 |
*/
|
101 |
int currentOverview = -1; |
102 |
|
103 |
|
104 |
public GdalNative(String fName) throws GdalException, IOException { |
105 |
super();
|
106 |
init(fName); |
107 |
} |
108 |
|
109 |
private void init(String fName) throws GdalException, IOException { |
110 |
fileName = fName; |
111 |
open(fName, GA_ReadOnly); |
112 |
if (getPtro() == -1) |
113 |
throw new GdalException("Error en la apertura del fichero. El fichero no tiene un formato v?lido."); |
114 |
// ext = RasterUtilities.getExtensionFromFileName(fName);
|
115 |
width = getRasterXSize(); |
116 |
height = getRasterYSize(); |
117 |
int[] dt = new int[getRasterCount()]; |
118 |
for (int i = 0; i < getRasterCount(); i++) |
119 |
dt[i] = this.getRasterBand(i + 1).getRasterDataType(); |
120 |
setDataType(dt); |
121 |
shortName = getDriverShortName(); |
122 |
fileTransparency = new Transparency(); |
123 |
colorInterpr = new DatasetColorInterpretation();
|
124 |
metadata = new DatasetMetadata(getMetadata(), colorInterpr);
|
125 |
|
126 |
// Asignamos la interpretaci?n de color leida por gdal a cada banda. Esto
|
127 |
// nos sirve para saber que banda de la imagen va asignada a cada banda de
|
128 |
// visualizaci?n (ARGB)
|
129 |
colorInterpr.initColorInterpretation(getRasterCount()); |
130 |
metadata.initNoDataByBand(getRasterCount()); |
131 |
for (int i = 0; i < getRasterCount(); i++) { |
132 |
GdalRasterBand rb = getRasterBand(i + 1);
|
133 |
String colorInt = getColorInterpretationName(rb.getRasterColorInterpretation());
|
134 |
metadata.setNoDataValue(i, (rb.getRasterNoDataValue() == -9999.0) ? RasterLibrary.defaultNoDataValue : rb.getRasterNoDataValue());
|
135 |
metadata.setNoDataEnabled(rb.existsNoDataValue()); |
136 |
colorInterpr.setColorInterpValue(i, colorInt); |
137 |
if (colorInt.equals("Alpha")) |
138 |
fileTransparency.setTransparencyBand(i); |
139 |
|
140 |
if (rb.getRasterColorTable() != null && palette == null) { |
141 |
palette = new ColorTable();
|
142 |
palette.createPaletteFromGdalColorTable(rb.getRasterColorTable()); |
143 |
// fileTransparency.setTransparencyRangeList(palette.getTransparencyRange());
|
144 |
} |
145 |
} |
146 |
fileTransparency.setTransparencyByPixelFromMetadata(metadata); |
147 |
|
148 |
try {
|
149 |
trans = getGeoTransform(); |
150 |
|
151 |
boolean isCorrect = false; |
152 |
for (int i = 0; i < trans.adfgeotransform.length; i++) |
153 |
if (trans.adfgeotransform[i] != 0) |
154 |
isCorrect = true;
|
155 |
if (!isCorrect)
|
156 |
throw new GdalException(""); |
157 |
|
158 |
ownTransformation = new AffineTransform(trans.adfgeotransform[1], trans.adfgeotransform[4], trans.adfgeotransform[2], trans.adfgeotransform[5], trans.adfgeotransform[0], trans.adfgeotransform[3]); |
159 |
externalTransformation = (AffineTransform) ownTransformation.clone();
|
160 |
currentFullWidth = width; |
161 |
currentFullHeight = height; |
162 |
|
163 |
this.georeferenced = true; |
164 |
} catch (GdalException exc) {
|
165 |
// Transformaci?n para ficheros sin georreferenciaci?n. Se invierte la Y
|
166 |
// ya que las WC decrecen de
|
167 |
// arriba a abajo y los pixeles crecen de arriba a abajo
|
168 |
ownTransformation = new AffineTransform(1, 0, 0, -1, 0, height); |
169 |
externalTransformation = (AffineTransform) ownTransformation.clone();
|
170 |
currentFullWidth = width; |
171 |
currentFullHeight = height; |
172 |
this.georeferenced = false; |
173 |
} |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Obtiene el flag que informa de si el raster tiene valor no data o no.
|
178 |
* Consultar? todas las bandas del mismo y si alguna tiene valor no data
|
179 |
* devuelve true sino devolver? false.
|
180 |
* @return true si tiene valor no data y false si no lo tiene
|
181 |
* @throws GdalException
|
182 |
*/
|
183 |
public boolean existsNoDataValue() throws GdalException { |
184 |
for (int i = 0; i < getRasterCount(); i++) { |
185 |
GdalRasterBand rb = getRasterBand(i + 1);
|
186 |
if (rb.existsNoDataValue())
|
187 |
return true; |
188 |
} |
189 |
return false; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Obtiene el flag que informa de si el raster tiene valor no data o no
|
194 |
* en una banda concreta.
|
195 |
* @return true si tiene valor no data en esa banda y false si no lo tiene
|
196 |
* @param band Posici?n de la banda a consultar (0..n)
|
197 |
* @throws GdalException
|
198 |
*/
|
199 |
public boolean existsNoDataValue(int band) throws GdalException { |
200 |
GdalRasterBand rb = getRasterBand(band + 1);
|
201 |
return rb.existsNoDataValue();
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Devuelve el valor NoData en caso de existir, sino existe devuelve null.
|
206 |
* @return
|
207 |
*/
|
208 |
public double getNoDataValue() { |
209 |
if (metadata == null) |
210 |
return RasterLibrary.defaultNoDataValue;
|
211 |
|
212 |
if (metadata.getNoDataValue().length == 0) |
213 |
return RasterLibrary.defaultNoDataValue;
|
214 |
|
215 |
return metadata.getNoDataValue()[0]; |
216 |
} |
217 |
|
218 |
/**
|
219 |
* Asigna el tipo de dato
|
220 |
* @param dt entero que representa el tipo de dato
|
221 |
*/
|
222 |
public void setDataType(int[] dt) { |
223 |
dataType = dt; |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Obtiene el tipo de dato
|
228 |
* @return entero que representa el tipo de dato
|
229 |
*/
|
230 |
public int[] getDataType() { |
231 |
return dataType;
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
|
236 |
* del punto real.
|
237 |
* Supone rasters no girados
|
238 |
* @param pt punto en coordenadas del punto real
|
239 |
* @return punto en coordenadas del raster
|
240 |
*/
|
241 |
public Point2D worldToRasterWithoutRot(Point2D pt) { |
242 |
Point2D p = new Point2D.Double(); |
243 |
AffineTransform at = new AffineTransform( externalTransformation.getScaleX(), 0, |
244 |
0, externalTransformation.getScaleY(),
|
245 |
externalTransformation.getTranslateX(), externalTransformation.getTranslateY()); |
246 |
try {
|
247 |
at.inverseTransform(pt, p); |
248 |
} catch (NoninvertibleTransformException e) { |
249 |
return pt;
|
250 |
} |
251 |
return p;
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
|
256 |
* del punto real.
|
257 |
* Supone rasters no girados
|
258 |
* @param pt punto en coordenadas del punto real
|
259 |
* @return punto en coordenadas del raster
|
260 |
*/
|
261 |
public Point2D worldToRaster(Point2D pt) { |
262 |
Point2D p = new Point2D.Double(); |
263 |
try {
|
264 |
externalTransformation.inverseTransform(pt, p); |
265 |
} catch (NoninvertibleTransformException e) { |
266 |
return pt;
|
267 |
} |
268 |
return p;
|
269 |
} |
270 |
|
271 |
/**
|
272 |
* Obtiene un punto del raster en coordenadas pixel a partir de un punto en coordenadas
|
273 |
* reales.
|
274 |
* @param pt Punto en coordenadas reales
|
275 |
* @return Punto en coordenadas pixel.
|
276 |
*/
|
277 |
public Point2D rasterToWorld(Point2D pt) { |
278 |
Point2D p = new Point2D.Double(); |
279 |
externalTransformation.transform(pt, p); |
280 |
return p;
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* Calcula el overview a usar. Hay que tener en cuenta que tenemos que tener calculadas las variables
|
285 |
* viewPortScale, currentFullWidth y currentFulHeight
|
286 |
* @param coordenada pixel expresada en double que indica la posici?n superior izquierda
|
287 |
* @throws GdalException
|
288 |
*/
|
289 |
private void calcOverview(Point2D tl, Point2D br) throws GdalException { |
290 |
gdalBands[0] = getRasterBand(1); |
291 |
currentOverview = -1;
|
292 |
if (gdalBands[0].getOverviewCount() > 0) { |
293 |
GdalRasterBand ovb = null;
|
294 |
for (int i = gdalBands[0].getOverviewCount() - 1; i > 0; i--) { |
295 |
ovb = gdalBands[0].getOverview(i);
|
296 |
if (ovb.getRasterBandXSize() > getRasterXSize() * viewportScaleX) {
|
297 |
currentOverview = i; |
298 |
viewportScaleX *= ((double) width / (double) ovb.getRasterBandXSize()); |
299 |
viewportScaleY *= ((double) height / (double) ovb.getRasterBandYSize()); |
300 |
stepX = 1D / viewportScaleX;
|
301 |
stepY = 1D / viewportScaleY;
|
302 |
currentFullWidth = ovb.getRasterBandXSize(); |
303 |
currentFullHeight = ovb.getRasterBandYSize(); |
304 |
currentViewX = Math.min(tl.getX(), br.getX());
|
305 |
lastReadLine = Math.min(tl.getY(), br.getY());
|
306 |
break;
|
307 |
} |
308 |
} |
309 |
} |
310 |
} |
311 |
|
312 |
public void setView(double dWorldTLX, double dWorldTLY, |
313 |
double dWorldBRX, double dWorldBRY, |
314 |
int nWidth, int nHeight) throws GdalException { |
315 |
currentFullWidth = width; |
316 |
currentFullHeight = height; |
317 |
Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY)); |
318 |
Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY)); |
319 |
// Calcula cual es la primera l?nea a leer;
|
320 |
currentViewWidth = nWidth; |
321 |
currentViewHeight = nHeight; |
322 |
// wcWidth = Math.abs(br.getX() - tl.getX());
|
323 |
|
324 |
currentViewX = Math.min(tl.getX(), br.getX());
|
325 |
|
326 |
viewportScaleX = (double) currentViewWidth / (br.getX() - tl.getX());
|
327 |
viewportScaleY = (double) currentViewHeight / (br.getY() - tl.getY());
|
328 |
stepX = 1D / viewportScaleX;
|
329 |
stepY = 1D / viewportScaleY;
|
330 |
|
331 |
lastReadLine = Math.min(tl.getY(), br.getY());
|
332 |
|
333 |
//Para lectura del renderizado (ARGB). readWindow selecciona las bandas que necesita.
|
334 |
|
335 |
// calcula el overview a usar
|
336 |
gdalBands = new GdalRasterBand[4]; |
337 |
calcOverview(tl, br); |
338 |
|
339 |
// Selecciona las bandas y los overviews necesarios
|
340 |
/*gdalBands[0] = getRasterBand(rBandNr);
|
341 |
gdalBands[1] = gdalBands[0];
|
342 |
gdalBands[2] = gdalBands[1];
|
343 |
|
344 |
if(getRasterCount() >= 2) {
|
345 |
gdalBands[1] = getRasterBand(gBandNr);
|
346 |
gdalBands[2] = gdalBands[1];
|
347 |
}
|
348 |
if(this.getRasterCount() >= 3)
|
349 |
gdalBands[2] = getRasterBand(bBandNr);
|
350 |
if(colorInterpr.isAlphaBand())
|
351 |
gdalBands[3] = getRasterBand(aBandNr);
|
352 |
|
353 |
assignDataTypeFromGdalRasterBands(gdalBands);
|
354 |
|
355 |
if (currentOverview > 0) {
|
356 |
gdalBands[0] = gdalBands[0].getOverview(currentOverview);
|
357 |
if(getRasterCount() >= 2) {
|
358 |
gdalBands[1] = gdalBands[1].getOverview(currentOverview);
|
359 |
}
|
360 |
if(this.getRasterCount() >= 3)
|
361 |
gdalBands[2] = gdalBands[2].getOverview(currentOverview);
|
362 |
if(colorInterpr.isAlphaBand())
|
363 |
gdalBands[3] = gdalBands[3].getOverview(currentOverview);
|
364 |
|
365 |
}*/
|
366 |
} |
367 |
|
368 |
/**
|
369 |
* Selecciona bandas y overview en el objeto GdalRasterBand[] para el n?mero de bandas solicitado.
|
370 |
* @param nbands N?mero de bandas solicitado.
|
371 |
* @throws GdalException
|
372 |
*/
|
373 |
public void selectGdalBands(int nbands) throws GdalException { |
374 |
gdalBands = new GdalRasterBand[nbands];
|
375 |
// Selecciona las bandas y los overviews necesarios
|
376 |
gdalBands[0] = getRasterBand(1); |
377 |
for (int i = 0; i < nbands; i++) |
378 |
gdalBands[i] = gdalBands[0];
|
379 |
|
380 |
assignDataTypeFromGdalRasterBands(gdalBands); |
381 |
// setDataType(gdalBands[0].getRasterDataType());
|
382 |
|
383 |
for (int i = 2; i <= nbands; i++) { |
384 |
if (getRasterCount() >= i) {
|
385 |
gdalBands[i - 1] = getRasterBand(i);
|
386 |
for (int j = i; j < nbands; j++) |
387 |
gdalBands[j] = gdalBands[i - 1];
|
388 |
} |
389 |
} |
390 |
|
391 |
if (currentOverview > 0) { |
392 |
gdalBands[0] = gdalBands[0].getOverview(currentOverview); |
393 |
for (int i = 2; i <= nbands; i++) { |
394 |
if (getRasterCount() >= i)
|
395 |
gdalBands[i - 1] = gdalBands[i - 1].getOverview(currentOverview); |
396 |
} |
397 |
} |
398 |
} |
399 |
|
400 |
int lastY = -1; |
401 |
|
402 |
/**
|
403 |
* Lee una l?nea de bytes
|
404 |
* @param line Buffer donde se cargan los datos
|
405 |
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
406 |
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
407 |
* por la izquierda a mitad de pixel
|
408 |
* @param gdalBuffer Buffer con la l?nea de datos original
|
409 |
*/
|
410 |
private void readLine(byte[][] line, double initOffset, GdalBuffer[] gdalBuffer) { |
411 |
double j = 0D; |
412 |
int i = 0; |
413 |
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) { |
414 |
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) { |
415 |
line[iBand][i] = gdalBuffer[iBand].buffByte[(int) j];
|
416 |
} |
417 |
} |
418 |
} |
419 |
|
420 |
/**
|
421 |
* Lee una l?nea de shorts
|
422 |
* @param line Buffer donde se cargan los datos
|
423 |
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
424 |
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
425 |
* por la izquierda a mitad de pixel
|
426 |
* @param gdalBuffer Buffer con la l?nea de datos original
|
427 |
*/
|
428 |
private void readLine(short[][] line, double initOffset, GdalBuffer[] gdalBuffer) { |
429 |
double j = 0D; |
430 |
int i = 0; |
431 |
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) { |
432 |
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) { |
433 |
line[iBand][i] = (short) (gdalBuffer[iBand].buffShort[(int) j] & 0xffff); |
434 |
} |
435 |
} |
436 |
} |
437 |
|
438 |
/**
|
439 |
* Lee una l?nea de ints
|
440 |
* @param line Buffer donde se cargan los datos
|
441 |
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
442 |
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
443 |
* por la izquierda a mitad de pixel
|
444 |
* @param gdalBuffer Buffer con la l?nea de datos original
|
445 |
*/
|
446 |
private void readLine(int[][] line, double initOffset, GdalBuffer[] gdalBuffer) { |
447 |
double j = 0D; |
448 |
int i = 0; |
449 |
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) { |
450 |
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) { |
451 |
line[iBand][i] = (gdalBuffer[iBand].buffInt[(int) j] & 0xffffffff); |
452 |
} |
453 |
} |
454 |
} |
455 |
|
456 |
/**
|
457 |
* Lee una l?nea de float
|
458 |
* @param line Buffer donde se cargan los datos
|
459 |
* @param initOffset Desplazamiento inicial desde el margen izquierdo. Esto es necesario para cuando
|
460 |
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
461 |
* por la izquierda a mitad de pixel
|
462 |
* @param gdalBuffer Buffer con la l?nea de datos original
|
463 |
*/
|
464 |
private void readLine(float[][] line, double initOffset, GdalBuffer[] gdalBuffer) { |
465 |
double j = 0D; |
466 |
int i = 0; |
467 |
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) { |
468 |
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) { |
469 |
line[iBand][i] = gdalBuffer[iBand].buffFloat[(int) j];
|
470 |
} |
471 |
} |
472 |
} |
473 |
|
474 |
/**
|
475 |
* Lee una l?nea de doubles
|
476 |
* @param line Buffer donde se cargan los datos
|
477 |
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
478 |
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
479 |
* por la izquierda a mitad de pixel
|
480 |
* @param gdalBuffer Buffer con la l?nea de datos original
|
481 |
*/
|
482 |
private void readLine(double[][] line, double initOffset, GdalBuffer[] gdalBuffer) { |
483 |
double j = 0D; |
484 |
int i = 0; |
485 |
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) { |
486 |
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) { |
487 |
line[iBand][i] = gdalBuffer[iBand].buffDouble[(int) j];
|
488 |
} |
489 |
} |
490 |
} |
491 |
|
492 |
/**
|
493 |
* Lee una l?nea completa del raster y devuelve un array del tipo correcto. Esta funci?n es util
|
494 |
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
495 |
* @param nLine N?mero de l?nea a leer
|
496 |
* @param band Banda requerida
|
497 |
* @return Object que es un array unidimendional del tipo de datos del raster
|
498 |
* @throws GdalException
|
499 |
*/
|
500 |
public Object readCompleteLine(int nLine, int band) throws GdalException { |
501 |
GdalRasterBand gdalBand = super.getRasterBand(band + 1); |
502 |
GdalBuffer gdalBuf = null;
|
503 |
|
504 |
gdalBuf = gdalBand.readRaster(0, nLine, getRasterXSize(), 1, getRasterXSize(), 1, dataType[band]); |
505 |
|
506 |
if (dataType[band] == GDT_Byte)
|
507 |
return gdalBuf.buffByte;
|
508 |
|
509 |
if (dataType[band] == GDT_Int16 || dataType[band] == GDT_UInt16)
|
510 |
return gdalBuf.buffShort;
|
511 |
|
512 |
if (dataType[band] == GDT_Int32 || dataType[band] == GDT_UInt32)
|
513 |
return gdalBuf.buffInt;
|
514 |
|
515 |
if (dataType[band] == GDT_Float32)
|
516 |
return gdalBuf.buffFloat;
|
517 |
|
518 |
if (dataType[band] == GDT_Float64)
|
519 |
return gdalBuf.buffDouble;
|
520 |
|
521 |
if (dataType[band] == GDT_CInt16 || dataType[band] == GDT_CInt32 ||
|
522 |
dataType[band] == GDT_CFloat32 || dataType[band] == GDT_CFloat64) |
523 |
return null; |
524 |
|
525 |
return null; |
526 |
} |
527 |
|
528 |
/**
|
529 |
* Lee una bloque completo del raster y devuelve un array tridimensional del tipo correcto. Esta funci?n es util
|
530 |
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
531 |
* @param nLine N?mero de l?nea a leer
|
532 |
* @param band Banda requerida
|
533 |
* @return Object que es un array unidimendional del tipo de datos del raster
|
534 |
* @throws GdalException
|
535 |
*/
|
536 |
public Object readBlock(int pos, int blockHeight) throws GdalException, InterruptedException { |
537 |
bBandNr = super.getRasterCount();
|
538 |
int nX = getRasterXSize();
|
539 |
|
540 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
541 |
|
542 |
GdalRasterBand[] gdalBand = new GdalRasterBand[bBandNr]; |
543 |
for (int iBand = 0; iBand < gdalBand.length; iBand++) |
544 |
gdalBand[iBand] = super.getRasterBand(iBand + 1); |
545 |
|
546 |
GdalBuffer[] gdalBuf = new GdalBuffer[bBandNr]; |
547 |
|
548 |
if (dataType[0] == GDT_Byte) { |
549 |
byte[][][] buf = new byte[bBandNr][blockHeight][getRasterXSize()]; |
550 |
for (int iBand = 0; iBand < gdalBuf.length; iBand++) { |
551 |
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, nX, blockHeight, nX, blockHeight, dataType[0]); |
552 |
for (int iRow = 0; iRow < blockHeight; iRow++) { |
553 |
for (int iCol = 0; iCol < nX; iCol++) |
554 |
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffByte[iRow * nX + iCol]; |
555 |
if(task.getEvent() != null) |
556 |
task.manageEvent(task.getEvent()); |
557 |
} |
558 |
} |
559 |
return buf;
|
560 |
} else if (dataType[0] == GDT_CInt16 || dataType[0] == GDT_Int16 || dataType[0] == GDT_UInt16) { |
561 |
short[][][] buf = new short[bBandNr][blockHeight][getRasterXSize()]; |
562 |
for (int iBand = 0; iBand < gdalBuf.length; iBand++) { |
563 |
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, nX, blockHeight, nX, blockHeight, dataType[0]); |
564 |
for (int iRow = 0; iRow < blockHeight; iRow++) { |
565 |
for (int iCol = 0; iCol < nX; iCol++) |
566 |
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffShort[iRow * nX + iCol]; |
567 |
if(task.getEvent() != null) |
568 |
task.manageEvent(task.getEvent()); |
569 |
} |
570 |
} |
571 |
return buf;
|
572 |
} else if (dataType[0] == GDT_CInt32 || dataType[0] == GDT_Int32 || dataType[0] == GDT_UInt32) { |
573 |
int[][][] buf = new int[bBandNr][blockHeight][getRasterXSize()]; |
574 |
for (int iBand = 0; iBand < gdalBuf.length; iBand++) { |
575 |
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, nX, blockHeight, nX, blockHeight, dataType[0]); |
576 |
for (int iRow = 0; iRow < blockHeight; iRow++) { |
577 |
for (int iCol = 0; iCol < nX; iCol++) |
578 |
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffInt[iRow * nX + iCol]; |
579 |
if(task.getEvent() != null) |
580 |
task.manageEvent(task.getEvent()); |
581 |
} |
582 |
} |
583 |
return buf;
|
584 |
} else if(dataType[0] == GDT_Float32 || dataType[0] == GDT_CFloat32) { |
585 |
float[][][] buf = new float[bBandNr][blockHeight][getRasterXSize()]; |
586 |
for (int iBand = 0; iBand < gdalBuf.length; iBand++) { |
587 |
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, nX, blockHeight, nX, blockHeight, dataType[0]); |
588 |
for (int iRow = 0; iRow < blockHeight; iRow++) { |
589 |
for (int iCol = 0; iCol < nX; iCol++) |
590 |
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffFloat[iRow * nX + iCol]; |
591 |
if(task.getEvent() != null) |
592 |
task.manageEvent(task.getEvent()); |
593 |
} |
594 |
} |
595 |
return buf;
|
596 |
} else if(dataType[0] == GDT_Float64 || dataType[0] == GDT_CFloat64) { |
597 |
double[][][] buf = new double[bBandNr][blockHeight][getRasterXSize()]; |
598 |
for (int iBand = 0; iBand < gdalBuf.length; iBand++) { |
599 |
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, nX, blockHeight, nX, blockHeight, dataType[0]); |
600 |
for (int iRow = 0; iRow < blockHeight; iRow++) { |
601 |
for (int iCol = 0; iCol < nX; iCol++) |
602 |
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffDouble[iRow * nX + iCol]; |
603 |
if(task.getEvent() != null) |
604 |
task.manageEvent(task.getEvent()); |
605 |
} |
606 |
} |
607 |
return buf;
|
608 |
} |
609 |
|
610 |
return null; |
611 |
} |
612 |
|
613 |
/**
|
614 |
* Lectura de una l?nea de datos.
|
615 |
* @param line
|
616 |
* @throws GdalException
|
617 |
*/
|
618 |
public void readLine(Object line) throws GdalException { |
619 |
int w = (int) (Math.ceil(((double)currentViewWidth)*stepX) + 1); |
620 |
int x = (int) (currentViewX); |
621 |
int y = (int) (lastReadLine); |
622 |
GdalBuffer r = null, g = null, b = null; |
623 |
GdalBuffer a = new GdalBuffer();
|
624 |
|
625 |
while(y >= gdalBands[0].getRasterBandYSize()) |
626 |
y--; |
627 |
|
628 |
if (x+w > gdalBands[0].getRasterBandXSize()) |
629 |
w = gdalBands[0].getRasterBandXSize()-x;
|
630 |
|
631 |
if(gdalBands[0].getRasterColorTable() != null) { |
632 |
palette = new ColorTable();
|
633 |
palette.createPaletteFromGdalColorTable(gdalBands[0].getRasterColorTable());
|
634 |
r = gdalBands[0].readRaster(x, y, w, 1, w, 1, dataType[0]); |
635 |
} else {
|
636 |
a.buffByte = new byte[w]; |
637 |
r = gdalBands[0].readRaster(x, y, w, 1, w, 1, dataType[0]); |
638 |
g = b = r; |
639 |
if (getRasterCount() > 1 && gdalBands[1] != null) |
640 |
g = gdalBands[1].readRaster(x, y, w, 1, w, 1, dataType[0]); |
641 |
if (getRasterCount() > 2 && gdalBands[2] != null) |
642 |
b = gdalBands[2].readRaster(x, y, w, 1, w, 1, dataType[0]); |
643 |
} |
644 |
|
645 |
lastReadLine += stepY; |
646 |
|
647 |
double initOffset = Math.abs(currentViewX - ((int)currentViewX)); |
648 |
GdalBuffer[] bands = {r, g, b};
|
649 |
|
650 |
if (dataType[0] == GDT_Byte) |
651 |
readLine((byte[][])line, initOffset, bands); |
652 |
else if (dataType[0] == GDT_CInt16 || dataType[0] == GDT_Int16 || dataType[0] == GDT_UInt16) |
653 |
readLine((short[][])line, initOffset, bands); |
654 |
else if (dataType[0] == GDT_CInt32 || dataType[0] == GDT_Int32 || dataType[0] == GDT_UInt32) |
655 |
readLine((int[][])line, initOffset, bands); |
656 |
else if(dataType[0] == GDT_Float32 || dataType[0] == GDT_CFloat32) |
657 |
readLine((float[][])line, initOffset, bands); |
658 |
else if(dataType[0] == GDT_Float64 || dataType[0] == GDT_CFloat64) |
659 |
readLine((double[][])line, initOffset, bands); |
660 |
|
661 |
return;
|
662 |
} |
663 |
|
664 |
/**
|
665 |
* Cuando se hace una petici?n de carga de buffer la extensi?n pedida puede
|
666 |
* estar ajustada a la extensi?n del raster o no estarlo. En caso de no
|
667 |
* estarlo los pixeles del buffer que caen fuera de la extensi?n del raster
|
668 |
* tendr?n valor de NoData. Esta funci?n calcula en que pixel del buffer hay
|
669 |
* que empezar a escribir en caso de que este sea mayor que los datos a leer.
|
670 |
*
|
671 |
* @param dWorldTLX Posici?n X superior izquierda en coord reales
|
672 |
* @param dWorldTLY Posici?n Y superior izquierda en coord reales
|
673 |
* @param dWorldBRX Posici?n X inferior derecha en coord reales
|
674 |
* @param dWorldBRY Posici?n Y inferior derecha en coord reales
|
675 |
* @param nWidth Ancho en pixeles del buffer
|
676 |
* @param nHeight Alto en pixeles del buffer
|
677 |
* @return desplazamiento dentro del buffer en X e Y
|
678 |
*/
|
679 |
private int[] calcStepBuffer(Extent dataExtent, int nWidth, int nHeight, int[] stpBuffer) { |
680 |
Extent imageExtent = getExtentWithoutRot(); |
681 |
Extent ajustDataExtent = RasterUtilities.calculateAdjustedView(dataExtent, imageExtent); |
682 |
if(!RasterUtilities.compareExtents(dataExtent, ajustDataExtent)){
|
683 |
Point2D p1 = worldToRasterWithoutRot(new Point2D.Double(ajustDataExtent.minX(), ajustDataExtent.maxY())); |
684 |
Point2D p2 = worldToRasterWithoutRot(new Point2D.Double(ajustDataExtent.maxX(), ajustDataExtent.minY())); |
685 |
Point2D p3 = worldToRasterWithoutRot(new Point2D.Double(dataExtent.minX(), dataExtent.maxY())); |
686 |
// Point2D p4 = worldToRasterWithoutRot(new Point2D.Double(dataExtent.maxX(), dataExtent.minY()));
|
687 |
//Ese es el ancho y alto q tendr?a el buffer en caso de haberse ajustado
|
688 |
int w = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX())); |
689 |
int h = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY())); |
690 |
|
691 |
stpBuffer[0] = (int)(p1.getX() + (-p3.getX())); |
692 |
stpBuffer[1] = (int)(p1.getY() + (-p3.getY())); |
693 |
stpBuffer[2] = stpBuffer[0] + w; |
694 |
stpBuffer[3] = stpBuffer[1] + h; |
695 |
return new int[]{w, h}; |
696 |
} |
697 |
return new int[]{nWidth, nHeight}; |
698 |
} |
699 |
|
700 |
/**
|
701 |
* Lee una ventana de datos sin resampleo a partir de coordenadas reales.
|
702 |
* @param buf Buffer donde se almacenan los datos
|
703 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
704 |
* @param dWorldTLX Posici?n X superior izquierda en coord reales
|
705 |
* @param dWorldTLY Posici?n Y superior izquierda en coord reales
|
706 |
* @param dWorldBRX Posici?n X inferior derecha en coord reales
|
707 |
* @param dWorldBRY Posici?n Y inferior derecha en coord reales
|
708 |
* @param nWidth Ancho en pixeles del buffer
|
709 |
* @param nHeight Alto en pixeles del buffer
|
710 |
* @throws GdalException
|
711 |
*/
|
712 |
public void readWindow(IBuffer buf, BandList bandList, double ulx, double uly,double lrx, double lry, |
713 |
int nWidth, int nHeight, boolean adjustToExtent) throws GdalException, InterruptedException { |
714 |
Extent petExtent = new Extent(ulx, uly, lrx, lry);
|
715 |
setView(ulx, uly, lrx, lry, nWidth, nHeight); |
716 |
Point2D tl = worldToRaster(new Point2D.Double(ulx, uly)); |
717 |
Point2D br = worldToRaster(new Point2D.Double(lrx, lry)); |
718 |
|
719 |
if(tl.getX() > br.getX())
|
720 |
tl.setLocation(tl.getX() - 1, tl.getY());
|
721 |
else
|
722 |
br.setLocation(br.getX() - 1, br.getY());
|
723 |
|
724 |
if(tl.getY() > br.getY())
|
725 |
tl.setLocation(tl.getX(), tl.getY() - 1);
|
726 |
else
|
727 |
br.setLocation(br.getX(), br.getY() - 1);
|
728 |
|
729 |
if(gdalBands.length == 0) |
730 |
return;
|
731 |
|
732 |
selectGdalBands(buf.getBandCount()); |
733 |
|
734 |
int x = (int) Math.round(Math.min(tl.getX(), br.getX())); |
735 |
int y = (int) Math.round(Math.min(tl.getY(), br.getY())); |
736 |
|
737 |
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()}; |
738 |
//Si el buffer no se ajusta al extent entonces calculamos en que posici?n comienza a escribirse dentro del buffer
|
739 |
//ya que lo que cae fuera ser?n valores NoData
|
740 |
if(!adjustToExtent){
|
741 |
int[] wh = calcStepBuffer(petExtent, nWidth, nHeight, stpBuffer); |
742 |
if(x < 0) |
743 |
x = 0;
|
744 |
if(y < 0) |
745 |
y = 0;
|
746 |
readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer); |
747 |
return;
|
748 |
} |
749 |
|
750 |
readData(buf, bandList, x, y, nWidth, nHeight, nWidth, nHeight, 0, 0, stpBuffer); |
751 |
} |
752 |
|
753 |
/**
|
754 |
* Lee una ventana de datos con resampleo a partir de coordenadas reales. Este m?todo lee la
|
755 |
* ventana de una vez cargando los datos de un golpe en el buffer. Las coordenadas se solicitan
|
756 |
* en coordenadas del mundo real por lo que estas pueden caer en cualquier parte de un pixel.
|
757 |
* Esto se hace m?s evidente cuando supersampleamos en la petici?n, es decir el buffer de de
|
758 |
* mayor tama?o que el n?mero de pixels solicitado.
|
759 |
*
|
760 |
* Para resolver esto escribiremos con la funci?n readRaster los datos sobre un buffer mayor
|
761 |
* que el solicitado. Despu?s calcularemos el desplazamiento en pixels dentro de este buffer
|
762 |
* de mayor tama?o hasta llegar a la coordenada real donde comienza la petici?n real que ha
|
763 |
* hecho el usuario. Esto es as? porque cuando supersampleamos no queremos los pixeles del
|
764 |
* raster de disco completos sino que en los bordes del buffer quedan cortados.
|
765 |
*
|
766 |
* @param buf Buffer donde se almacenan los datos
|
767 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
768 |
* @param dWorldTLX Posici?n X superior izquierda en coord reales
|
769 |
* @param dWorldTLY Posici?n Y superior izquierda en coord reales
|
770 |
* @param dWorldBRX Posici?n X inferior derecha en coord reales
|
771 |
* @param dWorldBRY Posici?n Y inferior derecha en coord reales
|
772 |
* @param nWidth Ancho en pixeles de la petici?n
|
773 |
* @param nHeight Alto en pixeles de la petici?n
|
774 |
* @param bufWidth Ancho del buffer
|
775 |
* @param bufHeight Alto del buffer
|
776 |
* @throws GdalException
|
777 |
*/
|
778 |
public void readWindow(IBuffer buf, BandList bandList, double ulx, double uly, double lrx, double lry, |
779 |
double nWidth, double nHeight, int bufWidth, int bufHeight, boolean adjustToExtent) throws GdalException, InterruptedException { |
780 |
Extent petExtent = new Extent(ulx, uly, lrx, lry);
|
781 |
setView(ulx, uly, lrx, lry, bufWidth, bufHeight); |
782 |
Point2D tl = worldToRaster(new Point2D.Double(ulx, uly)); |
783 |
Point2D br = worldToRaster(new Point2D.Double(lrx, lry)); |
784 |
|
785 |
if(tl.getX() > br.getX())
|
786 |
tl.setLocation(tl.getX() - 1, tl.getY());
|
787 |
else
|
788 |
br.setLocation(br.getX() - 1, br.getY());
|
789 |
|
790 |
if(tl.getY() > br.getY())
|
791 |
tl.setLocation(tl.getX(), tl.getY() - 1);
|
792 |
else
|
793 |
br.setLocation(br.getX(), br.getY() - 1);
|
794 |
|
795 |
adjustPoints(tl, br); |
796 |
|
797 |
if(gdalBands.length == 0) |
798 |
return;
|
799 |
|
800 |
selectGdalBands(buf.getBandCount()); |
801 |
|
802 |
int x = (int) Math.min(tl.getX(), br.getX()); |
803 |
int y = (int) Math.min(tl.getY(), br.getY()); |
804 |
//int endX = (int) Math.max(br.getX(), tl.getX());
|
805 |
//int endY = (int) Math.max(br.getY(), tl.getY());
|
806 |
|
807 |
int stpX = 0; |
808 |
int stpY = 0; |
809 |
|
810 |
/*if(bufWidth > Math.ceil(nWidth)){
|
811 |
stpX = (int)(((tl.getX() - x) * bufWidth) / nWidth);
|
812 |
bufWidth = (int)((Math.abs(endX - x) * bufWidth) / nWidth);
|
813 |
}
|
814 |
if(bufHeight > Math.ceil(nHeight)){
|
815 |
stpY = (int)(((tl.getY() - y) * bufHeight) / nHeight);
|
816 |
bufHeight = (int)((Math.abs(endY - y) * bufHeight) / nHeight);
|
817 |
}*/
|
818 |
|
819 |
//nWidth = (int)Math.abs(endX - x) + 1;
|
820 |
//nHeight = (int)Math.abs(endY - y) + 1;
|
821 |
|
822 |
nWidth = (nWidth * currentFullWidth) / width; |
823 |
nHeight = (nHeight * currentFullHeight) / height; |
824 |
double auxX = (double) x / width; // Para evitar el overflow (no entra en un entero con ficheros grandes) |
825 |
double auxY = (double) y / height; // Para evitar el overflow (no entra en un entero con ficheros grandes) |
826 |
x = (int) (auxX * currentFullWidth); //(x * currentFullWidth) / width; |
827 |
y = (int) (auxY * currentFullHeight); //(y * currentFullHeight) / height; |
828 |
|
829 |
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()}; |
830 |
//Si el buffer no se ajusta al extent entonces calculamos en que posici?n comienza a escribirse dentro del buffer
|
831 |
//ya que lo que cae fuera ser?n valores NoData
|
832 |
if(!adjustToExtent){
|
833 |
int[] wh = calcStepBuffer(petExtent, bufWidth, bufHeight, stpBuffer); |
834 |
if(x < 0) |
835 |
x = 0;
|
836 |
if(y < 0) |
837 |
y = 0;
|
838 |
stpBuffer[0] = (int)((stpBuffer[0] * bufWidth) / nWidth); |
839 |
stpBuffer[1] = (int)((stpBuffer[1] * bufHeight) / nHeight); |
840 |
stpBuffer[2] = (int)((stpBuffer[2] * bufWidth) / nWidth); |
841 |
stpBuffer[3] = (int)((stpBuffer[3] * bufHeight) / nHeight); |
842 |
bufWidth = (int)Math.abs(stpBuffer[2] - stpBuffer[0]); |
843 |
bufHeight = (int)Math.abs(stpBuffer[3] - stpBuffer[1]); |
844 |
readData(buf, bandList, x, y, wh[0], wh[1], bufWidth, bufHeight, 0, 0, stpBuffer); |
845 |
return;
|
846 |
} |
847 |
|
848 |
if ((x + nWidth) > gdalBands[0].getRasterBandXSize()) |
849 |
nWidth = gdalBands[0].getRasterBandXSize() - x;
|
850 |
|
851 |
if ((y + nHeight) > gdalBands[0].getRasterBandYSize()) |
852 |
nHeight = gdalBands[0].getRasterBandYSize() - y;
|
853 |
|
854 |
readData(buf, bandList, x, y, (int)nWidth, (int)nHeight, bufWidth, bufHeight, stpX, stpY, stpBuffer); |
855 |
} |
856 |
|
857 |
private void adjustPoints(Point2D ul, Point2D lr) { |
858 |
double a = (ul.getX() - (int)ul.getX()); |
859 |
double b = (ul.getY() - (int)ul.getY()); |
860 |
ul.setLocation( (a > 0.95 || a < 0.05) ? Math.round(ul.getX()) : ul.getX(), |
861 |
(b > 0.95 || b < 0.05) ? Math.round(ul.getY()) : ul.getY()); |
862 |
lr.setLocation( (a > 0.95 || a < 0.05) ? Math.round(lr.getX()) : lr.getX(), |
863 |
(b > 0.95 || b < 0.05) ? Math.round(lr.getY()) : lr.getY()); |
864 |
} |
865 |
|
866 |
/**
|
867 |
* Lee una ventana de datos sin resampleo a partir de coordenadas en pixeles.
|
868 |
* @param buf Buffer donde se almacenan los datos
|
869 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
870 |
* @param x Posici?n X en pixeles
|
871 |
* @param y Posici?n Y en pixeles
|
872 |
* @param w Ancho en pixeles
|
873 |
* @param h Alto en pixeles
|
874 |
* @throws GdalException
|
875 |
*/
|
876 |
public void readWindow(IBuffer buf, BandList bandList, int x, int y, int w, int h) |
877 |
throws GdalException, InterruptedException { |
878 |
gdalBands = new GdalRasterBand[getRasterCount()];
|
879 |
isSupersampling = false;
|
880 |
if(gdalBands.length == 0) |
881 |
return;
|
882 |
|
883 |
// Selecciona las bandas
|
884 |
gdalBands[0] = getRasterBand(1); |
885 |
|
886 |
for(int iBand = 1; iBand < gdalBands.length; iBand++) |
887 |
gdalBands[iBand] = getRasterBand(iBand + 1);
|
888 |
|
889 |
assignDataTypeFromGdalRasterBands(gdalBands); |
890 |
|
891 |
int yMax = y + h;
|
892 |
readDataByLine(buf, bandList, x, y, w, yMax); |
893 |
} |
894 |
|
895 |
/**
|
896 |
* Lee una ventana de datos con resampleo a partir de coordenadas en pixeles. Este m?todo lee la
|
897 |
* ventana de una vez cargando los datos de un golpe en el buffer.
|
898 |
* @param buf Buffer donde se almacenan los datos
|
899 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
900 |
* @param x Posici?n X en pixeles
|
901 |
* @param y Posici?n Y en pixeles
|
902 |
* @param w Ancho en pixeles
|
903 |
* @param h Alto en pixeles
|
904 |
* @param bufWidth Ancho del buffer
|
905 |
* @param bufHeight Alto del buffer
|
906 |
* @throws GdalException
|
907 |
*/
|
908 |
public void readWindow(IBuffer buf, BandList bandList, int x, int y, int w, int h, int bufWidth, int bufHeight) throws GdalException, InterruptedException { |
909 |
gdalBands = new GdalRasterBand[getRasterCount()];
|
910 |
|
911 |
if(gdalBands.length == 0) |
912 |
return;
|
913 |
|
914 |
// Selecciona las bandas
|
915 |
gdalBands[0] = getRasterBand(1); |
916 |
|
917 |
for(int iBand = 1; iBand < gdalBands.length; iBand++) |
918 |
gdalBands[iBand] = getRasterBand(iBand + 1);
|
919 |
|
920 |
assignDataTypeFromGdalRasterBands(gdalBands); |
921 |
|
922 |
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()}; |
923 |
readData(buf, bandList, x, y, w, h, bufWidth, bufHeight, 0, 0, stpBuffer); |
924 |
} |
925 |
|
926 |
/**
|
927 |
* Asigna el tipo de datos de las bandas a partir de una lista de GdalRasterBands
|
928 |
* @param gdalBands
|
929 |
* @throws GdalException
|
930 |
*/
|
931 |
private void assignDataTypeFromGdalRasterBands(GdalRasterBand[] gdalBands) throws GdalException { |
932 |
int[] dt = new int[gdalBands.length]; |
933 |
for (int i = 0; i < gdalBands.length; i++) { |
934 |
if(gdalBands[i] != null) |
935 |
dt[i] = gdalBands[i].getRasterDataType(); |
936 |
} |
937 |
setDataType(dt); |
938 |
} |
939 |
|
940 |
/**
|
941 |
* Lee una ventana de datos sin resampleo a partir de coordenadas en pixeles. Esta funci?n es usuada por
|
942 |
* readWindow para coordenadas reales y readWindow en coordenadas pixel.
|
943 |
* @param buf Buffer donde se almacenan los datos
|
944 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
945 |
* @param x Posici?n X en pixeles
|
946 |
* @param y Posici?n Y en pixeles
|
947 |
* @param w Ancho en pixeles
|
948 |
* @param h Alto en pixeles
|
949 |
* @param bufWidth Ancho del buffer
|
950 |
* @param bufHeight Alto del buffer
|
951 |
* @param stepX Desplazamiento en pixeles en X a partir de la posici?n x. Este desplazamiento es util cuando hay un
|
952 |
* supersampleo ya que puede ser que de los pixeles que est?n en el borde izquierdo de la petici?n solo queramos una
|
953 |
* parte de ellos.
|
954 |
* @param stepY Desplazamiento en pixeles en Y a partir de la posici?n y. Este desplazamiento es util cuando hay un
|
955 |
* supersampleo ya que puede ser que de los pixeles que est?n en el borde superior de la petici?n solo queramos una
|
956 |
* parte de ellos.
|
957 |
* @param stepBuffer El buffer puede empezar a escribirse a partir de un pixel determinado y acabar de escribir antes
|
958 |
* de fin de buffer. Este par?metro indica el desplazamiento desde el inicio del buffer y la posici?n final.
|
959 |
* <UL>
|
960 |
* <LI>stepBuffer[0]:Desplazamiento en X desde el inicio</LI>
|
961 |
* <LI>stepBuffer[1]:Desplazamiento en Y desde el inicio</LI>
|
962 |
* <LI>stepBuffer[2]:Posici?n X final</LI>
|
963 |
* <LI>stepBuffer[3]:Posici?n Y final</LI>
|
964 |
* </UL>
|
965 |
* @throws GdalException
|
966 |
*/
|
967 |
private void readData(IBuffer buf, BandList bandList, int x, int y, int w, int h, |
968 |
int bufWidth, int bufHeight, int stpX, int stpY, int[] stepBuffer) throws GdalException, InterruptedException { |
969 |
|
970 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
971 |
|
972 |
GdalBuffer gdalBuf = null;
|
973 |
for(int iBand = 0; iBand < gdalBands.length; iBand++) { |
974 |
int[] drawableBands = bandList.getBufferBandToDraw(RasterUtilities.getFormatedRasterFileName(fileName), iBand); |
975 |
if(drawableBands == null || (drawableBands.length == 1 && drawableBands[0] == -1)) |
976 |
continue;
|
977 |
int init = (int)((bufWidth * stpY) + stpX); //Pos inicial. Desplazamos stpX pixels hacia la derecha y bajamos stpY lineas |
978 |
int pos = init;
|
979 |
gdalBuf = gdalBands[iBand].readRaster(x, y, w, h, bufWidth, bufHeight, dataType[iBand]); |
980 |
if(dataType[iBand] == Gdal.GDT_Byte){
|
981 |
for (int line = stepBuffer[1]; line < stepBuffer[3]/*buf.getHeight()*/; line++) { |
982 |
pos = (int)((bufWidth * (line - stepBuffer[0])) + init); |
983 |
for (int col = stepBuffer[0]; col < stepBuffer[2]/*buf.getWidth()*/; col ++) { |
984 |
buf.setElem(line, col, iBand, gdalBuf.buffByte[pos]); |
985 |
pos ++; |
986 |
} |
987 |
if(task.getEvent() != null) |
988 |
task.manageEvent(task.getEvent()); |
989 |
} |
990 |
}else if((dataType[iBand] == Gdal.GDT_UInt16) || (dataType[iBand] == Gdal.GDT_Int16) || (dataType[iBand] == Gdal.GDT_CInt16)){ |
991 |
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) { |
992 |
pos = (int)((bufWidth * (line - stepBuffer[0])) + init); |
993 |
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++){ |
994 |
buf.setElem(line, col, iBand, gdalBuf.buffShort[pos]); |
995 |
pos ++; |
996 |
} |
997 |
if(task.getEvent() != null) |
998 |
task.manageEvent(task.getEvent()); |
999 |
} |
1000 |
}else if((dataType[iBand] == Gdal.GDT_UInt32) || (dataType[iBand] == Gdal.GDT_Int32) || (dataType[iBand] == Gdal.GDT_CInt32)){ |
1001 |
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) { |
1002 |
pos = (int)((bufWidth * (line - stepBuffer[0])) + init); |
1003 |
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++){ |
1004 |
buf.setElem(line, col, iBand, gdalBuf.buffInt[pos]); |
1005 |
pos ++; |
1006 |
} |
1007 |
if(task.getEvent() != null) |
1008 |
task.manageEvent(task.getEvent()); |
1009 |
} |
1010 |
}else if(dataType[iBand] == Gdal.GDT_Float32){ |
1011 |
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) { |
1012 |
pos = (int)((bufWidth * (line - stepBuffer[0])) + init); |
1013 |
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++){ |
1014 |
buf.setElem(line, col, iBand, gdalBuf.buffFloat[pos]); |
1015 |
pos ++; |
1016 |
} |
1017 |
if(task.getEvent() != null) |
1018 |
task.manageEvent(task.getEvent()); |
1019 |
} |
1020 |
}else if(dataType[iBand] == Gdal.GDT_Float64){ |
1021 |
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) { |
1022 |
pos = (int)((bufWidth * (line - stepBuffer[0])) + init); |
1023 |
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++){ |
1024 |
buf.setElem(line, col, iBand, gdalBuf.buffDouble[pos]); |
1025 |
pos ++; |
1026 |
} |
1027 |
if(task.getEvent() != null) |
1028 |
task.manageEvent(task.getEvent()); |
1029 |
} |
1030 |
} |
1031 |
} |
1032 |
} |
1033 |
|
1034 |
/**
|
1035 |
* Lee una ventana de datos sin resampleo a partir de coordenadas en pixeles. Esta funci?n es usuada por
|
1036 |
* readWindow para coordenadas reales y readWindow en coordenadas pixel.
|
1037 |
* @param buf Buffer donde se almacenan los datos
|
1038 |
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
1039 |
* @param x Posici?n X en pixeles
|
1040 |
* @param y Posici?n Y en pixeles
|
1041 |
* @param w Ancho en pixeles
|
1042 |
* @param yMax altura m?xima de y
|
1043 |
* @throws GdalException
|
1044 |
*/
|
1045 |
private void readDataByLine(IBuffer buf, BandList bandList, int x, int y, int w, int yMax) throws GdalException, InterruptedException { |
1046 |
GdalBuffer gdalBuf = null;
|
1047 |
int rasterBufLine;
|
1048 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
1049 |
|
1050 |
for(int iBand = 0; iBand < gdalBands.length; iBand++) { |
1051 |
int[] drawableBands = bandList.getBufferBandToDraw(RasterUtilities.getFormatedRasterFileName(fileName), iBand); |
1052 |
if(drawableBands == null || (drawableBands.length == 1 && drawableBands[0] == -1)) |
1053 |
continue;
|
1054 |
if(dataType[iBand] == Gdal.GDT_Byte) {
|
1055 |
for (int line = y; line < yMax; line++) { |
1056 |
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]); |
1057 |
rasterBufLine = line - y; |
1058 |
buf.setLineInBandByte(gdalBuf.buffByte, rasterBufLine, iBand); |
1059 |
if(task.getEvent() != null) |
1060 |
task.manageEvent(task.getEvent()); |
1061 |
} |
1062 |
}else if((dataType[iBand] == Gdal.GDT_UInt16) || (dataType[iBand] == Gdal.GDT_Int16) || (dataType[iBand] == Gdal.GDT_CInt16)) { |
1063 |
for (int line = y; line < yMax; line++) { |
1064 |
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]); |
1065 |
rasterBufLine = line - y; |
1066 |
buf.setLineInBandShort(gdalBuf.buffShort, rasterBufLine, iBand); |
1067 |
if(task.getEvent() != null) |
1068 |
task.manageEvent(task.getEvent()); |
1069 |
} |
1070 |
}else if((dataType[iBand] == Gdal.GDT_UInt32) || (dataType[iBand] == Gdal.GDT_Int32) || (dataType[iBand] == Gdal.GDT_CInt32)) { |
1071 |
for (int line = y; line < yMax; line++) { |
1072 |
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]); |
1073 |
rasterBufLine = line - y; |
1074 |
buf.setLineInBandInt(gdalBuf.buffInt, rasterBufLine, iBand); |
1075 |
if(task.getEvent() != null) |
1076 |
task.manageEvent(task.getEvent()); |
1077 |
} |
1078 |
}else if(dataType[iBand] == Gdal.GDT_Float32){ |
1079 |
for (int line = y; line < yMax; line++) { |
1080 |
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]); |
1081 |
rasterBufLine = line - y; |
1082 |
buf.setLineInBandFloat(gdalBuf.buffFloat, rasterBufLine, iBand); |
1083 |
if(task.getEvent() != null) |
1084 |
task.manageEvent(task.getEvent()); |
1085 |
} |
1086 |
}else if(dataType[iBand] == Gdal.GDT_Float64){ |
1087 |
for (int line = y; line < yMax; line++) { |
1088 |
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]); |
1089 |
rasterBufLine = line - y; |
1090 |
buf.setLineInBandDouble(gdalBuf.buffDouble, rasterBufLine, iBand); |
1091 |
if(task.getEvent() != null) |
1092 |
task.manageEvent(task.getEvent()); |
1093 |
} |
1094 |
} |
1095 |
} |
1096 |
} |
1097 |
|
1098 |
/**
|
1099 |
* Obtiene el valor de un pixel determinado por las coordenadas x e y que se pasan
|
1100 |
* por par?metro
|
1101 |
* @param x Coordenada X del pixel
|
1102 |
* @param y Coordenada Y del pixel
|
1103 |
* @return Array de Object donde cada posici?n representa una banda y el valor ser? Integer
|
1104 |
* en caso de ser byte, shot o int, Float en caso de ser float y Double en caso de ser double.
|
1105 |
*/
|
1106 |
public Object[] getData(int x, int y) { |
1107 |
try {
|
1108 |
Object[] data = new Object[getRasterCount()]; |
1109 |
for(int i = 0; i < getRasterCount(); i++){ |
1110 |
GdalRasterBand rb = getRasterBand(i + 1);
|
1111 |
GdalBuffer r = rb.readRaster(x, y, 1, 1, 1, 1, dataType[i]); |
1112 |
switch(dataType[i]){
|
1113 |
case 0: break; //Sin tipo |
1114 |
case 1: data[i] = new Integer(r.buffByte[0]); //Buffer byte (8) |
1115 |
break;
|
1116 |
case 2: //Buffer short (16) |
1117 |
case 3: data[i] = new Integer(r.buffShort[0]); //Buffer short (16) |
1118 |
break;
|
1119 |
case 4: //Buffer int (32) |
1120 |
case 5: data[i] = new Integer(r.buffInt[0]); //Buffer int (32) |
1121 |
break;
|
1122 |
case 6: data[i] = new Float(r.buffFloat[0]); //Buffer float (32) |
1123 |
break;
|
1124 |
case 7: data[i] = new Double(r.buffDouble[0]); //Buffer double (64) |
1125 |
break;
|
1126 |
} |
1127 |
} |
1128 |
return data;
|
1129 |
} catch (GdalException e) {
|
1130 |
return null; |
1131 |
} |
1132 |
} |
1133 |
|
1134 |
public int getBlockSize(){ |
1135 |
return this.getBlockSize(); |
1136 |
} |
1137 |
|
1138 |
/**
|
1139 |
* Devuelve la transformaci?n del fichero de georreferenciaci?n
|
1140 |
* @return AffineTransform
|
1141 |
*/
|
1142 |
public AffineTransform getOwnTransformation() { |
1143 |
return ownTransformation;
|
1144 |
} |
1145 |
|
1146 |
/**
|
1147 |
* Calcula el extent en coordenadas del mundo real sin rotaci?n. Solo coordenadas y tama?o de pixel
|
1148 |
* @return Extent
|
1149 |
*/
|
1150 |
public Extent getExtentWithoutRot() {
|
1151 |
AffineTransform at = new AffineTransform( externalTransformation.getScaleX(), 0, |
1152 |
0, externalTransformation.getScaleY(),
|
1153 |
externalTransformation.getTranslateX(), externalTransformation.getTranslateY()); |
1154 |
Point2D p1 = new Point2D.Double(0, 0); |
1155 |
Point2D p2 = new Point2D.Double(width, height); |
1156 |
at.transform(p1, p1); |
1157 |
at.transform(p2, p2); |
1158 |
return new Extent(p1, p2); |
1159 |
} |
1160 |
|
1161 |
/**
|
1162 |
* Asigna una transformaci?n que es aplicada sobre la que ya tiene el propio fichero
|
1163 |
* @param t
|
1164 |
*/
|
1165 |
public void setExternalTransform(AffineTransform t){ |
1166 |
externalTransformation = t; |
1167 |
} |
1168 |
|
1169 |
/**
|
1170 |
* Obtiene el nombre del driver de Gdal
|
1171 |
* @return Cadena que representa el nombre del driver de gdal
|
1172 |
*/
|
1173 |
public String getGdalShortName() { |
1174 |
return shortName;
|
1175 |
} |
1176 |
|
1177 |
} |
1178 |
|
1179 |
|
1180 |
|
1181 |
|