gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / provider / DefaultRasterProvider.java @ 149
History | View | Annotate | Download (34.3 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.impl.provider; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.NoninvertibleTransformException; |
26 |
import java.awt.geom.Point2D; |
27 |
import java.io.File; |
28 |
import java.lang.reflect.Constructor; |
29 |
import java.lang.reflect.InvocationTargetException; |
30 |
import java.util.Iterator; |
31 |
|
32 |
import org.cresques.cts.IProjection; |
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.DataManager; |
35 |
import org.gvsig.fmap.dal.DataServerExplorer; |
36 |
import org.gvsig.fmap.dal.coverage.RasterLibrary; |
37 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
38 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
39 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
40 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
41 |
import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
42 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
43 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
44 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
45 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
46 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
47 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
48 |
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException; |
49 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
50 |
import org.gvsig.fmap.dal.coverage.store.props.ColorTable; |
51 |
import org.gvsig.fmap.dal.coverage.store.props.Histogram; |
52 |
import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
53 |
import org.gvsig.fmap.dal.coverage.util.FileUtils; |
54 |
import org.gvsig.fmap.dal.coverage.util.RasterUtils; |
55 |
import org.gvsig.fmap.dal.exception.DataException; |
56 |
import org.gvsig.fmap.dal.exception.OpenException; |
57 |
import org.gvsig.fmap.dal.exception.ReadException; |
58 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
59 |
import org.gvsig.fmap.dal.raster.impl.DefaultCoverageStore; |
60 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
61 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
62 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
63 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
64 |
import org.gvsig.raster.impl.datastruct.DefaultNoData; |
65 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
66 |
import org.gvsig.raster.impl.datastruct.serializer.ColorTableRmfSerializer; |
67 |
import org.gvsig.raster.impl.datastruct.serializer.NoDataRmfSerializer; |
68 |
import org.gvsig.raster.impl.store.AbstractRasterStoreParameters; |
69 |
import org.gvsig.raster.impl.store.GeoInfo; |
70 |
import org.gvsig.raster.impl.store.properties.DatasetColorInterpretation; |
71 |
import org.gvsig.raster.impl.store.properties.DatasetHistogram; |
72 |
import org.gvsig.raster.impl.store.properties.DatasetMetadata; |
73 |
import org.gvsig.raster.impl.store.properties.DatasetStatistics; |
74 |
import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
75 |
import org.gvsig.raster.impl.store.rmf.RmfBlocksManager; |
76 |
import org.gvsig.raster.impl.store.serializer.ColorInterpretationRmfSerializer; |
77 |
import org.gvsig.raster.impl.store.serializer.GeoInfoRmfSerializer; |
78 |
import org.gvsig.raster.impl.store.serializer.ProjectionRmfSerializer; |
79 |
import org.gvsig.raster.util.DefaultProviderServices; |
80 |
import org.gvsig.tools.ToolsLocator; |
81 |
import org.gvsig.tools.dynobject.DynObject; |
82 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
83 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
84 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
85 |
|
86 |
/**
|
87 |
* Manejador de ficheros raster georeferenciados.
|
88 |
*
|
89 |
* Esta clase abstracta es el ancestro de todas las clases que proporcionan
|
90 |
* soporte para ficheros raster georeferenciados.<br>
|
91 |
* Actua tambien como una 'Fabrica', ocultando al cliente la manera en que
|
92 |
* se ha implementado ese manejo. Una clase nueva que soportara un nuevo
|
93 |
* tipo de raster tendr?a que registrar su extensi?n o extensiones usando
|
94 |
* el m?todo @see registerExtension.<br>
|
95 |
*
|
96 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
97 |
*/
|
98 |
public abstract class DefaultRasterProvider extends GeoInfo { |
99 |
/**
|
100 |
* Flags que representan a las bandas visualizables
|
101 |
*/
|
102 |
public static final int RED_BAND = 0x01; |
103 |
public static final int GREEN_BAND = 0x02; |
104 |
public static final int BLUE_BAND = 0x04; |
105 |
public static final int ALPHA_BAND = 0x08; |
106 |
|
107 |
protected int bandCount = 1; |
108 |
private int[] dataType = null; |
109 |
protected double noData = 0; |
110 |
protected String wktProjection = ""; |
111 |
|
112 |
protected DatasetStatistics stats = new DatasetStatistics(this); |
113 |
protected DatasetHistogram histogram = null; |
114 |
protected AbstractRasterStoreParameters param = null; |
115 |
protected RmfBlocksManager rmfBlocksManager = null; |
116 |
protected ColorTable colorTable = null; |
117 |
protected DatasetColorInterpretation colorInterpretation = null; |
118 |
|
119 |
/**
|
120 |
* Indica si el valor NoData esta activo
|
121 |
*/
|
122 |
protected boolean noDataEnabled = false; |
123 |
protected FileUtils fileUtil = RasterLocator.getManager().getFileUtils();
|
124 |
protected RasterUtils rasterUtil = RasterLocator.getManager().getRasterUtils();
|
125 |
|
126 |
public DefaultRasterProvider(AbstractRasterStoreParameters params,
|
127 |
DataStoreProviderServices storeServices, DynObject metadata) { |
128 |
super(params, storeServices, metadata);
|
129 |
if(params.getFileName() != null) { |
130 |
File f = new File(params.getFileName()); |
131 |
if(f.exists())
|
132 |
setFileSize(f.length()); |
133 |
} |
134 |
} |
135 |
|
136 |
public DefaultRasterProvider(String params) { |
137 |
super(params);
|
138 |
} |
139 |
|
140 |
public DefaultRasterProvider() {
|
141 |
super(null, null, null); |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Acciones de inicilizaci?n comunes a todos los drivers.
|
146 |
* Este m?todo debe ser llamado explicitamente por el constructor de cada driver.
|
147 |
* Estas son acciones de inicializaci?n que se ejecutan despu?s del constructor de cada driver.
|
148 |
* Las acciones que hayan de ser realizadas antes se definen en el constructor de RasterDataset.
|
149 |
*/
|
150 |
protected void init() { |
151 |
} |
152 |
|
153 |
/*
|
154 |
* (non-Javadoc)
|
155 |
* @see java.lang.Object#clone()
|
156 |
*/
|
157 |
public RasterProvider cloneProvider() {
|
158 |
try {
|
159 |
DefaultRasterProvider provider = singleDatasetInstance(param); |
160 |
// Estas van por referencia
|
161 |
provider.histogram = histogram; |
162 |
provider.stats = stats; |
163 |
return provider;
|
164 |
} catch (NotSupportedExtensionException e) {
|
165 |
e.printStackTrace(); |
166 |
} catch (RasterDriverException e) {
|
167 |
e.printStackTrace(); |
168 |
} |
169 |
return null; |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Factoria para abrir distintos tipos de raster.
|
174 |
* @param fName Nombre del fichero.
|
175 |
*
|
176 |
* @return SingleDataset, o null si hay problemas.
|
177 |
*/
|
178 |
@SuppressWarnings("unchecked") |
179 |
public static DefaultRasterProvider singleDatasetInstance(AbstractRasterStoreParameters param) throws NotSupportedExtensionException, RasterDriverException { |
180 |
String idFormat = null; |
181 |
|
182 |
if (param.getFileName() != null) |
183 |
idFormat = RasterLocator.getManager().getFileUtils().getExtensionFromFileName(param.getFileName()); |
184 |
|
185 |
DefaultRasterProvider grf = null;
|
186 |
|
187 |
Class clase = null; |
188 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
189 |
|
190 |
if (idFormat != null) { |
191 |
ExtensionPoint point = extensionPoints.get("RasterReader");
|
192 |
Extension ext = point.get(idFormat); |
193 |
if (ext != null) |
194 |
clase = ext.getExtension(); |
195 |
} |
196 |
|
197 |
if (clase == null) { |
198 |
ExtensionPoint pointDefaultReader = extensionPoints.get("DefaultDriver");
|
199 |
Extension extReader = pointDefaultReader.get("reader");
|
200 |
clase = extReader.getExtension(); |
201 |
} |
202 |
|
203 |
Class [] args = {param.getClass(), DefaultCoverageStore.class}; |
204 |
try {
|
205 |
Constructor hazNuevo = clase.getConstructor(args);
|
206 |
Object [] args2 = {param, null}; |
207 |
grf = (DefaultRasterProvider) hazNuevo.newInstance(args2); |
208 |
} catch (SecurityException e) { |
209 |
throw new RasterDriverException("Error SecurityException in open", e); |
210 |
} catch (NoSuchMethodException e) { |
211 |
throw new RasterDriverException("Error NoSuchMethodException in open", e); |
212 |
} catch (IllegalArgumentException e) { |
213 |
throw new RasterDriverException("Error IllegalArgumentException in open", e); |
214 |
} catch (InstantiationException e) { |
215 |
throw new RasterDriverException("Error InstantiationException in open", e); |
216 |
} catch (IllegalAccessException e) { |
217 |
throw new RasterDriverException("Error IllegalAccessException in open", e); |
218 |
} catch (InvocationTargetException e) { |
219 |
throw new NotSupportedExtensionException("Error opening this image with " + clase, e); |
220 |
} |
221 |
return grf;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Factoria para abrir distintos tipos de raster.
|
226 |
* @param fName Nombre del fichero.
|
227 |
*
|
228 |
* @return SingleDataset, o null si hay problemas.
|
229 |
*/
|
230 |
@SuppressWarnings("unchecked") |
231 |
public static DefaultRasterProvider singleDatasetInstance(String param) throws NotSupportedExtensionException, RasterDriverException { |
232 |
String idFormat = null; |
233 |
|
234 |
if (param instanceof String) |
235 |
idFormat = RasterLocator.getManager().getFileUtils().getExtensionFromFileName(((String) param));
|
236 |
|
237 |
DefaultRasterProvider grf = null;
|
238 |
|
239 |
Class clase = null; |
240 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
241 |
if (idFormat != null) { |
242 |
ExtensionPoint point = extensionPoints.get("RasterReader");
|
243 |
Extension ext = point.get(idFormat); |
244 |
if (ext != null) |
245 |
clase = ext.getExtension(); |
246 |
} |
247 |
|
248 |
if (clase == null) { |
249 |
ExtensionPoint pointDefaultReader = extensionPoints.get("DefaultDriver");
|
250 |
Extension extReader = pointDefaultReader.get("reader");
|
251 |
clase = extReader.getExtension(); |
252 |
} |
253 |
|
254 |
Class [] args = {String.class}; |
255 |
try {
|
256 |
Constructor hazNuevo = clase.getConstructor(args);
|
257 |
Object [] args2 = {param}; |
258 |
grf = (DefaultRasterProvider) hazNuevo.newInstance(args2); |
259 |
} catch (SecurityException e) { |
260 |
throw new RasterDriverException("Error SecurityException in open", e); |
261 |
} catch (NoSuchMethodException e) { |
262 |
throw new RasterDriverException("Error NoSuchMethodException in open", e); |
263 |
} catch (IllegalArgumentException e) { |
264 |
throw new RasterDriverException("Error IllegalArgumentException in open", e); |
265 |
} catch (InstantiationException e) { |
266 |
throw new RasterDriverException("Error InstantiationException in open", e); |
267 |
} catch (IllegalAccessException e) { |
268 |
throw new RasterDriverException("Error IllegalAccessException in open", e); |
269 |
} catch (InvocationTargetException e) { |
270 |
throw new NotSupportedExtensionException("Error opening this image with " + clase, e); |
271 |
} |
272 |
return grf;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Carga un fichero raster. Puede usarse para calcular el extent e instanciar
|
277 |
* un objeto de este tipo.
|
278 |
*/
|
279 |
abstract public GeoInfo load(); |
280 |
|
281 |
/**
|
282 |
* Cierra el fichero y libera los recursos.
|
283 |
*/
|
284 |
abstract public void close(); |
285 |
|
286 |
/**
|
287 |
* Carga metadatos desde el fichero Rmf si estos existen
|
288 |
* @param fName Nombre del fichero
|
289 |
* @throws ParsingException
|
290 |
*/
|
291 |
protected void loadFromRmf(RmfBlocksManager manager) throws ParsingException { |
292 |
if (!manager.checkRmf())
|
293 |
return;
|
294 |
|
295 |
if (!new File(manager.getPath()).exists()) |
296 |
return;
|
297 |
|
298 |
GeoInfoRmfSerializer geoInfoSerializer = new GeoInfoRmfSerializer(this); |
299 |
ColorTableRmfSerializer colorTableSerializer = new ColorTableRmfSerializer();
|
300 |
NoDataRmfSerializer noDataSerializer = new NoDataRmfSerializer(new DefaultNoData(noData, RasterLibrary.NODATATYPE_LAYER)); |
301 |
ColorInterpretationRmfSerializer colorInterpSerializer = new ColorInterpretationRmfSerializer();
|
302 |
ProjectionRmfSerializer projectionRmfSerializer = new ProjectionRmfSerializer();
|
303 |
|
304 |
manager.addClient(geoInfoSerializer); |
305 |
manager.addClient(colorTableSerializer); |
306 |
manager.addClient(noDataSerializer); |
307 |
manager.addClient(colorInterpSerializer); |
308 |
manager.addClient(projectionRmfSerializer); |
309 |
|
310 |
manager.read(null);
|
311 |
|
312 |
manager.removeAllClients(); |
313 |
|
314 |
if (colorTableSerializer.getResult() != null) |
315 |
setColorTable((ColorTable) colorTableSerializer.getResult()); |
316 |
|
317 |
if (noDataSerializer.getResult() != null) { |
318 |
noData = ((DefaultNoData) noDataSerializer.getResult()).getValue(); |
319 |
noDataEnabled = (((DefaultNoData) noDataSerializer.getResult()).getType() != RasterLibrary.NODATATYPE_DISABLED); |
320 |
} |
321 |
|
322 |
if (colorInterpSerializer.getResult() != null) { |
323 |
DatasetColorInterpretation ci = (DatasetColorInterpretation) colorInterpSerializer.getResult(); |
324 |
setColorInterpretation(ci); |
325 |
if (ci.getBand(DatasetColorInterpretation.ALPHA_BAND) != -1) |
326 |
getTransparency().setTransparencyBand(ci.getBand(DatasetColorInterpretation.ALPHA_BAND)); |
327 |
} |
328 |
|
329 |
if (projectionRmfSerializer.getResult() != null) |
330 |
wktProjection = RasterLocator.getManager().getCRSUtils().convertIProjectionToWkt((IProjection) projectionRmfSerializer.getResult()); |
331 |
} |
332 |
|
333 |
/**
|
334 |
* Obtiene el ancho de la imagen
|
335 |
* @return Ancho de la imagen
|
336 |
*/
|
337 |
abstract public double getWidth(); |
338 |
|
339 |
/**
|
340 |
* Obtiene el ancho de la imagen
|
341 |
* @return Ancho de la imagen
|
342 |
*/
|
343 |
abstract public double getHeight(); |
344 |
|
345 |
/**
|
346 |
* Asigna un nuevo Extent
|
347 |
* @param e Extent
|
348 |
*/
|
349 |
public abstract void setView(Extent e); |
350 |
|
351 |
/**
|
352 |
* Obtiene el extent asignado
|
353 |
* @return Extent
|
354 |
*/
|
355 |
public abstract Extent getView(); |
356 |
|
357 |
/**
|
358 |
* Obtiene el valor del raster en la coordenada que se le pasa.
|
359 |
* El valor ser? Double, Int, Byte, etc. dependiendo del tipo de
|
360 |
* raster.
|
361 |
* @param x coordenada X
|
362 |
* @param y coordenada Y
|
363 |
* @return
|
364 |
*/
|
365 |
abstract public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException; |
366 |
|
367 |
/**
|
368 |
* Obtiene el n?nero de bandas del fichero
|
369 |
* @return Entero que representa el n?mero de bandas
|
370 |
*/
|
371 |
public int getBandCount() { |
372 |
return bandCount;
|
373 |
} |
374 |
|
375 |
/**
|
376 |
* @return Returns the dataType.
|
377 |
*/
|
378 |
public int[] getDataType() { |
379 |
return dataType;
|
380 |
} |
381 |
|
382 |
/**
|
383 |
* @param dataType The dataType to set.
|
384 |
*/
|
385 |
public void setDataType(int[] dataType) { |
386 |
this.dataType = dataType;
|
387 |
} |
388 |
|
389 |
/*
|
390 |
* (non-Javadoc)
|
391 |
* @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeX()
|
392 |
*/
|
393 |
public double getPixelSizeX() { |
394 |
return externalTransformation.getScaleX();
|
395 |
} |
396 |
|
397 |
/*
|
398 |
* (non-Javadoc)
|
399 |
* @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeY()
|
400 |
*/
|
401 |
public double getPixelSizeY() { |
402 |
return externalTransformation.getScaleY();
|
403 |
} |
404 |
|
405 |
/**
|
406 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
407 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
408 |
* pixeles de disco.
|
409 |
* @param ulx Posici?n X superior izquierda
|
410 |
* @param uly Posici?n Y superior izquierda
|
411 |
* @param lrx Posici?n X inferior derecha
|
412 |
* @param lry Posici?n Y inferior derecha
|
413 |
* @param rasterBuf Buffer de datos
|
414 |
* @param bandList
|
415 |
* @return Buffer de datos
|
416 |
*/
|
417 |
abstract public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry, BandList bandList, Buffer rasterBuf)throws ProcessInterruptedException, RasterDriverException; |
418 |
|
419 |
/**
|
420 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
421 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
422 |
* pixeles de disco.
|
423 |
* @param x Posici?n X superior izquierda
|
424 |
* @param y Posici?n Y superior izquierda
|
425 |
* @param w Ancho en coordenadas reales
|
426 |
* @param h Alto en coordenadas reales
|
427 |
* @param rasterBuf Buffer de datos
|
428 |
* @param bandList
|
429 |
* @param adjustToExtent Flag que dice si el extent solicitado debe ajustarse al extent del raster o no.
|
430 |
* @return Buffer de datos
|
431 |
*/
|
432 |
abstract public Buffer getWindowRaster(double x, double y, double w, double h, BandList bandList, Buffer rasterBuf, boolean adjustToExtent)throws ProcessInterruptedException, RasterDriverException; |
433 |
|
434 |
/**
|
435 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
436 |
* Se aplica supersampleo o subsampleo dependiendo del tama?o del buffer especificado.
|
437 |
*
|
438 |
* @param minX Posici?n m?nima X superior izquierda
|
439 |
* @param minY Posici?n m?nima Y superior izquierda
|
440 |
* @param maxX Posici?n m?xima X inferior derecha
|
441 |
* @param maxY Posici?n m?xima Y inferior derecha
|
442 |
* @param bufWidth Ancho del buffer de datos
|
443 |
* @param bufHeight Alto del buffer de datos
|
444 |
* @param rasterBuf Buffer de datos
|
445 |
* @param adjustToExtent Flag que dice si el extent solicitado debe ajustarse al extent del raster o no.
|
446 |
* @param bandList
|
447 |
* @return Buffer de datos
|
448 |
*/
|
449 |
abstract public Buffer getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent)throws ProcessInterruptedException, RasterDriverException; |
450 |
|
451 |
/**
|
452 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas pixel.
|
453 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
454 |
* pixeles de disco.
|
455 |
* @param x Posici?n X superior izquierda
|
456 |
* @param y Posici?n Y superior izquierda
|
457 |
* @param w Ancho en coordenadas reales
|
458 |
* @param h Alto en coordenadas reales
|
459 |
* @param rasterBuf Buffer de datos
|
460 |
* @param bandList
|
461 |
* @return Buffer de datos
|
462 |
*/
|
463 |
abstract public Buffer getWindowRaster(int x, int y, int w, int h, BandList bandList, Buffer rasterBuf)throws ProcessInterruptedException, RasterDriverException; |
464 |
|
465 |
/**
|
466 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas pixel.
|
467 |
* Se aplica supersampleo o subsampleo dependiendo del tama?o del buffer especificado.
|
468 |
*
|
469 |
* @param x Posici?n X superior izquierda
|
470 |
* @param y Posici?n Y superior izquierda
|
471 |
* @param w Ancho en coordenadas reales
|
472 |
* @param h Alto en coordenadas reales
|
473 |
* @param bufWidth Ancho del buffer de datos
|
474 |
* @param bufHeight Alto del buffer de datos
|
475 |
* @param rasterBuf Buffer de datos
|
476 |
* @param bandList
|
477 |
* @return Buffer de datos
|
478 |
*/
|
479 |
abstract public Buffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf)throws ProcessInterruptedException, RasterDriverException; |
480 |
|
481 |
abstract public int getBlockSize(); |
482 |
|
483 |
/**
|
484 |
* Obtiene el valor NoData asociado al raster. Si hay un valor en el fichero
|
485 |
* RMF se devuelve este, sino se buscar? en la cabecera del raster o metadatos de
|
486 |
* este. Si finalmente no encuentra ninguno se devuelve el valor por defecto
|
487 |
* definido en la libreria.
|
488 |
* @return
|
489 |
*/
|
490 |
public double getNoDataValue() { |
491 |
return noData;
|
492 |
} |
493 |
|
494 |
/**
|
495 |
* Pone el valor original de noData. Primero lo consulta del valor del metadata
|
496 |
* y luego del RMF.
|
497 |
*/
|
498 |
public void resetNoDataValue() { |
499 |
/**
|
500 |
* Intentamos recuperar el valor del metadatas, en caso de no encontrarlo le
|
501 |
* asignamos el noData por defecto
|
502 |
*/
|
503 |
noDataEnabled = false;
|
504 |
do {
|
505 |
if (getMetadata() == null) { |
506 |
noData = RasterLibrary.defaultNoDataValue; |
507 |
break;
|
508 |
} |
509 |
|
510 |
if (getMetadata().getNoDataValue().length == 0) { |
511 |
noData = RasterLibrary.defaultNoDataValue; |
512 |
break;
|
513 |
} |
514 |
|
515 |
noData = getMetadata().getNoDataValue()[0];
|
516 |
noDataEnabled = getMetadata().isNoDataEnabled(); |
517 |
} while (false); |
518 |
|
519 |
try {
|
520 |
DefaultNoData noDataObject = (DefaultNoData) loadObjectFromRmf(DefaultNoData.class, null);
|
521 |
if (noDataObject != null) { |
522 |
if (noDataObject.getType() > 0) |
523 |
noData = noDataObject.getValue(); |
524 |
noDataEnabled = (noDataObject.getType() > 0);
|
525 |
} |
526 |
} catch (RmfSerializerException e) {
|
527 |
} |
528 |
} |
529 |
|
530 |
/**
|
531 |
* Establece el valor del NoData
|
532 |
* @param value
|
533 |
*/
|
534 |
public void setNoDataValue(double value) { |
535 |
noData = value; |
536 |
noDataEnabled = true;
|
537 |
} |
538 |
|
539 |
/**
|
540 |
* Dice si el fichero tiene georreferenciaci?n o no.
|
541 |
* @return true si tiene georreferenciaci?n y false si no la tiene
|
542 |
*/
|
543 |
public boolean isGeoreferenced(){ |
544 |
return true; |
545 |
} |
546 |
|
547 |
/**
|
548 |
* Dado unas coordenadas reales, un tama?o de buffer y un tama?o de raster.
|
549 |
* Si el buffer es de mayor tama?o que el raster (supersampleo) quiere decir que
|
550 |
* por cada pixel de buffer se repiten varios del raster. Esta funci?n calcula el
|
551 |
* n?mero de pixels de desplazamiento en X e Y que corresponden al primer pixel del
|
552 |
* buffer en la esquina superior izquierda. Esto es necesario porque la coordenada
|
553 |
* solicitada es real y puede no caer sobre un pixel completo. Este calculo es
|
554 |
* util cuando un cliente quiere supersamplear sobre un buffer y que no se lo haga
|
555 |
* el driver autom?ticamente.
|
556 |
* @param dWorldTLX Coordenada real X superior izquierda
|
557 |
* @param dWorldTLY Coordenada real Y superior izquierda
|
558 |
* @param dWorldBRX Coordenada real X inferior derecha
|
559 |
* @param dWorldBRY Coordenada real Y inferior derecha
|
560 |
* @param nWidth Ancho del raster
|
561 |
* @param nHeight Alto del raster
|
562 |
* @param bufWidth Ancho del buffer
|
563 |
* @param bufHeight Alto del buffer
|
564 |
* @return Array de dos elementos con el desplazamiento en X e Y.
|
565 |
*/
|
566 |
public double[] calcSteps(double dWorldTLX, double dWorldTLY, double dWorldBRX, double dWorldBRY, |
567 |
double nWidth, double nHeight, int bufWidth, int bufHeight) { |
568 |
|
569 |
Point2D p1 = new Point2D.Double(dWorldTLX, dWorldTLY); |
570 |
Point2D p2 = new Point2D.Double(dWorldBRX, dWorldBRY); |
571 |
|
572 |
Point2D tl = worldToRaster(new Point2D.Double(p1.getX(), p1.getY())); |
573 |
Point2D br = worldToRaster(new Point2D.Double(p2.getX(), p2.getY())); |
574 |
|
575 |
double wPx = (bufWidth / Math.abs(br.getX() - tl.getX())); |
576 |
double hPx = (bufHeight / Math.abs(br.getY() - tl.getY())); |
577 |
|
578 |
int x = (int)((tl.getX() > br.getX()) ? Math.floor(br.getX()) : Math.floor(tl.getX())); |
579 |
int y = (int)((tl.getY() > br.getY()) ? Math.floor(br.getY()) : Math.floor(tl.getY())); |
580 |
|
581 |
double a = (tl.getX() > br.getX()) ? (Math.abs(br.getX() - x)) : (Math.abs(tl.getX() - x)); |
582 |
double b = (tl.getY() > br.getY()) ? (Math.abs(br.getY() - y)) : (Math.abs(tl.getY() - y)); |
583 |
|
584 |
double stpX = (int)((a * bufWidth) / Math.abs(br.getX() - tl.getX())); |
585 |
double stpY = (int)((b * bufHeight) / Math.abs(br.getY() - tl.getY())); |
586 |
|
587 |
return new double[]{stpX, stpY, wPx, hPx}; |
588 |
} |
589 |
|
590 |
/**
|
591 |
* Lee una l?nea completa del raster y devuelve un array del tipo correcto. Esta funci?n es util
|
592 |
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
593 |
* @param nLine N?mero de l?nea a leer
|
594 |
* @param band Banda requerida
|
595 |
* @return Object que es un array unidimendional del tipo de datos del raster
|
596 |
* @throws InvalidSetViewException
|
597 |
* @throws FileNotOpenException
|
598 |
* @throws RasterDriverException
|
599 |
*/
|
600 |
abstract public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException; |
601 |
|
602 |
/**
|
603 |
* Lee un bloque completo de datos del raster y devuelve un array tridimensional del tipo correcto. Esta funci?n es util
|
604 |
* para una lectura rapida de todo el fichero sin necesidad de asignar vista.
|
605 |
* @param pos Posici?n donde se empieza a leer
|
606 |
* @param blockHeight Altura m?xima del bloque leido
|
607 |
* @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
|
608 |
* @throws InvalidSetViewException
|
609 |
* @throws FileNotOpenException
|
610 |
* @throws RasterDriverException
|
611 |
*/
|
612 |
abstract public Object readBlock(int pos, int blockHeight) |
613 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException;
|
614 |
|
615 |
/**
|
616 |
* Convierte un punto desde coordenadas pixel a coordenadas del mundo.
|
617 |
* @param pt Punto a transformar
|
618 |
* @return punto transformado en coordenadas del mundo
|
619 |
*/
|
620 |
public Point2D rasterToWorld(Point2D pt) { |
621 |
Point2D p = new Point2D.Double(); |
622 |
externalTransformation.transform(pt, p); |
623 |
return p;
|
624 |
} |
625 |
|
626 |
/**
|
627 |
* Convierte un punto desde del mundo a coordenadas pixel.
|
628 |
* @param pt Punto a transformar
|
629 |
* @return punto transformado en coordenadas pixel
|
630 |
*/
|
631 |
public Point2D worldToRaster(Point2D pt) { |
632 |
Point2D p = new Point2D.Double(); |
633 |
try {
|
634 |
externalTransformation.inverseTransform(pt, p); |
635 |
} catch (NoninvertibleTransformException e) { |
636 |
return pt;
|
637 |
} |
638 |
return p;
|
639 |
} |
640 |
|
641 |
/**
|
642 |
* Calcula el extent en coordenadas del mundo real
|
643 |
* @return Extent
|
644 |
*/
|
645 |
public Extent getExtent() {
|
646 |
return new ExtentImpl( rasterToWorld(new Point2D.Double(0, 0)), |
647 |
rasterToWorld(new Point2D.Double(getWidth(), getHeight())), |
648 |
rasterToWorld(new Point2D.Double(getWidth(), 0)), |
649 |
rasterToWorld(new Point2D.Double(0, getHeight()))); |
650 |
} |
651 |
|
652 |
/**
|
653 |
* Calcula el extent en coordenadas del mundo real sin rotaci?n. Solo coordenadas y tama?o de pixel
|
654 |
* @return Extent
|
655 |
*/
|
656 |
public Extent getExtentWithoutRot() {
|
657 |
AffineTransform at = new AffineTransform( externalTransformation.getScaleX(), 0, |
658 |
0, externalTransformation.getScaleY(),
|
659 |
externalTransformation.getTranslateX(), externalTransformation.getTranslateY()); |
660 |
Point2D p1 = new Point2D.Double(0, 0); |
661 |
Point2D p2 = new Point2D.Double(getWidth(), getHeight()); |
662 |
at.transform(p1, p1); |
663 |
at.transform(p2, p2); |
664 |
return new ExtentImpl(p1, p2); |
665 |
} |
666 |
|
667 |
/**
|
668 |
* ASigna el par?metro de inicializaci?n del driver.
|
669 |
*/
|
670 |
public void setParam(AbstractRasterStoreParameters param) { |
671 |
this.name = param.getFileName();
|
672 |
this.param = param;
|
673 |
} |
674 |
|
675 |
/**
|
676 |
* Obtiene el ancho de una overview de una banda
|
677 |
* @return
|
678 |
*/
|
679 |
abstract public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException; |
680 |
|
681 |
/**
|
682 |
* Obtiene el alto de una overview de una banda
|
683 |
* @return
|
684 |
*/
|
685 |
abstract public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException; |
686 |
|
687 |
/**
|
688 |
* Informa de si el dataset soporta overviews o no.
|
689 |
* @return true si soporta overviews y false si no las soporta.
|
690 |
*/
|
691 |
abstract public boolean overviewsSupport(); |
692 |
|
693 |
/**
|
694 |
* Obtiene el gestor de ficheros RMF
|
695 |
* @return RmfBloksManager
|
696 |
*/
|
697 |
public RmfBlocksManager getRmfBlocksManager() {
|
698 |
if (rmfBlocksManager == null) { |
699 |
String fileRMF = fileUtil.getNameWithoutExtension(getFName()) + ".rmf"; |
700 |
rmfBlocksManager = new RmfBlocksManager(fileRMF);
|
701 |
} |
702 |
return rmfBlocksManager;
|
703 |
} |
704 |
|
705 |
/*
|
706 |
* (non-Javadoc)
|
707 |
* @see org.gvsig.raster.impl.provider.RasterProvider#isInside(java.awt.geom.Point2D)
|
708 |
*/
|
709 |
public boolean isInside(Point2D p){ |
710 |
//Realizamos los calculos solo si el punto est? dentro del extent de la imagen rotada, as? nos ahorramos los calculos
|
711 |
//cuando el puntero est? fuera
|
712 |
|
713 |
Point2D pt = new Point2D.Double(); |
714 |
try {
|
715 |
|
716 |
getAffineTransform().inverseTransform(p, pt); |
717 |
if( pt.getX() >= 0 && pt.getX() < getWidth() && |
718 |
pt.getY() >= 0 && pt.getY() < getHeight())
|
719 |
return true; |
720 |
} catch (NoninvertibleTransformException e) { |
721 |
return false; |
722 |
} |
723 |
|
724 |
return false; |
725 |
} |
726 |
|
727 |
/**
|
728 |
* Consulta de si un raster tiene rotaci?n o no.
|
729 |
* @return true si tiene rotaci?n y false si no la tiene.
|
730 |
*/
|
731 |
public boolean isRotated() { |
732 |
if(externalTransformation.getShearX() != 0 || externalTransformation.getShearY() != 0) |
733 |
return true; |
734 |
return false; |
735 |
} |
736 |
|
737 |
/**
|
738 |
* Devuelve si el RasterDataSet tiene el valor noData activo
|
739 |
* @return the noDataEnabled
|
740 |
*/
|
741 |
public boolean isNoDataEnabled() { |
742 |
return noDataEnabled;
|
743 |
} |
744 |
|
745 |
/**
|
746 |
* Define si el RasterDataSet tiene el valor noData activo
|
747 |
* @param noDataEnabled the noDataEnabled to set
|
748 |
*/
|
749 |
public void setNoDataEnabled(boolean noDataEnabled) { |
750 |
this.noDataEnabled = noDataEnabled;
|
751 |
} |
752 |
|
753 |
/**
|
754 |
* Devuelve si el Dataset es reproyectable
|
755 |
* @return
|
756 |
*/
|
757 |
public boolean isReproyectable() { |
758 |
return false; |
759 |
} |
760 |
|
761 |
public String getWktProjection() { |
762 |
return wktProjection;
|
763 |
} |
764 |
|
765 |
/*
|
766 |
* (non-Javadoc)
|
767 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#saveObjectToRmf(java.lang.Class, java.lang.Object)
|
768 |
*/
|
769 |
@SuppressWarnings("unchecked") |
770 |
public void saveObjectToRmf(Class class1, Object value) throws RmfSerializerException { |
771 |
((DefaultProviderServices)RasterLocator.getManager().getProviderServices()).saveObjectToRmfFile(getRmfBlocksManager(), class1, value); |
772 |
} |
773 |
|
774 |
/**
|
775 |
* Carga un objecto desde un serializador del tipo class1. Usa value para iniciar dicho
|
776 |
* serializador
|
777 |
*
|
778 |
* @param class1
|
779 |
* @param value
|
780 |
* @return
|
781 |
* @throws RmfSerializerException
|
782 |
*/
|
783 |
@SuppressWarnings("unchecked") |
784 |
private static Object loadObjectFromRmfFile(RmfBlocksManager blocksManager, Class class1, Object value) throws RmfSerializerException { |
785 |
ClassSerializer serializerObject = ((DefaultProviderServices)RasterLocator.getManager().getProviderServices()).getSerializerObject(class1, value); |
786 |
|
787 |
if (serializerObject == null) |
788 |
throw new RmfSerializerException("No se ha podido encontrar el serializador para el Rmf"); |
789 |
|
790 |
if (!blocksManager.checkRmf())
|
791 |
throw new RmfSerializerException("Error al comprobar el fichero Rmf"); |
792 |
|
793 |
blocksManager.addClient(serializerObject); |
794 |
try {
|
795 |
blocksManager.read(null);
|
796 |
} catch (ParsingException e) {
|
797 |
throw new RmfSerializerException("Error al leer el fichero Rmf", e); |
798 |
} |
799 |
blocksManager.removeAllClients(); |
800 |
|
801 |
return serializerObject.getResult();
|
802 |
} |
803 |
|
804 |
/*
|
805 |
* (non-Javadoc)
|
806 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#loadObjectFromRmf(java.lang.Class, java.lang.Object)
|
807 |
*/
|
808 |
@SuppressWarnings("unchecked") |
809 |
public Object loadObjectFromRmf(Class class1, Object value) throws RmfSerializerException { |
810 |
return loadObjectFromRmfFile(getRmfBlocksManager(), class1, value);
|
811 |
} |
812 |
|
813 |
/**
|
814 |
* Carga un objeto del fichero RMF especificado por parametro
|
815 |
* @param file
|
816 |
* @param class1
|
817 |
* @param value
|
818 |
* @return
|
819 |
* @throws RmfSerializerException
|
820 |
*/
|
821 |
@SuppressWarnings("unchecked") |
822 |
public static Object loadObjectFromRmfFile(String file, Class class1, Object value) throws RmfSerializerException { |
823 |
String fileRMF = RasterLocator.getManager().getFileUtils().getNameWithoutExtension(file) + ".rmf"; |
824 |
RmfBlocksManager blocksManager = new RmfBlocksManager(fileRMF);
|
825 |
return loadObjectFromRmfFile(blocksManager, class1, value);
|
826 |
} |
827 |
|
828 |
/**
|
829 |
* Guarda en el RMF el objecto actual en caso de que exista un serializador para el
|
830 |
* @param value
|
831 |
* @throws RmfSerializerException
|
832 |
*/
|
833 |
public void saveObjectToRmf(Object value) throws RmfSerializerException { |
834 |
saveObjectToRmf(value.getClass(), value); |
835 |
} |
836 |
|
837 |
/**
|
838 |
* Carga un objecto desde un serializador usando el tipo del mismo objeto pasado por parametro.
|
839 |
* Usa value para iniciar dicho serializador
|
840 |
* @param value
|
841 |
* @return
|
842 |
* @throws RmfSerializerException
|
843 |
*/
|
844 |
public Object loadObjectFromRmf(Object value) throws RmfSerializerException { |
845 |
return loadObjectFromRmf(value.getClass(), value);
|
846 |
} |
847 |
|
848 |
/*
|
849 |
* (non-Javadoc)
|
850 |
* @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getDatasetCount()
|
851 |
*/
|
852 |
public int getDatasetCount() { |
853 |
return 1; |
854 |
} |
855 |
|
856 |
|
857 |
public double getCellSize() { |
858 |
try {
|
859 |
Extent e = getExtent(); |
860 |
double dCellsize = (e.getMax().getX() - e.getMin().getX() ) / getWidth();
|
861 |
return dCellsize;
|
862 |
} catch (NullPointerException e) { |
863 |
return 1; |
864 |
} |
865 |
} |
866 |
|
867 |
|
868 |
public RasterProvider newProvider() {
|
869 |
return null; |
870 |
} |
871 |
|
872 |
/*
|
873 |
* (non-Javadoc)
|
874 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#getColorTable()
|
875 |
*/
|
876 |
public ColorTable getColorTable() {
|
877 |
return colorTable;
|
878 |
} |
879 |
|
880 |
/**
|
881 |
* Define el objeto paleta. Si se define null quiere decir que no tiene paleta
|
882 |
* para su visualizaci?n.
|
883 |
* @param value
|
884 |
*/
|
885 |
public void setColorTable(ColorTable value) { |
886 |
colorTable = value; |
887 |
} |
888 |
|
889 |
/*
|
890 |
* (non-Javadoc)
|
891 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#getTransparency()
|
892 |
*/
|
893 |
public Transparency getTransparency() { |
894 |
return null; |
895 |
} |
896 |
|
897 |
/*
|
898 |
* (non-Javadoc)
|
899 |
* @see org.gvsig.raster.impl.provider.RasterProvider#getMetadata()
|
900 |
*/
|
901 |
public DatasetMetadata getMetadata() {
|
902 |
return null; |
903 |
} |
904 |
|
905 |
/*
|
906 |
* (non-Javadoc)
|
907 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#getColorInterpretation()
|
908 |
*/
|
909 |
public ColorInterpretation getColorInterpretation(){
|
910 |
return colorInterpretation;
|
911 |
} |
912 |
|
913 |
/**
|
914 |
* Asigna el objeto que contiene que contiene la interpretaci?n de
|
915 |
* color por banda
|
916 |
* @param DatasetColorInterpretation
|
917 |
*/
|
918 |
public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){ |
919 |
this.colorInterpretation = colorInterpretation;
|
920 |
} |
921 |
|
922 |
/*
|
923 |
* (non-Javadoc)
|
924 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#getStatistics()
|
925 |
*/
|
926 |
public DatasetStatistics getStatistics() {
|
927 |
return stats;
|
928 |
} |
929 |
|
930 |
/*
|
931 |
* (non-Javadoc)
|
932 |
* @see org.gvsig.raster.impl.dataset.RasterProvider#getHistogram()
|
933 |
*/
|
934 |
public Histogram getHistogram() {
|
935 |
if (histogram == null) |
936 |
histogram = new DatasetHistogram(this); |
937 |
return histogram;
|
938 |
} |
939 |
|
940 |
//****************************************************
|
941 |
//*********Implementing Disposable methods************
|
942 |
//****************************************************
|
943 |
|
944 |
/*
|
945 |
* (non-Javadoc)
|
946 |
* @see org.gvsig.tools.dispose.impl.AbstractDisposable#doDispose()
|
947 |
*/
|
948 |
public void doDispose() { |
949 |
|
950 |
} |
951 |
|
952 |
//****************************************************
|
953 |
//*****Implementing DataStoreProvider methods*********
|
954 |
//****************************************************
|
955 |
|
956 |
/**
|
957 |
* Gets the specific parameter. All of them inherit from RasterStoreParameters
|
958 |
*/
|
959 |
//public abstract RasterStoreParameters getRasterParameters();
|
960 |
|
961 |
@SuppressWarnings("deprecation") |
962 |
@Override
|
963 |
public DataServerExplorer getExplorer() throws ReadException, |
964 |
ValidateDataParametersException { |
965 |
DataManager manager = DALLocator.getDataManager(); |
966 |
FilesystemServerExplorerParameters params; |
967 |
try {
|
968 |
params = (FilesystemServerExplorerParameters) manager |
969 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
970 |
params.setRoot(this.getRasterParameters().getFile().getParent());
|
971 |
return manager.createServerExplorer(params);
|
972 |
} catch (DataException e) {
|
973 |
throw new ReadException(this.getName(), e); |
974 |
} |
975 |
} |
976 |
|
977 |
public AbstractRasterStoreParameters getRasterParameters() {
|
978 |
return (AbstractRasterStoreParameters)getDataStoreParameters();
|
979 |
} |
980 |
|
981 |
/*
|
982 |
* (non-Javadoc)
|
983 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#getChilds()
|
984 |
*/
|
985 |
@SuppressWarnings("unchecked") |
986 |
public Iterator getChilds() { |
987 |
return null; |
988 |
} |
989 |
|
990 |
/*
|
991 |
* (non-Javadoc)
|
992 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#getResource()
|
993 |
*/
|
994 |
public ResourceProvider getResource() {
|
995 |
return null; |
996 |
} |
997 |
|
998 |
/*
|
999 |
* (non-Javadoc)
|
1000 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#getSourceId()
|
1001 |
*/
|
1002 |
public Object getSourceId() { |
1003 |
return this.getRasterParameters().getFile(); |
1004 |
} |
1005 |
|
1006 |
/*
|
1007 |
* (non-Javadoc)
|
1008 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#open()
|
1009 |
*/
|
1010 |
public void open() throws OpenException { |
1011 |
} |
1012 |
} |