svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / GdalDriver.java @ 12749
History | View | Annotate | Download (14.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.Point2D; |
23 |
import java.awt.geom.Rectangle2D; |
24 |
|
25 |
import org.cresques.cts.ICoordTrans; |
26 |
import org.cresques.cts.IProjection; |
27 |
import org.gvsig.raster.dataset.BandList; |
28 |
import org.gvsig.raster.dataset.FileNotOpenException; |
29 |
import org.gvsig.raster.dataset.GeoInfo; |
30 |
import org.gvsig.raster.dataset.IBuffer; |
31 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
32 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
33 |
import org.gvsig.raster.dataset.RasterDataset; |
34 |
import org.gvsig.raster.dataset.RasterDriverException; |
35 |
import org.gvsig.raster.dataset.io.rmf.ParsingException; |
36 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
37 |
import org.gvsig.raster.dataset.properties.DatasetMetadata; |
38 |
import org.gvsig.raster.datastruct.ColorTable; |
39 |
import org.gvsig.raster.datastruct.Extent; |
40 |
import org.gvsig.raster.datastruct.Transparency; |
41 |
import org.gvsig.raster.util.RasterUtilities; |
42 |
import org.gvsig.raster.util.extensionPoints.ExtensionPoints; |
43 |
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton; |
44 |
|
45 |
import es.gva.cit.jgdal.GdalException; |
46 |
|
47 |
/**
|
48 |
* Clase que representa al driver de acceso a datos de gdal.
|
49 |
* @author Luis W. Sevilla
|
50 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
51 |
*/
|
52 |
public class GdalDriver extends RasterDataset { |
53 |
public final static int BAND_HEIGHT = 64; |
54 |
protected GdalNative file = null; |
55 |
|
56 |
public GdalDriver(){super(null, null);} |
57 |
|
58 |
private Extent viewRequest = null; |
59 |
|
60 |
public static void register() { |
61 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
62 |
extensionPoints.add("RasterReader", "bmp", GdalDriver.class); |
63 |
extensionPoints.add("RasterReader", "gif", GdalDriver.class); |
64 |
extensionPoints.add("RasterReader", "tif", GdalDriver.class); |
65 |
extensionPoints.add("RasterReader", "tiff",GdalDriver.class); |
66 |
extensionPoints.add("RasterReader", "jpg", GdalDriver.class); |
67 |
extensionPoints.add("RasterReader", "png", GdalDriver.class); |
68 |
extensionPoints.add("RasterReader", "vrt", GdalDriver.class); |
69 |
extensionPoints.add("RasterReader", "dat", GdalDriver.class); // Envi |
70 |
extensionPoints.add("RasterReader", "lan", GdalDriver.class); // Erdas |
71 |
extensionPoints.add("RasterReader", "gis", GdalDriver.class); // Erdas |
72 |
extensionPoints.add("RasterReader", "img", GdalDriver.class); // Erdas |
73 |
extensionPoints.add("RasterReader", "pix", GdalDriver.class); // PCI Geomatics |
74 |
extensionPoints.add("RasterReader", "aux", GdalDriver.class); // PCI Geomatics |
75 |
extensionPoints.add("RasterReader", "adf", GdalDriver.class); // ESRI Grids |
76 |
extensionPoints.add("RasterReader", "mpr", GdalDriver.class); // Ilwis |
77 |
extensionPoints.add("RasterReader", "mpl", GdalDriver.class); // Ilwis |
78 |
extensionPoints.add("RasterReader", "map", GdalDriver.class); // PC Raster |
79 |
extensionPoints.add("RasterReader", "asc", GdalDriver.class); |
80 |
extensionPoints.add("RasterReader", "pgm", GdalDriver.class); //Ficheros PNM en escala de grises |
81 |
extensionPoints.add("RasterReader", "ppm", GdalDriver.class); //Ficheros PNM en RGB |
82 |
extensionPoints.add("RasterReader", "rst", GdalDriver.class); //IDRISIS |
83 |
extensionPoints.add("RasterReader", "rmf", GdalDriver.class); //Raster Matrix Format |
84 |
extensionPoints.add("RasterReader", "nos", GdalDriver.class); |
85 |
extensionPoints.add("RasterReader", "kap", GdalDriver.class); |
86 |
} |
87 |
|
88 |
public GdalDriver(IProjection proj, Object param)throws NotSupportedExtensionException { |
89 |
super(proj, param);
|
90 |
setParam(param); |
91 |
try {
|
92 |
if(param instanceof String) { |
93 |
file = new GdalNative(((String)param)); |
94 |
ownTransformation = file.getOwnTransformation(); |
95 |
externalTransformation = (AffineTransform)ownTransformation.clone();
|
96 |
load(); |
97 |
}else {
|
98 |
//TODO: FUNCIONALIDAD: Formatos gestionados por gdal que no tienen extensi?n. Estos tendr?n un objeto IRegistrableRasterFormat Por ej: Grass
|
99 |
} |
100 |
bandCount = file.getRasterCount(); |
101 |
} catch (GdalException e) {
|
102 |
e.printStackTrace(); |
103 |
throw new NotSupportedExtensionException("Extension not supported"); |
104 |
} catch(Exception e) { |
105 |
System.out.println("Error en GdalOpen"); |
106 |
e.printStackTrace(); |
107 |
file = null;
|
108 |
} |
109 |
|
110 |
//Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
111 |
setDataType(RasterUtilities.getRasterBufTypeFromGdalType(file.getDataType())); |
112 |
|
113 |
super.init();
|
114 |
|
115 |
try {
|
116 |
loadFromRmf(getRmfBlocksManager()); |
117 |
} catch (ParsingException e) {
|
118 |
//No lee desde rmf
|
119 |
} |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Obtenemos o calculamos el extent de la imagen.
|
124 |
*/
|
125 |
public GeoInfo load() {
|
126 |
return this; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Cierra el fichero de imagen
|
131 |
*/
|
132 |
public void close() { |
133 |
try {
|
134 |
if(file != null){ |
135 |
file.close(); |
136 |
file = null;
|
137 |
} |
138 |
} catch (GdalException e) {
|
139 |
e.printStackTrace(); |
140 |
} |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
|
145 |
* de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
|
146 |
* ha de estar en coordenadas del fichero.
|
147 |
*/
|
148 |
public void setView(Extent e) { |
149 |
viewRequest = e; |
150 |
} |
151 |
|
152 |
/**
|
153 |
* Obtiene extent de la vista actual
|
154 |
*/
|
155 |
public Extent getView() {
|
156 |
return viewRequest;
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Obtiene la anchura del fichero
|
161 |
*/
|
162 |
public int getWidth() { |
163 |
return file.width;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* Obtiene la altura del fichero
|
168 |
*/
|
169 |
public int getHeight() { |
170 |
return file.height;
|
171 |
} |
172 |
|
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
* @see org.gvsig.fmap.driver.GeoRasterFile#readCompletetLine(int, int)
|
176 |
*/
|
177 |
public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
178 |
if(line > this.getHeight() || band > this.getBandCount()) |
179 |
throw new InvalidSetViewException("Request out of grid"); |
180 |
|
181 |
try{
|
182 |
return file.readCompleteLine(line, band);
|
183 |
}catch(GdalException e){
|
184 |
throw new RasterDriverException("Error reading data from Gdal library"); |
185 |
} |
186 |
} |
187 |
|
188 |
/*
|
189 |
* (non-Javadoc)
|
190 |
* @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
|
191 |
*/
|
192 |
public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
193 |
if(pos < 0) |
194 |
throw new InvalidSetViewException("Request out of grid"); |
195 |
|
196 |
if((pos + blockHeight) > getHeight())
|
197 |
blockHeight = Math.abs(getHeight() - pos);
|
198 |
try{
|
199 |
return file.readBlock(pos, blockHeight);
|
200 |
}catch(GdalException e){
|
201 |
throw new RasterDriverException("Error reading data from Gdal library"); |
202 |
} |
203 |
} |
204 |
|
205 |
/* (non-Javadoc)
|
206 |
* @see org.cresques.io.GeoRasterFile#getData(int, int, int)
|
207 |
*/
|
208 |
public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException{ |
209 |
if(file != null){ |
210 |
if(x < 0 || y < 0 || x >= file.width || y >= file.height) |
211 |
throw new InvalidSetViewException("Request out of grid"); |
212 |
Object[] data = file.getData(x, y); |
213 |
return data[band];
|
214 |
} |
215 |
throw new FileNotOpenException("GdalNative not exist"); |
216 |
} |
217 |
|
218 |
/*
|
219 |
* (non-Javadoc)
|
220 |
* @see org.gvsig.raster.dataset.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.raster.dataset.BandList, org.gvsig.raster.dataset.IBuffer)
|
221 |
*/
|
222 |
public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry, BandList bandList, IBuffer rasterBuf) { |
223 |
Extent selectedExtent = new Extent(ulx, uly, lrx, lry);
|
224 |
setView(selectedExtent); |
225 |
|
226 |
try {
|
227 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight(), true);
|
228 |
} catch (Exception e) { |
229 |
e.printStackTrace(); |
230 |
} |
231 |
|
232 |
return rasterBuf;
|
233 |
} |
234 |
|
235 |
/*
|
236 |
* (non-Javadoc)
|
237 |
* @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
|
238 |
*/
|
239 |
public IBuffer getWindowRaster(double ulx, double uly, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) { |
240 |
//El incremento o decremento de las X e Y depende de los signos de rotaci?n y escala en la matriz de transformaci?n. Por esto
|
241 |
//tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
|
242 |
Extent ext = getExtent(); |
243 |
Point2D pInit = rasterToWorld(new Point2D.Double(0, 0)); |
244 |
Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight())); |
245 |
double wRaster = Math.abs(pEnd.getX() - pInit.getX()); |
246 |
double hRaster = Math.abs(pEnd.getY() - pInit.getY()); |
247 |
double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
|
248 |
double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
|
249 |
|
250 |
Extent selectedExtent = new Extent(ulx, uly, lrx, lry);
|
251 |
setView(selectedExtent); |
252 |
|
253 |
try {
|
254 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight(), adjustToExtent); |
255 |
} catch (Exception e) { |
256 |
e.printStackTrace(); |
257 |
} |
258 |
|
259 |
return rasterBuf;
|
260 |
} |
261 |
|
262 |
/*
|
263 |
* (non-Javadoc)
|
264 |
* @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
|
265 |
*/
|
266 |
public IBuffer getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) { |
267 |
Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
|
268 |
setView(selectedExtent); |
269 |
|
270 |
double width = 0; |
271 |
double height = 0; |
272 |
|
273 |
Point2D ul = new Point2D.Double(viewRequest.getULX(), viewRequest.getULY()); |
274 |
Point2D lr = new Point2D.Double(viewRequest.getLRX(), viewRequest.getLRY()); |
275 |
ul = worldToRaster(ul); |
276 |
lr = worldToRaster(lr); |
277 |
width = Math.abs(lr.getX() - ul.getX());
|
278 |
height = Math.abs(lr.getY() - ul.getY());
|
279 |
|
280 |
try {
|
281 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), width, height, bufWidth, bufHeight, adjustToExtent); |
282 |
} catch (Exception e) { |
283 |
e.printStackTrace(); |
284 |
} |
285 |
|
286 |
return rasterBuf;
|
287 |
} |
288 |
|
289 |
/*
|
290 |
* (non-Javadoc)
|
291 |
* @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
|
292 |
*/
|
293 |
public IBuffer getWindowRaster(int x, int y, int w, int h, BandList bandList, IBuffer rasterBuf) { |
294 |
try {
|
295 |
setView( |
296 |
new Extent( RasterUtilities.getMapRectFromPxRect(getExtent().toRectangle2D(),
|
297 |
getWidth(), |
298 |
getHeight(), |
299 |
new Rectangle2D.Double(x, y, w, h))) |
300 |
); |
301 |
file.readWindow(rasterBuf, bandList, x, y, w, h); |
302 |
} catch (Exception e) { |
303 |
e.printStackTrace(); |
304 |
} |
305 |
return rasterBuf;
|
306 |
} |
307 |
|
308 |
/*
|
309 |
* (non-Javadoc)
|
310 |
* @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
|
311 |
*/
|
312 |
public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf) { |
313 |
try {
|
314 |
setView( |
315 |
new Extent( RasterUtilities.getMapRectFromPxRect(getExtent().toRectangle2D(),
|
316 |
getWidth(), |
317 |
getHeight(), |
318 |
new Rectangle2D.Double(x, y, w, h))) |
319 |
); |
320 |
file.readWindow(rasterBuf, bandList, x, y, w, h, bufWidth, bufHeight); |
321 |
} catch (Exception e) { |
322 |
e.printStackTrace(); |
323 |
} |
324 |
return rasterBuf;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* Devuelve el tama?o de bloque
|
329 |
* @return Tama?o de bloque
|
330 |
*/
|
331 |
public int getBlockSize(){ |
332 |
if(file != null) |
333 |
return file.getBlockSize();
|
334 |
else
|
335 |
return 0; |
336 |
} |
337 |
|
338 |
/**
|
339 |
* Obtiene el objeto que contiene los metadatos
|
340 |
*/
|
341 |
public DatasetMetadata getMetadata() {
|
342 |
if(file != null) |
343 |
return file.metadata;
|
344 |
else
|
345 |
return null; |
346 |
} |
347 |
|
348 |
/**
|
349 |
* Obtiene el objeto que contiene que contiene la interpretaci?n de
|
350 |
* color por banda
|
351 |
* @return
|
352 |
*/
|
353 |
public DatasetColorInterpretation getColorInterpretation(){
|
354 |
if(file != null) |
355 |
return file.colorInterpr;
|
356 |
return null; |
357 |
} |
358 |
|
359 |
/**
|
360 |
* Obtiene el objeto que contiene el estado de la transparencia
|
361 |
*/
|
362 |
public Transparency getTransparencyDatasetStatus() { |
363 |
return file.fileTransparency;
|
364 |
} |
365 |
|
366 |
/**
|
367 |
* Obtiene el flag que dice si la imagen est? o no georreferenciada
|
368 |
* @return true si est? georreferenciada y false si no lo est?.
|
369 |
*/
|
370 |
public boolean isGeoreferenced() { |
371 |
if(file != null) |
372 |
return file.georeferenced;
|
373 |
else
|
374 |
return false; |
375 |
} |
376 |
|
377 |
/**
|
378 |
* Obtiene el objeto que contiene la paleta de color.
|
379 |
*/
|
380 |
public ColorTable getColorTable() {
|
381 |
if(file != null) |
382 |
return file.palette;
|
383 |
else
|
384 |
return null; |
385 |
} |
386 |
|
387 |
/**
|
388 |
* Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
389 |
* el valor de esta variable cada vez que dibuja.
|
390 |
* @return true si se ha supersampleado y false si no se ha hecho.
|
391 |
*/
|
392 |
public boolean isSupersampling() { |
393 |
if(file != null) |
394 |
return file.isSupersampling;
|
395 |
else
|
396 |
return false; |
397 |
} |
398 |
|
399 |
public GdalNative getNative(){
|
400 |
return file;
|
401 |
} |
402 |
|
403 |
/**
|
404 |
* Obtiene el nombre del driver
|
405 |
*/
|
406 |
public String getName() { |
407 |
return "gvSIG Gdal Raster Driver"; |
408 |
} |
409 |
|
410 |
/*
|
411 |
* (non-Javadoc)
|
412 |
* @see org.gvsig.raster.driver.GeoData#getStringProjection()
|
413 |
*/
|
414 |
public String getWktProjection() throws RasterDriverException{ |
415 |
try {
|
416 |
return file.getProjectionRef();
|
417 |
} catch (GdalException e) {
|
418 |
throw new RasterDriverException("Error getting projection"); |
419 |
} |
420 |
} |
421 |
|
422 |
/*
|
423 |
* (non-Javadoc)
|
424 |
* @see org.gvsig.raster.dataset.RasterDataset#setCanceled(boolean, int)
|
425 |
*/
|
426 |
public void setCanceled(boolean value, int process) { |
427 |
super.setCanceled(value, process);
|
428 |
|
429 |
if(process == CANCEL_READ || process == 0) |
430 |
file.readCancel = value; |
431 |
} |
432 |
|
433 |
/*
|
434 |
* (non-Javadoc)
|
435 |
* @see org.gvsig.raster.dataset.RasterDataset#isCanceled(int)
|
436 |
*/
|
437 |
public boolean isCanceled(int process) { |
438 |
if(process == CANCEL_READ) {
|
439 |
return file.readCancel;
|
440 |
} else
|
441 |
return super.isCanceled(process); |
442 |
} |
443 |
|
444 |
public void reProject(ICoordTrans rp) { |
445 |
|
446 |
} |
447 |
|
448 |
/*
|
449 |
* (non-Javadoc)
|
450 |
* @see org.gvsig.raster.dataset.RasterDataset#setAffineTransform(java.awt.geom.AffineTransform)
|
451 |
*/
|
452 |
public void setAffineTransform(AffineTransform t){ |
453 |
super.setAffineTransform(t);
|
454 |
file.setExternalTransform(t); |
455 |
} |
456 |
} |
457 |
|
458 |
|
459 |
|