001:
002: /*
003: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
004: * for visualizing and manipulating spatial features with geometry and attributes.
005: *
006: * Copyright (C) 2003 Vivid Solutions
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033:
034: package com.vividsolutions.wms;
035:
036: import java.awt.Image;
037: import java.awt.Toolkit;
038: import java.net.MalformedURLException;
039: import java.net.URL;
040: import java.net.URLEncoder;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import org.apache.log4j.Logger;
047:
048: import com.vividsolutions.jump.I18N;
049:
050: /**
051: * Represents all of the parameters of a getMap request from a WMS server.
052: * @author Chris Hodgson chodgson@refractions.net
053: */
054: public class MapRequest {
055:
056: private static Logger LOG = Logger.getLogger(MapRequest.class);
057:
058: private WMService service;
059: private int imgWidth;
060: private int imgHeight;
061: private List layerList;
062: private BoundingBox bbox;
063: private boolean transparent;
064: private String format;
065: //[UT]
066: private String version = WMService.WMS_1_0_0;
067:
068: /**
069: * Creates a new MapRequest.
070: * @param service the WMService which this MapRequest will use
071: */
072: public MapRequest(WMService service) {
073: this .service = service;
074: imgWidth = 100;
075: imgHeight = 100;
076: layerList = new ArrayList();
077: bbox = service.getCapabilities().getTopLayer().getBoundingBox();
078: transparent = false;
079: format = null;
080: }
081:
082: /**
083: * Gets the WMService that this object will make requests from.
084: * @return the WMService that this object will make requests from
085: */
086: public WMService getService() {
087: return service;
088: }
089:
090: /**
091: * Returns the format of this request.
092: * This may be a string such as GIF, JPEG, or PNG for a WMS 1.0 server, or
093: * a mime-type string in the case of a WMS 1.1 server. It may also be null if
094: * the format has not yet been set.
095: * @return the string representing the format of this request
096: */
097: public String getFormat() {
098: return format;
099: }
100:
101: /**
102: * Gets the width of the requested image, in pixels.
103: * The default image width is 100.
104: * @return the width of the requested image
105: */
106: public int getImageWidth() {
107: return imgWidth;
108: }
109:
110: /**
111: * Gets the height of the requested image, in pixels.
112: * The default image height is 100.
113: * @return the height of the requested image
114: */
115: public int getImageHeight() {
116: return imgHeight;
117: }
118:
119: /**
120: * Returns the list of layers to be requested. Each item in the
121: * list should be a String which is the name of a layer.
122: * @return the list of layer names to be requested
123: */
124: public List getLayers() {
125: //<<TODO:NAMING>> Might be clearer to name this method getLayerNames [Jon Aquino]
126: return Collections.unmodifiableList(layerList);
127: }
128:
129: /**
130: * Gets the BoundingBox of the image being requested.
131: * @return the BoundingBox of the image being requested
132: */
133: public BoundingBox getBoundingBox() {
134: return bbox;
135: }
136:
137: /**
138: * Gets whether or not a transparent image is being requested.
139: * @return true if a transparent image is being requested, false otherwise
140: */
141: public boolean getTransparent() {
142: return transparent;
143: }
144:
145: /**
146: * Sets the format of this request. The format must be a string which is in
147: * the list of supported formats as provided by getSupportedFormatList()
148: * (not necessarily the same String object, but the same sequence of characters).
149: * This will be an unformatted string for a WMS 1.0 server (GIF, JPEG, PNG) or
150: * a mime-type string for a WMS 1.1 server (image/gif, image/jpeg, image/png).
151: * If the format specified is not in the list, an IllegalArgumentException
152: * will be thrown.
153: *
154: * @param format a format string which is in the list of supported formats
155: * @throws IllegalArgumentException if the specified format isn't in the list of supported formats
156: * @see MapImageFormatChooser
157: *
158: */
159: public void setFormat(String format)
160: throws IllegalArgumentException {
161: // <<TODO:UNCOMMENT>> Temporarily commented out, until mapserver is fixed [Chris Hodgson]
162: // Temporarily removing the requirement that the requested format
163: // be in the list of supported formats, in order to work around a Mapserver bug.
164: //String[] formats = service.getCapabilities().getMapFormats();
165: //for( int i = 0; i < formats.length; i++ ) {
166: // if( formats[i].equals( format ) ) {
167: this .format = format;
168: return;
169: // }
170: //}
171: //throw new IllegalArgumentException();
172: }
173:
174: /**
175: * Sets the width of the image being requested.
176: * @param imageWidth the width of the image being requested
177: */
178: public void setImageWidth(int imageWidth) {
179: this .imgWidth = imageWidth;
180: }
181:
182: /**
183: * Sets the height of the image being requested.
184: * @param imageHeight the height of the image being requested
185: */
186: public void setImageHeight(int imageHeight) {
187: this .imgHeight = imageHeight;
188: }
189:
190: /**
191: * Sets the width and height of the image being requested.
192: * @param imageWidth the width of the image being requested
193: * @param imageHeight the height of the image being requested
194: */
195: public void setImageSize(int imageWidth, int imageHeight) {
196: this .imgWidth = imageWidth;
197: this .imgHeight = imageHeight;
198: }
199:
200: /**
201: * Sets the layers to be requested. Each item in the list should be a string
202: * which corresponds to the name of a layer. The order of the list is
203: * important as the layers are rendered in the same order they are listed.
204: * @param layerList an ordered List of the names of layers to be displayed
205: */
206: public void setLayers(List layerList) {
207: //<<TODO:NAMING>> Might be clearer to name this method setLayerNames [Jon Aquino]
208: this .layerList = layerList;
209: }
210:
211: /**
212: * Sets the BoundingBox of the image being requested.
213: * @param bbox the BoundingBox of the image being requested
214: */
215: public void setBoundingBox(BoundingBox bbox) {
216: this .bbox = bbox;
217: }
218:
219: /**
220: * Sets whether or not to request an image with a transparent background.
221: * Requesting a transparent background doesn't guarantee that the resulting
222: * image will actually have a transparent background. Not all servers
223: * support transparency, and not all formats support transparency.
224: * @param transparent true to request a transparent background, false otherwise.
225: */
226: public void setTransparent(boolean transparent) {
227: this .transparent = transparent;
228: }
229:
230: /**
231: * Returns a String containing the string representations of each item in the
232: * list (as provided by toString()), separated by commas.
233: * @param list the list to be returned as a coma-separated String
234: * @return a comma-separted String of the items in the list
235: */
236: //[UT] 02.05.2005 made static and public
237: public static String listToString(List list) {
238: Iterator it = list.iterator();
239: StringBuffer buf = new StringBuffer();
240: while (it.hasNext()) {
241: String layer = (String) it.next();
242: //Unfortunately we can't use the new two-parameter constructor because
243: //that was introduced in JDK 1.4, and JUMP is supposed to support 1.3. [Jon Aquino]
244: layer = URLEncoder.encode(layer);
245: // try {
246: // layer = URLEncoder.encode( layer, "UTF-8" );
247: // } catch( UnsupportedEncodingException uee ) {
248: // // ok then, we just won't bother to encode at all...
249: // }
250: buf.append(layer);
251: if (it.hasNext()) {
252: buf.append(",");
253: }
254: }
255: return buf.toString();
256: }
257:
258: /**
259: * @return the URL for this request
260: * @throws MalformedURLException if there is a problem building the URL for some reason
261: */
262: //[UT] changed to accept WMS 1.1.1
263: public URL getURL() throws MalformedURLException {
264: StringBuffer urlBuf = new StringBuffer();
265: String ver = "REQUEST=map&WMTVER=1.0";
266: if (WMService.WMS_1_1_0.equals(version)) {
267: ver = "REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.0";
268: } else if (WMService.WMS_1_1_1.equals(version)) {
269: ver = "REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1";
270: }
271: urlBuf.append(service.getServerUrl() + ver + "&WIDTH="
272: + imgWidth + "&HEIGHT=" + imgHeight);
273: urlBuf.append("&LAYERS=" + listToString(layerList));
274: if (transparent) {
275: urlBuf.append("&TRANSPARENT=TRUE");
276: }
277: if (format != null) {
278: urlBuf.append("&FORMAT=" + format);
279: }
280: if (bbox != null) {
281: urlBuf.append("&BBOX=" + bbox.getMinX() + ","
282: + bbox.getMinY() + "," + bbox.getMaxX() + ","
283: + bbox.getMaxY());
284: if (bbox.getSRS() != null
285: && !bbox.getSRS().equals("LatLon")) {
286: urlBuf.append("&SRS=" + bbox.getSRS());
287: }
288: }
289: // [UT] some style info is *required*, so add this to be spec conform
290: urlBuf.append("&STYLES=");
291:
292: LOG.info(urlBuf.toString());
293:
294: return new URL(urlBuf.toString());
295: }
296:
297: /**
298: * Connect to the service and get an Image of the map.
299: * @return the retrieved map Image
300: */
301: public Image getImage() throws MalformedURLException {
302: return Toolkit.getDefaultToolkit().createImage(getURL());
303:
304: /* for using local file method of download
305: InputStream inStream = requestUrl.openStream();
306: File tempImage = File.createTempFile( "wms", ".img");
307: System.out.println( tempImage.getAbsolutePath() );
308: FileOutputStream outStream = new FileOutputStream( tempImage );
309: byte[] bytes = new byte[1024];
310: int count;
311: while( (count = inStream.read( bytes )) > 0 ) {
312: outStream.write( bytes, 0, count );
313: }
314: outStream.close();
315: Image img = Toolkit.getDefaultToolkit().createImage( tempImage.getAbsolutePath() );
316: // tempImage.delete(); // this breaks the asynchronous loadin used in the above line
317: */
318:
319: }
320:
321: //UT
322: public void setVersion(String ver) {
323: this.version = ver;
324: }
325: }
|