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