View Javadoc

1   /*
2    * Trip Tracker, a real-time position tracking system for the Internet.
3    * Copyright (C) 2006  Team Trip Tracker
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  
20  package triptracker.client.map.core;
21  
22  import java.awt.image.BufferedImage;
23  import java.io.IOException;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.LinkedHashSet;
29  import java.util.Set;
30  
31  import javax.imageio.ImageIO;
32  import javax.swing.ImageIcon;
33  
34  public class WMSMapProvider extends AbstractMapProvider {
35  //	private List<String> params = new ArrayList<String>();
36  	private String srs = "EPSG:4326";
37  //	private String srs = "EPSG:32632";
38  	private Set<String> layers = new LinkedHashSet<String>();
39  	private Set<String> styles = new LinkedHashSet<String>();
40  	private String imageFormat = "image/png";
41  	private String bgColor = "0xFFFFFF";
42  	private boolean transparent = false;
43  	private String providerUrl = "http://triptracker.dyndns.org/mapservice.php"
44  			+ "?source=1&";
45  //	private String providerUrl = "http://basiswms.statkart.no/wmsconnector"
46  //		+ "/com.esri.wsit.WMSServlet/Topografisk_Norgeskart_wms?";
47  //	private String providerUrl
48  //			= "http://www.norgeibilder.no/services/wms/wms.aspx?";
49  
50  	public WMSMapProvider() {
51  		super();
52  		addDefLayers();
53  	}
54  	
55  	public WMSMapProvider(int height, int width) {
56  		super(height, width);
57  		addDefLayers();
58  	}
59  	
60  	/***
61  	 * Adds default layers for the statkart.no WMS Topografisk_Norgeskart_wms.
62  	 */
63  	private void addDefLayers() {
64  		addLayer("N5000Landtone");
65  		addLayer("N5000Vannflate");
66  		addLayer("N5000Markslag");
67  		addLayer("N5000Vannkontur");
68  		addLayer("N5000Veger");
69  		addLayer("N5000Kommunegrenser");
70  //		addLayer("N5000Bebyggelse");
71  		addLayer("N5000Fylkesnavn");
72  		addLayer("N2000Vannflate");
73  		addLayer("N2000Markslag");
74  		addLayer("N1000Markslag");
75  		addLayer("N1000Vannflate");
76  		addLayer("N1000Vannkontur");
77  		addLayer("N1000Elver");
78  		addLayer("N500Markslag");
79  		addLayer("N2000Vannkontur");
80  		addLayer("N2000Elver");
81  		addLayer("N2000Veger");
82  //		addLayer("N2000Bebyggelse");
83  		addLayer("N1000Veger");
84  		addLayer("N2000Kommunegrenser");
85  		addLayer("N1000Kommunegrenser");
86  		addLayer("N500Vannflate");
87  		addLayer("N500Vannkontur");
88  		addLayer("N500Elver");
89  		addLayer("N500Veger");
90  //		addLayer("N500Hoydekurver");
91  		addLayer("N500Kommunegrenser");
92  //		addLayer("N500Bebyggelse");
93  		addLayer("N250Vannflate");
94  		addLayer("N250Markslag_1");
95  		addLayer("N250Vannkontur");
96  		addLayer("N250Elver");
97  		addLayer("N250Veger");
98  		addLayer("N250Jernbane");
99  		addLayer("N250Hoydekurver");
100 		addLayer("N250Kommunegrenser");
101 //		addLayer("N250Bebyggelse");
102 		addLayer("N250Stedsnavn_punkt");
103 		addLayer("N250Stedsnavn_punkt_1");
104 		addLayer("N250Stedsnavn_linje");
105 		addLayer("N250Stedsnavn_linje-1");
106 		addLayer("N500Stedsnavn_punkt");
107 		addLayer("N50Markslag");
108 		addLayer("N50Vannflate");
109 		addLayer("N50Vannkontur");
110 		addLayer("N50Elver");
111 		addLayer("N50Veger");
112 		addLayer("N50Jernbane");
113 		addLayer("N50Bygninger");
114 		addLayer("N50Hoydekurver");
115 		addLayer("N50Kommunegrenser");
116 		addLayer("N50Bebyggelse");
117 		addLayer("N50Stedsnavn_punkt");
118 		addLayer("N50Stedsnavn_linje");
119 		addLayer("FKB_HOYDE_LINJE");
120 		addLayer("FKB_BYGG_FLATE");
121 		addLayer("FKB_VEGNETT_LINJE");
122 		addLayer("FKB_GATENAVN");
123 		addLayer("FKB_ADRESSE_PUNKT");
124 		addLayer("FKB_ADRESSE_PUNKT");
125 		addLayer("FKB_Stedsnavn_punkt");
126 		addLayer("FKB_Stedsnavn_linje");
127 	}	
128 	
129 //	/***
130 //	 * Adds default layers for the Norge i Bilder WMS.
131 //	 */
132 //	private void addDefLayersNoB() {
133 //		addLayer("SatelliteImage");
134 //		addLayer("Backdrops");
135 //	}	
136 
137    	/***
138 	 * {@inheritDoc}
139 	 */
140 	@Override
141 	public ImageIcon getMap(double minX, double minY,
142 			double maxX, double maxY) {
143 		URL url = null;
144 		BufferedImage bufImage = null;
145 
146 //		if (true) {
147 //			UTMPoint coord = Coordinate.llToUTM(22, minY, minX);
148 //			minX = coord.getEasting();
149 //			minY = coord.getNorthing();
150 //			coord = Coordinate.llToUTM(22, maxY, maxX);
151 //			maxX = coord.getEasting();
152 //			maxY = coord.getNorthing();
153 //		}
154 		
155 		String bBox = "BBOX=" + minX + "," + minY + "," + maxX + "," + maxY;
156 
157 //		System.out.println("URL: " + providerUrl + getURLParams() + "&" + bBox);
158 
159 		try {
160 			url = new URL(providerUrl + getURLParams() + "&" + bBox); 
161 		} catch (MalformedURLException e) {
162 			return null;
163 		}
164 		
165         try {
166 			bufImage = ImageIO.read(url);
167 		} catch (IOException e) {
168 			return null;
169 		}
170 		
171 		ImageIcon image = null;
172 		if (bufImage == null) {
173 			System.out.println("WMSMapProvider.getMap(): "
174 					+ "Received map image == null");
175 		}
176 		else 
177 			image = new ImageIcon(bufImage);
178 		return image;
179 	}
180 	
181 	/***
182 	 * Compiles the list of arguments for the provider URL.
183 	 * 
184 	 * @return provider URL arguments
185 	 */
186 	private String getURLParams() {
187 		StringBuilder params = new StringBuilder();
188 		Iterator<String> iter;
189 		
190 		params.append("REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1");
191 		
192 		// Add all layers.
193 		params.append("&LAYERS=");
194 		if (layers.size() > 0) {
195 			iter = layers.iterator();
196 			params.append(iter.next());
197 			
198 			while (iter.hasNext()) {
199 				params.append("," + iter.next());
200 			}
201 		}
202 		
203 		params.append("&STYLES=");
204 		// Add all styles.
205 
206 		if (styles.size() > 0) {
207 			iter = styles.iterator();
208 			params.append(iter.next());
209 			
210 			while (iter.hasNext()) {
211 				params.append("," + iter.next());
212 			}
213 		}
214 		
215 		params.append("&FORMAT=" + getImageFormat());
216 		params.append("&BGCOLOR=" + getBackgroundColor());
217 		params.append("&TRANSPARENT=" + isTransparent());
218 		params.append("&SRS=" + getSRS());
219 		params.append("&WIDTH=" + getWidth());
220 		params.append("&HEIGHT=" + getHeight());
221 
222 		return params.toString();
223 	}
224 	
225 	public void addLayers(Collection<String> layers) {
226 		this.layers.addAll(layers);
227 	}
228 	
229 	public void addLayer(String layerName) {
230 		layers.add(layerName);
231 	}
232 	
233 	public boolean hasLayer(String layerName) {
234 		return layers.contains(layerName);
235 	}
236 	
237 	public void removeLayer(String layerName) {
238 		layers.remove(layerName);
239 	}
240 	
241 	public void removeAllLayers() {
242 		layers.clear();
243 	}
244 	public void addStyles(Collection<String> styles) {
245 		this.styles.addAll(styles);
246 	}
247 	
248 	public void addStyle(String styleName) {
249 		styles.add(styleName);
250 	}
251 	
252 	public boolean hasStyle(String styleName) {
253 		return styles.contains(styleName);
254 	}
255 	
256 	public void removeStyle(String styleName) {
257 		styles.remove(styleName);
258 	}
259 	
260 	public void removeAllStyles() {
261 		styles.clear();
262 	}
263 
264 	public String getImageFormat() {
265 		return imageFormat;
266 	}
267 
268 	public void setImageFormat(String imageFormat) {
269 		this.imageFormat = imageFormat;
270 	}
271 
272 	public String getProviderUrl() {
273 		return providerUrl;
274 	}
275 
276 	public void setProviderUrl(String providerUrl) {
277 		this.providerUrl = providerUrl;
278 	}
279 
280 	public String getSRS() {
281 		return srs;
282 	}
283 
284 	public void setSRS(String srs) {
285 		this.srs = srs;
286 	}
287 
288 	public String getBackgroundColor() {
289 		return bgColor;
290 	}
291 
292 	public void setBackgroundColor(String bgColor) {
293 		this.bgColor = bgColor;
294 	}
295 
296 	public boolean isTransparent() {
297 		return transparent;
298 	}
299 
300 	public void setTransparent(boolean transparent) {
301 		this.transparent = transparent;
302 	}
303 
304 }