1 |
|
/* gvSIG. Geographic Information System of the Valencian Government
|
2 |
|
*
|
3 |
|
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
|
* of the Valencian Government (CIT)
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
|
* MA 02110-1301, USA.
|
20 |
|
*
|
21 |
|
*/
|
22 |
|
package org.gvsig.raster.gdal.io;
|
23 |
|
|
24 |
|
import java.awt.Color;
|
25 |
|
import java.awt.Rectangle;
|
26 |
|
import java.awt.geom.AffineTransform;
|
27 |
|
import java.awt.geom.NoninvertibleTransformException;
|
28 |
|
import java.awt.geom.Point2D;
|
29 |
|
import java.io.IOException;
|
30 |
|
import java.util.ArrayList;
|
31 |
|
import java.util.List;
|
32 |
|
|
33 |
|
import org.gdal.gdal.Dataset;
|
34 |
|
import org.gdal.gdal.gdal;
|
35 |
|
import org.gdal.ogr.ogr;
|
36 |
|
|
37 |
|
import org.gvsig.fmap.dal.coverage.RasterLibrary;
|
38 |
|
import org.gvsig.fmap.dal.coverage.RasterLocator;
|
39 |
|
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
|
40 |
|
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
|
41 |
|
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
|
42 |
|
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
|
43 |
|
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
|
44 |
|
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
|
45 |
|
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
|
46 |
|
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
|
47 |
|
import org.gvsig.fmap.dal.coverage.util.FileUtils;
|
48 |
|
import org.gvsig.jgdal.Gdal;
|
49 |
|
import org.gvsig.jgdal.GdalBuffer;
|
50 |
|
import org.gvsig.jgdal.GdalColorEntry;
|
51 |
|
import org.gvsig.jgdal.GdalColorTable;
|
52 |
|
import org.gvsig.jgdal.GdalDataset;
|
53 |
|
import org.gvsig.jgdal.GdalException;
|
54 |
|
import org.gvsig.jgdal.GdalRasterBand;
|
55 |
|
import org.gvsig.jgdal.GeoTransform;
|
56 |
|
import org.gvsig.raster.impl.datastruct.ColorItemImpl;
|
57 |
|
import org.gvsig.raster.impl.datastruct.DefaultNoData;
|
58 |
|
import org.gvsig.raster.impl.datastruct.ExtentImpl;
|
59 |
|
import org.gvsig.raster.impl.process.RasterTask;
|
60 |
|
import org.gvsig.raster.impl.process.RasterTaskQueue;
|
61 |
|
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
|
62 |
|
import org.gvsig.raster.impl.store.properties.DataStoreColorTable;
|
63 |
|
import org.gvsig.raster.impl.store.properties.DataStoreMetadata;
|
64 |
|
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
|
65 |
|
import org.gvsig.tools.dispose.Disposable;
|
66 |
|
import org.gvsig.tools.task.TaskStatus;
|
67 |
|
/**
|
68 |
|
* Soporte 'nativo' para ficheros desde GDAL.
|
69 |
|
*
|
70 |
|
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
71 |
|
* @author Nacho Brodin (nachobrodin@gmail.com)
|
72 |
|
*/
|
73 |
|
public class GdalNative extends Gdal implements Disposable {
|
74 |
|
// private boolean isInitialized = false;
|
75 |
|
private String fileName = null;
|
76 |
|
private String shortName = "";
|
77 |
|
public GeoTransform trans = null;
|
78 |
|
public int width = 0, height = 0;
|
79 |
|
public double originX = 0D, originY = 0D;
|
80 |
|
public String version = "";
|
81 |
|
protected int rBandNr = 1, gBandNr = 2, bBandNr = 3, aBandNr = 4;
|
82 |
|
private int[] dataType = null;
|
83 |
|
DataStoreMetadata metadata = null;
|
84 |
|
protected boolean georeferenced = true;
|
85 |
|
|
86 |
|
/**
|
87 |
|
* Vectores que contiene los desplazamientos de un pixel cuando hay supersampling.
|
88 |
|
* , es decir el n?mero de pixels de pantalla que tiene un pixel de imagen. Como todos
|
89 |
|
* los pixeles no tienen el mismo ancho y alto ha de meterse en un array y no puede ser
|
90 |
|
* una variable. Adem?s hay que tener en cuenta que el primer y ?ltimo pixel son de
|
91 |
|
* distinto tama?o que el resto.
|
92 |
|
*/
|
93 |
|
public int[] stepArrayX = null;
|
94 |
|
public int[] stepArrayY = null;
|
95 |
|
protected GdalRasterBand[] gdalBands = null;
|
96 |
|
private double lastReadLine = -1;
|
97 |
|
private int overviewWidth = -1;
|
98 |
|
private int overviewHeight = -1;
|
99 |
|
private int currentViewWidth = -1;
|
100 |
|
private int currentViewHeight = -1;
|
101 |
|
private double currentViewX = 0D;
|
102 |
|
private double viewportScaleX = 0D;
|
103 |
|
private double viewportScaleY = 0D;
|
104 |
|
private double stepX = 0D;
|
105 |
|
private double stepY = 0D;
|
106 |
|
public boolean isSupersampling = false;
|
107 |
|
private boolean open = false;
|
108 |
|
/**
|
109 |
|
* Estado de transparencia del raster.
|
110 |
|
*/
|
111 |
|
protected DataStoreTransparency fileTransparency = null;
|
112 |
|
protected DataStoreColorTable palette = null;
|
113 |
|
protected DataStoreColorInterpretation colorInterpr = null;
|
114 |
|
protected AffineTransform ownTransformation = null;
|
115 |
|
protected AffineTransform externalTransformation = new AffineTransform();
|
116 |
|
|
117 |
|
public static int getGdalTypeFromRasterBufType(int rasterBufType) {
|
118 |
|
switch (rasterBufType) {
|
119 |
|
case Buffer.TYPE_BYTE: return Gdal.GDT_Byte;
|
120 |
|
case Buffer.TYPE_USHORT: return Gdal.GDT_UInt16;
|
121 |
|
case Buffer.TYPE_SHORT: return Gdal.GDT_Int16;
|
122 |
|
case Buffer.TYPE_INT: return Gdal.GDT_Int32;
|
123 |
|
case Buffer.TYPE_FLOAT: return Gdal.GDT_Float32;
|
124 |
|
case Buffer.TYPE_DOUBLE: return Gdal.GDT_Float64;
|
125 |
|
case Buffer.TYPE_UNDEFINED: return Gdal.GDT_Unknown;
|
126 |
|
case Buffer.TYPE_IMAGE: return Gdal.GDT_Byte;
|
127 |
|
}
|
128 |
|
return Gdal.GDT_Unknown;
|
129 |
|
}
|
130 |
|
|
131 |
|
/**
|
132 |
|
* Conversi?n de los tipos de datos de gdal a los tipos de datos de RasterBuf
|
133 |
|
* @param gdalType Tipo de dato de gdal
|
134 |
|
* @return Tipo de dato de RasterBuf
|
135 |
|
*/
|
136 |
|
public static int getRasterBufTypeFromGdalType(int gdalType) {
|
137 |
|
switch (gdalType) {
|
138 |
|
case 1:// Eight bit unsigned integer GDT_Byte = 1
|
139 |
|
return Buffer.TYPE_BYTE;
|
140 |
|
|
141 |
|
case 3:// Sixteen bit signed integer GDT_Int16 = 3,
|
142 |
|
return Buffer.TYPE_SHORT;
|
143 |
|
|
144 |
|
case 2:// Sixteen bit unsigned integer GDT_UInt16 = 2
|
145 |
|
//return RasterBuffer.TYPE_USHORT;
|
146 |
|
return Buffer.TYPE_SHORT; //Apa?o para usar los tipos de datos que soportamos
|
147 |
|
|
148 |
|
case 5:// Thirty two bit signed integer GDT_Int32 = 5
|
149 |
|
return Buffer.TYPE_INT;
|
150 |
|
|
151 |
|
case 6:// Thirty two bit floating point GDT_Float32 = 6
|
152 |
|
return Buffer.TYPE_FLOAT;
|
153 |
|
|
154 |
|
case 7:// Sixty four bit floating point GDT_Float64 = 7
|
155 |
|
return Buffer.TYPE_DOUBLE;
|
156 |
|
|
157 |
|
// TODO:Estos tipos de datos no podemos gestionarlos. Habria que definir
|
158 |
|
// el tipo complejo y usar el tipo long que de momento no se gasta.
|
159 |
|
case 4:// Thirty two bit unsigned integer GDT_UInt32 = 4,
|
160 |
|
return Buffer.TYPE_INT;
|
161 |
|
//return RasterBuffer.TYPE_UNDEFINED; // Deberia devolver un Long
|
162 |
|
|
163 |
|
case 8:// Complex Int16 GDT_CInt16 = 8
|
164 |
|
case 9:// Complex Int32 GDT_CInt32 = 9
|
165 |
|
case 10:// Complex Float32 GDT_CFloat32 = 10
|
166 |
|
case 11:// Complex Float64 GDT_CFloat64 = 11
|
167 |
|
return Buffer.TYPE_UNDEFINED;
|
168 |
|
}
|
169 |
|
return Buffer.TYPE_UNDEFINED;
|
170 |
|
}
|
171 |
|
|
172 |
|
/**
|
173 |
|
* Overview usada en el ?ltimo setView
|
174 |
|
*/
|
175 |
|
int currentOverview = -1;
|
176 |
|
|
177 |
|
public GdalNative(String fName) throws GdalException, IOException {
|
178 |
|
super();
|
179 |
|
init(fName);
|
180 |
|
}
|
181 |
|
|
182 |
|
private static long initializeGdal(String fName) throws GdalException {
|
183 |
|
gdal.AllRegister();
|
184 |
|
ogr.RegisterAll();
|
185 |
|
Dataset data = gdal.Open(fName, 1);
|
186 |
|
if (data == null)
|
187 |
|
throw new GdalException("Error en la apertura del fichero. El fichero no tiene un formato v?lido.");
|
188 |
|
|
189 |
|
return GdalDataset.getCPtr(data);
|
190 |
|
}
|
191 |
|
|
192 |
|
// public GdalNative(long cPtr, String fName) throws GdalException, IOException {
|
193 |
|
// super(cPtr);
|
194 |
|
// isInitialized = true;
|
195 |
|
// init(fName);
|
196 |
|
// }
|
197 |
|
|
198 |
|
private void init(String fName) throws GdalException, IOException {
|
199 |
|
gdal.AllRegister();
|
200 |
|
ogr.RegisterAll();
|
201 |
|
fileName = fName;
|
202 |
|
open(fName, GA_ReadOnly);
|
203 |
|
open = true;
|
204 |
|
if (getDataset() == null)
|
205 |
|
throw new GdalException("Error en la apertura del fichero. El fichero no tiene un formato v?lido.");
|
206 |
|
// if(isInitialized){
|
207 |
|
// ext = RasterUtilities.getExtensionFromFileName(fName);
|
208 |
|
width = getRasterXSize();
|
209 |
|
height = getRasterYSize();
|
210 |
|
|
211 |
|
int[] dt = new int[getRasterCount()];
|
212 |
|
for (int i = 0; i < getRasterCount(); i++)
|
213 |
|
dt[i] = this.getRasterBand(i + 1).getRasterDataType();
|
214 |
|
setDataType(dt);
|
215 |
|
shortName = getDriverShortName();
|
216 |
|
colorInterpr = new DataStoreColorInterpretation(getRasterCount());
|
217 |
|
fileTransparency = new DataStoreTransparency(colorInterpr);
|
218 |
|
metadata = new DataStoreMetadata(getMetadata(), colorInterpr);
|
219 |
|
|
220 |
|
// Asignamos la interpretaci?n de color leida por gdal a cada banda. Esto
|
221 |
|
// nos sirve para saber que banda de la imagen va asignada a cada banda de
|
222 |
|
// visualizaci?n (ARGB)
|
223 |
|
metadata.initNoDataByBand(getRasterCount());
|
224 |
|
for (int i = 0; i < getRasterCount(); i++) {
|
225 |
|
GdalRasterBand rb = getRasterBand(i + 1);
|
226 |
|
String colorInt = getColorInterpretationName(rb.getRasterColorInterpretation());
|
227 |
|
metadata.setNoDataEnabled(rb.existsNoDataValue());
|
228 |
|
if(rb.existsNoDataValue()) {
|
229 |
|
metadata.setNoDataValue(i, rb.getRasterNoDataValue());
|
230 |
|
metadata.setNoDataEnabled(rb.existsNoDataValue());
|
231 |
|
}
|
232 |
|
colorInterpr.setColorInterpValue(i, colorInt);
|
233 |
|
if (colorInt.equals("Alpha"))
|
234 |
|
fileTransparency.setTransparencyBand(i);
|
235 |
|
|
236 |
|
if (rb.getRasterColorTable() != null && palette == null) {
|
237 |
|
palette = new DataStoreColorTable(gdalColorTable2ColorItems(rb.getRasterColorTable()), false);
|
238 |
|
// fileTransparency.setTransparencyRangeList(palette.getTransparencyRange());
|
239 |
|
}
|
240 |
|
}
|
241 |
|
fileTransparency.setTransparencyByPixelFromMetadata(metadata);
|
242 |
|
|
243 |
|
try {
|
244 |
|
trans = getGeoTransform();
|
245 |
|
|
246 |
|
boolean isCorrect = false;
|
247 |
|
for (int i = 0; i < trans.adfgeotransform.length; i++)
|
248 |
|
if (trans.adfgeotransform[i] != 0)
|
249 |
|
isCorrect = true;
|
250 |
|
if (!isCorrect)
|
251 |
|
throw new GdalException("");
|
252 |
|
|
253 |
|
double psX = trans.adfgeotransform[1];
|
254 |
|
double psY = trans.adfgeotransform[5];
|
255 |
|
double rotX = trans.adfgeotransform[4];
|
256 |
|
double rotY = trans.adfgeotransform[2];
|
257 |
|
double offX = trans.adfgeotransform[0];
|
258 |
|
double offY = trans.adfgeotransform[3];
|
259 |
|
|
260 |
|
ownTransformation = new AffineTransform(psX, rotX, rotY, psY, offX, offY);
|
261 |
|
//trans.adfgeotransform[1], trans.adfgeotransform[4], trans.adfgeotransform[2], trans.adfgeotransform[5], trans.adfgeotransform[0], trans.adfgeotransform[3]);
|
262 |
|
externalTransformation = (AffineTransform) ownTransformation.clone();
|
263 |
|
overviewWidth = width;
|
264 |
|
overviewHeight = height;
|
265 |
|
|
266 |
|
this.georeferenced = true;
|
267 |
|
} catch (GdalException exc) {
|
268 |
|
// Transformaci�n para ficheros sin georreferenciaci�n. Se invierte la Y
|
269 |
|
// ya que las WC decrecen de
|
270 |
|
// arriba a abajo y los pixeles crecen de arriba a abajo
|
271 |
|
ownTransformation = new AffineTransform(1, 0, 0, -1, 0, height);
|
272 |
|
externalTransformation = (AffineTransform) ownTransformation.clone();
|
273 |
|
overviewWidth = width;
|
274 |
|
overviewHeight = height;
|
275 |
|
this.georeferenced = false;
|
276 |
|
}
|
277 |
|
// }
|
278 |
|
}
|
279 |
|
|
280 |
|
/**
|
281 |
|
* Returns true if this provider is open and false if don't
|
282 |
|
* @return
|
283 |
|
*/
|
284 |
|
public boolean isOpen() {
|
285 |
|
return open;
|
286 |
|
}
|
287 |
|
|
288 |
|
/**
|
289 |
|
* Obtiene el flag que informa de si el raster tiene valor no data o no.
|
290 |
|
* Consultar� todas las bandas del mismo y si alguna tiene valor no data
|
291 |
|
* devuelve true sino devolver� false.
|
292 |
|
* @return true si tiene valor no data y false si no lo tiene
|
293 |
|
* @throws GdalException
|
294 |
|
*/
|
295 |
|
public boolean existsNoDataValue() throws GdalException {
|
296 |
|
for (int i = 0; i < getRasterCount(); i++) {
|
297 |
|
GdalRasterBand rb = getRasterBand(i + 1);
|
298 |
|
if (rb.existsNoDataValue())
|
299 |
|
return true;
|
300 |
|
}
|
301 |
|
return false;
|
302 |
|
}
|
303 |
|
|
304 |
|
/**
|
305 |
|
* Obtiene el flag que informa de si el raster tiene valor no data o no
|
306 |
|
* en una banda concreta.
|
307 |
|
* @return true si tiene valor no data en esa banda y false si no lo tiene
|
308 |
|
* @param band Posici�n de la banda a consultar (0..n)
|
309 |
|
* @throws GdalException
|
310 |
|
*/
|
311 |
|
public boolean existsNoDataValue(int band) throws GdalException {
|
312 |
|
GdalRasterBand rb = getRasterBand(band + 1);
|
313 |
|
return rb.existsNoDataValue();
|
314 |
|
}
|
315 |
|
|
316 |
|
/**
|
317 |
|
* Gets nodata value
|
318 |
|
* @return
|
319 |
|
*/
|
320 |
|
public NoData getNoDataValue() {
|
321 |
|
Number value = null;
|
322 |
|
int type = getRasterBufTypeFromGdalType(getDataType()[0]);
|
323 |
|
if (metadata != null && metadata.isNoDataEnabled() && metadata.getNoDataValue().length > 0) {
|
324 |
|
switch (type) {
|
325 |
|
case Buffer.TYPE_BYTE:
|
326 |
|
if (metadata == null || metadata.getNoDataValue().length == 0)
|
327 |
|
value = new Byte(RasterLibrary.defaultByteNoDataValue);
|
328 |
|
else
|
329 |
|
value = new Byte((byte)metadata.getNoDataValue()[0]);
|
330 |
|
break;
|
331 |
|
case Buffer.TYPE_SHORT:
|
332 |
|
if (metadata == null || metadata.getNoDataValue().length == 0)
|
333 |
|
value = new Short(RasterLibrary.defaultShortNoDataValue);
|
334 |
|
else
|
335 |
|
value = new Short((short)metadata.getNoDataValue()[0]);
|
336 |
|
break;
|
337 |
|
case Buffer.TYPE_INT:
|
338 |
|
if (metadata == null || metadata.getNoDataValue().length == 0)
|
339 |
|
value = new Integer((int)RasterLibrary.defaultIntegerNoDataValue);
|
340 |
|
else
|
341 |
|
value = new Integer((int)metadata.getNoDataValue()[0]);
|
342 |
|
break;
|
343 |
|
case Buffer.TYPE_FLOAT:
|
344 |
|
if (metadata == null || metadata.getNoDataValue().length == 0)
|
345 |
|
value = new Float(RasterLibrary.defaultFloatNoDataValue);
|
346 |
|
else
|
347 |
|
value = new Float(metadata.getNoDataValue()[0]);
|
348 |
|
break;
|
349 |
|
case Buffer.TYPE_DOUBLE:
|
350 |
|
if (metadata == null || metadata.getNoDataValue().length == 0)
|
351 |
|
value = new Double(RasterLibrary.defaultFloatNoDataValue);
|
352 |
|
else
|
353 |
|
value = new Double(metadata.getNoDataValue()[0]);
|
354 |
|
break;
|
355 |
|
}
|
356 |
|
}
|
357 |
|
|
358 |
|
NoData nodata = new DefaultNoData(value, value, fileName);
|
359 |
|
nodata.setNoDataTransparent(false);
|
360 |
|
return nodata;
|
361 |
|
}
|
362 |
|
|
363 |
|
/**
|
364 |
|
* Asigna el tipo de dato
|
365 |
|
* @param dt entero que representa el tipo de dato
|
366 |
|
*/
|
367 |
|
public void setDataType(int[] dt) {
|
368 |
|
dataType = dt;
|
369 |
|
}
|
370 |
|
|
371 |
|
/**
|
372 |
|
* Obtiene el tipo de dato
|
373 |
|
* @return entero que representa el tipo de dato
|
374 |
|
*/
|
375 |
|
public int[] getDataType() {
|
376 |
|
return dataType;
|
377 |
|
}
|
378 |
|
|
379 |
|
/**
|
380 |
|
* Gets the color interpretation
|
381 |
|
* @return
|
382 |
|
*/
|
383 |
|
public ColorInterpretation getColorInterpretation() {
|
384 |
|
return colorInterpr;
|
385 |
|
}
|
386 |
|
|
387 |
|
/**
|
388 |
|
* Gets the color table
|
389 |
|
* @return
|
390 |
|
*/
|
391 |
|
public ColorTable getColorTable() {
|
392 |
|
return palette;
|
393 |
|
}
|
394 |
|
|
395 |
|
/**
|
396 |
|
* Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
|
397 |
|
* del punto real.
|
398 |
|
* Supone rasters no girados
|
399 |
|
* @param pt punto en coordenadas del punto real
|
400 |
|
* @return punto en coordenadas del raster
|
401 |
|
*/
|
402 |
|
public Point2D worldToRasterWithoutRot(Point2D pt) {
|
403 |
|
Point2D p = new Point2D.Double();
|
404 |
|
AffineTransform at = new AffineTransform( externalTransformation.getScaleX(), 0,
|
405 |
|
0, externalTransformation.getScaleY(),
|
406 |
|
externalTransformation.getTranslateX(), externalTransformation.getTranslateY());
|
407 |
|
try {
|
408 |
|
at.inverseTransform(pt, p);
|
409 |
|
} catch (NoninvertibleTransformException e) {
|
410 |
|
return pt;
|
411 |
|
}
|
412 |
|
return p;
|
413 |
|
}
|
414 |
|
|
415 |
|
/**
|
416 |
|
* Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
|
417 |
|
* del punto real.
|
418 |
|
* Supone rasters no girados
|
419 |
|
* @param pt punto en coordenadas del punto real
|
420 |
|
* @return punto en coordenadas del raster
|
421 |
|
*/
|
422 |
|
public Point2D worldToRaster(Point2D pt) {
|
423 |
|
Point2D p = new Point2D.Double();
|
424 |
|
try {
|
425 |
|
externalTransformation.inverseTransform(pt, p);
|
426 |
|
} catch (NoninvertibleTransformException e) {
|
427 |
|
return pt;
|
428 |
|
}
|
429 |
|
return p;
|
430 |
|
}
|
431 |
|
|
432 |
|
/**
|
433 |
|
* Obtiene un punto del raster en coordenadas pixel a partir de un punto en coordenadas
|
434 |
|
* reales.
|
435 |
|
* @param pt Punto en coordenadas reales
|
436 |
|
* @return Punto en coordenadas pixel.
|
437 |
|
*/
|
438 |
|
public Point2D rasterToWorld(Point2D pt) {
|
439 |
|
Point2D p = new Point2D.Double();
|
440 |
|
externalTransformation.transform(pt, p);
|
441 |
|
return p;
|
442 |
|
}
|
443 |
|
|
444 |
|
/**
|
445 |
|
* Calcula el overview a usar. Hay que tener en cuenta que tenemos que tener calculadas las variables
|
446 |
|
* viewPortScale, currentFullWidth y currentFulHeight
|
447 |
|
* @param coordenada pixel expresada en double que indica la posici�n superior izquierda
|
448 |
|
* @throws GdalException
|
449 |
|
*/
|
450 |
|
private void calcOverview(Point2D tl, Point2D br) throws GdalException {
|
451 |
|
gdalBands[0] = getRasterBand(1);
|
452 |
|
currentOverview = -1;
|
453 |
|
if (gdalBands[0].getOverviewCount() > 0) {
|
454 |
|
GdalRasterBand ovb = null;
|
455 |
|
for (int i = gdalBands[0].getOverviewCount() - 1; i > 0; i--) {
|
456 |
|
ovb = gdalBands[0].getOverview(i);
|
457 |
|
if (ovb.getRasterBandXSize() > getRasterXSize() * viewportScaleX) {
|
458 |
|
currentOverview = i;
|
459 |
|
viewportScaleX *= ((double) width / (double) ovb.getRasterBandXSize());
|
460 |
|
viewportScaleY *= ((double) height / (double) ovb.getRasterBandYSize());
|
461 |
|
stepX = 1D / viewportScaleX;
|
462 |
|
stepY = 1D / viewportScaleY;
|
463 |
|
overviewWidth = ovb.getRasterBandXSize();
|
464 |
|
overviewHeight = ovb.getRasterBandYSize();
|
465 |
|
currentViewX = Math.min(tl.getX(), br.getX());
|
466 |
|
lastReadLine = Math.min(tl.getY(), br.getY());
|
467 |
|
break;
|
468 |
|
}
|
469 |
|
}
|
470 |
|
}
|
471 |
|
}
|
472 |
|
|
473 |
|
public void setView(double dWorldTLX, double dWorldTLY,
|
474 |
|
double dWorldBRX, double dWorldBRY,
|
475 |
|
int nWidth, int nHeight) throws GdalException {
|
476 |
|
overviewWidth = width;
|
477 |
|
overviewHeight = height;
|
478 |
|
Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
|
479 |
|
Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
|
480 |
|
// Calcula cual es la primera l�nea a leer;
|
481 |
|
currentViewWidth = nWidth;
|
482 |
|
currentViewHeight = nHeight;
|
483 |
|
// wcWidth = Math.abs(br.getX() - tl.getX());
|
484 |
|
|
485 |
|
currentViewX = Math.min(tl.getX(), br.getX());
|
486 |
|
|
487 |
|
viewportScaleX = (double) currentViewWidth / (br.getX() - tl.getX());
|
488 |
|
viewportScaleY = (double) currentViewHeight / (br.getY() - tl.getY());
|
489 |
|
stepX = 1D / viewportScaleX;
|
490 |
|
stepY = 1D / viewportScaleY;
|
491 |
|
|
492 |
|
lastReadLine = Math.min(tl.getY(), br.getY());
|
493 |
|
|
494 |
|
//Para lectura del renderizado (ARGB). readWindow selecciona las bandas que necesita.
|
495 |
|
|
496 |
|
// calcula el overview a usar
|
497 |
|
gdalBands = new GdalRasterBand[4];
|
498 |
|
calcOverview(tl, br);
|
499 |
|
}
|
500 |
|
|
501 |
|
/**
|
502 |
|
* Selecciona bandas y overview en el objeto GdalRasterBand[] para el n�mero de bandas solicitado.
|
503 |
|
* @param nbands N�mero de bandas solicitado.
|
504 |
|
* @throws GdalException
|
505 |
|
*/
|
506 |
|
public void selectGdalBands(int nbands) throws GdalException {
|
507 |
|
gdalBands = new GdalRasterBand[nbands];
|
508 |
|
// Selecciona las bandas y los overviews necesarios
|
509 |
|
gdalBands[0] = getRasterBand(1);
|
510 |
|
for (int i = 0; i < nbands; i++)
|
511 |
|
gdalBands[i] = gdalBands[0];
|
512 |
|
|
513 |
|
assignDataTypeFromGdalRasterBands(gdalBands);
|
514 |
|
// setDataType(gdalBands[0].getRasterDataType());
|
515 |
|
|
516 |
|
for (int i = 2; i <= nbands; i++) {
|
517 |
|
if (getRasterCount() >= i) {
|
518 |
|
gdalBands[i - 1] = getRasterBand(i);
|
519 |
|
for (int j = i; j < nbands; j++)
|
520 |
|
gdalBands[j] = gdalBands[i - 1];
|
521 |
|
}
|
522 |
|
}
|
523 |
|
|
524 |
|
if (currentOverview > 0) {
|
525 |
|
gdalBands[0] = gdalBands[0].getOverview(currentOverview);
|
526 |
|
for (int i = 2; i <= nbands; i++) {
|
527 |
|
if (getRasterCount() >= i)
|
528 |
|
gdalBands[i - 1] = gdalBands[i - 1].getOverview(currentOverview);
|
529 |
|
}
|
530 |
|
}
|
531 |
|
}
|
532 |
|
|
533 |
|
int lastY = -1;
|
534 |
|
|
535 |
|
/**
|
536 |
|
* Lee una l�nea de bytes
|
537 |
|
* @param line Buffer donde se cargan los datos
|
538 |
|
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
539 |
|
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
540 |
|
* por la izquierda a mitad de pixel
|
541 |
|
* @param gdalBuffer Buffer con la l�nea de datos original
|
542 |
|
*/
|
543 |
|
private void readLine(byte[][] line, double initOffset, GdalBuffer[] gdalBuffer) {
|
544 |
|
double j = 0D;
|
545 |
|
int i = 0;
|
546 |
|
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) {
|
547 |
|
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) {
|
548 |
|
line[iBand][i] = gdalBuffer[iBand].buffByte[(int) j];
|
549 |
|
}
|
550 |
|
}
|
551 |
|
}
|
552 |
|
|
553 |
|
/**
|
554 |
|
* Lee una l�nea de shorts
|
555 |
|
* @param line Buffer donde se cargan los datos
|
556 |
|
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
557 |
|
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
558 |
|
* por la izquierda a mitad de pixel
|
559 |
|
* @param gdalBuffer Buffer con la l�nea de datos original
|
560 |
|
*/
|
561 |
|
private void readLine(short[][] line, double initOffset, GdalBuffer[] gdalBuffer) {
|
562 |
|
double j = 0D;
|
563 |
|
int i = 0;
|
564 |
|
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) {
|
565 |
|
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) {
|
566 |
|
line[iBand][i] = (short) (gdalBuffer[iBand].buffShort[(int) j] & 0xffff);
|
567 |
|
}
|
568 |
|
}
|
569 |
|
}
|
570 |
|
|
571 |
|
/**
|
572 |
|
* Lee una l�nea de ints
|
573 |
|
* @param line Buffer donde se cargan los datos
|
574 |
|
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
575 |
|
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
576 |
|
* por la izquierda a mitad de pixel
|
577 |
|
* @param gdalBuffer Buffer con la l�nea de datos original
|
578 |
|
*/
|
579 |
|
private void readLine(int[][] line, double initOffset, GdalBuffer[] gdalBuffer) {
|
580 |
|
double j = 0D;
|
581 |
|
int i = 0;
|
582 |
|
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) {
|
583 |
|
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) {
|
584 |
|
line[iBand][i] = (gdalBuffer[iBand].buffInt[(int) j] & 0xffffffff);
|
585 |
|
}
|
586 |
|
}
|
587 |
|
}
|
588 |
|
|
589 |
|
/**
|
590 |
|
* Lee una l�nea de float
|
591 |
|
* @param line Buffer donde se cargan los datos
|
592 |
|
* @param initOffset Desplazamiento inicial desde el margen izquierdo. Esto es necesario para cuando
|
593 |
|
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
594 |
|
* por la izquierda a mitad de pixel
|
595 |
|
* @param gdalBuffer Buffer con la l�nea de datos original
|
596 |
|
*/
|
597 |
|
private void readLine(float[][] line, double initOffset, GdalBuffer[] gdalBuffer) {
|
598 |
|
double j = 0D;
|
599 |
|
int i = 0;
|
600 |
|
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) {
|
601 |
|
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) {
|
602 |
|
line[iBand][i] = gdalBuffer[iBand].buffFloat[(int) j];
|
603 |
|
}
|
604 |
|
}
|
605 |
|
}
|
606 |
|
|
607 |
|
/**
|
608 |
|
* Lee una l�nea de doubles
|
609 |
|
* @param line Buffer donde se cargan los datos
|
610 |
|
* @param initOffset Desplazamiento inicial desde el margen inzquierdo. Esto es necesario para cuando
|
611 |
|
* se supersamplea ya que cada pixel de imagen ocupa muchos pixeles de pantalla y puede empezar a dibujarse
|
612 |
|
* por la izquierda a mitad de pixel
|
613 |
|
* @param gdalBuffer Buffer con la l�nea de datos original
|
614 |
|
*/
|
615 |
|
private void readLine(double[][] line, double initOffset, GdalBuffer[] gdalBuffer) {
|
616 |
|
double j = 0D;
|
617 |
|
int i = 0;
|
618 |
|
for (int iBand = 0; iBand < gdalBuffer.length; iBand++) {
|
619 |
|
for (i = 0, j = initOffset; i < currentViewWidth && j < gdalBuffer[0].getSize(); i++, j += stepX) {
|
620 |
|
line[iBand][i] = gdalBuffer[iBand].buffDouble[(int) j];
|
621 |
|
}
|
622 |
|
}
|
623 |
|
}
|
624 |
|
|
625 |
|
/**
|
626 |
|
* Lee una l�nea completa del raster y devuelve un array del tipo correcto. Esta funci�n es util
|
627 |
|
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
628 |
|
* @param nLine N�mero de l�nea a leer
|
629 |
|
* @param band Banda requerida
|
630 |
|
* @return Object que es un array unidimendional del tipo de datos del raster
|
631 |
|
* @throws GdalException
|
632 |
|
*/
|
633 |
|
public Object readCompleteLine(int nLine, int band) throws GdalException {
|
634 |
|
GdalRasterBand gdalBand = super.getRasterBand(band + 1);
|
635 |
|
GdalBuffer gdalBuf = null;
|
636 |
|
|
637 |
|
gdalBuf = gdalBand.readRaster(0, nLine, getRasterXSize(), 1, getRasterXSize(), 1, dataType[band]);
|
638 |
|
|
639 |
|
if (dataType[band] == GDT_Byte)
|
640 |
|
return gdalBuf.buffByte;
|
641 |
|
|
642 |
|
if (dataType[band] == GDT_Int16 || dataType[band] == GDT_UInt16)
|
643 |
|
return gdalBuf.buffShort;
|
644 |
|
|
645 |
|
if (dataType[band] == GDT_Int32 || dataType[band] == GDT_UInt32)
|
646 |
|
return gdalBuf.buffInt;
|
647 |
|
|
648 |
|
if (dataType[band] == GDT_Float32)
|
649 |
|
return gdalBuf.buffFloat;
|
650 |
|
|
651 |
|
if (dataType[band] == GDT_Float64)
|
652 |
|
return gdalBuf.buffDouble;
|
653 |
|
|
654 |
|
if (dataType[band] == GDT_CInt16 || dataType[band] == GDT_CInt32 ||
|
655 |
|
dataType[band] == GDT_CFloat32 || dataType[band] == GDT_CFloat64)
|
656 |
|
return null;
|
657 |
|
|
658 |
|
return null;
|
659 |
|
}
|
660 |
|
|
661 |
|
/**
|
662 |
|
* Lee una bloque completo del raster y devuelve un array tridimensional del tipo correcto. Esta funci�n es util
|
663 |
|
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
664 |
|
* @param nLine N�mero de l�nea a leer
|
665 |
|
* @param band Banda requerida
|
666 |
|
* @return Object que es un array unidimendional del tipo de datos del raster
|
667 |
|
* @throws GdalException
|
668 |
|
*/
|
669 |
|
public Object readBlock(int pos, int blockHeight, double scale) throws GdalException, ProcessInterruptedException {
|
670 |
|
bBandNr = super.getRasterCount();
|
671 |
|
int widthBuffer = (int)(getRasterXSize() * scale);
|
672 |
|
int heightBuffer = (int)(blockHeight * scale);
|
673 |
|
|
674 |
|
RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
|
675 |
|
|
676 |
|
GdalRasterBand[] gdalBand = new GdalRasterBand[bBandNr];
|
677 |
|
for (int iBand = 0; iBand < gdalBand.length; iBand++)
|
678 |
|
gdalBand[iBand] = super.getRasterBand(iBand + 1);
|
679 |
|
|
680 |
|
GdalBuffer[] gdalBuf = new GdalBuffer[bBandNr];
|
681 |
|
|
682 |
|
if (dataType[0] == GDT_Byte) {
|
683 |
|
byte[][][] buf = new byte[bBandNr][heightBuffer][widthBuffer];
|
684 |
|
for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
|
685 |
|
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, getRasterXSize(), blockHeight, widthBuffer, heightBuffer, dataType[0]);
|
686 |
|
for (int iRow = 0; iRow < heightBuffer; iRow++) {
|
687 |
|
for (int iCol = 0; iCol < widthBuffer; iCol++)
|
688 |
|
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffByte[iRow * widthBuffer + iCol];
|
689 |
|
if(task.getEvent() != null)
|
690 |
|
task.manageEvent(task.getEvent());
|
691 |
|
}
|
692 |
|
gdalBuf[iBand].buffByte = null;
|
693 |
|
}
|
694 |
|
return buf;
|
695 |
|
} else if (dataType[0] == GDT_CInt16 || dataType[0] == GDT_Int16 || dataType[0] == GDT_UInt16) {
|
696 |
|
short[][][] buf = new short[bBandNr][heightBuffer][widthBuffer];
|
697 |
|
for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
|
698 |
|
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, getRasterXSize(), blockHeight, widthBuffer, heightBuffer, dataType[0]);
|
699 |
|
for (int iRow = 0; iRow < heightBuffer; iRow++) {
|
700 |
|
for (int iCol = 0; iCol < widthBuffer; iCol++)
|
701 |
|
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffShort[iRow * widthBuffer + iCol];
|
702 |
|
if(task.getEvent() != null)
|
703 |
|
task.manageEvent(task.getEvent());
|
704 |
|
}
|
705 |
|
gdalBuf[iBand].buffShort = null;
|
706 |
|
}
|
707 |
|
return buf;
|
708 |
|
} else if (dataType[0] == GDT_CInt32 || dataType[0] == GDT_Int32 || dataType[0] == GDT_UInt32) {
|
709 |
|
int[][][] buf = new int[bBandNr][heightBuffer][widthBuffer];
|
710 |
|
for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
|
711 |
|
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, getRasterXSize(), blockHeight, widthBuffer, heightBuffer, dataType[0]);
|
712 |
|
for (int iRow = 0; iRow < heightBuffer; iRow++) {
|
713 |
|
for (int iCol = 0; iCol < widthBuffer; iCol++)
|
714 |
|
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffInt[iRow * widthBuffer + iCol];
|
715 |
|
if(task.getEvent() != null)
|
716 |
|
task.manageEvent(task.getEvent());
|
717 |
|
}
|
718 |
|
gdalBuf[iBand].buffInt = null;
|
719 |
|
}
|
720 |
|
return buf;
|
721 |
|
} else if(dataType[0] == GDT_Float32 || dataType[0] == GDT_CFloat32) {
|
722 |
|
float[][][] buf = new float[bBandNr][heightBuffer][widthBuffer];
|
723 |
|
for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
|
724 |
|
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, getRasterXSize(), blockHeight, widthBuffer, heightBuffer, dataType[0]);
|
725 |
|
for (int iRow = 0; iRow < heightBuffer; iRow++) {
|
726 |
|
for (int iCol = 0; iCol < widthBuffer; iCol++)
|
727 |
|
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffFloat[iRow * widthBuffer + iCol];
|
728 |
|
if(task.getEvent() != null)
|
729 |
|
task.manageEvent(task.getEvent());
|
730 |
|
}
|
731 |
|
gdalBuf[iBand].buffFloat = null;
|
732 |
|
}
|
733 |
|
return buf;
|
734 |
|
} else if(dataType[0] == GDT_Float64 || dataType[0] == GDT_CFloat64) {
|
735 |
|
double[][][] buf = new double[bBandNr][heightBuffer][widthBuffer];
|
736 |
|
for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
|
737 |
|
gdalBuf[iBand] = gdalBand[iBand].readRaster(0, pos, getRasterXSize(), blockHeight, widthBuffer, heightBuffer, dataType[0]);
|
738 |
|
for (int iRow = 0; iRow < heightBuffer; iRow++) {
|
739 |
|
for (int iCol = 0; iCol < widthBuffer; iCol++)
|
740 |
|
buf[iBand][iRow][iCol] = gdalBuf[iBand].buffDouble[iRow * widthBuffer + iCol];
|
741 |
|
if(task.getEvent() != null)
|
742 |
|
task.manageEvent(task.getEvent());
|
743 |
|
}
|
744 |
|
gdalBuf[iBand].buffDouble = null;
|
745 |
|
}
|
746 |
|
return buf;
|
747 |
|
}
|
748 |
|
|
749 |
|
return null;
|
750 |
|
}
|
751 |
|
|
752 |
|
/**
|
753 |
|
* Lectura de una l�nea de datos.
|
754 |
|
* @param line
|
755 |
|
* @throws GdalException
|
756 |
|
*/
|
757 |
|
public void readLine(Object line) throws GdalException {
|
758 |
|
int w = (int) (Math.ceil(((double)currentViewWidth)*stepX) + 1);
|
759 |
|
int x = (int) (currentViewX);
|
760 |
|
int y = (int) (lastReadLine);
|
761 |
|
GdalBuffer r = null, g = null, b = null;
|
762 |
|
GdalBuffer a = new GdalBuffer();
|
763 |
|
|
764 |
|
while(y >= gdalBands[0].getRasterBandYSize())
|
765 |
|
y--;
|
766 |
|
|
767 |
|
if (x+w > gdalBands[0].getRasterBandXSize())
|
768 |
|
w = gdalBands[0].getRasterBandXSize()-x;
|
769 |
|
|
770 |
|
if(gdalBands[0].getRasterColorTable() != null) {
|
771 |
|
palette = new DataStoreColorTable(gdalColorTable2ColorItems(gdalBands[0].getRasterColorTable()), false);
|
772 |
|
r = gdalBands[0].readRaster(x, y, w, 1, w, 1, dataType[0]);
|
773 |
|
} else {
|
774 |
|
a.buffByte = new byte[w];
|
775 |
|
r = gdalBands[0].readRaster(x, y, w, 1, w, 1, dataType[0]);
|
776 |
|
g = b = r;
|
777 |
|
if (getRasterCount() > 1 && gdalBands[1] != null)
|
778 |
|
g = gdalBands[1].readRaster(x, y, w, 1, w, 1, dataType[0]);
|
779 |
|
if (getRasterCount() > 2 && gdalBands[2] != null)
|
780 |
|
b = gdalBands[2].readRaster(x, y, w, 1, w, 1, dataType[0]);
|
781 |
|
}
|
782 |
|
|
783 |
|
lastReadLine += stepY;
|
784 |
|
|
785 |
|
double initOffset = Math.abs(currentViewX - ((int)currentViewX));
|
786 |
|
GdalBuffer[] bands = {r, g, b};
|
787 |
|
|
788 |
|
if (dataType[0] == GDT_Byte)
|
789 |
|
readLine((byte[][])line, initOffset, bands);
|
790 |
|
else if (dataType[0] == GDT_CInt16 || dataType[0] == GDT_Int16 || dataType[0] == GDT_UInt16)
|
791 |
|
readLine((short[][])line, initOffset, bands);
|
792 |
|
else if (dataType[0] == GDT_CInt32 || dataType[0] == GDT_Int32 || dataType[0] == GDT_UInt32)
|
793 |
|
readLine((int[][])line, initOffset, bands);
|
794 |
|
else if(dataType[0] == GDT_Float32 || dataType[0] == GDT_CFloat32)
|
795 |
|
readLine((float[][])line, initOffset, bands);
|
796 |
|
else if(dataType[0] == GDT_Float64 || dataType[0] == GDT_CFloat64)
|
797 |
|
readLine((double[][])line, initOffset, bands);
|
798 |
|
|
799 |
|
return;
|
800 |
|
}
|
801 |
|
|
802 |
|
private List<ColorItem> gdalColorTable2ColorItems(GdalColorTable table) {
|
803 |
|
try {
|
804 |
|
List<ColorItem> colorItems = new ArrayList<ColorItem>();
|
805 |
|
for (int iEntry = 0; iEntry < table.getColorEntryCount(); iEntry++) {
|
806 |
|
GdalColorEntry entry = table.getColorEntryAsRGB(iEntry);
|
807 |
|
|
808 |
|
ColorItem colorItem = new ColorItemImpl();
|
809 |
|
colorItem.setNameClass("");
|
810 |
|
colorItem.setValue(iEntry);
|
811 |
|
colorItem.setColor(new Color( (int) (entry.c1 & 0xff),
|
812 |
|
(int) (entry.c2 & 0xff),
|
813 |
|
(int) (entry.c3 & 0xff),
|
814 |
|
(int) (entry.c4 & 0xff)));
|
815 |
|
|
816 |
|
colorItems.add(colorItem);
|
817 |
|
}
|
818 |
|
return colorItems;
|
819 |
|
} catch (GdalException ex) {
|
820 |
|
// No se crea la paleta
|
821 |
|
}
|
822 |
|
return null;
|
823 |
|
}
|
824 |
|
|
825 |
|
|
826 |
|
/**
|
827 |
|
* Cuando se hace una petici�n de carga de buffer la extensi�n pedida puede
|
828 |
|
* estar ajustada a la extensi�n del raster o no estarlo. En caso de no
|
829 |
|
* estarlo los pixeles del buffer que caen fuera de la extensi�n del raster
|
830 |
|
* tendr�n valor de NoData. Esta funci�n calcula en que pixel del buffer hay
|
831 |
|
* que empezar a escribir en caso de que este sea mayor que los datos a leer.
|
832 |
|
*
|
833 |
|
* @param dWorldTLX Posici�n X superior izquierda en coord reales
|
834 |
|
* @param dWorldTLY Posici�n Y superior izquierda en coord reales
|
835 |
|
* @param dWorldBRX Posici�n X inferior derecha en coord reales
|
836 |
|
* @param dWorldBRY Posici�n Y inferior derecha en coord reales
|
837 |
|
* @param nWidth Ancho en pixeles del buffer
|
838 |
|
* @param nHeight Alto en pixeles del buffer
|
839 |
|
* @return desplazamiento dentro del buffer en X e Y
|
840 |
|
*/
|
841 |
|
private int[] calcStepBuffer(Extent dataExtent, int nWidth, int nHeight, int[] stpBuffer) {
|
842 |
|
Extent imageExtent = getExtentWithoutRot();
|
843 |
|
Extent ajustDataExtent = RasterLocator.getManager().getRasterUtils().calculateAdjustedView(dataExtent, imageExtent);
|
844 |
|
if(!RasterLocator.getManager().getRasterUtils().compareExtents(dataExtent, ajustDataExtent)){
|
845 |
|
Point2D p1 = worldToRasterWithoutRot(new Point2D.Double(ajustDataExtent.minX(), ajustDataExtent.maxY()));
|
846 |
|
Point2D p2 = worldToRasterWithoutRot(new Point2D.Double(ajustDataExtent.maxX(), ajustDataExtent.minY()));
|
847 |
|
Point2D p3 = worldToRasterWithoutRot(new Point2D.Double(dataExtent.minX(), dataExtent.maxY()));
|
848 |
|
// Point2D p4 = worldToRasterWithoutRot(new Point2D.Double(dataExtent.maxX(), dataExtent.minY()));
|
849 |
|
//Ese es el ancho y alto q tendr�a el buffer en caso de haberse ajustado
|
850 |
|
int w = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX()));
|
851 |
|
int h = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY()));
|
852 |
|
|
853 |
|
stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
|
854 |
|
stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
|
855 |
|
stpBuffer[2] = stpBuffer[0] + w;
|
856 |
|
stpBuffer[3] = stpBuffer[1] + h;
|
857 |
|
return new int[]{w, h};
|
858 |
|
}
|
859 |
|
return new int[]{nWidth, nHeight};
|
860 |
|
}
|
861 |
|
|
862 |
|
/**
|
863 |
|
* Lee una ventana de datos sin resampleo a partir de coordenadas reales.
|
864 |
|
* @param buf Buffer donde se almacenan los datos
|
865 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
866 |
|
* @param dWorldTLX Posici�n X superior izquierda en coord reales
|
867 |
|
* @param dWorldTLY Posici�n Y superior izquierda en coord reales
|
868 |
|
* @param dWorldBRX Posici�n X inferior derecha en coord reales
|
869 |
|
* @param dWorldBRY Posici�n Y inferior derecha en coord reales
|
870 |
|
* @param nWidth Ancho en pixeles del buffer
|
871 |
|
* @param nHeight Alto en pixeles del buffer
|
872 |
|
* @throws GdalException
|
873 |
|
*/
|
874 |
|
public void readWindow(Buffer buf, BandList bandList, double ulx, double uly,double lrx, double lry,
|
875 |
|
int nWidth, int nHeight, boolean adjustToExtent, TaskStatus status) throws GdalException, ProcessInterruptedException {
|
876 |
|
Extent petExtent = new ExtentImpl(ulx, uly, lrx, lry);
|
877 |
|
setView(ulx, uly, lrx, lry, nWidth, nHeight);
|
878 |
|
Point2D tl = worldToRaster(new Point2D.Double(ulx, uly));
|
879 |
|
Point2D br = worldToRaster(new Point2D.Double(lrx, lry));
|
880 |
|
|
881 |
|
if(tl.getX() > br.getX())
|
882 |
|
tl.setLocation(tl.getX() - 1, tl.getY());
|
883 |
|
else
|
884 |
|
br.setLocation(br.getX() - 1, br.getY());
|
885 |
|
|
886 |
|
if(tl.getY() > br.getY())
|
887 |
|
tl.setLocation(tl.getX(), tl.getY() - 1);
|
888 |
|
else
|
889 |
|
br.setLocation(br.getX(), br.getY() - 1);
|
890 |
|
|
891 |
|
if(gdalBands.length == 0)
|
892 |
|
return;
|
893 |
|
|
894 |
|
selectGdalBands(/*buf.getBandCount()*/getRasterCount());
|
895 |
|
|
896 |
|
int x = (int) Math.round(Math.min(tl.getX(), br.getX()));
|
897 |
|
int y = (int) Math.round(Math.min(tl.getY(), br.getY()));
|
898 |
|
|
899 |
|
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()};
|
900 |
|
//Si el buffer no se ajusta al extent entonces calculamos en que posici�n comienza a escribirse dentro del buffer
|
901 |
|
//ya que lo que cae fuera ser�n valores NoData
|
902 |
|
if(!adjustToExtent){
|
903 |
|
int[] wh = calcStepBuffer(petExtent, nWidth, nHeight, stpBuffer);
|
904 |
|
if(x < 0)
|
905 |
|
x = 0;
|
906 |
|
if(y < 0)
|
907 |
|
y = 0;
|
908 |
|
readDataCachedBuffer(buf, bandList, new int[]{x, y, wh[0], wh[1]},
|
909 |
|
wh[0], wh[1], 0, 0, stpBuffer, status);
|
910 |
|
return;
|
911 |
|
}
|
912 |
|
|
913 |
|
readDataCachedBuffer(buf, bandList, new int[]{x, y, nWidth, nHeight},
|
914 |
|
nWidth, nHeight, 0, 0, stpBuffer, status);
|
915 |
|
}
|
916 |
|
|
917 |
|
public void readWindow(Buffer buf, BandList bandList, Extent ext, Rectangle adjustedWindow, TaskStatus status) throws GdalException, ProcessInterruptedException {
|
918 |
|
setView(ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), buf.getWidth(), buf.getHeight());
|
919 |
|
|
920 |
|
if(gdalBands.length == 0)
|
921 |
|
return;
|
922 |
|
|
923 |
|
selectGdalBands(getRasterCount());
|
924 |
|
|
925 |
|
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()};
|
926 |
|
|
927 |
|
adjustedWindow = getAdjustedWindowInOverviewCoordinates(adjustedWindow);
|
928 |
|
|
929 |
|
readDataCachedBuffer(buf,
|
930 |
|
bandList,
|
931 |
|
new int[]{(int)adjustedWindow.getX(), (int)adjustedWindow.getY(), (int)adjustedWindow.getWidth(), (int)adjustedWindow.getHeight()},
|
932 |
|
buf.getWidth(),
|
933 |
|
buf.getHeight(),
|
934 |
|
0, 0, stpBuffer, status);
|
935 |
|
}
|
936 |
|
|
937 |
|
/**
|
938 |
|
* Adjust the request rectangle to the overview size. The requests in Gdal have to be
|
939 |
|
* in the overview scale
|
940 |
|
* @param adjustedWindow
|
941 |
|
* @return
|
942 |
|
*/
|
943 |
|
private Rectangle getAdjustedWindowInOverviewCoordinates(Rectangle adjustedWindow) {
|
944 |
|
int nWidth = (int)(((long)adjustedWindow.getWidth() * overviewWidth) / width);
|
945 |
|
int nHeight = (int)(((long)adjustedWindow.getHeight() * overviewHeight) / height);
|
946 |
|
int x = (int)(((long)adjustedWindow.getX() * (long)overviewWidth) / (long)width);
|
947 |
|
int y = (int) (((long)adjustedWindow.getY() * (long)overviewHeight) / (long)height);
|
948 |
|
return new Rectangle(x, y, nWidth, nHeight);
|
949 |
|
}
|
950 |
|
|
951 |
|
/**
|
952 |
|
* Lee una ventana de datos con resampleo a partir de coordenadas reales. Este m�todo lee la
|
953 |
|
* ventana de una vez cargando los datos de un golpe en el buffer. Las coordenadas se solicitan
|
954 |
|
* en coordenadas del mundo real por lo que estas pueden caer en cualquier parte de un pixel.
|
955 |
|
* Esto se hace m�s evidente cuando supersampleamos en la petici�n, es decir el buffer de de
|
956 |
|
* mayor tama�o que el n�mero de pixels solicitado.
|
957 |
|
*
|
958 |
|
* Para resolver esto escribiremos con la funci�n readRaster los datos sobre un buffer mayor
|
959 |
|
* que el solicitado. Despu�s calcularemos el desplazamiento en pixels dentro de este buffer
|
960 |
|
* de mayor tama�o hasta llegar a la coordenada real donde comienza la petici�n real que ha
|
961 |
|
* hecho el usuario. Esto es as� porque cuando supersampleamos no queremos los pixeles del
|
962 |
|
* raster de disco completos sino que en los bordes del buffer quedan cortados.
|
963 |
|
*
|
964 |
|
* @param buf Buffer donde se almacenan los datos
|
965 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
966 |
|
* @param dWorldTLX Posici�n X superior izquierda en coord reales
|
967 |
|
* @param dWorldTLY Posici�n Y superior izquierda en coord reales
|
968 |
|
* @param dWorldBRX Posici�n X inferior derecha en coord reales
|
969 |
|
* @param dWorldBRY Posici�n Y inferior derecha en coord reales
|
970 |
|
* @param nWidth Ancho en pixeles de la petici�n
|
971 |
|
* @param nHeight Alto en pixeles de la petici�n
|
972 |
|
* @param bufWidth Ancho del buffer
|
973 |
|
* @param bufHeight Alto del buffer
|
974 |
|
* @throws GdalException
|
975 |
|
*/
|
976 |
|
public void readWindow(Buffer buf, BandList bandList, double ulx, double uly, double lrx, double lry,
|
977 |
|
double nWidth, double nHeight, int bufWidth, int bufHeight, boolean adjustToExtent, TaskStatus status) throws GdalException, ProcessInterruptedException {
|
978 |
|
Extent petExtent = new ExtentImpl(ulx, uly, lrx, lry);
|
979 |
|
setView(ulx, uly, lrx, lry, bufWidth, bufHeight);
|
980 |
|
Point2D ul = worldToRaster(new Point2D.Double(ulx, uly));
|
981 |
|
Point2D lr = worldToRaster(new Point2D.Double(lrx, lry));
|
982 |
|
ul.setLocation(ul.getX() < 0 ? 1 : ul.getX(), ul.getY() < 0 ? 1 : ul.getY());
|
983 |
|
lr.setLocation(lr.getX() < 0 ? 1 : lr.getX(), lr.getY() < 0 ? 1 : lr.getY());
|
984 |
|
ul.setLocation(ul.getX() - 0.5, ul.getY() - 0.5);
|
985 |
|
lr.setLocation(lr.getX() - 0.5, lr.getY() - 0.5);
|
986 |
|
|
987 |
|
adjustPoints(ul, lr);
|
988 |
|
|
989 |
|
if(gdalBands.length == 0)
|
990 |
|
return;
|
991 |
|
|
992 |
|
selectGdalBands(/*buf.getBandCount()*/getRasterCount());
|
993 |
|
|
994 |
|
Rectangle requestWindow = new Rectangle(
|
995 |
|
(int) Math.min(ul.getX(), lr.getX()),
|
996 |
|
(int) Math.min(ul.getY(), lr.getY()),
|
997 |
|
(int)nWidth,
|
998 |
|
(int)nHeight);
|
999 |
|
|
1000 |
|
requestWindow = getAdjustedWindowInOverviewCoordinates(requestWindow);
|
1001 |
|
|
1002 |
|
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()};
|
1003 |
|
//Si el buffer no se ajusta al extent entonces calculamos en que posici�n comienza a escribirse dentro del buffer
|
1004 |
|
//ya que lo que cae fuera ser�n valores NoData
|
1005 |
|
if(!adjustToExtent){
|
1006 |
|
int[] wh = calcStepBuffer(petExtent, bufWidth, bufHeight, stpBuffer);
|
1007 |
|
if(requestWindow.getX() < 0)
|
1008 |
|
requestWindow.setLocation(0, (int)requestWindow.getY());
|
1009 |
|
if(requestWindow.getY() < 0)
|
1010 |
|
requestWindow.setLocation((int)requestWindow.getX(), 0);
|
1011 |
|
stpBuffer[0] = (int)((stpBuffer[0] * bufWidth) / requestWindow.getWidth());
|
1012 |
|
stpBuffer[1] = (int)((stpBuffer[1] * bufHeight) / requestWindow.getHeight());
|
1013 |
|
stpBuffer[2] = (int)((stpBuffer[2] * bufWidth) / requestWindow.getWidth());
|
1014 |
|
stpBuffer[3] = (int)((stpBuffer[3] * bufHeight) / requestWindow.getHeight());
|
1015 |
|
bufWidth = (int)Math.abs(stpBuffer[2] - stpBuffer[0]);
|
1016 |
|
bufHeight = (int)Math.abs(stpBuffer[3] - stpBuffer[1]);
|
1017 |
|
readDataCachedBuffer(buf, bandList,
|
1018 |
|
new int[]{(int)requestWindow.getX(), (int)requestWindow.getY(), wh[0], wh[1]},
|
1019 |
|
bufWidth, bufHeight, 0, 0, stpBuffer, status);
|
1020 |
|
return;
|
1021 |
|
}
|
1022 |
|
|
1023 |
|
if ((requestWindow.getX() + requestWindow.getWidth()) > gdalBands[0].getRasterBandXSize())
|
1024 |
|
requestWindow.setSize((int)(gdalBands[0].getRasterBandXSize() - requestWindow.getX()), (int)requestWindow.getHeight());
|
1025 |
|
|
1026 |
|
if ((requestWindow.getY() + requestWindow.getHeight()) > gdalBands[0].getRasterBandYSize())
|
1027 |
|
requestWindow.setSize((int)requestWindow.getWidth(), (int)(gdalBands[0].getRasterBandYSize() - requestWindow.getY()));
|
1028 |
|
|
1029 |
|
readDataCachedBuffer(buf, bandList,
|
1030 |
|
new int[]{(int)requestWindow.getX(), (int)requestWindow.getY(), (int)requestWindow.getWidth(), (int)requestWindow.getHeight()},
|
1031 |
|
bufWidth, bufHeight, 0, 0, stpBuffer, status);
|
1032 |
|
}
|
1033 |
|
|
1034 |
|
private void adjustPoints(Point2D ul, Point2D lr) {
|
1035 |
|
double a = (ul.getX() - (int)ul.getX());
|
1036 |
|
double b = (ul.getY() - (int)ul.getY());
|
1037 |
|
ul.setLocation( (a > 0.95 || a < 0.05) ? Math.round(ul.getX()) : ul.getX(),
|
1038 |
|
(b > 0.95 || b < 0.05) ? Math.round(ul.getY()) : ul.getY());
|
1039 |
|
lr.setLocation( (a > 0.95 || a < 0.05) ? Math.round(lr.getX()) : lr.getX(),
|
1040 |
|
(b > 0.95 || b < 0.05) ? Math.round(lr.getY()) : lr.getY());
|
1041 |
|
}
|
1042 |
|
|
1043 |
|
/**
|
1044 |
|
* Lee una ventana de datos con resampleo a partir de coordenadas en pixeles. Este m�todo lee la
|
1045 |
|
* ventana de una vez cargando los datos de un golpe en el buffer.
|
1046 |
|
* @param buf Buffer donde se almacenan los datos
|
1047 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
1048 |
|
* @param x Posici�n X en pixeles
|
1049 |
|
* @param y Posici�n Y en pixeles
|
1050 |
|
* @param w Ancho en pixeles
|
1051 |
|
* @param h Alto en pixeles
|
1052 |
|
* @param bufWidth Ancho del buffer
|
1053 |
|
* @param bufHeight Alto del buffer
|
1054 |
|
* @throws GdalException
|
1055 |
|
*/
|
1056 |
|
public void readWindow(Buffer buf, BandList bandList, int x, int y, int w, int h, TaskStatus status) throws GdalException, ProcessInterruptedException {
|
1057 |
|
gdalBands = new GdalRasterBand[getRasterCount()];
|
1058 |
|
|
1059 |
|
if(buf.getWidth() == w && buf.getHeight() == h)
|
1060 |
|
isSupersampling = false;
|
1061 |
|
|
1062 |
|
if(gdalBands.length == 0)
|
1063 |
|
return;
|
1064 |
|
|
1065 |
|
// Selecciona las bandas
|
1066 |
|
gdalBands[0] = getRasterBand(1);
|
1067 |
|
|
1068 |
|
for(int iBand = 1; iBand < gdalBands.length; iBand++)
|
1069 |
|
gdalBands[iBand] = getRasterBand(iBand + 1);
|
1070 |
|
|
1071 |
|
assignDataTypeFromGdalRasterBands(gdalBands);
|
1072 |
|
|
1073 |
|
int[] stpBuffer = new int[]{0, 0 , buf.getWidth(), buf.getHeight()};
|
1074 |
|
readDataCachedBuffer(buf, bandList, new int[]{x, y, w, h}, buf.getWidth(), buf.getHeight(), 0, 0, stpBuffer, status);
|
1075 |
|
}
|
1076 |
|
|
1077 |
|
/**
|
1078 |
|
* Asigna el tipo de datos de las bandas a partir de una lista de GdalRasterBands
|
1079 |
|
* @param gdalBands
|
1080 |
|
* @throws GdalException
|
1081 |
|
*/
|
1082 |
|
private void assignDataTypeFromGdalRasterBands(GdalRasterBand[] gdalBands) throws GdalException {
|
1083 |
|
int[] dt = new int[gdalBands.length];
|
1084 |
|
for (int i = 0; i < gdalBands.length; i++) {
|
1085 |
|
if(gdalBands[i] != null)
|
1086 |
|
dt[i] = gdalBands[i].getRasterDataType();
|
1087 |
|
}
|
1088 |
|
setDataType(dt);
|
1089 |
|
}
|
1090 |
|
|
1091 |
|
/**
|
1092 |
|
* Lee una ventana de datos. Esta funci?n es usuada por
|
1093 |
|
* readWindow para coordenadas reales y readWindow en coordenadas pixel. Esta es una versi?n de readData pero
|
1094 |
|
* comprueba si el buffer es cacheado y si lo es pide por trozos para no intentar cargar desde gdal demasiados
|
1095 |
|
* datos.
|
1096 |
|
* @param buf Buffer donde se almacenan los datos
|
1097 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
1098 |
|
* @param inputWindow
|
1099 |
|
* <UL>
|
1100 |
|
* <LI>inputWindow[0]:Posici?n X en pixeles de la imagen de entrada</LI>
|
1101 |
|
* <LI>inputWindow[1]:Posici?n Y en pixeles de la imagen de entrada</LI>
|
1102 |
|
* <LI>inputWindow[2]:Ancho en p?xeles a leer de la imagen de entrada</LI>
|
1103 |
|
* <LI>inputWindow[3]:Alto en p?xeles a leer de la imagen de entrada</LI>
|
1104 |
|
* </UL>
|
1105 |
|
* @param bufWidth Ancho del buffer de la imagen de entrada. Si no coincide con inputWindow[2] el propio gdal resamplea
|
1106 |
|
* @param bufHeight Alto del buffer de la imagen de entrada. Si no coincide con inputWindow[3] el propio gdal resamplea
|
1107 |
|
* @param stepX Desplazamiento en p?xeles en X a partir de la posici?n x. Este desplazamiento es util cuando hay un
|
1108 |
|
* supersampleo ya que puede ser que de los pixeles que est?n en el borde izquierdo de la petici?n solo queramos una
|
1109 |
|
* parte de ellos.
|
1110 |
|
* @param stepY Desplazamiento en p?xeles en Y a partir de la posici?n y. Este desplazamiento es util cuando hay un
|
1111 |
|
* supersampleo ya que puede ser que de los p?xeles que est?n en el borde superior de la petici?n solo queramos una
|
1112 |
|
* parte de ellos.
|
1113 |
|
* @param stepBuffer El buffer puede empezar a escribirse a partir de un pixel determinado y acabar de escribir antes
|
1114 |
|
* de fin de buffer. Este par?metro indica el desplazamiento desde el inicio del buffer y la posici?n final.
|
1115 |
|
* <UL>
|
1116 |
|
* <LI>stepBuffer[0]:Desplazamiento en X desde el inicio</LI>
|
1117 |
|
* <LI>stepBuffer[1]:Desplazamiento en Y desde el inicio</LI>
|
1118 |
|
* <LI>stepBuffer[2]:Posici?n X final m?s uno</LI>
|
1119 |
|
* <LI>stepBuffer[3]:Posici?n Y final m?s uno</LI>
|
1120 |
|
* </UL>
|
1121 |
|
* @throws GdalException
|
1122 |
|
*/
|
1123 |
|
private void readDataCachedBuffer(Buffer buf,
|
1124 |
|
BandList bandList,
|
1125 |
|
int[] inputWindow,
|
1126 |
|
int bufWidth,
|
1127 |
|
int bufHeight,
|
1128 |
|
int stpX,
|
1129 |
|
int stpY,
|
1130 |
|
int[] stepBuffer,
|
1131 |
|
TaskStatus status) throws GdalException, ProcessInterruptedException {
|
1132 |
|
if(buf.isCached()) {
|
1133 |
|
int nBlocks = (int)(buf.getHeight() / buf.getBlockHeight());
|
1134 |
|
int lastblock = buf.getHeight() - (nBlocks * buf.getBlockHeight());
|
1135 |
|
if(lastblock > 0)
|
1136 |
|
nBlocks ++;
|
1137 |
|
int initYSrc = inputWindow[1];
|
1138 |
|
int stepYSrc = (buf.getBlockHeight() * inputWindow[3]) / buf.getHeight();
|
1139 |
|
int lastBlockYSrc = (lastblock * inputWindow[3]) / buf.getHeight();
|
1140 |
|
int initYBuffer = 0;
|
1141 |
|
for (int i = 0; i < nBlocks; i++) {
|
1142 |
|
if(lastblock > 0 && i == (nBlocks - 1)) {
|
1143 |
|
int[] newStepBuffer = new int[]{0, initYBuffer, stepBuffer[2], initYBuffer + lastblock};
|
1144 |
|
int[] newWindow = new int[]{inputWindow[0], initYSrc, inputWindow[2], lastBlockYSrc};
|
1145 |
|
readData(buf,
|
1146 |
|
bandList,
|
1147 |
|
newWindow,
|
1148 |
|
bufWidth, lastblock, 0, 0, newStepBuffer);
|
1149 |
|
} else {
|
1150 |
|
int[] newStepBuffer = new int[]{0, initYBuffer, stepBuffer[2], initYBuffer + buf.getBlockHeight()};
|
1151 |
|
int[] newWindow = new int[]{inputWindow[0], initYSrc, inputWindow[2], stepYSrc};
|
1152 |
|
readData(buf,
|
1153 |
|
bandList,
|
1154 |
|
newWindow,
|
1155 |
|
bufWidth, buf.getBlockHeight(), 0, 0, newStepBuffer);
|
1156 |
|
initYSrc += stepYSrc;
|
1157 |
|
initYBuffer += buf.getBlockHeight();
|
1158 |
|
}
|
1159 |
|
}
|
1160 |
|
} else {
|
1161 |
|
readData(buf, bandList, inputWindow, bufWidth, bufHeight, 0, 0, stepBuffer);
|
1162 |
|
}
|
1163 |
|
}
|
1164 |
|
|
1165 |
|
/**
|
1166 |
|
* Lee una ventana de datos. Esta funci?n es usuada por
|
1167 |
|
* readWindow para coordenadas reales y readWindow en coordenadas pixel.
|
1168 |
|
* @param buf Buffer donde se almacenan los datos
|
1169 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
1170 |
|
* @param inputWindow
|
1171 |
|
* <UL>
|
1172 |
|
* <LI>inputWindow[0]:Posici?n X en pixeles de la imagen de entrada</LI>
|
1173 |
|
* <LI>inputWindow[1]:Posici?n Y en pixeles de la imagen de entrada</LI>
|
1174 |
|
* <LI>inputWindow[2]:Ancho en p?xeles a leer de la imagen de entrada</LI>
|
1175 |
|
* <LI>inputWindow[3]:Alto en p?xeles a leer de la imagen de entrada</LI>
|
1176 |
|
* </UL>
|
1177 |
|
* @param bufWidth Ancho del buffer de la imagen de entrada. Si no coincide con inputWindow[2] el propio gdal resamplea
|
1178 |
|
* @param bufHeight Alto del buffer de la imagen de entrada. Si no coincide con inputWindow[3] el propio gdal resamplea
|
1179 |
|
* @param stepX Desplazamiento en p?xeles en X a partir de la posici?n x. Este desplazamiento es util cuando hay un
|
1180 |
|
* supersampleo ya que puede ser que de los pixeles que est?n en el borde izquierdo de la petici?n solo queramos una
|
1181 |
|
* parte de ellos.
|
1182 |
|
* @param stepY Desplazamiento en p?xeles en Y a partir de la posici?n y. Este desplazamiento es util cuando hay un
|
1183 |
|
* supersampleo ya que puede ser que de los p?xeles que est?n en el borde superior de la petici?n solo queramos una
|
1184 |
|
* parte de ellos.
|
1185 |
|
* @param stepBuffer El buffer puede empezar a escribirse a partir de un pixel determinado y acabar de escribir antes
|
1186 |
|
* de fin de buffer. Este par?metro indica el desplazamiento desde el inicio del buffer y la posici?n final.
|
1187 |
|
* <UL>
|
1188 |
|
* <LI>stepBuffer[0]:Desplazamiento en X desde el inicio</LI>
|
1189 |
|
* <LI>stepBuffer[1]:Desplazamiento en Y desde el inicio</LI>
|
1190 |
|
* <LI>stepBuffer[2]:Posici?n X final m?s uno</LI>
|
1191 |
|
* <LI>stepBuffer[3]:Posici?n Y final m?s uno</LI>
|
1192 |
|
* </UL>
|
1193 |
|
* @throws GdalException
|
1194 |
|
*/
|
1195 |
|
private void readData(Buffer buf,
|
1196 |
|
BandList bandList,
|
1197 |
|
int[] inputWindow,
|
1198 |
|
int bufWidth,
|
1199 |
|
int bufHeight,
|
1200 |
|
int stpX,
|
1201 |
|
int stpY,
|
1202 |
|
int[] stepBuffer) throws GdalException, ProcessInterruptedException {
|
1203 |
|
|
1204 |
|
RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
|
1205 |
|
FileUtils fUtil = RasterLocator.getManager().getFileUtils();
|
1206 |
|
|
1207 |
|
GdalBuffer gdalBuf = null;
|
1208 |
|
for(int iBand = 0; iBand < gdalBands.length; iBand++) {
|
1209 |
|
int[] drawableBands = bandList.getBufferBandToDraw(fUtil.getFormatedRasterFileName(fileName), iBand);
|
1210 |
|
if(drawableBands == null || (drawableBands.length == 1 && drawableBands[0] == -1))
|
1211 |
|
continue;
|
1212 |
|
int init = (int)((bufWidth * stpY) + stpX); //Pos inicial. Desplazamos stpX pixels hacia la derecha y bajamos stpY lineas
|
1213 |
|
int pos = init;
|
1214 |
|
gdalBuf = gdalBands[iBand].readRaster( inputWindow[0],
|
1215 |
|
inputWindow[1],
|
1216 |
|
inputWindow[2],
|
1217 |
|
inputWindow[3],
|
1218 |
|
bufWidth,
|
1219 |
|
bufHeight,
|
1220 |
|
dataType[iBand]);
|
1221 |
|
int lineInputWindow = 0;
|
1222 |
|
if(dataType[iBand] == Gdal.GDT_Byte) {
|
1223 |
|
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) {
|
1224 |
|
pos = (int)((bufWidth * (lineInputWindow - stepBuffer[0])) + init);
|
1225 |
|
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++) {
|
1226 |
|
for (int i = 0; i < drawableBands.length; i++)
|
1227 |
|
buf.setElem(line, col, drawableBands[i], gdalBuf.buffByte[pos]);
|
1228 |
|
pos ++;
|
1229 |
|
}
|
1230 |
|
lineInputWindow ++;
|
1231 |
|
if(task.getEvent() != null)
|
1232 |
|
task.manageEvent(task.getEvent());
|
1233 |
|
}
|
1234 |
|
gdalBuf.buffByte = null;
|
1235 |
|
} else if((dataType[iBand] == Gdal.GDT_UInt16) || (dataType[iBand] == Gdal.GDT_Int16) || (dataType[iBand] == Gdal.GDT_CInt16)) {
|
1236 |
|
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) {
|
1237 |
|
pos = (int)((bufWidth * (lineInputWindow - stepBuffer[0])) + init);
|
1238 |
|
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++) {
|
1239 |
|
for (int i = 0; i < drawableBands.length; i++)
|
1240 |
|
buf.setElem(line, col, drawableBands[i], gdalBuf.buffShort[pos]);
|
1241 |
|
pos ++;
|
1242 |
|
}
|
1243 |
|
lineInputWindow ++;
|
1244 |
|
if(task.getEvent() != null)
|
1245 |
|
task.manageEvent(task.getEvent());
|
1246 |
|
}
|
1247 |
|
gdalBuf.buffShort = null;
|
1248 |
|
} else if((dataType[iBand] == Gdal.GDT_UInt32) || (dataType[iBand] == Gdal.GDT_Int32) || (dataType[iBand] == Gdal.GDT_CInt32)) {
|
1249 |
|
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) {
|
1250 |
|
pos = (int)((bufWidth * (lineInputWindow - stepBuffer[0])) + init);
|
1251 |
|
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++) {
|
1252 |
|
for (int i = 0; i < drawableBands.length; i++)
|
1253 |
|
buf.setElem(line, col, drawableBands[i], gdalBuf.buffInt[pos]);
|
1254 |
|
pos ++;
|
1255 |
|
}
|
1256 |
|
lineInputWindow ++;
|
1257 |
|
if(task.getEvent() != null)
|
1258 |
|
task.manageEvent(task.getEvent());
|
1259 |
|
}
|
1260 |
|
gdalBuf.buffInt = null;
|
1261 |
|
} else if(dataType[iBand] == Gdal.GDT_Float32) {
|
1262 |
|
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) {
|
1263 |
|
pos = (int)((bufWidth * (lineInputWindow - stepBuffer[0])) + init);
|
1264 |
|
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++) {
|
1265 |
|
for (int i = 0; i < drawableBands.length; i++)
|
1266 |
|
buf.setElem(line, col, drawableBands[i], gdalBuf.buffFloat[pos]);
|
1267 |
|
pos ++;
|
1268 |
|
}
|
1269 |
|
lineInputWindow ++;
|
1270 |
|
if(task.getEvent() != null)
|
1271 |
|
task.manageEvent(task.getEvent());
|
1272 |
|
}
|
1273 |
|
gdalBuf.buffFloat = null;
|
1274 |
|
} else if(dataType[iBand] == Gdal.GDT_Float64) {
|
1275 |
|
for (int line = stepBuffer[1]; line < stepBuffer[3]; line++) {
|
1276 |
|
pos = (int)((bufWidth * (lineInputWindow - stepBuffer[0])) + init);
|
1277 |
|
for (int col = stepBuffer[0]; col < stepBuffer[2]; col ++) {
|
1278 |
|
for (int i = 0; i < drawableBands.length; i++)
|
1279 |
|
buf.setElem(line, col, drawableBands[i], gdalBuf.buffDouble[pos]);
|
1280 |
|
pos ++;
|
1281 |
|
}
|
1282 |
|
lineInputWindow ++;
|
1283 |
|
if(task.getEvent() != null)
|
1284 |
|
task.manageEvent(task.getEvent());
|
1285 |
|
}
|
1286 |
|
gdalBuf.buffDouble = null;
|
1287 |
|
}
|
1288 |
|
}
|
1289 |
|
}
|
1290 |
|
|
1291 |
|
/**
|
1292 |
|
* Lee una ventana de datos sin resampleo a partir de coordenadas en pixeles. Esta funci�n es usuada por
|
1293 |
|
* readWindow para coordenadas reales y readWindow en coordenadas pixel.
|
1294 |
|
* @param buf Buffer donde se almacenan los datos
|
1295 |
|
* @param bandList Lista de bandas que queremos leer y sobre que bandas del buffer de destino queremos escribirlas
|
1296 |
|
* @param x Posici�n X en pixeles
|
1297 |
|
* @param y Posici�n Y en pixeles
|
1298 |
|
* @param w Ancho en pixeles
|
1299 |
|
* @param yMax altura m�xima de y
|
1300 |
|
* @throws GdalException
|
1301 |
|
*/
|
1302 |
|
@SuppressWarnings("unused")
|
1303 |
|
private void readDataByLine(Buffer buf, BandList bandList, int x, int y, int w, int yMax) throws GdalException, ProcessInterruptedException {
|
1304 |
|
GdalBuffer gdalBuf = null;
|
1305 |
|
int rasterBufLine;
|
1306 |
|
RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
|
1307 |
|
FileUtils fUtil = RasterLocator.getManager().getFileUtils();
|
1308 |
|
|
1309 |
|
for(int iBand = 0; iBand < gdalBands.length; iBand++) {
|
1310 |
|
int[] drawableBands = bandList.getBufferBandToDraw(fUtil.getFormatedRasterFileName(fileName), iBand);
|
1311 |
|
if(drawableBands == null || (drawableBands.length == 1 && drawableBands[0] == -1))
|
1312 |
|
continue;
|
1313 |
|
if(dataType[iBand] == Gdal.GDT_Byte) {
|
1314 |
|
for (int line = y; line < yMax; line++) {
|
1315 |
|
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]);
|
1316 |
|
rasterBufLine = line - y;
|
1317 |
|
for (int i = 0; i < drawableBands.length; i++) {
|
1318 |
|
buf.setLineInBandByte(gdalBuf.buffByte, rasterBufLine, drawableBands[i]);
|
1319 |
|
}
|
1320 |
|
if(task.getEvent() != null)
|
1321 |
|
task.manageEvent(task.getEvent());
|
1322 |
|
}
|
1323 |
|
}else if((dataType[iBand] == Gdal.GDT_UInt16) || (dataType[iBand] == Gdal.GDT_Int16) || (dataType[iBand] == Gdal.GDT_CInt16)) {
|
1324 |
|
for (int line = y; line < yMax; line++) {
|
1325 |
|
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]);
|
1326 |
|
rasterBufLine = line - y;
|
1327 |
|
for (int i = 0; i < drawableBands.length; i++) {
|
1328 |
|
buf.setLineInBandShort(gdalBuf.buffShort, rasterBufLine, drawableBands[i]);
|
1329 |
|
}
|
1330 |
|
if(task.getEvent() != null)
|
1331 |
|
task.manageEvent(task.getEvent());
|
1332 |
|
}
|
1333 |
|
}else if((dataType[iBand] == Gdal.GDT_UInt32) || (dataType[iBand] == Gdal.GDT_Int32) || (dataType[iBand] == Gdal.GDT_CInt32)) {
|
1334 |
|
for (int line = y; line < yMax; line++) {
|
1335 |
|
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]);
|
1336 |
|
rasterBufLine = line - y;
|
1337 |
|
for (int i = 0; i < drawableBands.length; i++) {
|
1338 |
|
buf.setLineInBandInt(gdalBuf.buffInt, rasterBufLine, drawableBands[i]);
|
1339 |
|
}
|
1340 |
|
if(task.getEvent() != null)
|
1341 |
|
task.manageEvent(task.getEvent());
|
1342 |
|
}
|
1343 |
|
}else if(dataType[iBand] == Gdal.GDT_Float32){
|
1344 |
|
for (int line = y; line < yMax; line++) {
|
1345 |
|
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]);
|
1346 |
|
rasterBufLine = line - y;
|
1347 |
|
for (int i = 0; i < drawableBands.length; i++) {
|
1348 |
|
buf.setLineInBandFloat(gdalBuf.buffFloat, rasterBufLine, drawableBands[i]);
|
1349 |
|
}
|
1350 |
|
if(task.getEvent() != null)
|
1351 |
|
task.manageEvent(task.getEvent());
|
1352 |
|
}
|
1353 |
|
}else if(dataType[iBand] == Gdal.GDT_Float64){
|
1354 |
|
for (int line = y; line < yMax; line++) {
|
1355 |
|
gdalBuf = gdalBands[iBand].readRaster(x, line, w, 1, w, 1, dataType[iBand]);
|
1356 |
|
rasterBufLine = line - y;
|
1357 |
|
for (int i = 0; i < drawableBands.length; i++) {
|
1358 |
|
buf.setLineInBandDouble(gdalBuf.buffDouble, rasterBufLine, drawableBands[i]);
|
1359 |
|
}
|
1360 |
|
if(task.getEvent() != null)
|
1361 |
|
task.manageEvent(task.getEvent());
|
1362 |
|
}
|
1363 |
|
}
|
1364 |
|
}
|
1365 |
|
}
|
1366 |
|
|
1367 |
|
/**
|
1368 |
|
* Obtiene el valor de un pixel determinado por las coordenadas x e y que se pasan
|
1369 |
|
* por par�metro
|
1370 |
|
* @param x Coordenada X del pixel
|
1371 |
|
* @param y Coordenada Y del pixel
|
1372 |
|
* @return Array de Object donde cada posici�n representa una banda y el valor ser� Integer
|
1373 |
|
* en caso de ser byte, shot o int, Float en caso de ser float y Double en caso de ser double.
|
1374 |
|
*/
|
1375 |
|
public Object[] getData(int x, int y) {
|
1376 |
|
try {
|
1377 |
|
Object[] data = new Object[getRasterCount()];
|
1378 |
|
for(int i = 0; i < getRasterCount(); i++){
|
1379 |
|
GdalRasterBand rb = getRasterBand(i + 1);
|
1380 |
|
GdalBuffer r = rb.readRaster(x, y, 1, 1, 1, 1, dataType[i]);
|
1381 |
|
switch(dataType[i]){
|
1382 |
|
case 0: break; //Sin tipo
|
1383 |
|
case 1: data[i] = new Integer(r.buffByte[0]); //Buffer byte (8)
|
1384 |
|
break;
|
1385 |
|
case 2: //Buffer short (16)
|
1386 |
|
case 3: data[i] = new Integer(r.buffShort[0]); //Buffer short (16)
|
1387 |
|
break;
|
1388 |
|
case 4: //Buffer int (32)
|
1389 |
|
case 5: data[i] = new Integer(r.buffInt[0]); //Buffer int (32)
|
1390 |
|
break;
|
1391 |
|
case 6: data[i] = new Float(r.buffFloat[0]); //Buffer float (32)
|
1392 |
|
break;
|
1393 |
|
case 7: data[i] = new Double(r.buffDouble[0]); //Buffer double (64)
|
1394 |
|
break;
|
1395 |
|
}
|
1396 |
|
}
|
1397 |
|
return data;
|
1398 |
|
} catch (GdalException e) {
|
1399 |
|
return null;
|
1400 |
|
}
|
1401 |
|
}
|
1402 |
|
|
1403 |
|
public int getBlockSize(){
|
1404 |
|
return this.getBlockSize();
|
1405 |
|
}
|
1406 |
|
|
1407 |
|
/**
|
1408 |
|
* Devuelve la transformaci�n del fichero de georreferenciaci�n
|
1409 |
|
* @return AffineTransform
|
1410 |
|
*/
|
1411 |
|
public AffineTransform getOwnTransformation() {
|
1412 |
|
return ownTransformation;
|
1413 |
|
}
|
1414 |
|
|
1415 |
|
/**
|
1416 |
|
* Calcula el extent en coordenadas del mundo real sin rotaci�n. Solo coordenadas y tama�o de pixel
|
1417 |
|
* @return Extent
|
1418 |
|
*/
|
1419 |
|
public Extent getExtentWithoutRot() {
|
1420 |
|
AffineTransform at = new AffineTransform( externalTransformation.getScaleX(), 0,
|
1421 |
|
0, externalTransformation.getScaleY(),
|
1422 |
|
externalTransformation.getTranslateX(), externalTransformation.getTranslateY());
|
1423 |
|
Point2D p1 = new Point2D.Double(0, 0);
|
1424 |
|
Point2D p2 = new Point2D.Double(width, height);
|
1425 |
|
at.transform(p1, p1);
|
1426 |
|
at.transform(p2, p2);
|
1427 |
|
return new ExtentImpl(p1, p2);
|
1428 |
|
}
|
1429 |
|
|
1430 |
|
/**
|
1431 |
|
* Asigna una transformaci�n que es aplicada sobre la que ya tiene el propio fichero
|
1432 |
|
* @param t
|
1433 |
|
*/
|
1434 |
|
public void setExternalTransform(AffineTransform t){
|
1435 |
|
externalTransformation = t;
|
1436 |
|
}
|
1437 |
|
|
1438 |
|
/**
|
1439 |
|
* Obtiene el nombre del driver de Gdal
|
1440 |
|
* @return Cadena que representa el nombre del driver de gdal
|
1441 |
|
*/
|
1442 |
|
public String getGdalShortName() {
|
1443 |
|
return shortName;
|
1444 |
|
}
|
1445 |
|
|
1446 |
|
public void dispose() {
|
1447 |
|
open = false;
|
1448 |
|
try {
|