gvsig-raster / org.gvsig.raster.gdal / trunk / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / raster / gdal / io / GdalProvider.java @ 2885
History | View | Annotate | Download (17.2 KB)
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.geom.AffineTransform; |
25 |
import java.io.BufferedReader; |
26 |
import java.io.File; |
27 |
import java.io.FileNotFoundException; |
28 |
import java.io.FileReader; |
29 |
import java.io.IOException; |
30 |
|
31 |
import org.gvsig.fmap.dal.DALFileLocator; |
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
35 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
36 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
37 |
import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
38 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
39 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
40 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
41 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
42 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
43 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
44 |
import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
45 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
46 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
47 |
import org.gvsig.jgdal.GdalException; |
48 |
import org.gvsig.metadata.MetadataLocator; |
49 |
import org.gvsig.raster.cache.tile.provider.TileServer; |
50 |
import org.gvsig.raster.impl.buffer.SpiRasterQuery; |
51 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
52 |
import org.gvsig.raster.impl.provider.AbstractRasterProvider; |
53 |
import org.gvsig.raster.impl.provider.RasterProvider; |
54 |
import org.gvsig.raster.impl.provider.tile.FileTileServer; |
55 |
import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
56 |
import org.gvsig.raster.impl.store.DefaultRasterStore; |
57 |
import org.gvsig.raster.impl.store.DefaultStoreFactory; |
58 |
import org.gvsig.raster.impl.store.properties.DataStoreMetadata; |
59 |
import org.gvsig.tools.ToolsLocator; |
60 |
import org.gvsig.tools.dynobject.DynObject; |
61 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
62 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
63 |
import org.slf4j.Logger; |
64 |
import org.slf4j.LoggerFactory; |
65 |
/**
|
66 |
* This class represents the data access for gdal formats.
|
67 |
*
|
68 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
69 |
*/
|
70 |
public class GdalProvider extends AbstractRasterProvider { |
71 |
public static String NAME = "Gdal Store"; |
72 |
public static String DESCRIPTION = "Gdal Raster file"; |
73 |
public static final String METADATA_DEFINITION_NAME = "GdalStore"; |
74 |
private static final Logger logger = LoggerFactory.getLogger(GdalProvider.class); |
75 |
|
76 |
public static final String FORMAT_GTiff = "GTiff"; |
77 |
public static final String FORMAT_VRT = "VRT"; |
78 |
public static final String FORMAT_NITF = "NITF"; |
79 |
public static final String FORMAT_HFA = "HFA"; |
80 |
public static final String FORMAT_ELAS = "ELAS"; |
81 |
public static final String FORMAT_MEM = "MEM"; |
82 |
public static final String FORMAT_BMP = "BMP"; |
83 |
public static final String FORMAT_PCIDSK = "PCIDSK"; |
84 |
public static final String FORMAT_ILWIS = "ILWIS"; |
85 |
public static final String FORMAT_HDF4 = "HDF4Image"; |
86 |
public static final String FORMAT_PNM = "PNM"; |
87 |
public static final String FORMAT_ENVI = "ENVI"; |
88 |
public static final String FORMAT_EHDR = "EHdr"; |
89 |
public static final String FORMAT_PAUX = "PAux"; |
90 |
public static final String FORMAT_MFF = "MFF"; |
91 |
public static final String FORMAT_MFF2 = "MFF2"; |
92 |
public static final String FORMAT_BT = "BT"; |
93 |
public static final String FORMAT_IDA = "IDA"; |
94 |
public static final String FORMAT_RMF = "RMF"; |
95 |
public static final String FORMAT_RST = "RST"; |
96 |
public static final String FORMAT_LEVELLER = "Leveller"; |
97 |
public static final String FORMAT_TERRAGEN = "Terragen"; |
98 |
public static final String FORMAT_ERS = "ERS"; |
99 |
public static final String FORMAT_INGR = "INGR"; |
100 |
public static final String FORMAT_GSAG = "GSAG"; |
101 |
public static final String FORMAT_GSBG = "GSBG"; |
102 |
public static final String FORMAT_ADRG = "ADRG"; |
103 |
public static final String FORMAT_JP2 = "JPEG2000"; |
104 |
public static final String FORMAT_GRD = "GRD"; |
105 |
public static final int BAND_HEIGHT = 64; |
106 |
protected GdalNative file = null; |
107 |
private Extent viewRequest = null; |
108 |
protected static String[] formatList = null; |
109 |
|
110 |
public static void register() { |
111 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
112 |
registerFormats(); |
113 |
|
114 |
ExtensionPoint point = extensionPoints.get("DefaultRasterProvider");
|
115 |
point.append("reader", GdalProvider.NAME, GdalProvider.class);
|
116 |
|
117 |
RasterLocator.getManager().getProviderServices().registerFileProvidersTiled(GdalProvider.class); |
118 |
|
119 |
DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
120 |
if (dataman != null && !dataman.getStoreProviders().contains(NAME)) { |
121 |
dataman.registerStoreProvider(NAME, |
122 |
GdalProvider.class, GdalDataParameters.class); |
123 |
} |
124 |
|
125 |
if(DALFileLocator.getFilesystemServerExplorerManager() != null) |
126 |
DALFileLocator.getFilesystemServerExplorerManager().registerProvider( |
127 |
NAME, DESCRIPTION, |
128 |
GdalFilesystemServerExplorer.class); |
129 |
|
130 |
dataman.registerStoreFactory(NAME, DefaultStoreFactory.class); |
131 |
} |
132 |
|
133 |
private static void registerFormats() { |
134 |
formatList = new String[] { |
135 |
"bmp",
|
136 |
"gif",
|
137 |
"tif",
|
138 |
"tiff",
|
139 |
"jpg",
|
140 |
"jpeg",
|
141 |
"png",
|
142 |
"vrt",
|
143 |
"dat", // Envi |
144 |
"lan", // Erdas |
145 |
"gis", // Erdas |
146 |
"img", // Erdas |
147 |
"pix", // PCI Geomatics |
148 |
"aux", // PCI Geomatics |
149 |
"adf", // ESRI Grids |
150 |
"mpr", // Ilwis |
151 |
"mpl", // Ilwis |
152 |
"map", // PC Raster |
153 |
"asc",
|
154 |
"pgm", //Ficheros PNM en escala de grises |
155 |
"ppm", //Ficheros PNM en RGB |
156 |
"rst", //IDRISIS |
157 |
"rmf", //Raster Matrix Format |
158 |
"nos",
|
159 |
"kap",
|
160 |
"hdr",
|
161 |
"raw",
|
162 |
"ers",
|
163 |
"xml",
|
164 |
"grd",
|
165 |
"txt"/*, |
166 |
"jp2"*/};
|
167 |
for (int i = 0; i < formatList.length; i++) |
168 |
RasterLocator.getManager().getProviderServices().addFormat(formatList[i], GdalProvider.class); |
169 |
} |
170 |
|
171 |
public String[] getFormatList() { |
172 |
return formatList;
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Returns true if the extension is supported and false if doesn't
|
177 |
* @param ext
|
178 |
* @return
|
179 |
*/
|
180 |
public boolean isExtensionSupported(String ext) { |
181 |
if(ext.indexOf(".") != -1) |
182 |
ext = ext.substring(ext.lastIndexOf(".") + 1, ext.length()); |
183 |
for (int i = 0; i < formatList.length; i++) { |
184 |
if(formatList[i].compareTo(ext) == 0) |
185 |
return true; |
186 |
} |
187 |
return false; |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Mandatory constructor to instantiate an empty provider
|
192 |
*/
|
193 |
public GdalProvider() {
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* Constructor. Abre el dataset.
|
198 |
* @param proj Proyecci?n
|
199 |
* @param fName Nombre del fichero
|
200 |
* @throws NotSupportedExtensionException
|
201 |
*/
|
202 |
public GdalProvider(String params) throws NotSupportedExtensionException { |
203 |
super(params);
|
204 |
if(params instanceof String) { |
205 |
GdalDataParameters p = new GdalDataParameters();
|
206 |
p.setURI((String)params);
|
207 |
super.init(p, null, ToolsLocator.getDynObjectManager() |
208 |
.createDynObject( |
209 |
MetadataLocator.getMetadataManager().getDefinition( |
210 |
DataStore.METADATA_DEFINITION_NAME))); |
211 |
init(p, null);
|
212 |
} |
213 |
} |
214 |
|
215 |
public GdalProvider (GdalDataParameters params,
|
216 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
217 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
218 |
.createDynObject( |
219 |
MetadataLocator.getMetadataManager().getDefinition( |
220 |
DataStore.METADATA_DEFINITION_NAME))); |
221 |
init(params, storeServices); |
222 |
} |
223 |
|
224 |
public GdalProvider(AbstractRasterDataParameters params,
|
225 |
DataStoreProviderServices storeServices, DynObject metadata) { |
226 |
super(params, storeServices, metadata);
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Creates file references and loads structures with the information and metadata
|
231 |
* @param params load parameters
|
232 |
* @throws NotSupportedExtensionException
|
233 |
*/
|
234 |
public void init (AbstractRasterDataParameters params, |
235 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
236 |
try {
|
237 |
setParam(storeServices, params); |
238 |
validRmf(params.getURI()); |
239 |
setFName(translateFileName(params.getURI())); |
240 |
file = new GdalNative(translateFileName(params.getURI()));
|
241 |
setColorInterpretation(file.colorInterpr); |
242 |
setColorTable(file.palette); |
243 |
noData = file.getNoDataValue(); |
244 |
String wktProjection = file.getProjectionRef();
|
245 |
if(wktProjection != null && wktProjection != "") { |
246 |
try {
|
247 |
if(RasterLocator.getManager().isCRSUtilSupported())
|
248 |
proj = RasterLocator.getManager().getCRSUtils().convertWktToIProjection(wktProjection); |
249 |
} catch (Exception e) { |
250 |
logger.info("Error reading WKT from the raster provider", e);
|
251 |
} |
252 |
} |
253 |
//CrsWkt crs = new CrsWkt(wktProjection);
|
254 |
//IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
255 |
ownTransformation = file.getOwnTransformation(); |
256 |
externalTransformation = (AffineTransform)ownTransformation.clone();
|
257 |
load(); |
258 |
|
259 |
if(file != null) |
260 |
bandCount = file.getRasterCount(); |
261 |
} catch (GdalException e) {
|
262 |
throw new NotSupportedExtensionException("Extension not supported", e); |
263 |
} catch(Exception e) { |
264 |
System.out.println("Error en GdalOpen"); |
265 |
e.printStackTrace(); |
266 |
file = null;
|
267 |
} |
268 |
|
269 |
//Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
270 |
int[] dt = new int[file.getDataType().length]; |
271 |
for (int i = 0; i < dt.length; i++) |
272 |
dt[i] = GdalNative.getRasterBufTypeFromGdalType(file.getDataType()[i]); |
273 |
setDataType(dt); |
274 |
|
275 |
super.init();
|
276 |
|
277 |
try {
|
278 |
loadFromRmf(getRmfBlocksManager()); |
279 |
} catch (ParsingException e) {
|
280 |
//No lee desde rmf
|
281 |
logger.debug("Problems reading from the RMF file", e);
|
282 |
} |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Comprueba si el fichero abierto es un RasterMetaFile o una imagen
|
287 |
* raster.
|
288 |
* @throws GdalException
|
289 |
*/
|
290 |
private void validRmf(String file) throws GdalException { |
291 |
if(file.endsWith(".rmf")) { |
292 |
File f = new File(file); |
293 |
try {
|
294 |
FileReader fr = new FileReader(f); |
295 |
BufferedReader br = new BufferedReader(fr); |
296 |
char[] buffer = new char[5]; |
297 |
br.read(buffer); |
298 |
StringBuffer st = new StringBuffer(new String(buffer)); |
299 |
br.close(); |
300 |
fr.close(); |
301 |
if(st.toString().equals("<?xml")) |
302 |
throw new GdalException("RasterMetaFile"); |
303 |
} catch (FileNotFoundException e) { |
304 |
throw new GdalException("File Not Found"); |
305 |
} catch (IOException e) { |
306 |
throw new GdalException(""); |
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
public RasterProvider load() {
|
312 |
return this; |
313 |
} |
314 |
|
315 |
public boolean isOpen() { |
316 |
if(file != null && file.isOpen()) |
317 |
return true; |
318 |
return false; |
319 |
} |
320 |
|
321 |
public String translateFileName(String fileName) { |
322 |
if(fileName.endsWith("hdr")) |
323 |
return fileName.substring(0, fileName.lastIndexOf(".")); |
324 |
return fileName;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
|
329 |
* de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
|
330 |
* ha de estar en coordenadas del fichero.
|
331 |
*/
|
332 |
public void setView(Extent e) { |
333 |
viewRequest = new ExtentImpl(e);
|
334 |
} |
335 |
|
336 |
public Extent getView() {
|
337 |
return viewRequest;
|
338 |
} |
339 |
|
340 |
public double getWidth() { |
341 |
return file.width;
|
342 |
} |
343 |
|
344 |
public double getHeight() { |
345 |
return file.height;
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* Read a line from the file
|
350 |
* @param line
|
351 |
* @param band
|
352 |
* @return
|
353 |
* @throws InvalidSetViewException
|
354 |
* @throws FileNotOpenException
|
355 |
* @throws RasterDriverException
|
356 |
* @Deprecated This operation is deprecated because is not useful and in the future
|
357 |
* it will not be maintained. The abstract operation has dissapear
|
358 |
*/
|
359 |
public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
360 |
if(line > this.getHeight() || band > this.getBandCount()) |
361 |
throw new InvalidSetViewException("Request out of grid"); |
362 |
|
363 |
try{
|
364 |
return file.readCompleteLine(line, band);
|
365 |
}catch(GdalException e){
|
366 |
throw new RasterDriverException("Error reading data from Gdal library"); |
367 |
} |
368 |
} |
369 |
|
370 |
public Object readBlock(int pos, int blockHeight, double scale) |
371 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
|
372 |
if(pos < 0) |
373 |
throw new InvalidSetViewException("Request out of grid"); |
374 |
|
375 |
if((pos + blockHeight) > getHeight())
|
376 |
blockHeight = Math.abs(((int)getHeight()) - pos); |
377 |
try{
|
378 |
return file.readBlock(pos, blockHeight, scale);
|
379 |
}catch(GdalException e){
|
380 |
throw new RasterDriverException("Error reading data from Gdal library"); |
381 |
} |
382 |
} |
383 |
|
384 |
public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
385 |
if(file != null){ |
386 |
if(x < 0 || y < 0 || x >= file.width || y >= file.height) |
387 |
throw new InvalidSetViewException("Request out of grid"); |
388 |
Object[] data = file.getData(x, y); |
389 |
return data[band];
|
390 |
} |
391 |
throw new FileNotOpenException("GdalNative not exist"); |
392 |
} |
393 |
|
394 |
@Override
|
395 |
public void loadBuffer(SpiRasterQuery q) |
396 |
throws ProcessInterruptedException, RasterDriverException {
|
397 |
setView(q.getAdjustedRequestBoundingBox()); |
398 |
|
399 |
try {
|
400 |
file.readWindow(q.getBufferForProviders(), |
401 |
q.getBandList(), |
402 |
q.getAdjustedRequestBoundingBox(), |
403 |
q.getAdjustedRequestPxWindow(), |
404 |
q.getTaskStatus()); |
405 |
} catch (GdalException e) {
|
406 |
throw new RasterDriverException("Error reading data", e); |
407 |
} |
408 |
|
409 |
} |
410 |
|
411 |
public int getBlockSize(){ |
412 |
if(file != null) |
413 |
return file.getBlockSize();
|
414 |
else
|
415 |
return 0; |
416 |
} |
417 |
|
418 |
public DataStoreMetadata getMetadata() {
|
419 |
if(file != null) |
420 |
return file.metadata;
|
421 |
else
|
422 |
return null; |
423 |
} |
424 |
|
425 |
public Transparency getTransparency() { |
426 |
return file.fileTransparency;
|
427 |
} |
428 |
|
429 |
public boolean isGeoreferenced() { |
430 |
if(file != null) |
431 |
return file.georeferenced;
|
432 |
else
|
433 |
return false; |
434 |
} |
435 |
|
436 |
/**
|
437 |
* Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
438 |
* el valor de esta variable cada vez que dibuja.
|
439 |
* @return true si se ha supersampleado y false si no se ha hecho.
|
440 |
*/
|
441 |
public boolean isSupersampling() { |
442 |
if(file != null) |
443 |
return file.isSupersampling;
|
444 |
else
|
445 |
return false; |
446 |
} |
447 |
|
448 |
public GdalNative getNative(){
|
449 |
return file;
|
450 |
} |
451 |
|
452 |
public void setAffineTransform(AffineTransform t){ |
453 |
super.setAffineTransform(t);
|
454 |
file.setExternalTransform(t); |
455 |
} |
456 |
|
457 |
public int getOverviewCount(int band) throws BandAccessException, RasterDriverException { |
458 |
if(band >= getBandCount())
|
459 |
throw new BandAccessException("Wrong band"); |
460 |
try {
|
461 |
return file.getRasterBand(band + 1).getOverviewCount(); |
462 |
} catch (GdalException e) {
|
463 |
throw new RasterDriverException(""); |
464 |
} |
465 |
} |
466 |
|
467 |
public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException { |
468 |
if (band >= getBandCount())
|
469 |
throw new BandAccessException("Wrong band"); |
470 |
try {
|
471 |
if (overview >= file.getRasterBand(band + 1).getOverviewCount()) |
472 |
throw new BandAccessException("Wrong overview count"); |
473 |
return file.getRasterBand(band + 1).getOverview(overview).getRasterBandXSize(); |
474 |
} catch (GdalException e) {
|
475 |
throw new RasterDriverException(""); |
476 |
} |
477 |
} |
478 |
|
479 |
public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException { |
480 |
if (band >= getBandCount())
|
481 |
throw new BandAccessException("Wrong band"); |
482 |
try {
|
483 |
if (overview >= file.getRasterBand(band + 1).getOverviewCount()) |
484 |
throw new BandAccessException("Wrong overview count"); |
485 |
return file.getRasterBand(band + 1).getOverview(overview).getRasterBandYSize(); |
486 |
} catch (GdalException e) {
|
487 |
throw new RasterDriverException(""); |
488 |
} |
489 |
} |
490 |
|
491 |
public boolean isOverviewsSupported() { |
492 |
return true; |
493 |
} |
494 |
|
495 |
public boolean isReproyectable() { |
496 |
return true; |
497 |
} |
498 |
|
499 |
public boolean needEnhanced() { |
500 |
return (getDataType()[0] != Buffer.TYPE_BYTE); |
501 |
//Desconozco pq raz?n estaba esta condici?n. Quiz?s haya que volver a a?adirla
|
502 |
//Eliminada 30/5/2013
|
503 |
//|| (getBandCount() == 1 && getDataType()[0] == Buffer.TYPE_BYTE));
|
504 |
} |
505 |
|
506 |
public String getProviderName() { |
507 |
return NAME;
|
508 |
} |
509 |
|
510 |
public void setStatus(RasterProvider provider) { |
511 |
if(provider instanceof GdalProvider) { |
512 |
//Not implemented yet
|
513 |
} |
514 |
} |
515 |
|
516 |
public TileServer getTileServer() {
|
517 |
if(tileServer == null) { |
518 |
DefaultRasterStore store = new DefaultRasterStore();
|
519 |
store.setProvider(this);
|
520 |
tileServer = new FileTileServer(store);
|
521 |
} |
522 |
return tileServer;
|
523 |
} |
524 |
|
525 |
public void close() { |
526 |
if(file != null){ |
527 |
file.dispose(); |
528 |
} |
529 |
try {
|
530 |
finalize(); |
531 |
} catch (Throwable e) { |
532 |
} |
533 |
} |
534 |
|
535 |
protected void finalize() throws Throwable { |
536 |
file = null;
|
537 |
viewRequest = null;
|
538 |
if(formatList != null) { |
539 |
for (int i = 0; i < formatList.length; i++) { |
540 |
formatList[i] = null;
|
541 |
} |
542 |
formatList = null;
|
543 |
} |
544 |
super.finalize();
|
545 |
} |
546 |
} |