gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.io / src / main / java / org / gvsig / raster / wms / io / WMSProvider.java @ 2484
History | View | Annotate | Download (39.6 KB)
1 | 2484 | nbrodin | /* 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.wms.io; |
||
23 | |||
24 | import java.awt.Image; |
||
25 | import java.awt.geom.AffineTransform; |
||
26 | import java.awt.geom.NoninvertibleTransformException; |
||
27 | import java.awt.geom.Point2D; |
||
28 | import java.awt.geom.Rectangle2D; |
||
29 | import java.io.File; |
||
30 | import java.io.IOException; |
||
31 | import java.net.URL; |
||
32 | import java.util.Hashtable; |
||
33 | import java.util.List; |
||
34 | import java.util.Vector; |
||
35 | |||
36 | import javax.swing.ImageIcon; |
||
37 | |||
38 | import org.cresques.cts.ICoordTrans; |
||
39 | import org.cresques.cts.IProjection; |
||
40 | import org.gvsig.compat.net.ICancellable; |
||
41 | import org.gvsig.fmap.crs.CRSFactory; |
||
42 | import org.gvsig.fmap.dal.DALLocator; |
||
43 | import org.gvsig.fmap.dal.DataStore; |
||
44 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
45 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
46 | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
||
47 | import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
||
48 | import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
||
49 | import org.gvsig.fmap.dal.coverage.exception.BandAccessException; |
||
50 | import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException; |
||
51 | import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
||
52 | import org.gvsig.fmap.dal.coverage.exception.InfoByPointException; |
||
53 | import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
||
54 | import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
||
55 | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
||
56 | import org.gvsig.fmap.dal.coverage.exception.QueryException; |
||
57 | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
||
58 | import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException; |
||
59 | import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
||
60 | import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
||
61 | import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer; |
||
62 | import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
||
63 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
64 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
65 | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
||
66 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
67 | import org.gvsig.metadata.MetadataLocator; |
||
68 | import org.gvsig.raster.cache.tile.provider.TileServer; |
||
69 | import org.gvsig.raster.impl.buffer.SpiRasterQuery; |
||
70 | import org.gvsig.raster.impl.datastruct.BandListImpl; |
||
71 | import org.gvsig.raster.impl.datastruct.DatasetBandImpl; |
||
72 | import org.gvsig.raster.impl.datastruct.ExtentImpl; |
||
73 | import org.gvsig.raster.impl.provider.AbstractRasterProvider; |
||
74 | import org.gvsig.raster.impl.provider.RasterProvider; |
||
75 | import org.gvsig.raster.impl.provider.RemoteRasterProvider; |
||
76 | import org.gvsig.raster.impl.store.DefaultRasterStore; |
||
77 | import org.gvsig.raster.impl.store.DefaultStoreFactory; |
||
78 | import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation; |
||
79 | import org.gvsig.raster.impl.store.properties.DataStoreTransparency; |
||
80 | import org.gvsig.raster.impl.store.properties.RemoteStoreHistogram; |
||
81 | import org.gvsig.raster.util.DefaultProviderServices; |
||
82 | import org.gvsig.raster.wms.io.downloader.WMSTileServer; |
||
83 | import org.gvsig.remoteclient.utils.Utilities; |
||
84 | import org.gvsig.remoteclient.wms.WMSStatus; |
||
85 | import org.gvsig.tools.ToolsLocator; |
||
86 | import org.slf4j.Logger; |
||
87 | import org.slf4j.LoggerFactory; |
||
88 | /**
|
||
89 | * Clase que representa al driver de acceso a datos de wms.
|
||
90 | *
|
||
91 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
92 | */
|
||
93 | public class WMSProvider extends AbstractRasterProvider implements RemoteRasterProvider { |
||
94 | public static String NAME = "Wms Store"; |
||
95 | public static String DESCRIPTION = "Wms Raster file"; |
||
96 | public static final String METADATA_DEFINITION_NAME = "WmsStore"; |
||
97 | private static final Logger logger = LoggerFactory.getLogger(WMSProvider.class); |
||
98 | private static final int FIXED_SIZE = 800; |
||
99 | private Extent viewRequest = null; |
||
100 | private static Hashtable<String, WMSConnector> |
||
101 | drivers = new Hashtable<String, WMSConnector> (); |
||
102 | private boolean open = false; |
||
103 | private DataStoreTransparency fileTransparency = null; |
||
104 | private final int minTilePrintWidth = 12; |
||
105 | private final int minTilePrintHeight = 12; |
||
106 | //The out size depends on the last request
|
||
107 | private int lastWidthRequest = 0; |
||
108 | private int lastHeightRequest = 0; |
||
109 | //Only for fixed size. Complete extent and FIXED_SIZE in long side
|
||
110 | private File fileLayerPixelSize = null; |
||
111 | private File lastRequest = null; |
||
112 | private AbstractRasterProvider lastRequestProvider = null; |
||
113 | |||
114 | public static void register() { |
||
115 | DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
||
116 | if (dataman != null && !dataman.getStoreProviders().contains(NAME)) { |
||
117 | dataman.registerStoreProvider(NAME, |
||
118 | WMSProvider.class, WMSDataParametersImpl.class); |
||
119 | } |
||
120 | |||
121 | if (!dataman.getExplorerProviders().contains(WMSServerExplorer.NAME)) {
|
||
122 | dataman.registerExplorerProvider(WMSServerExplorer.NAME, WMSServerExplorer.class, WMSServerExplorerParameters.class); |
||
123 | } |
||
124 | dataman.registerStoreFactory(NAME, DefaultStoreFactory.class); |
||
125 | } |
||
126 | |||
127 | public WMSProvider() throws NotSupportedExtensionException { |
||
128 | super();
|
||
129 | } |
||
130 | |||
131 | public void registerTileProviderFormats(Class<RasterProvider> c) { |
||
132 | |||
133 | } |
||
134 | |||
135 | /**
|
||
136 | * Constructor. Abre el dataset.
|
||
137 | * @param proj Proyecci?n
|
||
138 | * @param fName Nombre del fichero
|
||
139 | * @throws NotSupportedExtensionException
|
||
140 | */
|
||
141 | public WMSProvider(String params) throws InitializeException { |
||
142 | super(params);
|
||
143 | if(params instanceof String) { |
||
144 | WMSDataParameters p = new WMSDataParametersImpl();
|
||
145 | p.setURI((String)params);
|
||
146 | super.init(p, null, ToolsLocator.getDynObjectManager() |
||
147 | .createDynObject( |
||
148 | MetadataLocator.getMetadataManager().getDefinition( |
||
149 | DataStore.METADATA_DEFINITION_NAME))); |
||
150 | init(p, null);
|
||
151 | } |
||
152 | } |
||
153 | |||
154 | public WMSProvider(WMSDataParameters params,
|
||
155 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
156 | super(params, storeServices, ToolsLocator.getDynObjectManager()
|
||
157 | .createDynObject( |
||
158 | MetadataLocator.getMetadataManager().getDefinition( |
||
159 | DataStore.METADATA_DEFINITION_NAME))); |
||
160 | init(params, storeServices); |
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Gets the connector from the URL
|
||
165 | * @return
|
||
166 | * @throws RemoteServiceException
|
||
167 | */
|
||
168 | public WMSConnector getConnector() throws RemoteServiceException { |
||
169 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
170 | URL url = null; |
||
171 | try {
|
||
172 | url = new URL(p.getURI()); |
||
173 | } catch (Exception e) { |
||
174 | throw new RemoteServiceException("Malformed URL",e); |
||
175 | } |
||
176 | try {
|
||
177 | return WMSProvider.getConnectorFromURL(url, false); |
||
178 | } catch (IOException e) { |
||
179 | throw new RemoteServiceException("Error getting the connector",e); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /**
|
||
184 | * Crea las referencias al fichero y carga
|
||
185 | * las estructuras con la informaci?n y los metadatos.
|
||
186 | * @param proj Proyecci?n
|
||
187 | * @param param Parametros de carga
|
||
188 | * @throws NotSupportedExtensionException
|
||
189 | */
|
||
190 | public void init (DataStoreParameters params, |
||
191 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
192 | setParam(storeServices, params); |
||
193 | setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE}); |
||
194 | bandCount = 3;
|
||
195 | open = true;
|
||
196 | try {
|
||
197 | loadInitialInfo(); |
||
198 | } catch (RasterDriverException e) {
|
||
199 | throw new InitializeException(e.getMessage(), e); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /**
|
||
204 | * When a WMS provider is opened the information of data type, number of bands and transparency
|
||
205 | * is not available. Only after the first time a raster has been downloaded it can be know.
|
||
206 | * @param newDataType
|
||
207 | * @param buf
|
||
208 | * @param bandList
|
||
209 | * @return
|
||
210 | * @throws RasterDriverException
|
||
211 | */
|
||
212 | private void loadInitialInfo() throws RasterDriverException { |
||
213 | WMSDataParametersImpl p = (WMSDataParametersImpl)parameters; |
||
214 | Extent ext = getExtent(); |
||
215 | Rectangle2D bBox = ext.toRectangle2D();
|
||
216 | int w = 0; |
||
217 | int h = 0; |
||
218 | if(ext.width() > ext.height()) {
|
||
219 | w = 200;
|
||
220 | h = (int)((ext.height() * w) / ext.width());
|
||
221 | } else {
|
||
222 | h = 200;
|
||
223 | w = (int)((ext.width() * h) / ext.height());
|
||
224 | } |
||
225 | p.setWidth(w); |
||
226 | p.setHeight(h); |
||
227 | p.setExtent(bBox); |
||
228 | WMSStatus wmsStatus = loadWMSStatus(bBox); |
||
229 | |||
230 | lastRequest = downloadFile(wmsStatus, ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), w, h); |
||
231 | AbstractRasterProvider driver; |
||
232 | try {
|
||
233 | driver = DefaultProviderServices.loadProvider(lastRequest); |
||
234 | setDataType(driver.getDataType()); |
||
235 | bandCount = driver.getBandCount(); |
||
236 | if(bandCount > 3) { |
||
237 | getColorInterpretation().setColorInterpValue(3, DataStoreColorInterpretation.ALPHA_BAND);
|
||
238 | getTransparency().setTransparencyBand(3);
|
||
239 | } |
||
240 | setColorTable(driver.getColorTable()); |
||
241 | driver.close(); |
||
242 | } catch (ProviderNotRegisteredException e) {
|
||
243 | throw new RasterDriverException("", e); |
||
244 | } catch (InitializeException e) {
|
||
245 | throw new RasterDriverException("", e); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | public static final WMSConnector getConnectorFromURL(URL url, boolean updating) throws IOException { |
||
250 | WMSConnector drv = null;
|
||
251 | if(!updating) {
|
||
252 | drv = (WMSConnector) drivers.get(url.toString()); |
||
253 | } else {
|
||
254 | if(drivers.get(url.toString()) != null) |
||
255 | drivers.remove(url.toString()); |
||
256 | } |
||
257 | |||
258 | if (drv == null) { |
||
259 | drv = new WMSConnector(url);
|
||
260 | drivers.put(url.toString(), drv); |
||
261 | } |
||
262 | |||
263 | return drv;
|
||
264 | } |
||
265 | |||
266 | /**
|
||
267 | * Obtiene el objeto que contiene que contiene la interpretaci?n de
|
||
268 | * color por banda
|
||
269 | * @return
|
||
270 | */
|
||
271 | public ColorInterpretation getColorInterpretation() {
|
||
272 | if(super.getColorInterpretation() == null) { |
||
273 | ColorInterpretation colorInterpretation = new DataStoreColorInterpretation(getBandCount());
|
||
274 | if(getBandCount() == 1 || getBandCount() == 2) |
||
275 | colorInterpretation = DataStoreColorInterpretation.createGrayInterpretation(); |
||
276 | if(getBandCount() == 3) |
||
277 | colorInterpretation = DataStoreColorInterpretation.createRGBInterpretation(); |
||
278 | if(getBandCount() == 4) |
||
279 | colorInterpretation = DataStoreColorInterpretation.createRGBAInterpretation(); |
||
280 | setColorInterpretation(colorInterpretation); |
||
281 | } |
||
282 | return super.getColorInterpretation(); |
||
283 | } |
||
284 | |||
285 | public AffineTransform getAffineTransform() { |
||
286 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
287 | Extent e = getExtent(); |
||
288 | if(p.isSizeFixed()) {
|
||
289 | double psX = e.width() / (p.getWidth() - 1); |
||
290 | double psY = -(e.height() / (p.getHeight() - 1)); |
||
291 | ownTransformation = new AffineTransform( |
||
292 | psX , |
||
293 | 0,
|
||
294 | 0,
|
||
295 | psY, |
||
296 | e.getULX() - (psX / 2),
|
||
297 | e.getULY() - (psY / 2));
|
||
298 | } else {
|
||
299 | double psX = e.width() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest); |
||
300 | double psY = -(e.height() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest)); |
||
301 | ownTransformation = new AffineTransform( |
||
302 | psX, |
||
303 | 0,
|
||
304 | 0,
|
||
305 | psY, |
||
306 | e.getULX() - (psX / 2),
|
||
307 | e.getULY() - (psY / 2));
|
||
308 | /*ownTransformation = new AffineTransform(
|
||
309 | p.getExtent().getWidth() / (lastWidthRequest <= 0 ? p.getWidth() : lastWidthRequest) ,
|
||
310 | 0,
|
||
311 | 0,
|
||
312 | -(p.getExtent().getHeight() / (lastHeightRequest <= 0 ? p.getHeight() : lastHeightRequest)),
|
||
313 | p.getExtent().getMinX(),
|
||
314 | p.getExtent().getMinY());*/
|
||
315 | } |
||
316 | externalTransformation = (AffineTransform) ownTransformation.clone();
|
||
317 | return ownTransformation;
|
||
318 | } |
||
319 | |||
320 | /**
|
||
321 | * Calcula el extent en coordenadas del mundo real
|
||
322 | * @return Extent
|
||
323 | */
|
||
324 | public Extent getExtent() {
|
||
325 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
326 | Vector<?> layerNames = Utilities.createVector(p.getLayerQuery(), ","); |
||
327 | |||
328 | String[] ln = new String[layerNames.size()]; |
||
329 | for (int i = 0; i < ln.length; i++) { |
||
330 | ln[i] = (String)layerNames.get(i);
|
||
331 | } |
||
332 | Rectangle2D r = null; |
||
333 | try {
|
||
334 | r = getConnector().getLayersExtent(ln, p.getSRSCode()); |
||
335 | if (r == null) { |
||
336 | String latLonID = "EPSG:4326"; |
||
337 | r = getConnector().getLayersExtent(ln, latLonID); |
||
338 | if (r == null) { |
||
339 | r = getConnector().getLayersExtent(ln, "CRS:84");
|
||
340 | } |
||
341 | |||
342 | IProjection reqProj = CRSFactory.getCRS(p.getSRSCode()); |
||
343 | IProjection latLonProj = CRSFactory.getCRS(latLonID); |
||
344 | if ((reqProj != null) && (latLonProj != null)) { |
||
345 | ICoordTrans ct = latLonProj.getCT(reqProj); |
||
346 | Rectangle2D reprojectedRect = ct.convert(r);
|
||
347 | r = reprojectedRect; |
||
348 | } |
||
349 | } |
||
350 | } catch (RemoteServiceException e) {
|
||
351 | } |
||
352 | if(r == null) |
||
353 | return null; |
||
354 | return new ExtentImpl(r.getMinX(), r.getMaxY(), r.getMaxX(), r.getMinY()); |
||
355 | } |
||
356 | |||
357 | public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException { |
||
358 | return getConnector().getLayersExtent(new String[]{layerName}, srs); |
||
359 | } |
||
360 | |||
361 | public RasterProvider load() {
|
||
362 | return this; |
||
363 | } |
||
364 | |||
365 | public boolean isOpen() { |
||
366 | return open;
|
||
367 | } |
||
368 | |||
369 | public void close() { |
||
370 | open = false;
|
||
371 | } |
||
372 | |||
373 | public Transparency getTransparency() { |
||
374 | if(fileTransparency == null) |
||
375 | fileTransparency = new DataStoreTransparency(getColorInterpretation());
|
||
376 | return fileTransparency;
|
||
377 | } |
||
378 | |||
379 | public String translateFileName(String fileName) { |
||
380 | return fileName;
|
||
381 | } |
||
382 | |||
383 | public void setView(Extent e) { |
||
384 | viewRequest = e; |
||
385 | } |
||
386 | |||
387 | public Extent getView() {
|
||
388 | return viewRequest;
|
||
389 | } |
||
390 | |||
391 | public double getWidth() { |
||
392 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
393 | if(p.isSizeFixed()) {
|
||
394 | return p.getWidth();
|
||
395 | } |
||
396 | if (lastWidthRequest <= 0) |
||
397 | return p.getWidth();
|
||
398 | if (lastWidthRequest < minTilePrintWidth)
|
||
399 | return minTilePrintWidth;
|
||
400 | return lastWidthRequest;
|
||
401 | } |
||
402 | |||
403 | public double getHeight() { |
||
404 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
405 | if(p.isSizeFixed()) {
|
||
406 | return p.getHeight();
|
||
407 | } |
||
408 | if (lastHeightRequest <= 0) |
||
409 | return p.getHeight();
|
||
410 | if (lastHeightRequest < minTilePrintHeight)
|
||
411 | return minTilePrintHeight;
|
||
412 | return lastHeightRequest;
|
||
413 | } |
||
414 | |||
415 | /**
|
||
416 | * Gets WMS parameters
|
||
417 | * @return
|
||
418 | */
|
||
419 | public WMSDataParameters getParameters() {
|
||
420 | return (WMSDataParameters)parameters;
|
||
421 | } |
||
422 | |||
423 | public Object readCompleteLine(int line, int band) |
||
424 | throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
|
||
425 | return null; |
||
426 | } |
||
427 | |||
428 | /**
|
||
429 | * When the remote layer has fixed size this method downloads the file and return its reference.
|
||
430 | * File layer has in the long side FIXED_SIZE pixels and the bounding box is complete. This file could be
|
||
431 | * useful to build an histogram or calculate statistics. This represents a sample of data.
|
||
432 | * @return
|
||
433 | * @throws RasterDriverException
|
||
434 | */
|
||
435 | public File getFileLayer() throws RasterDriverException { |
||
436 | if(fileLayerPixelSize != null) |
||
437 | return fileLayerPixelSize;
|
||
438 | |||
439 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
440 | Extent e = getExtent(); |
||
441 | Rectangle2D bBox = new Rectangle2D.Double(e.getULX(), e.getLRY(), e.width(), e.height()); |
||
442 | WMSStatus wmsStatus = loadWMSStatus(bBox); |
||
443 | |||
444 | if(!p.isSizeFixed()) {
|
||
445 | int w = 0; |
||
446 | int h = 0; |
||
447 | if(e.width() > e.height()) {
|
||
448 | w = FIXED_SIZE; |
||
449 | h = (int)((e.height() * FIXED_SIZE) / e.width());
|
||
450 | } else {
|
||
451 | h = FIXED_SIZE; |
||
452 | w = (int)((e.width() * FIXED_SIZE) / e.height());
|
||
453 | } |
||
454 | wmsStatus.setWidth(w); |
||
455 | wmsStatus.setHeight(h); |
||
456 | fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), w, h); |
||
457 | } else {
|
||
458 | fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), |
||
459 | p.getFixedSize().width, p.getFixedSize().height); |
||
460 | } |
||
461 | return fileLayerPixelSize;
|
||
462 | } |
||
463 | |||
464 | /**
|
||
465 | * Reads a complete block of data and returns an tridimensional array of the right type. This function is useful
|
||
466 | * to read a file very fast without setting a view. In a WMS service when the size is fixed then it will read the
|
||
467 | * entire image but when the source hasn't pixel size it will read a sample of data. This set of data will have
|
||
468 | * the size defined in FIXED_SIZE.
|
||
469 | *
|
||
470 | * @param pos Posici?n donde se empieza a leer
|
||
471 | * @param blockHeight Altura m?xima del bloque leido
|
||
472 | * @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
|
||
473 | * @throws InvalidSetViewException
|
||
474 | * @throws FileNotOpenException
|
||
475 | * @throws RasterDriverException
|
||
476 | */
|
||
477 | public Object readBlock(int pos, int blockHeight, double scale) |
||
478 | throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
|
||
479 | File lastFile = getFileLayer();
|
||
480 | BandList bandList = new BandListImpl();
|
||
481 | for (int i = 0; i < 3; i++) { |
||
482 | try {
|
||
483 | bandList.addBand(new DatasetBandImpl(lastFile.getPath(), pos, Buffer.TYPE_BYTE, 3)); |
||
484 | } catch (BandNotFoundInListException e1) {
|
||
485 | } |
||
486 | } |
||
487 | bandList.setDrawableBands(new int[]{0, 1, 2}); |
||
488 | |||
489 | try {
|
||
490 | lastRequestProvider = openLastRequest(); |
||
491 | return lastRequestProvider.readBlock(pos, blockHeight, scale);
|
||
492 | } catch (ProviderNotRegisteredException exc) {
|
||
493 | throw new RasterDriverException("Error building GdalDriver", exc); |
||
494 | } catch (InitializeException exc) {
|
||
495 | throw new RasterDriverException("Error building GdalDriver", exc); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | public double getLastRequestHeight() throws RasterDriverException { |
||
500 | if(lastRequestProvider == null) { |
||
501 | try {
|
||
502 | lastRequestProvider = openLastRequest(); |
||
503 | } catch (ProviderNotRegisteredException e) {
|
||
504 | throw new RasterDriverException("Error building GdalDriver", e); |
||
505 | } catch (InitializeException e) {
|
||
506 | throw new RasterDriverException("Error building GdalDriver", e); |
||
507 | } |
||
508 | } |
||
509 | return lastRequestProvider.getHeight();
|
||
510 | } |
||
511 | |||
512 | public double getLastRequestWidth() throws RasterDriverException { |
||
513 | if(lastRequestProvider == null) { |
||
514 | try {
|
||
515 | lastRequestProvider = openLastRequest(); |
||
516 | } catch (ProviderNotRegisteredException e) {
|
||
517 | throw new RasterDriverException("Error building GdalDriver", e); |
||
518 | } catch (InitializeException e) {
|
||
519 | throw new RasterDriverException("Error building GdalDriver", e); |
||
520 | } |
||
521 | } |
||
522 | return lastRequestProvider.getWidth();
|
||
523 | } |
||
524 | |||
525 | public File getLastRequest() { |
||
526 | return lastRequest;
|
||
527 | } |
||
528 | |||
529 | public Buffer getBufferLastRequest() throws ProcessInterruptedException, RasterDriverException { |
||
530 | try {
|
||
531 | lastRequestProvider = openLastRequest(); |
||
532 | return getDownloadedRaster(lastRequestProvider);
|
||
533 | } catch (ProviderNotRegisteredException e) {
|
||
534 | throw new RasterDriverException("Error building GdalDriver", e); |
||
535 | } catch (InitializeException e) {
|
||
536 | throw new RasterDriverException("Error building GdalDriver", e); |
||
537 | } catch (QueryException e) {
|
||
538 | throw new RasterDriverException("Error building GdalDriver", e); |
||
539 | } |
||
540 | } |
||
541 | |||
542 | /**
|
||
543 | * Opens the last request downloaded
|
||
544 | * @return
|
||
545 | * @throws ProviderNotRegisteredException
|
||
546 | * @throws InitializeException
|
||
547 | * @throws RasterDriverException
|
||
548 | */
|
||
549 | private AbstractRasterProvider openLastRequest() throws ProviderNotRegisteredException, InitializeException, RasterDriverException { |
||
550 | if(lastRequestProvider != null) |
||
551 | lastRequestProvider.close(); |
||
552 | File lastFile = getFileLayer();
|
||
553 | lastRequestProvider = DefaultProviderServices.loadProvider(new File(lastFile.getPath())); |
||
554 | setColorTable(lastRequestProvider.getColorTable()); |
||
555 | return lastRequestProvider;
|
||
556 | } |
||
557 | |||
558 | public Object getData(int x, int y, int band) |
||
559 | throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
|
||
560 | return null; |
||
561 | } |
||
562 | |||
563 | /**
|
||
564 | * Gets the georeferencing file name form a raster file
|
||
565 | * @param file
|
||
566 | * a raster file
|
||
567 | * @return
|
||
568 | * a georeferencing file
|
||
569 | */
|
||
570 | private String getWorldFile(String file){ |
||
571 | String worldFile = file;
|
||
572 | int index = file.lastIndexOf("."); |
||
573 | if (index > 0){ |
||
574 | worldFile = file.substring(0, index) + getExtensionWorldFile();
|
||
575 | } |
||
576 | return worldFile;
|
||
577 | } |
||
578 | |||
579 | /**
|
||
580 | * Obtiene la extensi?n del fichero de georreferenciaci?n
|
||
581 | * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
|
||
582 | * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
|
||
583 | */
|
||
584 | private String getExtensionWorldFile() { |
||
585 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
586 | String extWorldFile = ".wld"; |
||
587 | if(p.getFormat().equals("image/tif") || p.getFormat().equals("image/tiff")) { |
||
588 | extWorldFile = ".tfw";
|
||
589 | } |
||
590 | return extWorldFile;
|
||
591 | } |
||
592 | |||
593 | public WMSStatus loadWMSStatus(Rectangle2D bBox) { |
||
594 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
595 | WMSStatus wmsStatus = new WMSStatus();
|
||
596 | wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ",")); |
||
597 | wmsStatus.setSrs(p.getSRSCode()); |
||
598 | wmsStatus.setFormat(p.getFormat()); |
||
599 | wmsStatus.setInfoFormat(p.getInfoFormat()); |
||
600 | List<RemoteWMSStyle> listStyles = p.getStyles();
|
||
601 | Vector<?> v = listStyles != null ? new Vector<RemoteWMSStyle>(listStyles) : null; |
||
602 | wmsStatus.setStyles(v); |
||
603 | wmsStatus.setDimensions(p.getDimensions()); |
||
604 | wmsStatus.setTransparency(p.isWmsTransparent()); |
||
605 | wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap")); |
||
606 | if(p.isSizeFixed()) {
|
||
607 | wmsStatus.setExtent(getExtent().toRectangle2D()); |
||
608 | } else
|
||
609 | wmsStatus.setExtent(bBox); |
||
610 | wmsStatus.setHeight(p.getHeight()); |
||
611 | wmsStatus.setWidth(p.getWidth()); |
||
612 | IProjection proj = (IProjection)p.getSRS(); |
||
613 | if(proj != null) |
||
614 | wmsStatus.setProjected(proj.isProjected()); |
||
615 | return wmsStatus;
|
||
616 | } |
||
617 | |||
618 | /**
|
||
619 | * This function downloads the file and creates the georeferencing file
|
||
620 | * @param wmsStatus
|
||
621 | * @param ulx
|
||
622 | * @param uly
|
||
623 | * @param lrx
|
||
624 | * @param lry
|
||
625 | * @param w
|
||
626 | * @param h
|
||
627 | * @return
|
||
628 | * @throws RasterDriverException
|
||
629 | */
|
||
630 | private File downloadFile(WMSStatus wmsStatus, double ulx, double uly, double lrx, double lry, int w, int h) throws RasterDriverException { |
||
631 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
632 | try {
|
||
633 | lastRequest = getConnector().getMap(wmsStatus, ((WMSDataParameters)parameters).getCancellable()); |
||
634 | } catch (RemoteServiceException e) {
|
||
635 | throw new RasterDriverException(e.getMessage(), e); |
||
636 | } |
||
637 | |||
638 | if(lastRequest == null) |
||
639 | return null; |
||
640 | |||
641 | String nameWorldFile = getWorldFile(lastRequest.getPath());
|
||
642 | try {
|
||
643 | if(p.isSizeFixed()) {
|
||
644 | Extent e = getExtent(); |
||
645 | fileUtil.createWorldFile(nameWorldFile, e, p.getWidth(), p.getHeight()); |
||
646 | } else {
|
||
647 | fileUtil.createWorldFile(nameWorldFile, new ExtentImpl(ulx, uly, lrx, lry), w, h);
|
||
648 | } |
||
649 | } catch (IOException e) { |
||
650 | throw new RasterDriverException("Error creating world file", e); |
||
651 | } |
||
652 | |||
653 | return lastRequest;
|
||
654 | } |
||
655 | |||
656 | @Override
|
||
657 | public void loadBuffer(SpiRasterQuery q) |
||
658 | throws ProcessInterruptedException, RasterDriverException {
|
||
659 | Extent bbox = q.getRequestBoundingBox(); |
||
660 | lastWidthRequest = q.getBufWidth(); |
||
661 | lastHeightRequest = q.getBufHeight(); |
||
662 | WMSStatus wmsStatus = loadWMSStatus(bbox.toRectangle2D()); |
||
663 | |||
664 | lastRequest = downloadFile(wmsStatus, |
||
665 | bbox.getULX(), |
||
666 | bbox.getULY(), |
||
667 | bbox.getLRX(), |
||
668 | bbox.getLRY(), |
||
669 | q.getBufWidth(), |
||
670 | q.getBufHeight()); |
||
671 | |||
672 | if (lastRequest == null) { |
||
673 | return;
|
||
674 | } |
||
675 | |||
676 | Buffer b = null; |
||
677 | try {
|
||
678 | b = getDownloadedRaster(lastRequest); |
||
679 | } catch (QueryException e) {
|
||
680 | throw new RasterDriverException("Error building GdalDriver", e); |
||
681 | } catch (ProviderNotRegisteredException e) {
|
||
682 | throw new RasterDriverException("Error building GdalDriver", e); |
||
683 | } catch (InitializeException e) {
|
||
684 | throw new RasterDriverException("Error building GdalDriver", e); |
||
685 | } |
||
686 | q.setBufferResult(b); |
||
687 | } |
||
688 | |||
689 | private Buffer getDownloadedRaster(File f) throws ProcessInterruptedException, QueryException, ProviderNotRegisteredException, InitializeException { |
||
690 | AbstractRasterProvider provider = DefaultProviderServices.loadProvider(new File(f.getPath())); |
||
691 | setColorTable(provider.getColorTable()); |
||
692 | return getDownloadedRaster(provider);
|
||
693 | } |
||
694 | |||
695 | private Buffer getDownloadedRaster(AbstractRasterProvider provider) throws ProcessInterruptedException, QueryException, ProviderNotRegisteredException, InitializeException { |
||
696 | DefaultRasterStore store = new DefaultRasterStore();
|
||
697 | store.setProvider(provider); |
||
698 | |||
699 | RasterQuery q = RasterLocator.getManager().createQuery(); |
||
700 | q.setAreaOfInterest(); |
||
701 | if(getColorInterpretation() != null) |
||
702 | q.setDrawableBands(getColorInterpretation().buildRenderBands()); |
||
703 | if(store.getBandCount() > 3) |
||
704 | q.forceARGBRequest(); |
||
705 | else
|
||
706 | q.forceRGBRequest(); |
||
707 | |||
708 | Buffer buf = store.query(q);
|
||
709 | |||
710 | store.close(); |
||
711 | return buf;
|
||
712 | } |
||
713 | |||
714 | /**
|
||
715 | * Assigns the list of bands RGB and read a window of data
|
||
716 | * @param rasterBuf
|
||
717 | * @param bandList
|
||
718 | * @param lastFile
|
||
719 | * @param ulx
|
||
720 | * @param uly
|
||
721 | * @param lrx
|
||
722 | * @param lry
|
||
723 | * @return
|
||
724 | * @throws RasterDriverException
|
||
725 | * @throws ProcessInterruptedException
|
||
726 | */
|
||
727 | /*public Buffer getBuffer(Buffer rasterBuf, BandList bandList, File lastFile,
|
||
728 | double ulx, double uly, double lrx, double lry) throws RasterDriverException, ProcessInterruptedException {
|
||
729 | try {
|
||
730 | //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
731 | String serverName = bandList.getBand(0).getFileName();
|
||
732 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
733 | bandList.getBand(i).setFileName(lastFile.getPath());
|
||
734 | }
|
||
735 | |||
736 | AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastFile.getPath()));
|
||
737 | setColorTable(driver.getColorTable());
|
||
738 | |||
739 | DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
|
||
740 | q.setAreaOfInterest(new ExtentImpl(ulx, uly, lrx, lry));
|
||
741 | q.setBandList(bandList);
|
||
742 | q.setBuffer(rasterBuf);
|
||
743 | Buffer buf = driver.getDataSet(q);
|
||
744 | |||
745 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
746 | bandList.getBand(i).setFileName(serverName);
|
||
747 | }
|
||
748 | |||
749 | return buf;
|
||
750 | } catch (ProviderNotRegisteredException e) {
|
||
751 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
752 | } catch (InitializeException e) {
|
||
753 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
754 | }
|
||
755 | }*/
|
||
756 | |||
757 | /*public void getWindow(Extent ex, int bufWidth, int bufHeight,
|
||
758 | BandList bandList, TileListener listener, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
|
||
759 | |||
760 | Buffer raster = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, 3, true);
|
||
761 | getWindow(ex, bufWidth, bufHeight, bandList, raster, true, null);
|
||
762 | raster.setDataExtent(ex.toRectangle2D());
|
||
763 | |||
764 | TileCacheManager m = TileCacheLocator.getManager();
|
||
765 | org.gvsig.raster.cache.tile.Tile t = m.createTile(-1, 0, 0);
|
||
766 | t.setData(new Object[]{raster});
|
||
767 | t.setUl(new Point2D.Double(ex.getULX(), ex.getULY()));
|
||
768 | t.setLr(new Point2D.Double(ex.getLRX(), ex.getLRY()));
|
||
769 | t.setDownloaderParams("AffineTransform", getAffineTransform());
|
||
770 | t.setDownloaderParams("Tiling", new Boolean(false));
|
||
771 | try {
|
||
772 | listener.tileReady(t);
|
||
773 | } catch (TileGettingException e) {
|
||
774 | throw new RasterDriverException("Error throwing a tile", e);
|
||
775 | }
|
||
776 | |||
777 | //listener.nextBuffer(raster, null, new ExtentImpl(minX, minY, maxX, maxY), getAffineTransform(), null, false);
|
||
778 | listener.endReading();
|
||
779 | }*/
|
||
780 | |||
781 | /*public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf, TaskStatus status)
|
||
782 | throws ProcessInterruptedException, RasterDriverException {
|
||
783 | Rectangle2D bBox = ex.toRectangle2D();
|
||
784 | lastWidthRequest = rasterBuf.getWidth();
|
||
785 | lastHeightRequest = rasterBuf.getHeight();
|
||
786 | WMSStatus wmsStatus = loadWMSStatus(bBox);
|
||
787 | |||
788 | lastRequest = downloadFile(wmsStatus, ex.getULX(), ex.getULY(), ex.getLRX(), ex.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
|
||
789 | |||
790 | if (lastRequest == null) {
|
||
791 | return rasterBuf;
|
||
792 | }
|
||
793 | |||
794 | try {
|
||
795 | //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
796 | String serverName = bandList.getBand(0).getFileName();
|
||
797 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
798 | bandList.getBand(i).setFileName(lastRequest.getPath());
|
||
799 | }
|
||
800 | |||
801 | AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
|
||
802 | setColorTable(driver.getColorTable());
|
||
803 | |||
804 | DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
|
||
805 | q.setAreaOfInterest(ex);
|
||
806 | q.setBandList(bandList);
|
||
807 | q.setBuffer(rasterBuf);
|
||
808 | Buffer buf = driver.getDataSet(q);
|
||
809 | |||
810 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
811 | bandList.getBand(i).setFileName(serverName);
|
||
812 | }
|
||
813 | driver.close();
|
||
814 | return buf;
|
||
815 | } catch (ProviderNotRegisteredException e) {
|
||
816 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
817 | } catch (InitializeException e) {
|
||
818 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
819 | }
|
||
820 | }*/
|
||
821 | |||
822 | /*public Buffer getWindow(double ulx, double uly, double w, double h,
|
||
823 | BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
|
||
824 | Rectangle2D bBox = new Rectangle2D.Double(ulx, uly, w, h);
|
||
825 | lastWidthRequest = rasterBuf.getWidth();
|
||
826 | lastHeightRequest = rasterBuf.getHeight();
|
||
827 | WMSStatus wmsStatus = loadWMSStatus(bBox);
|
||
828 | |||
829 | lastRequest = downloadFile(wmsStatus, ulx, uly, ulx + w, uly - h, rasterBuf.getWidth(), rasterBuf.getHeight());
|
||
830 | |||
831 | if (lastRequest == null) {
|
||
832 | return rasterBuf;
|
||
833 | }
|
||
834 | |||
835 | try {
|
||
836 | //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
837 | String serverName = bandList.getBand(0).getFileName();
|
||
838 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
839 | bandList.getBand(i).setFileName(lastRequest.getPath());
|
||
840 | }
|
||
841 | |||
842 | AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
|
||
843 | setColorTable(driver.getColorTable());
|
||
844 | |||
845 | DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
|
||
846 | q.setAreaOfInterest(ulx, uly, w, h);
|
||
847 | q.setBandList(bandList);
|
||
848 | q.setBuffer(rasterBuf);
|
||
849 | q.setAdjustToExtent(adjustToExtent);
|
||
850 | |||
851 | Buffer buf = driver.getDataSet(q);
|
||
852 | |||
853 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
854 | bandList.getBand(i).setFileName(serverName);
|
||
855 | }
|
||
856 | |||
857 | return buf;
|
||
858 | } catch (ProviderNotRegisteredException e) {
|
||
859 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
860 | } catch (InitializeException e) {
|
||
861 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
862 | }
|
||
863 | }*/
|
||
864 | |||
865 | /*public Buffer getWindow(Extent extent, int bufWidth, int bufHeight,
|
||
866 | BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
|
||
867 | Rectangle2D bBox = extent.toRectangle2D();//new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly));
|
||
868 | lastWidthRequest = rasterBuf.getWidth();
|
||
869 | lastHeightRequest = rasterBuf.getHeight();
|
||
870 | WMSStatus wmsStatus = loadWMSStatus(bBox);
|
||
871 | lastRequest = downloadFile(wmsStatus, extent.getULX(), extent.getULY(), extent.getLRX(), extent.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
|
||
872 | |||
873 | if (lastRequest == null) {
|
||
874 | return rasterBuf;
|
||
875 | }
|
||
876 | |||
877 | try {
|
||
878 | //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
879 | String serverName = bandList.getBand(0).getFileName();
|
||
880 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
881 | bandList.getBand(i).setFileName(lastRequest.getPath());
|
||
882 | }
|
||
883 | |||
884 | AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
|
||
885 | setColorTable(driver.getColorTable());
|
||
886 | |||
887 | DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
|
||
888 | q.setAreaOfInterest(extent, bufWidth, bufHeight);
|
||
889 | q.setBandList(bandList);
|
||
890 | q.setBuffer(rasterBuf);
|
||
891 | q.setAdjustToExtent(adjustToExtent);
|
||
892 | Buffer buf = driver.getDataSet(q);
|
||
893 | |||
894 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
895 | bandList.getBand(i).setFileName(serverName);
|
||
896 | }
|
||
897 | |||
898 | return buf;
|
||
899 | } catch (ProviderNotRegisteredException e) {
|
||
900 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
901 | } catch (InitializeException e) {
|
||
902 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
903 | }
|
||
904 | }*/
|
||
905 | |||
906 | // public Buffer getWindow(int x, int y,
|
||
907 | // BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
|
||
908 | // int w = rasterBuf.getWidth();
|
||
909 | // int h = rasterBuf.getHeight();
|
||
910 | // Point2D p1 = rasterToWorld(new Point2D.Double(x, y));
|
||
911 | // Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h));
|
||
912 | // lastWidthRequest = rasterBuf.getWidth();
|
||
913 | // lastHeightRequest = rasterBuf.getHeight();
|
||
914 | // Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY()));
|
||
915 | // WMSStatus wmsStatus = loadWMSStatus(bBox);
|
||
916 | //
|
||
917 | // lastRequest = downloadFile(wmsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight());
|
||
918 | //
|
||
919 | // if (lastRequest == null) {
|
||
920 | // return rasterBuf;
|
||
921 | // }
|
||
922 | //
|
||
923 | // AbstractRasterProvider driver = null;
|
||
924 | // try {
|
||
925 | // //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
926 | // String serverName = bandList.getBand(0).getFileName();
|
||
927 | // for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
928 | // bandList.getBand(i).setFileName(lastRequest.getPath());
|
||
929 | // }
|
||
930 | //
|
||
931 | // driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
|
||
932 | // Buffer buf = driver.getWindow(0, 0, w, h, bandList, rasterBuf);
|
||
933 | //
|
||
934 | // for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
935 | // bandList.getBand(i).setFileName(serverName);
|
||
936 | // }
|
||
937 | //
|
||
938 | // return buf;
|
||
939 | // } catch (ProviderNotRegisteredException e) {
|
||
940 | // throw new RasterDriverException("Error building GdalDriver", e);
|
||
941 | // } catch (InitializeException e) {
|
||
942 | // throw new RasterDriverException("Error building GdalDriver", e);
|
||
943 | // }
|
||
944 | // }
|
||
945 | |||
946 | /*public Buffer getWindow(int x, int y, int w, int h,
|
||
947 | BandList bandList, Buffer rasterBuf, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
|
||
948 | Point2D p1 = rasterToWorld(new Point2D.Double(x, y));
|
||
949 | Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h));
|
||
950 | lastWidthRequest = rasterBuf.getWidth();
|
||
951 | lastHeightRequest = rasterBuf.getHeight();
|
||
952 | Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY()));
|
||
953 | WMSStatus wmsStatus = loadWMSStatus(bBox);
|
||
954 | |||
955 | lastRequest = downloadFile(wmsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight());
|
||
956 | |||
957 | if (lastRequest == null) {
|
||
958 | return rasterBuf;
|
||
959 | }
|
||
960 | |||
961 | AbstractRasterProvider driver = null;
|
||
962 | try {
|
||
963 | //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
|
||
964 | String serverName = bandList.getBand(0).getFileName();
|
||
965 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
966 | bandList.getBand(i).setFileName(lastRequest.getPath());
|
||
967 | }
|
||
968 | |||
969 | driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
|
||
970 | setColorTable(driver.getColorTable());
|
||
971 | |||
972 | DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
|
||
973 | q.setAreaOfInterest(0, 0, w, h);
|
||
974 | q.setBandList(bandList);
|
||
975 | q.setBuffer(rasterBuf);
|
||
976 | Buffer buf = driver.getDataSet(q);
|
||
977 | |||
978 | for (int i = 0; i < bandList.getBandCount(); i++) {
|
||
979 | bandList.getBand(i).setFileName(serverName);
|
||
980 | }
|
||
981 | |||
982 | return buf;
|
||
983 | } catch (ProviderNotRegisteredException e) {
|
||
984 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
985 | } catch (InitializeException e) {
|
||
986 | throw new RasterDriverException("Error building GdalDriver", e);
|
||
987 | }
|
||
988 | }*/
|
||
989 | |||
990 | public Image getImageLegend() { |
||
991 | try {
|
||
992 | WMSStatus wmsStatus = loadWMSStatus(getExtent().toRectangle2D()); |
||
993 | wmsStatus.setOnlineResource((String) getParameters().getOnlineResource().get("GetCapabilities")); |
||
994 | File file = getConnector().getLegendGraphic(wmsStatus, getParameters().getLayerQuery(), null); |
||
995 | Image img = null; |
||
996 | if ((file != null) && (file.length() > 0)) { |
||
997 | img = new ImageIcon(file.getAbsolutePath()).getImage(); |
||
998 | } |
||
999 | return img;
|
||
1000 | } catch (Exception e) { |
||
1001 | logger.info("Problems in GetLegendGraphic", e);
|
||
1002 | } |
||
1003 | return null; |
||
1004 | } |
||
1005 | |||
1006 | public int getBlockSize() { |
||
1007 | return 0; |
||
1008 | } |
||
1009 | |||
1010 | public void setAffineTransform(AffineTransform t){ |
||
1011 | |||
1012 | } |
||
1013 | |||
1014 | public int getOverviewCount(int band) throws BandAccessException, RasterDriverException { |
||
1015 | return 0; |
||
1016 | } |
||
1017 | |||
1018 | public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException { |
||
1019 | return 0; |
||
1020 | } |
||
1021 | |||
1022 | public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException { |
||
1023 | return 0; |
||
1024 | } |
||
1025 | |||
1026 | public boolean isOverviewsSupported() { |
||
1027 | return false; |
||
1028 | } |
||
1029 | |||
1030 | public boolean isReproyectable() { |
||
1031 | return false; |
||
1032 | } |
||
1033 | |||
1034 | public String getName() { |
||
1035 | return NAME;
|
||
1036 | } |
||
1037 | |||
1038 | /**
|
||
1039 | * Convierte un punto desde coordenadas pixel a coordenadas del mundo.
|
||
1040 | * @param pt Punto a transformar
|
||
1041 | * @return punto transformado en coordenadas del mundo
|
||
1042 | */
|
||
1043 | public Point2D rasterToWorld(Point2D pt) { |
||
1044 | Point2D p = new Point2D.Double(); |
||
1045 | getAffineTransform().transform(pt, p); |
||
1046 | return p;
|
||
1047 | } |
||
1048 | |||
1049 | /**
|
||
1050 | * Convierte un punto desde del mundo a coordenadas pixel.
|
||
1051 | * @param pt Punto a transformar
|
||
1052 | * @return punto transformado en coordenadas pixel
|
||
1053 | */
|
||
1054 | public Point2D worldToRaster(Point2D pt) { |
||
1055 | Point2D p = new Point2D.Double(); |
||
1056 | try {
|
||
1057 | getAffineTransform().inverseTransform(pt, p); |
||
1058 | } catch (NoninvertibleTransformException e) { |
||
1059 | return pt;
|
||
1060 | } |
||
1061 | return p;
|
||
1062 | } |
||
1063 | |||
1064 | public void setStatus(RasterProvider provider) { |
||
1065 | if(provider instanceof WMSProvider) { |
||
1066 | } |
||
1067 | } |
||
1068 | |||
1069 | /**
|
||
1070 | * ASigna el par?metro de inicializaci?n del driver.
|
||
1071 | */
|
||
1072 | public void setParam(DataStoreProviderServices storeServices, DataStoreParameters param) { |
||
1073 | if(param instanceof WMSDataParameters) |
||
1074 | this.uri = ((WMSDataParameters)param).getURI();
|
||
1075 | this.param = param;
|
||
1076 | } |
||
1077 | |||
1078 | public String getInfoByPoint(int x, int y, Extent bbox, int w, int h, ICancellable cancellable) throws InfoByPointException { |
||
1079 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
1080 | WMSStatus wmsStatus = new WMSStatus();
|
||
1081 | wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ",")); |
||
1082 | wmsStatus.setSrs(p.getSRSCode()); |
||
1083 | wmsStatus.setFormat(p.getFormat()); |
||
1084 | wmsStatus.setInfoFormat(p.getInfoFormat()); |
||
1085 | List<RemoteWMSStyle> listStyles = p.getStyles();
|
||
1086 | Vector<?> v = listStyles != null ? new Vector<RemoteWMSStyle>(listStyles) : null; |
||
1087 | wmsStatus.setStyles(v); |
||
1088 | wmsStatus.setDimensions(p.getDimensions()); |
||
1089 | wmsStatus.setTransparency(p.isWmsTransparent()); |
||
1090 | wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap")); |
||
1091 | wmsStatus.setExtent(bbox.toRectangle2D()); |
||
1092 | wmsStatus.setHeight(h); |
||
1093 | wmsStatus.setWidth(w); |
||
1094 | try {
|
||
1095 | return getConnector().getFeatureInfo(wmsStatus, x, y, Integer.MAX_VALUE, cancellable); |
||
1096 | } catch (RemoteServiceException e) {
|
||
1097 | throw new InfoByPointException("Error in getFeatureInfo", e); |
||
1098 | } |
||
1099 | } |
||
1100 | |||
1101 | /**
|
||
1102 | * Gets the suffix of the downloaded image
|
||
1103 | * @return
|
||
1104 | */
|
||
1105 | public String getFileSuffix() { |
||
1106 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
1107 | String format = p.getFormat();
|
||
1108 | if (format == null){ |
||
1109 | return "xml"; |
||
1110 | } |
||
1111 | if (format.indexOf("png") >= 0){ |
||
1112 | return "png"; |
||
1113 | } |
||
1114 | if (format.indexOf("xml") >= 0){ |
||
1115 | return "xml"; |
||
1116 | } |
||
1117 | if (format.indexOf("gif") >= 0){ |
||
1118 | return "gif"; |
||
1119 | } |
||
1120 | if (format.indexOf("tif") >= 0){ |
||
1121 | return "tif"; |
||
1122 | } |
||
1123 | if (format.indexOf("bmp") >= 0){ |
||
1124 | return "bmp"; |
||
1125 | } |
||
1126 | if (format.indexOf("jpg") >= 0 |
||
1127 | || format.indexOf("jpeg") >= 0){ |
||
1128 | return "jpg"; |
||
1129 | } |
||
1130 | return "xml"; |
||
1131 | } |
||
1132 | |||
1133 | public TileServer getTileServer() {
|
||
1134 | if(tileServer == null) { |
||
1135 | DefaultRasterStore store = new DefaultRasterStore();
|
||
1136 | store.setProvider(this);
|
||
1137 | tileServer = new WMSTileServer(store);
|
||
1138 | } |
||
1139 | return tileServer;
|
||
1140 | } |
||
1141 | |||
1142 | public boolean isRasterEnclosed() { |
||
1143 | WMSDataParameters p = (WMSDataParameters)parameters; |
||
1144 | if(p.isSizeFixed()) {
|
||
1145 | return false; |
||
1146 | } |
||
1147 | return true; |
||
1148 | } |
||
1149 | |||
1150 | public String getRMFFile() { |
||
1151 | if(lastRequest != null) |
||
1152 | return fileUtil.getNameWithoutExtension(lastRequest.getAbsolutePath()) + ".rmf"; |
||
1153 | return null; |
||
1154 | } |
||
1155 | |||
1156 | public HistogramComputer getHistogramComputer() {
|
||
1157 | if (histogram == null) |
||
1158 | histogram = new RemoteStoreHistogram(this); |
||
1159 | return histogram;
|
||
1160 | } |
||
1161 | } |