]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/tests/WMSTest3.java
Ensure ITileProviders return BufferedImages with compatible ColorModel
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / tests / WMSTest3.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 WMSTest3 {
53
54     private static String serviceUrl = "http://wms.jpl.nasa.gov/wms.cgi";
55
56     
57     WMSTest3() 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());            chassis.setCanvasContext(canvasContext);
65
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);
71             // Ruler painter
72 //            new RulerPainter(canvasContext);
73
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);
78                 }
79             };
80
81             chassis.addChassisListener(new IChassisListener() {
82                 @Override
83                 public void chassisClosed(ICanvasChassis sender) {
84                     ICanvasContext ctx = sender.getCanvasContext();
85                     if (ctx!=null)
86                         ctx.dispose();
87                 }});
88
89         } catch (MalformedURLException e) {
90             e.printStackTrace();
91         }
92     }
93 */
94     /**
95      * Get a bufferedimage of the map data for the geographical coordinates.
96      * 
97      * @param x
98      * @param y
99      * @param z
100      * @return
101      * @throws MalformedURLException 
102      */
103 /*
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);
107
108         // Create an instance of HttpClient.
109         HttpClient client = new HttpClient();
110
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())); 
114
115         // Provide custom retry handler is necessary
116         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
117                 new DefaultHttpMethodRetryHandler(3, false));
118
119         try {
120             // Execute the method.
121             int statusCode = client.executeMethod(method);
122
123             if (statusCode != HttpStatus.SC_OK) {
124                 System.err.println("Method failed: " + method.getStatusLine());
125             }
126
127             InputStream response = method.getResponseBodyAsStream();
128             BufferedImage img = ImageIO.read(response);
129             if (img == null) {
130                 // Maybe the method produced a WMS error XML?
131                 return null;
132             }
133             return img;
134         } catch (HttpException e) {
135             System.err.println("Fatal protocol violation: " + e.getMessage());
136             e.printStackTrace();
137         } catch (IOException e) {
138             System.err.println("Fatal transport error: " + e.getMessage());
139             e.printStackTrace();
140         } finally {
141             // Release the connection.
142             method.releaseConnection();
143         }
144         return null;
145     }
146
147     
148     public static void main(String[] args) {
149         SwingUtilities.invokeLater(new Runnable() {
150             @Override
151             public void run() {
152                 try {
153                     new WMSTest3();
154                 } catch (URISyntaxException e) {
155                     e.printStackTrace();
156                 }
157             }
158         });
159     }
160 }
161 */