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.osm;
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;
31 import org.simantics.maps.wms.ServiceException;
34 * @author Tuukka Lehtonen
37 public class OSMTileProvider implements ITileProvider {
39 private WebService service;
43 public OSMTileProvider(WebService service, int tileSize) {
44 this.service = service;
45 this.tileSize = tileSize;
49 public URI getSource() {
50 return service.getURI();
54 public Rectangle2D getExtent() {
55 return new Rectangle2D.Double(-180, -90, 360, 180);
59 public Image get(TileKey key) throws ProvisionException {
60 int level = key.getLevel();
64 double xTiles = Math.pow(2, level + 1);
65 double yTiles = Math.pow(2, level);
68 throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
69 if (x < 0 || x >= (int) xTiles)
70 throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
71 if (y < 0 || y >= (int) yTiles)
72 throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
75 Rectangle2D r = new Rectangle2D.Double(x, y, 10, 10);
77 // System.out.println("TileProvider: requesting " + key + ", bbox=" + r + ", size=" + tileSize);
78 BufferedImage img = getMap(service, tileSize, tileSize, r, "image/png", level);//layers);
79 // System.out.println("TileProvider: response: " + img);
81 } catch (MalformedURLException e) {
82 throw new ProvisionException(e);
83 } catch (IOException e) {
84 throw new ProvisionException(e);
90 * Get a bufferedimage of the map data for the geographical coordinates.
96 * @throws MalformedURLException
98 public static BufferedImage getMap(WebService service, Dimension rasterSize, Rectangle2D bbox, String format, int level) throws MalformedURLException, IOException {
99 return getMap(service, rasterSize.width, rasterSize.height, bbox, format, level);
103 * Get a bufferedimage of the map data for the geographical coordinates.
109 * @throws MalformedURLException
111 public static BufferedImage getMap(WebService service, int rasterWidth, int rasterHeight, Rectangle2D bbox, String format, int level) throws MalformedURLException, IOException {
112 if (rasterWidth == 0 || rasterHeight == 0)
113 return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
115 HttpURLConnection connection = null;
117 connection = service.openConnection(("" + level + "/" + Math.round(bbox.getMinX()) + "/" + Math.round(bbox.getMinY())+".png"), null);
119 // Execute the method.
120 int statusCode = connection.getResponseCode();
122 if (statusCode != HttpURLConnection.HTTP_OK) {
123 System.err.println("Method failed: " + connection.getResponseMessage());
126 // for (Header h : method.getResponseHeaders()) {
127 // System.out.println("HDR: " + h.getName() + ": " + h.getValue());
130 String contentType = connection.getHeaderField("Content-Type");
131 if (contentType != null) {
132 // System.out.println("Content-Type: " + contentType);
133 if (contentType.equals(format)) {
134 InputStream response = connection.getInputStream();
135 BufferedImage img = ImageIO.read(response);
137 throw new IIOException("ImageIO returned null, unable to decode stream as image data.");
143 // Request failed, produce as much debug information as possible.
144 StringBuilder sb = new StringBuilder();
145 sb.append("Response headers:\n");
146 for (String h : connection.getHeaderFields().keySet()) {
149 sb.append(connection.getHeaderField(h));
152 String response = connection.getContent().toString();
153 sb.append("Response body:\n");
155 throw new ServiceException("WMS GetMap request failed: " + connection.getURL().toString() + "\n" + sb.toString());
157 // Release the connection.
158 if (connection != null)
159 connection.disconnect();