gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.io / org.gvsig.raster.io.base / src / main / java / org / gvsig / fmap / dal / coverage / dataset / io / wms / WMSProvider.java @ 211
History | View | Annotate | Download (28.5 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.fmap.dal.coverage.dataset.io.wms; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.NoninvertibleTransformException; |
26 |
import java.awt.geom.Point2D; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
import java.io.File; |
29 |
import java.io.IOException; |
30 |
import java.net.URL; |
31 |
import java.util.Hashtable; |
32 |
import java.util.Vector; |
33 |
|
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataStore; |
36 |
import org.gvsig.fmap.dal.DataStoreParameters; |
37 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
38 |
import org.gvsig.fmap.dal.coverage.dataset.io.GdalDriver; |
39 |
import org.gvsig.fmap.dal.coverage.dataset.io.param.WMSStoreParametersImpl; |
40 |
import org.gvsig.fmap.dal.coverage.dataset.io.server.DefaultWMSServerExplorerParameters; |
41 |
import org.gvsig.fmap.dal.coverage.dataset.io.server.WMSServerExplorerImpl; |
42 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
43 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
44 |
import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
45 |
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException; |
46 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
47 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
48 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
49 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
50 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
51 |
import org.gvsig.fmap.dal.coverage.exception.WMSException; |
52 |
import org.gvsig.fmap.dal.coverage.explorer.WMSServerExplorer; |
53 |
import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
54 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
55 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
56 |
import org.gvsig.metadata.MetadataLocator; |
57 |
import org.gvsig.raster.impl.datastruct.BandListImpl; |
58 |
import org.gvsig.raster.impl.datastruct.DatasetBandImpl; |
59 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
60 |
import org.gvsig.raster.impl.provider.DefaultRasterProvider; |
61 |
import org.gvsig.raster.impl.provider.RasterProvider; |
62 |
import org.gvsig.raster.impl.provider.RemoteRasterProvider; |
63 |
import org.gvsig.raster.impl.store.AbstractRasterDataStore; |
64 |
import org.gvsig.raster.impl.store.DefaultStoreFactory; |
65 |
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation; |
66 |
import org.gvsig.raster.impl.store.properties.DataStoreTransparency; |
67 |
import org.gvsig.remoteclient.utils.Utilities; |
68 |
import org.gvsig.remoteclient.wms.WMSStatus; |
69 |
import org.gvsig.tools.ToolsLocator; |
70 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
71 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
72 |
/**
|
73 |
* Clase que representa al driver de acceso a datos de wms.
|
74 |
*
|
75 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
76 |
*/
|
77 |
public class WMSProvider extends DefaultRasterProvider implements RemoteRasterProvider { |
78 |
public static String NAME = "Wms Store"; |
79 |
public static String DESCRIPTION = "Wms Raster file"; |
80 |
public static final String METADATA_DEFINITION_NAME = "WmsStore"; |
81 |
private static final int FIXED_SIZE = 800; |
82 |
|
83 |
private Extent viewRequest = null; |
84 |
private static Hashtable<URL, WMSConnector> |
85 |
drivers = new Hashtable<URL, WMSConnector> (); |
86 |
private boolean open = false; |
87 |
private DataStoreTransparency fileTransparency = null; |
88 |
private final int minTilePrintWidth = 12; |
89 |
private final int minTilePrintHeight = 12; |
90 |
//The out size depends on the last request
|
91 |
private int lastWidthRequest = 0; |
92 |
private int lastHeightRequest = 0; |
93 |
//Only for fixed size. Complete extent and FIXED_SIZE in long side
|
94 |
private File fileLayerPixelSize = null; |
95 |
private File lastRequest = null; |
96 |
|
97 |
public static void register() { |
98 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
99 |
ExtensionPoint point = extensionPoints.get("RasterReader");
|
100 |
point.append("wms", "", WMSProvider.class); |
101 |
|
102 |
DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
103 |
if (dataman != null && !dataman.getStoreProviders().contains(NAME)) { |
104 |
dataman.registerStoreProvider(NAME, |
105 |
WMSProvider.class, WMSStoreParametersImpl.class); |
106 |
} |
107 |
|
108 |
if (!dataman.getExplorerProviders().contains(WMSServerExplorerImpl.NAME)) {
|
109 |
dataman.registerExplorerProvider(WMSServerExplorer.NAME, WMSServerExplorerImpl.class, DefaultWMSServerExplorerParameters.class); |
110 |
} |
111 |
dataman.registerStoreFactory(NAME, DefaultStoreFactory.class); |
112 |
} |
113 |
|
114 |
public WMSProvider() throws NotSupportedExtensionException { |
115 |
super();
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Constructor. Abre el dataset.
|
120 |
* @param proj Proyecci?n
|
121 |
* @param fName Nombre del fichero
|
122 |
* @throws NotSupportedExtensionException
|
123 |
*/
|
124 |
public WMSProvider(String params) throws NotSupportedExtensionException { |
125 |
super(params);
|
126 |
if(params instanceof String) { |
127 |
WMSStoreParametersImpl p = new WMSStoreParametersImpl();
|
128 |
p.setHost((String)params);
|
129 |
super.init(p, null, ToolsLocator.getDynObjectManager() |
130 |
.createDynObject( |
131 |
MetadataLocator.getMetadataManager().getDefinition( |
132 |
DataStore.METADATA_DEFINITION_NAME))); |
133 |
init(p, null);
|
134 |
} |
135 |
} |
136 |
|
137 |
public WMSProvider(WMSStoreParametersImpl params,
|
138 |
AbstractRasterDataStore storeServices) throws NotSupportedExtensionException {
|
139 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
140 |
.createDynObject( |
141 |
MetadataLocator.getMetadataManager().getDefinition( |
142 |
DataStore.METADATA_DEFINITION_NAME))); |
143 |
init(params, storeServices); |
144 |
} |
145 |
|
146 |
/**
|
147 |
* Gets the connector from the URL
|
148 |
* @return
|
149 |
* @throws WMSException
|
150 |
*/
|
151 |
private WMSConnector getConnector() throws WMSException { |
152 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
153 |
URL url = null; |
154 |
try {
|
155 |
url = new URL(p.getHost()); |
156 |
} catch (Exception e) { |
157 |
throw new WMSException("Malformed URL",e); |
158 |
} |
159 |
try {
|
160 |
return WMSProvider.getConnectorFromURL(url);
|
161 |
} catch (IOException e) { |
162 |
throw new WMSException("Error getting the connector",e); |
163 |
} |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Crea las referencias al fichero y carga
|
168 |
* las estructuras con la informaci?n y los metadatos.
|
169 |
* @param proj Proyecci?n
|
170 |
* @param param Parametros de carga
|
171 |
* @throws NotSupportedExtensionException
|
172 |
*/
|
173 |
public void init (DataStoreParameters params, |
174 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
175 |
setParam(params); |
176 |
setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE}); |
177 |
bandCount = 3;
|
178 |
} |
179 |
|
180 |
public static final WMSConnector getConnectorFromURL(URL url) throws IOException { |
181 |
WMSConnector drv = (WMSConnector) drivers.get(url); |
182 |
if (drv == null) { |
183 |
drv = new WMSConnector(url);
|
184 |
drivers.put(url, drv); |
185 |
} |
186 |
return drv;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Obtiene el objeto que contiene que contiene la interpretaci?n de
|
191 |
* color por banda
|
192 |
* @return
|
193 |
*/
|
194 |
public DataStoreColorInterpretation getColorInterpretation() {
|
195 |
if(colorInterpretation == null) { |
196 |
colorInterpretation = new DataStoreColorInterpretation();
|
197 |
colorInterpretation.initColorInterpretation(getBandCount()); |
198 |
colorInterpretation.setColorInterpValue(0, DataStoreColorInterpretation.RED_BAND);
|
199 |
colorInterpretation.setColorInterpValue(1, DataStoreColorInterpretation.GREEN_BAND);
|
200 |
colorInterpretation.setColorInterpValue(2, DataStoreColorInterpretation.BLUE_BAND);
|
201 |
} |
202 |
return colorInterpretation;
|
203 |
} |
204 |
|
205 |
/*
|
206 |
* (non-Javadoc)
|
207 |
* @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getAffineTransform()
|
208 |
*/
|
209 |
public AffineTransform getAffineTransform() { |
210 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
211 |
Extent e = getExtent(); |
212 |
if(p.isSizeFixed()) {
|
213 |
ownTransformation = new AffineTransform( |
214 |
e.width() / p.getWidth() , |
215 |
0,
|
216 |
0,
|
217 |
-(e.height() / p.getHeight()), |
218 |
e.getULX(), |
219 |
e.getULY()); |
220 |
} else {
|
221 |
ownTransformation = new AffineTransform( |
222 |
e.width() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest) ,
|
223 |
0,
|
224 |
0,
|
225 |
-(e.height() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest)),
|
226 |
e.getULX(), |
227 |
e.getULY()); |
228 |
/*ownTransformation = new AffineTransform(
|
229 |
p.getExtent().getWidth() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest) ,
|
230 |
0,
|
231 |
0,
|
232 |
-(p.getExtent().getHeight() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest)),
|
233 |
p.getExtent().getMinX(),
|
234 |
p.getExtent().getMinY());*/
|
235 |
} |
236 |
externalTransformation = (AffineTransform) ownTransformation.clone();
|
237 |
return ownTransformation;
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* Calcula el extent en coordenadas del mundo real
|
242 |
* @return Extent
|
243 |
*/
|
244 |
public Extent getExtent() {
|
245 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
246 |
Vector<?> layerNames = Utilities.createVector(p.getLayerQuery(), ","); |
247 |
|
248 |
String[] ln = new String[layerNames.size()]; |
249 |
for (int i = 0; i < ln.length; i++) { |
250 |
ln[i] = (String)layerNames.get(i);
|
251 |
} |
252 |
Rectangle2D r = null; |
253 |
try {
|
254 |
r = getConnector().getLayersExtent(ln, p.getSRSCode()); |
255 |
} catch (WMSException e) {
|
256 |
} |
257 |
return new ExtentImpl(r.getMinX(), r.getMaxY(), r.getMaxX(), r.getMinY()); |
258 |
} |
259 |
|
260 |
|
261 |
/*
|
262 |
* (non-Javadoc)
|
263 |
* @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLayerExtent(java.lang.String, java.lang.String)
|
264 |
*/
|
265 |
public Rectangle2D getLayerExtent(String layerName, String srs) throws WMSException { |
266 |
return getConnector().getLayersExtent(new String[]{layerName}, srs); |
267 |
} |
268 |
|
269 |
/*
|
270 |
* (non-Javadoc)
|
271 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
|
272 |
*/
|
273 |
public RasterProvider load() {
|
274 |
return this; |
275 |
} |
276 |
|
277 |
/*
|
278 |
* (non-Javadoc)
|
279 |
* @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
|
280 |
*/
|
281 |
public boolean isOpen() { |
282 |
return open;
|
283 |
} |
284 |
|
285 |
/*
|
286 |
* (non-Javadoc)
|
287 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
|
288 |
*/
|
289 |
public void close() { |
290 |
open = false;
|
291 |
} |
292 |
|
293 |
/*
|
294 |
* (non-Javadoc)
|
295 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
|
296 |
*/
|
297 |
public Transparency getTransparency() { |
298 |
if(fileTransparency == null) |
299 |
fileTransparency = new DataStoreTransparency();
|
300 |
return fileTransparency;
|
301 |
} |
302 |
|
303 |
/*
|
304 |
* (non-Javadoc)
|
305 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
|
306 |
*/
|
307 |
public String translateFileName(String fileName) { |
308 |
return fileName;
|
309 |
} |
310 |
|
311 |
/*
|
312 |
* (non-Javadoc)
|
313 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
|
314 |
*/
|
315 |
public void setView(Extent e) { |
316 |
viewRequest = e; |
317 |
} |
318 |
|
319 |
/*
|
320 |
* (non-Javadoc)
|
321 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
|
322 |
*/
|
323 |
public Extent getView() {
|
324 |
return viewRequest;
|
325 |
} |
326 |
|
327 |
/*
|
328 |
* (non-Javadoc)
|
329 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
|
330 |
*/
|
331 |
public double getWidth() { |
332 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
333 |
if(p.isSizeFixed()) {
|
334 |
return p.getWidth();
|
335 |
} |
336 |
if (lastWidthRequest <= 0) |
337 |
return p.getWidth();
|
338 |
if (lastWidthRequest < minTilePrintWidth)
|
339 |
return minTilePrintWidth;
|
340 |
return lastWidthRequest;
|
341 |
} |
342 |
|
343 |
/*
|
344 |
* (non-Javadoc)
|
345 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
|
346 |
*/
|
347 |
public double getHeight() { |
348 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
349 |
if(p.isSizeFixed()) {
|
350 |
return p.getHeight();
|
351 |
} |
352 |
if (lastHeightRequest <= 0) |
353 |
return p.getHeight();
|
354 |
if (lastHeightRequest < minTilePrintHeight)
|
355 |
return minTilePrintHeight;
|
356 |
return lastHeightRequest;
|
357 |
} |
358 |
|
359 |
/*
|
360 |
* (non-Javadoc)
|
361 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readCompleteLine(int, int)
|
362 |
*/
|
363 |
public Object readCompleteLine(int line, int band) |
364 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
|
365 |
return null; |
366 |
} |
367 |
|
368 |
/**
|
369 |
* When the remote layer has fixed size this method downloads the file and return its reference.
|
370 |
* File layer has in the long side FIXED_SIZE pixels and the bounding box is complete. This file could be
|
371 |
* useful to build an histogram or calculate statistics. This represents a sample of data.
|
372 |
* @return
|
373 |
* @throws RasterDriverException
|
374 |
*/
|
375 |
public File getFileLayer() throws RasterDriverException { |
376 |
if(fileLayerPixelSize != null) |
377 |
return fileLayerPixelSize;
|
378 |
|
379 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
380 |
Extent e = getExtent(); |
381 |
Rectangle2D bBox = new Rectangle2D.Double(e.getULX(), e.getLRY(), e.width(), e.height()); |
382 |
WMSStatus wmsStatus = loadWMSStatus(bBox); |
383 |
|
384 |
if(!p.isSizeFixed()) {
|
385 |
int w = 0; |
386 |
int h = 0; |
387 |
if(e.width() > e.height()) {
|
388 |
w = FIXED_SIZE; |
389 |
h = (int)((e.height() * FIXED_SIZE) / e.width());
|
390 |
} else {
|
391 |
h = FIXED_SIZE; |
392 |
w = (int)((e.width() * FIXED_SIZE) / e.height());
|
393 |
} |
394 |
wmsStatus.setWidth(w); |
395 |
wmsStatus.setHeight(h); |
396 |
fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), w, h); |
397 |
} else {
|
398 |
fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), |
399 |
p.getFixedSize().width, p.getFixedSize().height); |
400 |
} |
401 |
return fileLayerPixelSize;
|
402 |
} |
403 |
|
404 |
/**
|
405 |
* Reads a complete block of data and returns an tridimensional array of the right type. This function is useful
|
406 |
* to read a file very fast without setting a view. In a WMS service when the size is fixed then it will read the
|
407 |
* entire image but when the source hasn't pixel size it will read a sample of data. This set of data will have
|
408 |
* the size defined in FIXED_SIZE.
|
409 |
*
|
410 |
* @param pos Posici?n donde se empieza a leer
|
411 |
* @param blockHeight Altura m?xima del bloque leido
|
412 |
* @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
|
413 |
* @throws InvalidSetViewException
|
414 |
* @throws FileNotOpenException
|
415 |
* @throws RasterDriverException
|
416 |
*/
|
417 |
public Object readBlock(int pos, int blockHeight) |
418 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
|
419 |
File lastFile = getFileLayer();
|
420 |
BandList bandList = new BandListImpl();
|
421 |
for (int i = 0; i < 3; i++) { |
422 |
try {
|
423 |
bandList.addBand(new DatasetBandImpl(lastFile.getPath(), pos, Buffer.TYPE_BYTE), pos); |
424 |
} catch (BandNotFoundInListException e1) {
|
425 |
} |
426 |
} |
427 |
bandList.setDrawableBands(new int[]{0, 1, 2}); |
428 |
|
429 |
try {
|
430 |
GdalDriver driver = new GdalDriver(lastFile.getPath());
|
431 |
return driver.readBlock(pos, blockHeight);
|
432 |
} catch (NotSupportedExtensionException exc) {
|
433 |
throw new RasterDriverException("Error building GdalDriver", exc); |
434 |
} |
435 |
} |
436 |
|
437 |
/*
|
438 |
* (non-Javadoc)
|
439 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
|
440 |
*/
|
441 |
public Object getData(int x, int y, int band) |
442 |
throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
|
443 |
return null; |
444 |
} |
445 |
|
446 |
/**
|
447 |
* Gets the georeferencing file name form a raster file
|
448 |
* @param file
|
449 |
* a raster file
|
450 |
* @return
|
451 |
* a georeferencing file
|
452 |
*/
|
453 |
private String getWorldFile(String file){ |
454 |
String worldFile = file;
|
455 |
int index = file.lastIndexOf("."); |
456 |
if (index > 0){ |
457 |
worldFile = file.substring(0, index) + getExtensionWorldFile();
|
458 |
} |
459 |
return worldFile;
|
460 |
} |
461 |
|
462 |
/**
|
463 |
* Obtiene la extensi?n del fichero de georreferenciaci?n
|
464 |
* @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
|
465 |
* del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
|
466 |
*/
|
467 |
private String getExtensionWorldFile() { |
468 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
469 |
String extWorldFile = ".wld"; |
470 |
if(p.getFormat().equals("image/tif") || p.getFormat().equals("image/tiff")) { |
471 |
extWorldFile = ".tfw";
|
472 |
} |
473 |
return extWorldFile;
|
474 |
} |
475 |
|
476 |
private WMSStatus loadWMSStatus(Rectangle2D bBox) { |
477 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
478 |
WMSStatus wmsStatus = new WMSStatus();
|
479 |
wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ",")); |
480 |
wmsStatus.setSrs(p.getSRSCode()); |
481 |
wmsStatus.setFormat(p.getFormat()); |
482 |
wmsStatus.setStyles(p.getStyles()); |
483 |
wmsStatus.setDimensions(p.getDimensions()); |
484 |
wmsStatus.setTransparency(p.isWmsTransparent()); |
485 |
wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap")); |
486 |
if(p.isSizeFixed()) {
|
487 |
wmsStatus.setExtent(getExtent().toRectangle2D()); |
488 |
} else
|
489 |
wmsStatus.setExtent(bBox); |
490 |
wmsStatus.setHeight(p.getHeight()); |
491 |
wmsStatus.setWidth(p.getWidth()); |
492 |
return wmsStatus;
|
493 |
} |
494 |
|
495 |
/**
|
496 |
* This function downloads the file and creates the georeferencing file
|
497 |
* @param wmsStatus
|
498 |
* @param ulx
|
499 |
* @param uly
|
500 |
* @param lrx
|
501 |
* @param lry
|
502 |
* @param w
|
503 |
* @param h
|
504 |
* @return
|
505 |
* @throws RasterDriverException
|
506 |
*/
|
507 |
private File downloadFile(WMSStatus wmsStatus, double ulx, double uly, double lrx, double lry, int w, int h) throws RasterDriverException { |
508 |
WMSStoreParametersImpl p = (WMSStoreParametersImpl)parameters; |
509 |
try {
|
510 |
lastRequest = getConnector().getMap(wmsStatus, ((WMSStoreParametersImpl)parameters).getCancellable()); |
511 |
} catch (WMSException e) {
|
512 |
throw new RasterDriverException(e.getMessage(), e); |
513 |
} |
514 |
|
515 |
if(lastRequest == null) |
516 |
return null; |
517 |
|
518 |
String nameWorldFile = getWorldFile(lastRequest.getPath());
|
519 |
try {
|
520 |
if(p.isSizeFixed()) {
|
521 |
Extent e = getExtent(); |
522 |
fileUtil.createWorldFile(nameWorldFile, e, p.getWidth(), p.getHeight()); |
523 |
} else {
|
524 |
fileUtil.createWorldFile(nameWorldFile, new ExtentImpl(ulx, uly, lrx, lry), w, h);
|
525 |
} |
526 |
} catch (IOException e) { |
527 |
throw new RasterDriverException("Error creating world file", e); |
528 |
} |
529 |
|
530 |
return lastRequest;
|
531 |
} |
532 |
|
533 |
/**
|
534 |
* Assigns the list of bands RGB and read a window of data
|
535 |
* @param rasterBuf
|
536 |
* @param bandList
|
537 |
* @param lastFile
|
538 |
* @param ulx
|
539 |
* @param uly
|
540 |
* @param lrx
|
541 |
* @param lry
|
542 |
* @return
|
543 |
* @throws RasterDriverException
|
544 |
* @throws ProcessInterruptedException
|
545 |
*/
|
546 |
public Buffer getBuffer(Buffer rasterBuf, BandList bandList, File lastFile, |
547 |
double ulx, double uly, double lrx, double lry) throws RasterDriverException, ProcessInterruptedException { |
548 |
try {
|
549 |
//El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
550 |
String serverName = bandList.getBand(0).getFileName(); |
551 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
552 |
bandList.getBand(i).setFileName(lastFile.getPath()); |
553 |
} |
554 |
|
555 |
GdalDriver driver = new GdalDriver(lastFile.getPath());
|
556 |
Buffer buf = driver.getWindowRaster(ulx, uly, lrx, lry, bandList, rasterBuf);
|
557 |
|
558 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
559 |
bandList.getBand(i).setFileName(serverName); |
560 |
} |
561 |
|
562 |
return buf;
|
563 |
} catch (NotSupportedExtensionException e) {
|
564 |
throw new RasterDriverException("Error building GdalDriver", e); |
565 |
} |
566 |
} |
567 |
|
568 |
/*
|
569 |
* (non-Javadoc)
|
570 |
* @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)
|
571 |
*/
|
572 |
public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry, |
573 |
BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException { |
574 |
Rectangle2D bBox = new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly)); |
575 |
lastWidthRequest = rasterBuf.getWidth(); |
576 |
lastHeightRequest = rasterBuf.getHeight(); |
577 |
WMSStatus wmsStatus = loadWMSStatus(bBox); |
578 |
|
579 |
lastRequest = downloadFile(wmsStatus, ulx, uly, lrx, lry, rasterBuf.getWidth(), rasterBuf.getHeight()); |
580 |
|
581 |
if (lastRequest == null) { |
582 |
return rasterBuf;
|
583 |
} |
584 |
|
585 |
try {
|
586 |
//El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
587 |
String serverName = bandList.getBand(0).getFileName(); |
588 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
589 |
bandList.getBand(i).setFileName(lastRequest.getPath()); |
590 |
} |
591 |
|
592 |
GdalDriver driver = new GdalDriver(lastRequest.getPath());
|
593 |
Buffer buf = driver.getWindowRaster(ulx, uly, lrx, lry, bandList, rasterBuf);
|
594 |
|
595 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
596 |
bandList.getBand(i).setFileName(serverName); |
597 |
} |
598 |
|
599 |
return buf;
|
600 |
} catch (NotSupportedExtensionException e) {
|
601 |
throw new RasterDriverException("Error building GdalDriver", e); |
602 |
} |
603 |
} |
604 |
|
605 |
/*
|
606 |
* (non-Javadoc)
|
607 |
* @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)
|
608 |
*/
|
609 |
public Buffer getWindowRaster(double ulx, double uly, double w, double h, |
610 |
BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException { |
611 |
Rectangle2D bBox = new Rectangle2D.Double(ulx, uly, w, h); |
612 |
lastWidthRequest = rasterBuf.getWidth(); |
613 |
lastHeightRequest = rasterBuf.getHeight(); |
614 |
WMSStatus wmsStatus = loadWMSStatus(bBox); |
615 |
|
616 |
lastRequest = downloadFile(wmsStatus, ulx, uly, ulx + w, uly - h, rasterBuf.getWidth(), rasterBuf.getHeight()); |
617 |
|
618 |
if (lastRequest == null) { |
619 |
return rasterBuf;
|
620 |
} |
621 |
|
622 |
try {
|
623 |
//El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
624 |
String serverName = bandList.getBand(0).getFileName(); |
625 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
626 |
bandList.getBand(i).setFileName(lastRequest.getPath()); |
627 |
} |
628 |
|
629 |
GdalDriver driver = new GdalDriver(lastRequest.getPath());
|
630 |
Buffer buf = driver.getWindowRaster(ulx, uly, w, h, bandList, rasterBuf, adjustToExtent);
|
631 |
|
632 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
633 |
bandList.getBand(i).setFileName(serverName); |
634 |
} |
635 |
|
636 |
return buf;
|
637 |
} catch (NotSupportedExtensionException e) {
|
638 |
throw new RasterDriverException("Error building GdalDriver", e); |
639 |
} |
640 |
} |
641 |
|
642 |
/*
|
643 |
* (non-Javadoc)
|
644 |
* @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)
|
645 |
*/
|
646 |
public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry, |
647 |
int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException { |
648 |
Rectangle2D bBox = new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly)); |
649 |
lastWidthRequest = rasterBuf.getWidth(); |
650 |
lastHeightRequest = rasterBuf.getHeight(); |
651 |
WMSStatus wmsStatus = loadWMSStatus(bBox); |
652 |
lastRequest = downloadFile(wmsStatus, ulx, uly, lrx, lry, rasterBuf.getWidth(), rasterBuf.getHeight()); |
653 |
|
654 |
if (lastRequest == null) { |
655 |
return rasterBuf;
|
656 |
} |
657 |
|
658 |
try {
|
659 |
//El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
660 |
String serverName = bandList.getBand(0).getFileName(); |
661 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
662 |
bandList.getBand(i).setFileName(lastRequest.getPath()); |
663 |
} |
664 |
|
665 |
GdalDriver driver = new GdalDriver(lastRequest.getPath());
|
666 |
Buffer buf = driver.getWindowRaster(ulx, uly, lrx, lry, bufWidth, bufHeight, bandList, rasterBuf, adjustToExtent);
|
667 |
|
668 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
669 |
bandList.getBand(i).setFileName(serverName); |
670 |
} |
671 |
|
672 |
return buf;
|
673 |
} catch (NotSupportedExtensionException e) {
|
674 |
throw new RasterDriverException("Error building GdalDriver", e); |
675 |
} |
676 |
} |
677 |
|
678 |
/*
|
679 |
* (non-Javadoc)
|
680 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
681 |
*/
|
682 |
public Buffer getWindowRaster(int x, int y, int w, int h, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException { |
683 |
Point2D p1 = rasterToWorld(new Point2D.Double(x, y)); |
684 |
Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h)); |
685 |
lastWidthRequest = rasterBuf.getWidth(); |
686 |
lastHeightRequest = rasterBuf.getHeight(); |
687 |
Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY())); |
688 |
WMSStatus wmsStatus = loadWMSStatus(bBox); |
689 |
|
690 |
lastRequest = downloadFile(wmsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight()); |
691 |
|
692 |
if (lastRequest == null) { |
693 |
return rasterBuf;
|
694 |
} |
695 |
|
696 |
GdalDriver driver = null;
|
697 |
try {
|
698 |
//El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
699 |
String serverName = bandList.getBand(0).getFileName(); |
700 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
701 |
bandList.getBand(i).setFileName(lastRequest.getPath()); |
702 |
} |
703 |
|
704 |
driver = new GdalDriver(lastRequest.getPath());
|
705 |
Buffer buf = driver.getWindowRaster(0, 0, w, h, rasterBuf.getWidth(), rasterBuf.getHeight(), bandList, rasterBuf); |
706 |
|
707 |
for (int i = 0; i < bandList.getBandCount(); i++) { |
708 |
bandList.getBand(i).setFileName(serverName); |
709 |
} |
710 |
|
711 |
return buf;
|
712 |
} catch (NotSupportedExtensionException e) {
|
713 |
throw new RasterDriverException("Error building GdalDriver", e); |
714 |
} |
715 |
} |
716 |
|
717 |
/*
|
718 |
* (non-Javadoc)
|
719 |
* @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)
|
720 |
*/
|
721 |
public Buffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException { |
722 |
lastWidthRequest = rasterBuf.getWidth(); |
723 |
lastHeightRequest = rasterBuf.getHeight(); |
724 |
return null; |
725 |
} |
726 |
|
727 |
/*
|
728 |
* (non-Javadoc)
|
729 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
|
730 |
*/
|
731 |
public int getBlockSize() { |
732 |
return 0; |
733 |
} |
734 |
|
735 |
/*
|
736 |
* (non-Javadoc)
|
737 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
|
738 |
*/
|
739 |
public void setAffineTransform(AffineTransform t){ |
740 |
|
741 |
} |
742 |
|
743 |
/*
|
744 |
* (non-Javadoc)
|
745 |
* @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
|
746 |
*/
|
747 |
public int getOverviewCount(int band) throws BandAccessException, RasterDriverException { |
748 |
return 0; |
749 |
} |
750 |
|
751 |
/*
|
752 |
* (non-Javadoc)
|
753 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
|
754 |
*/
|
755 |
public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException { |
756 |
return 0; |
757 |
} |
758 |
|
759 |
/*
|
760 |
* (non-Javadoc)
|
761 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
|
762 |
*/
|
763 |
public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException { |
764 |
return 0; |
765 |
} |
766 |
|
767 |
/*
|
768 |
* (non-Javadoc)
|
769 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
|
770 |
*/
|
771 |
public boolean overviewsSupport() { |
772 |
return false; |
773 |
} |
774 |
|
775 |
/*
|
776 |
* (non-Javadoc)
|
777 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
|
778 |
*/
|
779 |
public boolean isReproyectable() { |
780 |
return false; |
781 |
} |
782 |
|
783 |
/*
|
784 |
* (non-Javadoc)
|
785 |
* @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider#getName()
|
786 |
*/
|
787 |
public String getName() { |
788 |
return NAME;
|
789 |
} |
790 |
|
791 |
/**
|
792 |
* Convierte un punto desde coordenadas pixel a coordenadas del mundo.
|
793 |
* @param pt Punto a transformar
|
794 |
* @return punto transformado en coordenadas del mundo
|
795 |
*/
|
796 |
public Point2D rasterToWorld(Point2D pt) { |
797 |
Point2D p = new Point2D.Double(); |
798 |
getAffineTransform().transform(pt, p); |
799 |
return p;
|
800 |
} |
801 |
|
802 |
/**
|
803 |
* Convierte un punto desde del mundo a coordenadas pixel.
|
804 |
* @param pt Punto a transformar
|
805 |
* @return punto transformado en coordenadas pixel
|
806 |
*/
|
807 |
public Point2D worldToRaster(Point2D pt) { |
808 |
Point2D p = new Point2D.Double(); |
809 |
try {
|
810 |
getAffineTransform().inverseTransform(pt, p); |
811 |
} catch (NoninvertibleTransformException e) { |
812 |
return pt;
|
813 |
} |
814 |
return p;
|
815 |
} |
816 |
|
817 |
/*
|
818 |
* (non-Javadoc)
|
819 |
* @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
|
820 |
*/
|
821 |
public void setStatus(RasterProvider provider) { |
822 |
if(provider instanceof WMSProvider) { |
823 |
} |
824 |
} |
825 |
|
826 |
/*
|
827 |
* (non-Javadoc)
|
828 |
* @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLastRequest()
|
829 |
*/
|
830 |
public File getLastRequest() { |
831 |
return lastRequest;
|
832 |
} |
833 |
} |