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.tests;
14 import java.awt.Dimension;
15 import java.awt.Graphics2D;
16 import java.awt.GraphicsDevice;
17 import java.awt.GraphicsEnvironment;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Rectangle2D;
20 import java.awt.image.AffineTransformOp;
21 import java.awt.image.BufferedImage;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URISyntaxException;
27 import javax.imageio.ImageIO;
28 import javax.swing.SwingUtilities;
30 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.HttpException;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.methods.GetMethod;
35 import org.apache.commons.httpclient.params.HttpMethodParams;
36 import org.simantics.g2d.canvas.ICanvasContext;
37 import org.simantics.g2d.canvas.chassis.FullscreenAwtChassis;
38 import org.simantics.g2d.canvas.chassis.ICanvasChassis;
39 import org.simantics.g2d.canvas.chassis.IChassisListener;
40 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
41 import org.simantics.g2d.canvas.impl.CanvasContext;
42 import org.simantics.g2d.canvas.impl.PainterReflection.Painter;
43 import org.simantics.g2d.canvas.participant.GridPainter;
44 import org.simantics.g2d.canvas.participant.MouseMonitor;
45 import org.simantics.g2d.canvas.participant.PanZoomInteractor;
46 import org.simantics.utils.threads.AWTThread;
48 import org.simantics.maps.Service;
49 import org.simantics.maps.WebService;
50 import org.simantics.maps.wms.WMSGetMapQuery;
52 public class WMSTest3 {
54 private static String serviceUrl = "http://wms.jpl.nasa.gov/wms.cgi";
57 WMSTest3() throws URISyntaxException {
59 final Service service = new WebService(serviceUrl);
60 final BufferedImage bi = getData(service, new Dimension(600, 300), new Rectangle2D.Double(-180,-90,360,180), 1);
62 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
63 FullscreenAwtChassis chassis = new FullscreenAwtChassis("WMS Test", true);
64 ICanvasContext canvasContext = new CanvasContext(AWTThread.getThreadAccess()); chassis.setCanvasContext(canvasContext);
66 // Paints a cursor, follows mouse movement
67 new MouseMonitor(canvasContext);
68 // Pan & Zoom features
69 new PanZoomInteractor(canvasContext);
70 GridPainter grid = new GridPainter(canvasContext);
72 // new RulerPainter(canvasContext);
74 new AbstractCanvasParticipant(canvasContext) {
75 @Painter(priority = Integer.MAX_VALUE - 1000)
76 public void paint(Graphics2D g) {
77 g.drawImage(bi, new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BICUBIC), 100, 100);
81 chassis.addChassisListener(new IChassisListener() {
83 public void chassisClosed(ICanvasChassis sender) {
84 ICanvasContext ctx = sender.getCanvasContext();
89 } catch (MalformedURLException e) {
95 * Get a bufferedimage of the map data for the geographical coordinates.
101 * @throws MalformedURLException
104 public BufferedImage getData(Service service, Dimension rasterSize, Rectangle2D bbox, double zoom) throws MalformedURLException {
105 if (rasterSize.width == 0 || rasterSize.height == 0)
106 return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
108 // Create an instance of HttpClient.
109 HttpClient client = new HttpClient();
111 // Create a method instance.
112 WMSGetMapQuery req = new WMSGetMapQuery(rasterSize.width, rasterSize.height, bbox, "image/jpeg", "global_mosaic");
113 GetMethod method = new GetMethod(service.getRequestURL(req.toString()));
115 // Provide custom retry handler is necessary
116 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
117 new DefaultHttpMethodRetryHandler(3, false));
120 // Execute the method.
121 int statusCode = client.executeMethod(method);
123 if (statusCode != HttpStatus.SC_OK) {
124 System.err.println("Method failed: " + method.getStatusLine());
127 InputStream response = method.getResponseBodyAsStream();
128 BufferedImage img = ImageIO.read(response);
130 // Maybe the method produced a WMS error XML?
134 } catch (HttpException e) {
135 System.err.println("Fatal protocol violation: " + e.getMessage());
137 } catch (IOException e) {
138 System.err.println("Fatal transport error: " + e.getMessage());
141 // Release the connection.
142 method.releaseConnection();
148 public static void main(String[] args) {
149 SwingUtilities.invokeLater(new Runnable() {
154 } catch (URISyntaxException e) {