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 @ 1367
History | View | Annotate | Download (24.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.awt.geom.Point2D; |
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.io.BufferedReader; |
28 |
import java.io.File; |
29 |
import java.io.FileNotFoundException; |
30 |
import java.io.FileReader; |
31 |
import java.io.IOException; |
32 |
|
33 |
import org.gvsig.fmap.dal.DALFileLocator; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataStore; |
36 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
37 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
38 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
39 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
40 |
import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
41 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
42 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
43 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
44 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
45 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
46 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
47 |
import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
48 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
49 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
50 |
import org.gvsig.jgdal.GdalException; |
51 |
import org.gvsig.metadata.MetadataLocator; |
52 |
import org.gvsig.raster.cache.tile.provider.TileListener; |
53 |
import org.gvsig.raster.cache.tile.provider.TileServer; |
54 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
55 |
import org.gvsig.raster.impl.provider.DefaultRasterProvider; |
56 |
import org.gvsig.raster.impl.provider.RasterProvider; |
57 |
import org.gvsig.raster.impl.provider.tile.FileTileServer; |
58 |
import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
59 |
import org.gvsig.raster.impl.store.DefaultStoreFactory; |
60 |
import org.gvsig.raster.impl.store.properties.DataStoreMetadata; |
61 |
import org.gvsig.tools.ToolsLocator; |
62 |
import org.gvsig.tools.dynobject.DynObject; |
63 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
64 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
65 |
import org.gvsig.tools.task.TaskStatus; |
66 |
import org.slf4j.Logger; |
67 |
import org.slf4j.LoggerFactory; |
68 |
/**
|
69 |
* This class represents the data access for gdal formats.
|
70 |
*
|
71 |
* @author Luis W. Sevilla
|
72 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
73 |
*/
|
74 |
public class GdalProvider extends DefaultRasterProvider { |
75 |
public static String NAME = "Gdal Store"; |
76 |
public static String DESCRIPTION = "Gdal Raster file"; |
77 |
public static final String METADATA_DEFINITION_NAME = "GdalStore"; |
78 |
private static final Logger logger = LoggerFactory.getLogger(GdalProvider.class); |
79 |
|
80 |
public static final String FORMAT_GTiff = "GTiff"; |
81 |
public static final String FORMAT_VRT = "VRT"; |
82 |
public static final String FORMAT_NITF = "NITF"; |
83 |
public static final String FORMAT_HFA = "HFA"; |
84 |
public static final String FORMAT_ELAS = "ELAS"; |
85 |
public static final String FORMAT_MEM = "MEM"; |
86 |
public static final String FORMAT_BMP = "BMP"; |
87 |
public static final String FORMAT_PCIDSK = "PCIDSK"; |
88 |
public static final String FORMAT_ILWIS = "ILWIS"; |
89 |
public static final String FORMAT_HDF4 = "HDF4Image"; |
90 |
public static final String FORMAT_PNM = "PNM"; |
91 |
public static final String FORMAT_ENVI = "ENVI"; |
92 |
public static final String FORMAT_EHDR = "EHdr"; |
93 |
public static final String FORMAT_PAUX = "PAux"; |
94 |
public static final String FORMAT_MFF = "MFF"; |
95 |
public static final String FORMAT_MFF2 = "MFF2"; |
96 |
public static final String FORMAT_BT = "BT"; |
97 |
public static final String FORMAT_IDA = "IDA"; |
98 |
public static final String FORMAT_RMF = "RMF"; |
99 |
public static final String FORMAT_RST = "RST"; |
100 |
public static final String FORMAT_LEVELLER = "Leveller"; |
101 |
public static final String FORMAT_TERRAGEN = "Terragen"; |
102 |
public static final String FORMAT_ERS = "ERS"; |
103 |
public static final String FORMAT_INGR = "INGR"; |
104 |
public static final String FORMAT_GSAG = "GSAG"; |
105 |
public static final String FORMAT_GSBG = "GSBG"; |
106 |
public static final String FORMAT_ADRG = "ADRG"; |
107 |
public static final int BAND_HEIGHT = 64; |
108 |
protected GdalNative file = null; |
109 |
private Extent viewRequest = null; |
110 |
protected static String[] formatList = null; |
111 |
|
112 |
public static void register() { |
113 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
114 |
registerFormats(); |
115 |
|
116 |
ExtensionPoint point = extensionPoints.get("DefaultRasterProvider");
|
117 |
point.append("reader", GdalProvider.NAME, GdalProvider.class);
|
118 |
|
119 |
RasterLocator.getManager().registerFileProvidersTiled(GdalProvider.class); |
120 |
|
121 |
DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
122 |
if (dataman != null && !dataman.getStoreProviders().contains(NAME)) { |
123 |
dataman.registerStoreProvider(NAME, |
124 |
GdalProvider.class, GdalDataParameters.class); |
125 |
} |
126 |
|
127 |
if(DALFileLocator.getFilesystemServerExplorerManager() != null) |
128 |
DALFileLocator.getFilesystemServerExplorerManager().registerProvider( |
129 |
NAME, DESCRIPTION, |
130 |
GdalFilesystemServerExplorer.class); |
131 |
|
132 |
dataman.registerStoreFactory(NAME, DefaultStoreFactory.class); |
133 |
} |
134 |
|
135 |
private static void registerFormats() { |
136 |
formatList = new String[] { |
137 |
"bmp",
|
138 |
"gif",
|
139 |
"tif",
|
140 |
"tiff",
|
141 |
"jpg",
|
142 |
"jpeg",
|
143 |
"png",
|
144 |
"vrt",
|
145 |
"dat", // Envi |
146 |
"lan", // Erdas |
147 |
"gis", // Erdas |
148 |
"img", // Erdas |
149 |
"pix", // PCI Geomatics |
150 |
"aux", // PCI Geomatics |
151 |
"adf", // ESRI Grids |
152 |
"mpr", // Ilwis |
153 |
"mpl", // Ilwis |
154 |
"map", // PC Raster |
155 |
"asc",
|
156 |
"pgm", //Ficheros PNM en escala de grises |
157 |
"ppm", //Ficheros PNM en RGB |
158 |
"rst", //IDRISIS |
159 |
"rmf", //Raster Matrix Format |
160 |
"nos",
|
161 |
"kap",
|
162 |
"hdr",
|
163 |
"raw",
|
164 |
"ers",
|
165 |
"xml"};
|
166 |
for (int i = 0; i < formatList.length; i++) |
167 |
RasterLocator.getManager().addFormat(formatList[i], GdalProvider.class); |
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
* @see org.gvsig.raster.impl.provider.RasterProvider#getFormatList()
|
173 |
*/
|
174 |
public String[] getFormatList() { |
175 |
return formatList;
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Returns true if the extension is supported and false if doesn't
|
180 |
* @param ext
|
181 |
* @return
|
182 |
*/
|
183 |
public boolean isExtensionSupported(String ext) { |
184 |
if(ext.indexOf(".") != -1) |
185 |
ext = ext.substring(ext.lastIndexOf(".") + 1, ext.length()); |
186 |
for (int i = 0; i < formatList.length; i++) { |
187 |
if(formatList[i].compareTo(ext) == 0) |
188 |
return true; |
189 |
} |
190 |
return false; |
191 |
} |
192 |
|
193 |
/**
|
194 |
* Mandatory constructor to instantiate an empty provider
|
195 |
*/
|
196 |
public GdalProvider() {
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Constructor. Abre el dataset.
|
201 |
* @param proj Proyecci?n
|
202 |
* @param fName Nombre del fichero
|
203 |
* @throws NotSupportedExtensionException
|
204 |
*/
|
205 |
public GdalProvider(String params) throws NotSupportedExtensionException { |
206 |
super(params);
|
207 |
if(params instanceof String) { |
208 |
GdalDataParameters p = new GdalDataParameters();
|
209 |
p.setURI((String)params);
|
210 |
super.init(p, null, ToolsLocator.getDynObjectManager() |
211 |
.createDynObject( |
212 |
MetadataLocator.getMetadataManager().getDefinition( |
213 |
DataStore.METADATA_DEFINITION_NAME))); |
214 |
init(p, null);
|
215 |
} |
216 |
} |
217 |
|
218 |
public GdalProvider (GdalDataParameters params,
|
219 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
220 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
221 |
.createDynObject( |
222 |
MetadataLocator.getMetadataManager().getDefinition( |
223 |
DataStore.METADATA_DEFINITION_NAME))); |
224 |
init(params, storeServices); |
225 |
} |
226 |
|
227 |
public GdalProvider(AbstractRasterDataParameters params,
|
228 |
DataStoreProviderServices storeServices, DynObject metadata) { |
229 |
super(params, storeServices, metadata);
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* Creates file references and loads structures with the information and metadata
|
234 |
* @param params load parameters
|
235 |
* @throws NotSupportedExtensionException
|
236 |
*/
|
237 |
public void init (AbstractRasterDataParameters params, |
238 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
239 |
try {
|
240 |
setParam(storeServices, params); |
241 |
validRmf(params.getURI()); |
242 |
file = new GdalNative(translateFileName(params.getURI()));
|
243 |
setColorInterpretation(file.colorInterpr); |
244 |
setColorTable(file.palette); |
245 |
noData = file.getNoDataValue(); |
246 |
wktProjection = file.getProjectionRef(); |
247 |
if(wktProjection != null && wktProjection != "") { |
248 |
try {
|
249 |
proj = RasterLocator.getManager().getCRSUtils().convertWktToIProjection(wktProjection); |
250 |
} catch (Exception e) { |
251 |
logger.info("Error reading WKT from the raster provider", e);
|
252 |
} |
253 |
} |
254 |
//CrsWkt crs = new CrsWkt(wktProjection);
|
255 |
//IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
256 |
ownTransformation = file.getOwnTransformation(); |
257 |
externalTransformation = (AffineTransform)ownTransformation.clone();
|
258 |
load(); |
259 |
|
260 |
if(file != null) |
261 |
bandCount = file.getRasterCount(); |
262 |
} catch (GdalException e) {
|
263 |
throw new NotSupportedExtensionException("Extension not supported", e); |
264 |
} catch(Exception e) { |
265 |
System.out.println("Error en GdalOpen"); |
266 |
e.printStackTrace(); |
267 |
file = null;
|
268 |
} |
269 |
|
270 |
//Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
271 |
int[] dt = new int[file.getDataType().length]; |
272 |
for (int i = 0; i < dt.length; i++) |
273 |
dt[i] = rasterUtil.getRasterBufTypeFromGdalType(file.getDataType()[i]); |
274 |
setDataType(dt); |
275 |
|
276 |
super.init();
|
277 |
|
278 |
try {
|
279 |
loadFromRmf(getRmfBlocksManager()); |
280 |
} catch (ParsingException e) {
|
281 |
//No lee desde rmf
|
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 |
FileReader fr;
|
294 |
try {
|
295 |
fr = new FileReader(f); |
296 |
BufferedReader br = new BufferedReader(fr); |
297 |
char[] buffer = new char[5]; |
298 |
br.read(buffer); |
299 |
StringBuffer st = new StringBuffer(new String(buffer)); |
300 |
if(st.toString().equals("<?xml")) |
301 |
throw new GdalException("RasterMetaFile"); |
302 |
} catch (FileNotFoundException e) { |
303 |
throw new GdalException("File Not Found"); |
304 |
} catch (IOException e) { |
305 |
throw new GdalException(""); |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
/*
|
311 |
* (non-Javadoc)
|
312 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
|
313 |
*/
|
314 |
public RasterProvider load() {
|
315 |
return this; |
316 |
} |
317 |
|
318 |
/*
|
319 |
* (non-Javadoc)
|
320 |
* @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
|
321 |
*/
|
322 |
public boolean isOpen() { |
323 |
if(file != null && file.isOpen()) |
324 |
return true; |
325 |
return false; |
326 |
} |
327 |
|
328 |
/*
|
329 |
* (non-Javadoc)
|
330 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
|
331 |
*/
|
332 |
public String translateFileName(String fileName) { |
333 |
if(fileName.endsWith("hdr")) |
334 |
return fileName.substring(0, fileName.lastIndexOf(".")); |
335 |
return fileName;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
|
340 |
* de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
|
341 |
* ha de estar en coordenadas del fichero.
|
342 |
*/
|
343 |
public void setView(Extent e) { |
344 |
viewRequest = new ExtentImpl(e);
|
345 |
} |
346 |
|
347 |
/*
|
348 |
* (non-Javadoc)
|
349 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
|
350 |
*/
|
351 |
public Extent getView() {
|
352 |
return viewRequest;
|
353 |
} |
354 |
|
355 |
/*
|
356 |
* (non-Javadoc)
|
357 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
|
358 |
*/
|
359 |
public double getWidth() { |
360 |
return file.width;
|
361 |
} |
362 |
|
363 |
/*
|
364 |
* (non-Javadoc)
|
365 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
|
366 |
*/
|
367 |
public double getHeight() { |
368 |
return file.height;
|
369 |
} |
370 |
|
371 |
/**
|
372 |
* Read a line from the file
|
373 |
* @param line
|
374 |
* @param band
|
375 |
* @return
|
376 |
* @throws InvalidSetViewException
|
377 |
* @throws FileNotOpenException
|
378 |
* @throws RasterDriverException
|
379 |
* @Deprecated This operation is deprecated because is not useful and in the future
|
380 |
* it will not be maintained. The abstract operation has dissapear
|
381 |
*/
|
382 |
public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
383 |
if(line > this.getHeight() || band > this.getBandCount()) |
384 |
throw new InvalidSetViewException("Request out of grid"); |
385 |
|
386 |
try{
|
387 |
return file.readCompleteLine(line, band);
|
388 |
}catch(GdalException e){
|
389 |
throw new RasterDriverException("Error reading data from Gdal library"); |
390 |
} |
391 |
} |
392 |
|
393 |
/*
|
394 |
* (non-Javadoc)
|
395 |
* @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
|
396 |
*/
|
397 |
public Object readBlock(int pos, int blockHeight, double scale) |
398 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
|
399 |
if(pos < 0) |
400 |
throw new InvalidSetViewException("Request out of grid"); |
401 |
|
402 |
if((pos + blockHeight) > getHeight())
|
403 |
blockHeight = Math.abs(((int)getHeight()) - pos); |
404 |
try{
|
405 |
return file.readBlock(pos, blockHeight, scale);
|
406 |
}catch(GdalException e){
|
407 |
throw new RasterDriverException("Error reading data from Gdal library"); |
408 |
} |
409 |
} |
410 |
|
411 |
/*
|
412 |
* (non-Javadoc)
|
413 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
|
414 |
*/
|
415 |
public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
416 |
if(file != null){ |
417 |
if(x < 0 || y < 0 || x >= file.width || y >= file.height) |
418 |
throw new InvalidSetViewException("Request out of grid"); |
419 |
Object[] data = file.getData(x, y); |
420 |
return data[band];
|
421 |
} |
422 |
throw new FileNotOpenException("GdalNative not exist"); |
423 |
} |
424 |
|
425 |
/*
|
426 |
* (non-Javadoc)
|
427 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
|
428 |
*/
|
429 |
public void getWindow(Extent ex, int bufWidth, int bufHeight, |
430 |
BandList bandList, TileListener listener, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
|
431 |
|
432 |
} |
433 |
|
434 |
/*
|
435 |
* (non-Javadoc)
|
436 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
437 |
*/
|
438 |
public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf, TaskStatus status) |
439 |
throws ProcessInterruptedException, RasterDriverException {
|
440 |
setView(ex); |
441 |
|
442 |
try {
|
443 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), |
444 |
rasterBuf.getWidth(), rasterBuf.getHeight(), true, status);
|
445 |
} catch (GdalException e) {
|
446 |
throw new RasterDriverException("Error reading data", e); |
447 |
} |
448 |
|
449 |
return rasterBuf;
|
450 |
} |
451 |
|
452 |
/*
|
453 |
* (non-Javadoc)
|
454 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
|
455 |
*/
|
456 |
public Buffer getWindow(double ulx, double uly, double w, double h, |
457 |
BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException { |
458 |
//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
|
459 |
//tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
|
460 |
Extent ext = getExtent(); |
461 |
Point2D pInit = rasterToWorld(new Point2D.Double(0, 0)); |
462 |
Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight())); |
463 |
double wRaster = Math.abs(pEnd.getX() - pInit.getX()); |
464 |
double hRaster = Math.abs(pEnd.getY() - pInit.getY()); |
465 |
double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
|
466 |
double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
|
467 |
|
468 |
Extent selectedExtent = new ExtentImpl(ulx, uly, lrx, lry);
|
469 |
setView(selectedExtent); |
470 |
|
471 |
try {
|
472 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), |
473 |
rasterBuf.getWidth(), rasterBuf.getHeight(), adjustToExtent, status); |
474 |
} catch (GdalException e) {
|
475 |
throw new RasterDriverException("Error reading data"); |
476 |
} |
477 |
|
478 |
return rasterBuf;
|
479 |
} |
480 |
|
481 |
/*
|
482 |
* (non-Javadoc)
|
483 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
|
484 |
*/
|
485 |
public Buffer getWindow(Extent extent, int bufWidth, int bufHeight, |
486 |
BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException { |
487 |
//draw method
|
488 |
setView(extent); |
489 |
|
490 |
double width = 0; |
491 |
double height = 0; |
492 |
|
493 |
Point2D ul = new Point2D.Double(viewRequest.getULX(), viewRequest.getULY()); |
494 |
Point2D lr = new Point2D.Double(viewRequest.getLRX(), viewRequest.getLRY()); |
495 |
ul = worldToRaster(ul); |
496 |
lr = worldToRaster(lr); |
497 |
ul.setLocation(ul.getX() < 0 ? 1 : ul.getX(), ul.getY() < 0 ? 1 : ul.getY()); |
498 |
lr.setLocation(lr.getX() < 0 ? 1 : lr.getX(), lr.getY() < 0 ? 1 : lr.getY()); |
499 |
|
500 |
ul.setLocation(ul.getX() - 0.5, ul.getY() - 0.5); |
501 |
lr.setLocation(lr.getX() - 0.5, lr.getY() - 0.5); |
502 |
|
503 |
adjustPoints(ul, lr); |
504 |
|
505 |
width = Math.abs(((int)lr.getX()) - ((int)ul.getX())) + 1; |
506 |
height = Math.abs(((int)lr.getY()) - ((int)ul.getY())) + 1; |
507 |
|
508 |
try {
|
509 |
file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), |
510 |
width, height, bufWidth, bufHeight, adjustToExtent, status); |
511 |
} catch (GdalException e) {
|
512 |
throw new RasterDriverException("Error reading data"); |
513 |
} |
514 |
|
515 |
return rasterBuf;
|
516 |
} |
517 |
|
518 |
|
519 |
private void adjustPoints(Point2D ul, Point2D lr) { |
520 |
double a = (ul.getX() - (int)ul.getX()); |
521 |
double b = (ul.getY() - (int)ul.getY()); |
522 |
ul.setLocation( (a > 0.95 || a < 0.05) ? Math.round(ul.getX()) : ul.getX(), |
523 |
(b > 0.95 || b < 0.05) ? Math.round(ul.getY()) : ul.getY()); |
524 |
lr.setLocation( (a > 0.95 || a < 0.05) ? Math.round(lr.getX()) : lr.getX(), |
525 |
(b > 0.95 || b < 0.05) ? Math.round(lr.getY()) : lr.getY()); |
526 |
} |
527 |
|
528 |
/*
|
529 |
* (non-Javadoc)
|
530 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
531 |
*/
|
532 |
public Buffer getWindow(int x, int y, int w, int h, |
533 |
BandList bandList, Buffer rasterBuf, TaskStatus status)
|
534 |
throws ProcessInterruptedException, RasterDriverException {
|
535 |
try {
|
536 |
setView( |
537 |
new ExtentImpl( rasterUtil.getMapRectFromPxRect(getExtent().toRectangle2D(),
|
538 |
getWidth(), |
539 |
getHeight(), |
540 |
new Rectangle2D.Double(x, y, w, h))) |
541 |
); |
542 |
file.readWindow(rasterBuf, bandList, x, y, w, h, status); |
543 |
} catch (GdalException e) {
|
544 |
throw new RasterDriverException("Error reading data"); |
545 |
} |
546 |
return rasterBuf;
|
547 |
} |
548 |
|
549 |
/*
|
550 |
* (non-Javadoc)
|
551 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
|
552 |
*/
|
553 |
public int getBlockSize(){ |
554 |
if(file != null) |
555 |
return file.getBlockSize();
|
556 |
else
|
557 |
return 0; |
558 |
} |
559 |
|
560 |
/*
|
561 |
* (non-Javadoc)
|
562 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
|
563 |
*/
|
564 |
public DataStoreMetadata getMetadata() {
|
565 |
if(file != null) |
566 |
return file.metadata;
|
567 |
else
|
568 |
return null; |
569 |
} |
570 |
|
571 |
/*
|
572 |
* (non-Javadoc)
|
573 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
|
574 |
*/
|
575 |
public Transparency getTransparency() { |
576 |
return file.fileTransparency;
|
577 |
} |
578 |
|
579 |
/*
|
580 |
* (non-Javadoc)
|
581 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
|
582 |
*/
|
583 |
public boolean isGeoreferenced() { |
584 |
if(file != null) |
585 |
return file.georeferenced;
|
586 |
else
|
587 |
return false; |
588 |
} |
589 |
|
590 |
/**
|
591 |
* Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
592 |
* el valor de esta variable cada vez que dibuja.
|
593 |
* @return true si se ha supersampleado y false si no se ha hecho.
|
594 |
*/
|
595 |
public boolean isSupersampling() { |
596 |
if(file != null) |
597 |
return file.isSupersampling;
|
598 |
else
|
599 |
return false; |
600 |
} |
601 |
|
602 |
public GdalNative getNative(){
|
603 |
return file;
|
604 |
} |
605 |
|
606 |
/*
|
607 |
* (non-Javadoc)
|
608 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
|
609 |
*/
|
610 |
public void setAffineTransform(AffineTransform t){ |
611 |
super.setAffineTransform(t);
|
612 |
file.setExternalTransform(t); |
613 |
} |
614 |
|
615 |
/*
|
616 |
* (non-Javadoc)
|
617 |
* @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
|
618 |
*/
|
619 |
public int getOverviewCount(int band) throws BandAccessException, RasterDriverException { |
620 |
if(band >= getBandCount())
|
621 |
throw new BandAccessException("Wrong band"); |
622 |
try {
|
623 |
return file.getRasterBand(band + 1).getOverviewCount(); |
624 |
} catch (GdalException e) {
|
625 |
throw new RasterDriverException(""); |
626 |
} |
627 |
} |
628 |
|
629 |
/*
|
630 |
* (non-Javadoc)
|
631 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
|
632 |
*/
|
633 |
public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException { |
634 |
if (band >= getBandCount())
|
635 |
throw new BandAccessException("Wrong band"); |
636 |
try {
|
637 |
if (overview >= file.getRasterBand(band + 1).getOverviewCount()) |
638 |
throw new BandAccessException("Wrong overview count"); |
639 |
return file.getRasterBand(band + 1).getOverview(overview).getRasterBandXSize(); |
640 |
} catch (GdalException e) {
|
641 |
throw new RasterDriverException(""); |
642 |
} |
643 |
} |
644 |
|
645 |
/*
|
646 |
* (non-Javadoc)
|
647 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
|
648 |
*/
|
649 |
public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException { |
650 |
if (band >= getBandCount())
|
651 |
throw new BandAccessException("Wrong band"); |
652 |
try {
|
653 |
if (overview >= file.getRasterBand(band + 1).getOverviewCount()) |
654 |
throw new BandAccessException("Wrong overview count"); |
655 |
return file.getRasterBand(band + 1).getOverview(overview).getRasterBandYSize(); |
656 |
} catch (GdalException e) {
|
657 |
throw new RasterDriverException(""); |
658 |
} |
659 |
} |
660 |
|
661 |
/*
|
662 |
* (non-Javadoc)
|
663 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isOverviewsSupported()
|
664 |
*/
|
665 |
public boolean isOverviewsSupported() { |
666 |
return true; |
667 |
} |
668 |
|
669 |
/*
|
670 |
* (non-Javadoc)
|
671 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
|
672 |
*/
|
673 |
public boolean isReproyectable() { |
674 |
return true; |
675 |
} |
676 |
|
677 |
/*
|
678 |
* (non-Javadoc)
|
679 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#needEnhanced()
|
680 |
*/
|
681 |
public boolean needEnhanced() { |
682 |
return (getDataType()[0] != Buffer.TYPE_BYTE || |
683 |
(getBandCount() == 1 && getDataType()[0] == Buffer.TYPE_BYTE)); |
684 |
} |
685 |
|
686 |
/*
|
687 |
* (non-Javadoc)
|
688 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
|
689 |
*/
|
690 |
public String getName() { |
691 |
return NAME;
|
692 |
} |
693 |
|
694 |
/*
|
695 |
* (non-Javadoc)
|
696 |
* @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
|
697 |
*/
|
698 |
public void setStatus(RasterProvider provider) { |
699 |
if(provider instanceof GdalProvider) { |
700 |
//Not implemented yet
|
701 |
} |
702 |
} |
703 |
|
704 |
/*
|
705 |
* (non-Javadoc)
|
706 |
* @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
|
707 |
*/
|
708 |
public TileServer getTileServer() {
|
709 |
if(tileServer == null) |
710 |
tileServer = new FileTileServer(this); |
711 |
return tileServer;
|
712 |
} |
713 |
|
714 |
/*
|
715 |
* (non-Javadoc)
|
716 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
|
717 |
*/
|
718 |
public void close() { |
719 |
if(file != null){ |
720 |
file.dispose(); |
721 |
} |
722 |
try {
|
723 |
finalize(); |
724 |
} catch (Throwable e) { |
725 |
} |
726 |
} |
727 |
|
728 |
/*
|
729 |
* (non-Javadoc)
|
730 |
* @see java.lang.Object#finalize()
|
731 |
*/
|
732 |
protected void finalize() throws Throwable { |
733 |
file = null;
|
734 |
viewRequest = null;
|
735 |
if(formatList != null) { |
736 |
for (int i = 0; i < formatList.length; i++) { |
737 |
formatList[i] = null;
|
738 |
} |
739 |
formatList = null;
|
740 |
} |
741 |
super.finalize();
|
742 |
} |
743 |
} |