001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.demo.mappane;
017:
018: import java.awt.Component;
019: import java.awt.event.ComponentEvent;
020: import java.awt.event.ComponentListener;
021: import java.awt.image.BufferedImage;
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.net.URL;
027: import java.net.URLConnection;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Set;
031:
032: import javax.imageio.ImageIO;
033: import javax.swing.SwingUtilities;
034:
035: import org.apache.commons.lang.StringUtils;
036: import org.geotools.coverage.grid.GridCoverage2D;
037: import org.geotools.coverage.grid.GridCoverageFactory;
038: import org.geotools.data.DataUtilities;
039: import org.geotools.data.FeatureSource;
040: import org.geotools.data.ows.CRSEnvelope;
041: import org.geotools.data.ows.Layer;
042: import org.geotools.data.wms.WebMapServer;
043: import org.geotools.data.wms.request.GetMapRequest;
044: import org.geotools.factory.CommonFactoryFinder;
045: import org.geotools.factory.FactoryRegistryException;
046: import org.geotools.geometry.jts.ReferencedEnvelope;
047: import org.geotools.map.DefaultMapLayer;
048: import org.geotools.map.MapContext;
049: import org.geotools.map.MapLayer;
050: import org.geotools.map.event.MapBoundsEvent;
051: import org.geotools.map.event.MapBoundsListener;
052: import org.geotools.map.event.MapLayerEvent;
053: import org.geotools.referencing.CRS;
054: import org.geotools.resources.coverage.FeatureUtilities;
055: import org.geotools.styling.FeatureTypeStyle;
056: import org.geotools.styling.RasterSymbolizer;
057: import org.geotools.styling.Rule;
058: import org.geotools.styling.Style;
059: import org.geotools.styling.StyleFactory;
060: import org.geotools.styling.Symbolizer;
061: import org.opengis.referencing.FactoryException;
062: import org.opengis.referencing.crs.CoordinateReferenceSystem;
063:
064: public class WMSMapLayer extends DefaultMapLayer implements MapLayer,
065: MapBoundsListener, ComponentListener {
066: static GridCoverageFactory gcf = new GridCoverageFactory();
067: private Layer layer;
068: private WebMapServer wms;
069: GridCoverage2D grid;
070: private ReferencedEnvelope bbox;
071: private ReferencedEnvelope bounds;
072: private int width = 400;
073: private int height = 200;
074: private StyleFactory factory = CommonFactoryFinder
075: .getStyleFactory(null);
076: private boolean transparent = true;
077: private String bgColour;
078: private String exceptions = "application/vnd.ogc.se_inimage";
079:
080: public WMSMapLayer(WebMapServer wms, Layer layer) {
081: super ((FeatureSource) null, null, "");
082: this .layer = layer;
083: this .wms = wms;
084: setDefaultStyle();
085: setGrid();
086: }
087:
088: public void setDefaultStyle() {
089: RasterSymbolizer symbolizer = factory.createRasterSymbolizer();
090:
091: // SLDParser stylereader = new SLDParser(factory,sld);
092: // org.geotools.styling.Style[] style = stylereader.readXML();
093: Style style = factory.createStyle();
094: Rule[] rules = new Rule[1];
095: rules[0] = factory.createRule();
096: rules[0].setSymbolizers(new Symbolizer[] { symbolizer });
097:
098: FeatureTypeStyle type = factory.createFeatureTypeStyle(rules);
099: style.addFeatureTypeStyle(type);
100: setStyle(style);
101: }
102:
103: private void setGrid() {
104: GetMapRequest mapRequest = wms.createGetMapRequest();
105: mapRequest.addLayer(layer);
106: //System.out.println(width + " " + height);
107: mapRequest.setDimensions(getWidth(), getHeight());
108: mapRequest.setFormat("image/png");
109:
110: if (bgColour != null) {
111: mapRequest.setBGColour(bgColour);
112: }
113:
114: mapRequest.setExceptions(exceptions);
115:
116: Set srs = layer.getSrs();
117: String crs = "EPSG:4326";
118:
119: if (srs.contains("EPSG:4326")) { // really we should get the underlying
120: // map pane CRS
121: crs = "EPSG:4326";
122: } else {
123: crs = (String) srs.iterator().next();
124: }
125:
126: //System.out.println("crs = " + crs);
127: if (bbox == null) {
128: HashMap bboxes = layer.getBoundingBoxes();
129: Set keys = bboxes.keySet();
130: String k = "";
131:
132: for (Iterator it = keys.iterator(); it.hasNext(); k = (String) it
133: .next()) {
134: //System.out.println(k + " -> " + bboxes.get(k));
135: }
136:
137: CRSEnvelope bb = (CRSEnvelope) bboxes.get(crs);
138:
139: if (bb == null) { // something bad happened
140: bb = layer.getLatLonBoundingBox();
141: bb.setEPSGCode("EPSG:4326"); // for some reason WMS doesn't
142: // set
143: // this.
144:
145: crs = "EPSG:4326";
146: }
147:
148: CoordinateReferenceSystem coordinateReferenceSystem = null;
149:
150: try {
151: coordinateReferenceSystem = CRS.decode(crs);
152: } catch (FactoryRegistryException e) {
153: // TODO Auto-generated catch block
154: e.printStackTrace();
155: } catch (FactoryException e) {
156: // TODO Auto-generated catch block
157: e.printStackTrace();
158: }
159:
160: bbox = new ReferencedEnvelope(bb.getMinX(), bb.getMaxX(),
161: bb.getMinY(), bb.getMaxY(),
162: coordinateReferenceSystem);
163:
164: bounds = bbox;
165: }
166:
167: // fix the bounds for the shape of the window.
168:
169: //System.out.println(bbox.toString());
170: mapRequest.setSRS(crs);
171: mapRequest.setBBox(bbox.getMinX() + "," + bbox.getMinY() + ","
172: + bbox.getMaxX() + "," + bbox.getMaxY());
173: mapRequest.setTransparent(transparent);
174:
175: URL request = mapRequest.getFinalURL();
176:
177: //System.out.println(request.toString());
178: InputStream is = null;
179:
180: try {
181: URLConnection connection = request.openConnection();
182: String type = connection.getContentType();
183: is = connection.getInputStream();
184:
185: if (type.equalsIgnoreCase("image/png")) {
186: BufferedImage image = ImageIO.read(is);
187:
188: /*
189: * if (bbox == null) { Envelope2D env = new Envelope2D(bb
190: * .getCoordinateReferenceSystem(), bb.getMinX(), bb .getMinY(),
191: * bb.getLength(0), bb.getLength(1)); bbox = new
192: * ReferencedEnvelope(env); }
193: */
194: grid = gcf.create(layer.getTitle(), image, bbox);
195: //System.out.println("fetched new grid");
196: //if (featureSource == null)
197: featureSource = DataUtilities.source(FeatureUtilities
198: .wrapGridCoverage(grid));
199:
200: fireMapLayerListenerLayerChanged(new MapLayerEvent(
201: this , MapLayerEvent.DATA_CHANGED));
202: } else {
203: System.out.println("error content type is " + type);
204:
205: if (StringUtils.contains(type, "text")
206: || StringUtils.contains(type, "xml")) {
207: String line = "";
208: BufferedReader br = new BufferedReader(
209: new InputStreamReader(is));
210:
211: while ((line = br.readLine()) != null) {
212: System.out.println(line);
213: }
214: }
215: }
216: } catch (IOException e1) {
217: // TODO Auto-generated catch block
218: e1.printStackTrace();
219: } catch (Exception ex) {
220: // TODO Auto-generated catch block
221: ex.printStackTrace();
222: } finally {
223: try {
224: if (is != null) {
225: is.close();
226: }
227: } catch (IOException e) {
228: // TODO Auto-generated catch block
229: e.printStackTrace();
230: }
231: }
232: }
233:
234: public void mapBoundsChanged(MapBoundsEvent event) {
235: bbox = ((MapContext) event.getSource()).getAreaOfInterest();
236:
237: // System.out.println("old:" + bbox + "\n" + "new:"
238: // + event.getOldAreaOfInterest());
239: if (!bbox.equals(event.getOldAreaOfInterest())) {
240: System.out.println("bbox changed - fetching new grid");
241:
242: Thread t = new Thread() {
243: public void run() {
244: setGrid();
245: }
246: };
247:
248: SwingUtilities.invokeLater(t);
249: }
250: }
251:
252: public int getHeight() {
253: return height;
254: }
255:
256: public void setHeight(int height) {
257: this .height = height;
258: }
259:
260: public int getWidth() {
261: return width;
262: }
263:
264: public void setWidth(int width) {
265: this .width = width;
266: }
267:
268: public GridCoverage2D getGrid() {
269: return grid;
270: }
271:
272: public boolean isTransparent() {
273: return transparent;
274: }
275:
276: public void setTransparent(boolean transparent) {
277: this .transparent = transparent;
278: }
279:
280: public void componentHidden(ComponentEvent e) {
281: // TODO Auto-generated method stub
282: }
283:
284: public void componentMoved(ComponentEvent e) {
285: // TODO Auto-generated method stub
286: }
287:
288: public void componentResized(ComponentEvent e) {
289: Component c = (Component) e.getSource();
290: width = c.getWidth();
291: height = c.getHeight();
292: }
293:
294: public void componentShown(ComponentEvent e) {
295: Component c = (Component) e.getSource();
296: width = c.getWidth();
297: height = c.getHeight();
298: }
299:
300: public String getBgColour() {
301: return bgColour;
302: }
303:
304: public void setBgColour(String bgColour) {
305: this .bgColour = bgColour;
306: }
307:
308: public String getExceptions() {
309: return exceptions;
310: }
311:
312: /**
313: * Set the type of exception reports.
314: * Valid values are:
315: * "application/vnd.ogc.se_xml" (the default)
316: * "application/vnd.ogc.se_inimage"
317: * "application/vnd.ogc.se_blank"
318: *
319: * @param exceptions
320: */
321: public void setExceptions(String exceptions) {
322: this .exceptions = exceptions;
323: }
324:
325: public ReferencedEnvelope getBounds() {
326: //System.out.println("got bounds");
327: return bounds;
328: }
329: }
|