1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.maps.wms;
14 import java.awt.Dimension;
15 import java.awt.Image;
16 import java.awt.geom.Rectangle2D;
17 import java.awt.image.BufferedImage;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.HttpURLConnection;
21 import java.net.MalformedURLException;
24 import javax.imageio.IIOException;
25 import javax.imageio.ImageIO;
27 import org.simantics.maps.ProvisionException;
28 import org.simantics.maps.WebService;
29 import org.simantics.maps.tile.ITileProvider;
30 import org.simantics.maps.tile.TileKey;
33 * @author Tuukka Lehtonen
35 public class WMSTileProvider implements ITileProvider {
37 private WebService service;
41 private String[] layers;
43 public WMSTileProvider(WebService service, int tileSize, String... layers) {
44 this.service = service;
45 this.tileSize = tileSize;
50 public URI getSource() {
51 return service.getURI();
55 public Rectangle2D getExtent() {
56 return new Rectangle2D.Double(-180, -90, 360, 180);
60 public Image get(TileKey key) throws ProvisionException {
61 int level = key.getLevel();
65 double xTiles = Math.pow(2, level + 1);
66 double yTiles = Math.pow(2, level);
69 throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
70 if (x < 0 || x >= (int) xTiles)
71 throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
72 if (y < 0 || y >= (int) yTiles)
73 throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
76 Rectangle2D r = new Rectangle2D.Double();
83 double xdelta = w / xTiles;
84 double ydelta = h / yTiles;
86 double yy = yTiles - y - 1;
95 // System.out.println("TileProvider: requesting " + key + ", bbox=" + r + ", size=" + tileSize);
96 BufferedImage img = getMap(service, tileSize, tileSize, r, "image/jpeg", layers);
97 // System.out.println("TileProvider: response: " + img);
99 } catch (MalformedURLException e) {
100 throw new ProvisionException(e);
101 } catch (IOException e) {
102 throw new ProvisionException(e);
107 * Get a bufferedimage of the map data for the geographical coordinates.
113 * @throws MalformedURLException
115 public static BufferedImage getMap(WebService service, Dimension rasterSize, Rectangle2D bbox, String format, String... layers) throws MalformedURLException, IOException {
116 return getMap(service, rasterSize.width, rasterSize.height, bbox, format, layers);
120 * Get a bufferedimage of the map data for the geographical coordinates.
126 * @throws MalformedURLException
128 public static BufferedImage getMap(WebService service, int rasterWidth, int rasterHeight, Rectangle2D bbox, String format, String... layers) throws MalformedURLException, IOException {
129 if (rasterWidth == 0 || rasterHeight == 0)
130 return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
132 // Create a method instance.
133 WMSGetMapQuery req = new WMSGetMapQuery(rasterWidth, rasterHeight, bbox, format, layers);
134 // req.setStyles("visual");
136 // String requestUrl = service.getRequestURL("", req.toString());
137 // System.out.println("REQUEST: " + requestUrl);
139 HttpURLConnection connection = null;
141 connection = service.openConnection("", req.toString());
142 // Execute the method.
143 int statusCode = connection.getResponseCode();
145 if (statusCode != HttpURLConnection.HTTP_OK) {
146 System.err.println("Method failed: " + connection.getResponseMessage());
149 // for (Header h : method.getResponseHeaders()) {
150 // System.out.println("HDR: " + h.getName() + ": " + h.getValue());
153 String contentType = connection.getHeaderField("Content-Type");
154 if (contentType != null) {
155 // System.out.println("Content-Type: " + contentType);
156 if (contentType.equals(format)) {
157 InputStream response = connection.getInputStream();
158 BufferedImage img = ImageIO.read(response);
160 throw new IIOException("ImageIO returned null, unable to decode stream as image data.");
166 // Request failed, produce as much debug information as possible.
167 StringBuilder sb = new StringBuilder();
168 for (String h : connection.getHeaderFields().keySet()) {
171 sb.append(connection.getHeaderField(h));
174 String response = connection.getContent().toString();
175 sb.append("Response body:\n");
177 throw new ServiceException("WMS GetMap request failed: " + connection.getURL().toString() + "\n" + sb.toString());
179 // Release the connection.
180 connection.disconnect();