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