svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrWMS.java @ 1848
History | View | Annotate | Download (11.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.layers; |
42 |
|
43 |
import com.iver.cit.gvsig.fmap.DriverException; |
44 |
import com.iver.cit.gvsig.fmap.ViewPort; |
45 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
46 |
import com.iver.cit.gvsig.fmap.drivers.WMSException; |
47 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint; |
48 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
49 |
|
50 |
import com.iver.utiles.StringUtilities; |
51 |
import com.iver.utiles.XMLEntity; |
52 |
|
53 |
import com.iver.wmsclient.FeatureInfoQuery; |
54 |
import com.iver.wmsclient.MapQuery; |
55 |
import com.iver.wmsclient.UnsupportedVersionException; |
56 |
import com.iver.wmsclient.WMSClient; |
57 |
import com.iver.wmsclient.WMSClientFactory; |
58 |
|
59 |
import org.exolab.castor.xml.ValidationException; |
60 |
|
61 |
import java.awt.Graphics2D; |
62 |
import java.awt.Point; |
63 |
import java.awt.geom.AffineTransform; |
64 |
import java.awt.geom.NoninvertibleTransformException; |
65 |
import java.awt.geom.Point2D; |
66 |
import java.awt.geom.Rectangle2D; |
67 |
import java.awt.image.BufferedImage; |
68 |
|
69 |
import java.io.ByteArrayInputStream; |
70 |
import java.io.IOException; |
71 |
|
72 |
import java.net.MalformedURLException; |
73 |
import java.net.URL; |
74 |
|
75 |
import javax.imageio.ImageIO; |
76 |
|
77 |
|
78 |
/**
|
79 |
* Capa WMS.
|
80 |
*
|
81 |
* @author Fernando Gonz?lez Cort?s
|
82 |
*/
|
83 |
public class FLyrWMS extends FLyrDefault implements InfoByPoint { |
84 |
boolean isPrinting = false; |
85 |
boolean mustTileDraw = false; |
86 |
boolean mustTilePrint = true; |
87 |
int maxTileDrawWidth = -1; |
88 |
int maxTileDrawHeight = -1; |
89 |
int maxTilePrintWidth = 1500; |
90 |
int maxTilePrintHeight = 1500; |
91 |
|
92 |
private String m_SRS; |
93 |
private String m_Format; |
94 |
private String layerQuery; |
95 |
private String infoLayerQuery; |
96 |
private URL host; |
97 |
private WMSClient wmsClient;
|
98 |
private MapQuery lastMapQuery;
|
99 |
private Rectangle2D fullExtent; |
100 |
|
101 |
/**
|
102 |
* Slecciona el formato del WMS.
|
103 |
*
|
104 |
* @return formato seleccionado.
|
105 |
*
|
106 |
* @throws WMSException
|
107 |
* @throws IllegalStateException
|
108 |
* @throws ValidationException
|
109 |
* @throws UnsupportedVersionException
|
110 |
* @throws IOException
|
111 |
*/
|
112 |
private String selectFormat() |
113 |
throws WMSException, IllegalStateException, ValidationException, |
114 |
UnsupportedVersionException, IOException {
|
115 |
String[] formats; |
116 |
formats = getWmsClient().getInfoFormats(); |
117 |
|
118 |
for (int i = 0; i < formats.length; i++) { |
119 |
if (formats[i].equals("GML.1")) { |
120 |
return formats[i];
|
121 |
} |
122 |
|
123 |
if (formats[i].equals("GML.2")) { |
124 |
return formats[i];
|
125 |
} |
126 |
|
127 |
if (formats[i].equals("GML.3")) { |
128 |
return formats[i];
|
129 |
} |
130 |
|
131 |
if (formats[i].equals("application/vnd.ogc.gml")) { |
132 |
return formats[i];
|
133 |
} |
134 |
|
135 |
if (formats[i].indexOf("XML") != -1) { |
136 |
return formats[i];
|
137 |
} |
138 |
} |
139 |
|
140 |
throw new WMSException("No format supported"); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
|
145 |
* capa.
|
146 |
*
|
147 |
* @return XMLEntity.
|
148 |
* @throws XMLException
|
149 |
*/
|
150 |
public XMLEntity getXMLEntity() throws XMLException { |
151 |
XMLEntity xml = super.getXMLEntity();
|
152 |
|
153 |
xml.putProperty("fullExtent", StringUtilities.rect2String(fullExtent));
|
154 |
xml.putProperty("host", host.toExternalForm());
|
155 |
xml.putProperty("infoLayerQuery", infoLayerQuery);
|
156 |
xml.putProperty("layerQuery", layerQuery);
|
157 |
xml.putProperty("format", m_Format);
|
158 |
xml.putProperty("srs", m_SRS);
|
159 |
|
160 |
return xml;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* A partir del XMLEntity reproduce la capa.
|
165 |
*
|
166 |
* @param xml XMLEntity
|
167 |
*
|
168 |
* @throws XMLException
|
169 |
* @throws DriverException
|
170 |
* @throws DriverIOException
|
171 |
*/
|
172 |
public void setXMLEntity(XMLEntity xml) |
173 |
throws XMLException {
|
174 |
super.setXMLEntity(xml);
|
175 |
fullExtent = StringUtilities.string2Rect(xml.getStringProperty( |
176 |
"fullExtent"));
|
177 |
|
178 |
try {
|
179 |
host = new URL(xml.getStringProperty("host")); |
180 |
} catch (MalformedURLException e) { |
181 |
throw new XMLException(e); |
182 |
} |
183 |
|
184 |
infoLayerQuery = xml.getStringProperty("infoLayerQuery");
|
185 |
layerQuery = xml.getStringProperty("layerQuery");
|
186 |
m_Format = xml.getStringProperty("format");
|
187 |
m_SRS = xml.getStringProperty("srs");
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(com.iver.cit.gvsig.fmap.operations.QueriedPoint)
|
192 |
*/
|
193 |
public String queryByPoint(Point p) throws DriverException { |
194 |
FeatureInfoQuery query = new FeatureInfoQuery(lastMapQuery);
|
195 |
query.setFeatureCount(Integer.MAX_VALUE);
|
196 |
query.setX((int) p.getX());
|
197 |
query.setY((int) p.getY());
|
198 |
query.setInfoQuery(infoLayerQuery); |
199 |
|
200 |
try {
|
201 |
query.setInfoFormat(selectFormat()); |
202 |
|
203 |
return new String(getWmsClient().doFeatureInfo(query)); |
204 |
} catch (WMSException e) {
|
205 |
return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><exception>" + |
206 |
e.getMessage() + "</exception>";
|
207 |
} catch (ValidationException e) {
|
208 |
/*
|
209 |
* TODO Las traducciones en este m?todo han de ser
|
210 |
* las mismas que en el m?todo de dibujado
|
211 |
*/
|
212 |
throw new DriverException("No se reconoce el formato de la respuesta", |
213 |
e); |
214 |
} catch (UnsupportedVersionException e) {
|
215 |
throw new DriverException("Conflicto de versiones", e); |
216 |
} catch (IOException e) { |
217 |
throw new DriverException("Error en la conexi?n", e); |
218 |
} catch (com.iver.wmsclient.WMSException e) {
|
219 |
throw new DriverException(e.getMessage(), e); |
220 |
} catch (NoSuchFieldException e) { |
221 |
throw new RuntimeException( |
222 |
"No se rellenaron todos los campos de la petici?n");
|
223 |
} |
224 |
} |
225 |
|
226 |
/**
|
227 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
|
228 |
*/
|
229 |
public Rectangle2D getFullExtent() throws DriverException { |
230 |
return fullExtent;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
|
235 |
* java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
|
236 |
* com.iver.cit.gvsig.fmap.operations.Cancellable)
|
237 |
*/
|
238 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
239 |
Cancellable cancel) throws DriverException {
|
240 |
try {
|
241 |
lastMapQuery = getWmsClient().createQuery(); |
242 |
lastMapQuery.setBBOX(viewPort.getAdjustedExtent()); |
243 |
lastMapQuery.setFormat(m_Format); |
244 |
lastMapQuery.setHeight(viewPort.getImageHeight()); |
245 |
|
246 |
// System.err.println("m_Mapa.getHeight() = " + m_Mapa.getHeight());
|
247 |
lastMapQuery.setLayers(layerQuery); |
248 |
lastMapQuery.setSRS(m_SRS); |
249 |
lastMapQuery.setStyles("");
|
250 |
lastMapQuery.setWidth(viewPort.getImageWidth()); |
251 |
lastMapQuery.setExceptions("application/vnd.ogc.se_xml");
|
252 |
|
253 |
byte[] bytes; |
254 |
|
255 |
bytes = getWmsClient().doMapQuery(lastMapQuery); |
256 |
|
257 |
ByteArrayInputStream inbytes = new ByteArrayInputStream(bytes); |
258 |
BufferedImage tempImg = ImageIO.read(inbytes); |
259 |
// LWS Cambio de estrategia en el posicionado para la impresi?n.
|
260 |
Point2D p2=new Point2D.Double(viewPort.getAdjustedExtent().getX(), viewPort.getAdjustedExtent().getMaxY()); //viewPort.getOffset(); |
261 |
viewPort.getAffineTransform().transform(p2, p2); |
262 |
g.drawImage(tempImg,(int)p2.getX(),(int)p2.getY(), null); |
263 |
} catch (ValidationException e) {
|
264 |
throw new DriverException("No se reconoce el formato de la respuesta", |
265 |
e); |
266 |
} catch (UnsupportedVersionException e) {
|
267 |
throw new DriverException("Conflicto de versiones", e); |
268 |
} catch (IOException e) { |
269 |
throw new DriverException("Error en la conexi?n", e); |
270 |
} catch (com.iver.wmsclient.WMSException e) {
|
271 |
throw new DriverException(e.getMessage(), e); |
272 |
} catch (NoSuchFieldException e) { |
273 |
throw new RuntimeException( |
274 |
"No se rellenaron todos los campos de la petici?n");
|
275 |
} |
276 |
} |
277 |
|
278 |
/**
|
279 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
|
280 |
* com.iver.cit.gvsig.fmap.ViewPort,
|
281 |
* com.iver.cit.gvsig.fmap.operations.Cancellable)
|
282 |
*/
|
283 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel) |
284 |
throws DriverException {
|
285 |
isPrinting = true;
|
286 |
if (!mustTilePrint) {
|
287 |
draw(null, g, viewPort, cancel);
|
288 |
} else {
|
289 |
// Para no pedir imagenes demasiado grandes, vamos
|
290 |
// a hacer lo mismo que hace EcwFile: chunkear.
|
291 |
// Llamamos a drawView con cuadraditos m?s peque?os
|
292 |
// del BufferedImage ni caso, cuando se imprime viene con null
|
293 |
Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
|
294 |
tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
|
295 |
for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
296 |
// Parte que dibuja
|
297 |
try {
|
298 |
ViewPort vp = tiles.getTileViewPort(viewPort, tileNr); |
299 |
draw(null, g, vp, cancel);
|
300 |
} catch (NoninvertibleTransformException e) { |
301 |
// TODO Auto-generated catch block
|
302 |
e.printStackTrace(); |
303 |
} |
304 |
} |
305 |
} |
306 |
isPrinting = false;
|
307 |
} |
308 |
|
309 |
public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel) |
310 |
throws DriverException {
|
311 |
draw(null, g, viewPort, cancel);
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Devuelve el WMSClient.
|
316 |
*
|
317 |
* @return WMSClient
|
318 |
*
|
319 |
* @throws IllegalStateException
|
320 |
* @throws ValidationException
|
321 |
* @throws UnsupportedVersionException
|
322 |
* @throws IOException
|
323 |
*/
|
324 |
private WMSClient getWmsClient()
|
325 |
throws IllegalStateException, ValidationException, |
326 |
UnsupportedVersionException, IOException {
|
327 |
if (wmsClient == null) { |
328 |
wmsClient = WMSClientFactory.getClient(host); |
329 |
} |
330 |
|
331 |
return wmsClient;
|
332 |
} |
333 |
|
334 |
/**
|
335 |
* Devuelve el URL.
|
336 |
*
|
337 |
* @return URL.
|
338 |
*/
|
339 |
public URL getHost() { |
340 |
return host;
|
341 |
} |
342 |
|
343 |
/**
|
344 |
* Inserta el URL.
|
345 |
*
|
346 |
* @param host URL.
|
347 |
*/
|
348 |
public void setHost(URL host) { |
349 |
this.host = host;
|
350 |
} |
351 |
|
352 |
/**
|
353 |
* Devuelve la informaci?n de la consulta.
|
354 |
*
|
355 |
* @return String.
|
356 |
*/
|
357 |
public String getInfoLayerQuery() { |
358 |
return infoLayerQuery;
|
359 |
} |
360 |
|
361 |
/**
|
362 |
* Inserta la informaci?n de la consulta.
|
363 |
*
|
364 |
* @param infoLayerQuery String.
|
365 |
*/
|
366 |
public void setInfoLayerQuery(String infoLayerQuery) { |
367 |
this.infoLayerQuery = infoLayerQuery;
|
368 |
} |
369 |
|
370 |
/**
|
371 |
* Devuelve la consulta.
|
372 |
*
|
373 |
* @return String.
|
374 |
*/
|
375 |
public String getLayerQuery() { |
376 |
return layerQuery;
|
377 |
} |
378 |
|
379 |
/**
|
380 |
* Inserta la consulta.
|
381 |
*
|
382 |
* @param layerQuery consulta.
|
383 |
*/
|
384 |
public void setLayerQuery(String layerQuery) { |
385 |
this.layerQuery = layerQuery;
|
386 |
} |
387 |
|
388 |
/**
|
389 |
* Devuelve el formato.
|
390 |
*
|
391 |
* @return Formato.
|
392 |
*/
|
393 |
public String getFormat() { |
394 |
return m_Format;
|
395 |
} |
396 |
|
397 |
/**
|
398 |
* Inserta el formato.
|
399 |
*
|
400 |
* @param format Formato.
|
401 |
*/
|
402 |
public void setFormat(String format) { |
403 |
m_Format = format; |
404 |
} |
405 |
|
406 |
/**
|
407 |
* Devuelve el SRS.
|
408 |
*
|
409 |
* @return SRS.
|
410 |
*/
|
411 |
public String getSRS() { |
412 |
return m_SRS;
|
413 |
} |
414 |
|
415 |
/**
|
416 |
* Inserta el SRS.
|
417 |
*
|
418 |
* @param m_srs SRS.
|
419 |
*/
|
420 |
public void setSRS(String m_srs) { |
421 |
m_SRS = m_srs; |
422 |
} |
423 |
|
424 |
/**
|
425 |
* Inserta la extensi?n total de la capa.
|
426 |
*
|
427 |
* @param fullExtent Rect?ngulo.
|
428 |
*/
|
429 |
public void setFullExtent(Rectangle2D fullExtent) { |
430 |
this.fullExtent = fullExtent;
|
431 |
} |
432 |
} |