]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/tests/WMSTest2.java
Ensure ITileProviders return BufferedImages with compatible ColorModel
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / tests / WMSTest2.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps.tests;
13 /*
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;
26
27 import javax.imageio.ImageIO;
28 import javax.swing.SwingUtilities;
29
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;
47
48 import org.simantics.maps.Service;
49 import org.simantics.maps.WebService;
50 import org.simantics.maps.wms.WMSGetMapQuery;
51
52 public class WMSTest2 {
53
54     private static String serviceUrl = "http://wms.jpl.nasa.gov/wms.cgi";
55
56     
57     WMSTest2() throws URISyntaxException {
58         try {
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);
61
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);
66
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);
72             // Ruler painter
73 //            new RulerPainter(canvasContext);
74
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);
79                 }
80             };
81
82             chassis.addChassisListener(new IChassisListener() {
83                 @Override
84                 public void chassisClosed(ICanvasChassis sender) {
85                     ICanvasContext ctx = sender.getCanvasContext();
86                     if (ctx!=null)
87                         ctx.dispose();
88                 }});
89
90         } catch (MalformedURLException e) {
91             e.printStackTrace();
92         }
93     }
94 */
95     /**
96      * Get a bufferedimage of the map data for the geographical coordinates.
97      * 
98      * @param x
99      * @param y
100      * @param z
101      * @return
102      * @throws MalformedURLException 
103      */
104 /*
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);
108
109         // Create an instance of HttpClient.
110         HttpClient client = new HttpClient();
111
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())); 
115
116         // Provide custom retry handler is necessary
117         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
118                 new DefaultHttpMethodRetryHandler(3, false));
119
120         try {
121             // Execute the method.
122             int statusCode = client.executeMethod(method);
123
124             if (statusCode != HttpStatus.SC_OK) {
125                 System.err.println("Method failed: " + method.getStatusLine());
126             }
127
128             InputStream response = method.getResponseBodyAsStream();
129             BufferedImage img = ImageIO.read(response);
130             if (img == null) {
131                 // Maybe the method produced a WMS error XML?
132                 return null;
133             }
134             return img;
135         } catch (HttpException e) {
136             System.err.println("Fatal protocol violation: " + e.getMessage());
137             e.printStackTrace();
138         } catch (IOException e) {
139             System.err.println("Fatal transport error: " + e.getMessage());
140             e.printStackTrace();
141         } finally {
142             // Release the connection.
143             method.releaseConnection();
144         }
145         return null;
146     }
147
148     
149     public static void main(String[] args) {
150         SwingUtilities.invokeLater(new Runnable() {
151             @Override
152             public void run() {
153                 try {
154                     new WMSTest2();
155                 } catch (URISyntaxException e) {
156                     e.printStackTrace();
157                 }
158             }
159         });
160     }
161 }
162 */