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 WMSTest2 {
54 private static String serviceUrl = "http://wms.jpl.nasa.gov/wms.cgi";
57 WMSTest2() 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());
65 chassis.setCanvasContext(canvasContext);
67 // Paints a cursor, follows mouse movement
68 new MouseMonitor(canvasContext);
69 // Pan & Zoom features
70 new PanZoomInteractor(canvasContext);
71 GridPainter grid = new GridPainter(canvasContext);
73 // new RulerPainter(canvasContext);
75 new AbstractCanvasParticipant(canvasContext) {
76 @Painter(priority = Integer.MAX_VALUE - 1000)
77 public void paint(Graphics2D g) {
78 g.drawImage(bi, new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BICUBIC), 100, 100);
82 chassis.addChassisListener(new IChassisListener() {
84 public void chassisClosed(ICanvasChassis sender) {
85 ICanvasContext ctx = sender.getCanvasContext();
90 } catch (MalformedURLException e) {
96 * Get a bufferedimage of the map data for the geographical coordinates.
102 * @throws MalformedURLException
105 public BufferedImage getData(Service service, Dimension rasterSize, Rectangle2D bbox, double zoom) throws MalformedURLException {
106 if (rasterSize.width == 0 || rasterSize.height == 0)
107 return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
109 // Create an instance of HttpClient.
110 HttpClient client = new HttpClient();
112 // Create a method instance.
113 WMSGetMapQuery req = new WMSGetMapQuery(rasterSize.width, rasterSize.height, bbox, "image/jpeg", "global_mosaic");
114 GetMethod method = new GetMethod(service.getRequestURL(req.toString()));
116 // Provide custom retry handler is necessary
117 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
118 new DefaultHttpMethodRetryHandler(3, false));
121 // Execute the method.
122 int statusCode = client.executeMethod(method);
124 if (statusCode != HttpStatus.SC_OK) {
125 System.err.println("Method failed: " + method.getStatusLine());
128 InputStream response = method.getResponseBodyAsStream();
129 BufferedImage img = ImageIO.read(response);
131 // Maybe the method produced a WMS error XML?
135 } catch (HttpException e) {
136 System.err.println("Fatal protocol violation: " + e.getMessage());
138 } catch (IOException e) {
139 System.err.println("Fatal transport error: " + e.getMessage());
142 // Release the connection.
143 method.releaseConnection();
149 public static void main(String[] args) {
150 SwingUtilities.invokeLater(new Runnable() {
155 } catch (URISyntaxException e) {