source: josm/trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java@ 5617

Last change on this file since 5617 was 5617, checked in by simon04, 11 years ago

see #8254 - new imagery entry wms_endpoint: Store WMS endpoint only, select layers at usage

  • Property svn:eol-style set to native
File size: 18.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.awt.Image;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.List;
10import java.util.regex.Matcher;
11import java.util.regex.Pattern;
12
13import javax.swing.ImageIcon;
14
15import org.openstreetmap.gui.jmapviewer.Coordinate;
16import org.openstreetmap.gui.jmapviewer.interfaces.Attributed;
17import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTileSource;
18import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource.Mapnik;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.Bounds;
21import org.openstreetmap.josm.data.Preferences.pref;
22import org.openstreetmap.josm.io.OsmApi;
23import org.openstreetmap.josm.tools.CheckParameterUtil;
24import org.openstreetmap.josm.tools.ImageProvider;
25
26/**
27 * Class that stores info about an image background layer.
28 *
29 * @author Frederik Ramm <frederik@remote.org>
30 */
31public class ImageryInfo implements Comparable<ImageryInfo>, Attributed {
32
33 public enum ImageryType {
34 WMS("wms"),
35 TMS("tms"),
36 HTML("html"),
37 BING("bing"),
38 SCANEX("scanex"),
39 WMS_ENDPOINT("wms_endpoint");
40
41 private String urlString;
42
43 ImageryType(String urlString) {
44 this.urlString = urlString;
45 }
46
47 public String getUrlString() {
48 return urlString;
49 }
50
51 public static ImageryType fromUrlString(String s) {
52 for (ImageryType type : ImageryType.values()) {
53 if (type.getUrlString().equals(s)) {
54 return type;
55 }
56 }
57 return null;
58 }
59 }
60
61 public static class ImageryBounds extends Bounds {
62 public ImageryBounds(String asString, String separator) {
63 super(asString, separator);
64 }
65
66 private List<Shape> shapes = new ArrayList<Shape>();
67
68 public void addShape(Shape shape) {
69 this.shapes.add(shape);
70 }
71
72 public void setShapes(List<Shape> shapes) {
73 this.shapes = shapes;
74 }
75
76 public List<Shape> getShapes() {
77 return shapes;
78 }
79 }
80
81 private String name;
82 private String url = null;
83 private boolean defaultEntry = false;
84 private String cookies = null;
85 private String eulaAcceptanceRequired= null;
86 private ImageryType imageryType = ImageryType.WMS;
87 private double pixelPerDegree = 0.0;
88 private int defaultMaxZoom = 0;
89 private int defaultMinZoom = 0;
90 private ImageryBounds bounds = null;
91 private List<String> serverProjections;
92 private String attributionText;
93 private String attributionLinkURL;
94 private String attributionImage;
95 private String attributionImageURL;
96 private String termsOfUseText;
97 private String termsOfUseURL;
98 private String countryCode = "";
99 private String icon;
100 // when adding a field, also adapt the ImageryInfo(ImageryInfo) constructor
101
102 /** auxiliary class to save an ImageryInfo object in the preferences */
103 public static class ImageryPreferenceEntry {
104 @pref String name;
105 @pref String type;
106 @pref String url;
107 @pref double pixel_per_eastnorth;
108 @pref String eula;
109 @pref String attribution_text;
110 @pref String attribution_url;
111 @pref String logo_image;
112 @pref String logo_url;
113 @pref String terms_of_use_text;
114 @pref String terms_of_use_url;
115 @pref String country_code = "";
116 @pref int max_zoom;
117 @pref int min_zoom;
118 @pref String cookies;
119 @pref String bounds;
120 @pref String shapes;
121 @pref String projections;
122 @pref String icon;
123
124 public ImageryPreferenceEntry() {
125 }
126
127 public ImageryPreferenceEntry(ImageryInfo i) {
128 name = i.name;
129 type = i.imageryType.getUrlString();
130 url = i.url;
131 pixel_per_eastnorth = i.pixelPerDegree;
132 eula = i.eulaAcceptanceRequired;
133 attribution_text = i.attributionText;
134 attribution_url = i.attributionLinkURL;
135 logo_image = i.attributionImage;
136 logo_url = i.attributionImageURL;
137 terms_of_use_text = i.termsOfUseText;
138 terms_of_use_url = i.termsOfUseURL;
139 country_code = i.countryCode;
140 max_zoom = i.defaultMaxZoom;
141 min_zoom = i.defaultMinZoom;
142 cookies = i.cookies;
143 icon = i.icon;
144 if (i.bounds != null) {
145 bounds = i.bounds.encodeAsString(",");
146 String shapesString = "";
147 for (Shape s : i.bounds.getShapes()) {
148 if (!shapesString.isEmpty()) {
149 shapesString += ";";
150 }
151 shapesString += s.encodeAsString(",");
152 }
153 if (!shapesString.isEmpty()) {
154 shapes = shapesString;
155 }
156 }
157 if (i.serverProjections != null && !i.serverProjections.isEmpty()) {
158 String val = "";
159 for (String p : i.serverProjections) {
160 if (!val.isEmpty())
161 val += ",";
162 val += p;
163 }
164 projections = val;
165 }
166 }
167 }
168
169 public ImageryInfo() {
170 }
171
172 public ImageryInfo(String name) {
173 this.name=name;
174 }
175
176 public ImageryInfo(String name, String url) {
177 this.name=name;
178 setExtendedUrl(url);
179 }
180
181 public ImageryInfo(String name, String url, String eulaAcceptanceRequired) {
182 this.name=name;
183 setExtendedUrl(url);
184 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
185 }
186
187 public ImageryInfo(String name, String url, String eulaAcceptanceRequired, String cookies) {
188 this.name=name;
189 setExtendedUrl(url);
190 this.cookies=cookies;
191 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
192 }
193
194 public ImageryInfo(String name, String url, String type, String eulaAcceptanceRequired, String cookies) {
195 this.name=name;
196 setExtendedUrl(url);
197 ImageryType t = ImageryType.fromUrlString(type);
198 this.cookies=cookies;
199 if (t != null) {
200 this.imageryType = t;
201 }
202 }
203
204 public ImageryInfo(String name, String url, String cookies, double pixelPerDegree) {
205 this.name=name;
206 setExtendedUrl(url);
207 this.cookies=cookies;
208 this.pixelPerDegree=pixelPerDegree;
209 }
210
211 public ImageryInfo(ImageryPreferenceEntry e) {
212 CheckParameterUtil.ensureParameterNotNull(e.name, "name");
213 CheckParameterUtil.ensureParameterNotNull(e.url, "url");
214 name = e.name;
215 url = e.url;
216 cookies = e.cookies;
217 eulaAcceptanceRequired = e.eula;
218 imageryType = ImageryType.fromUrlString(e.type);
219 if (imageryType == null) throw new IllegalArgumentException("unknown type");
220 pixelPerDegree = e.pixel_per_eastnorth;
221 defaultMaxZoom = e.max_zoom;
222 defaultMinZoom = e.min_zoom;
223 if (e.bounds != null) {
224 bounds = new ImageryBounds(e.bounds, ",");
225 if (e.shapes != null) {
226 try {
227 for (String s : e.shapes.split(";")) {
228 bounds.addShape(new Shape(s, ","));
229 }
230 } catch (IllegalArgumentException ex) {
231 Main.warn(ex.toString());
232 }
233 }
234 }
235 if (e.projections != null) {
236 serverProjections = Arrays.asList(e.projections.split(","));
237 }
238 attributionText = e.attribution_text;
239 attributionLinkURL = e.attribution_url;
240 attributionImage = e.logo_image;
241 attributionImageURL = e.logo_url;
242 termsOfUseText = e.terms_of_use_text;
243 termsOfUseURL = e.terms_of_use_url;
244 countryCode = e.country_code;
245 icon = e.icon;
246 }
247
248 public ImageryInfo(ImageryInfo i) {
249 this.name = i.name;
250 this.url = i.url;
251 this.defaultEntry = i.defaultEntry;
252 this.cookies = i.cookies;
253 this.eulaAcceptanceRequired = null;
254 this.imageryType = i.imageryType;
255 this.pixelPerDegree = i.pixelPerDegree;
256 this.defaultMaxZoom = i.defaultMaxZoom;
257 this.defaultMinZoom = i.defaultMinZoom;
258 this.bounds = i.bounds;
259 this.serverProjections = i.serverProjections;
260 this.attributionText = i.attributionText;
261 this.attributionLinkURL = i.attributionLinkURL;
262 this.attributionImage = i.attributionImage;
263 this.attributionImageURL = i.attributionImageURL;
264 this.termsOfUseText = i.termsOfUseText;
265 this.termsOfUseURL = i.termsOfUseURL;
266 this.countryCode = i.countryCode;
267 this.icon = i.icon;
268 }
269
270 @Override
271 public boolean equals(Object o) {
272 if (this == o) return true;
273 if (o == null || getClass() != o.getClass()) return false;
274
275 ImageryInfo that = (ImageryInfo) o;
276
277 if (imageryType != that.imageryType) return false;
278 if (url != null ? !url.equals(that.url) : that.url != null) return false;
279 if (name != null ? !name.equals(that.name) : that.name != null) return false;
280
281 return true;
282 }
283
284 @Override
285 public int hashCode() {
286 int result = url != null ? url.hashCode() : 0;
287 result = 31 * result + (imageryType != null ? imageryType.hashCode() : 0);
288 return result;
289 }
290
291 @Override
292 public String toString() {
293 return "ImageryInfo{" +
294 "name='" + name + '\'' +
295 ", countryCode='" + countryCode + '\'' +
296 ", url='" + url + '\'' +
297 ", imageryType=" + imageryType +
298 '}';
299 }
300
301 @Override
302 public int compareTo(ImageryInfo in)
303 {
304 int i = countryCode.compareTo(in.countryCode);
305 if (i == 0) {
306 i = name.compareTo(in.name);
307 }
308 if (i == 0) {
309 i = url.compareTo(in.url);
310 }
311 if (i == 0) {
312 i = Double.compare(pixelPerDegree, in.pixelPerDegree);
313 }
314 return i;
315 }
316
317 public boolean equalsBaseValues(ImageryInfo in)
318 {
319 return url.equals(in.url);
320 }
321
322 public void setPixelPerDegree(double ppd) {
323 this.pixelPerDegree = ppd;
324 }
325
326 public void setDefaultMaxZoom(int defaultMaxZoom) {
327 this.defaultMaxZoom = defaultMaxZoom;
328 }
329
330 public void setDefaultMinZoom(int defaultMinZoom) {
331 this.defaultMinZoom = defaultMinZoom;
332 }
333
334 public void setBounds(ImageryBounds b) {
335 this.bounds = b;
336 }
337
338 public ImageryBounds getBounds() {
339 return bounds;
340 }
341
342 @Override
343 public boolean requiresAttribution() {
344 return attributionText != null || attributionImage != null || termsOfUseText != null || termsOfUseURL != null;
345 }
346
347 @Override
348 public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) {
349 return attributionText;
350 }
351
352 @Override
353 public String getAttributionLinkURL() {
354 return attributionLinkURL;
355 }
356
357 @Override
358 public Image getAttributionImage() {
359 ImageIcon i = ImageProvider.getIfAvailable(attributionImage);
360 if (i != null) {
361 return i.getImage();
362 }
363 return null;
364 }
365
366 @Override
367 public String getAttributionImageURL() {
368 return attributionImageURL;
369 }
370
371 @Override
372 public String getTermsOfUseText() {
373 return termsOfUseText;
374 }
375
376 @Override
377 public String getTermsOfUseURL() {
378 return termsOfUseURL;
379 }
380
381 public void setAttributionText(String text) {
382 attributionText = text;
383 }
384
385 public void setAttributionImageURL(String text) {
386 attributionImageURL = text;
387 }
388
389 public void setAttributionImage(String text) {
390 attributionImage = text;
391 }
392
393 public void setAttributionLinkURL(String text) {
394 attributionLinkURL = text;
395 }
396
397 public void setTermsOfUseText(String text) {
398 termsOfUseText = text;
399 }
400
401 public void setTermsOfUseURL(String text) {
402 termsOfUseURL = text;
403 }
404
405 public void setExtendedUrl(String url) {
406 CheckParameterUtil.ensureParameterNotNull(url);
407
408 // Default imagery type is WMS
409 this.url = url;
410 this.imageryType = ImageryType.WMS;
411
412 defaultMaxZoom = 0;
413 defaultMinZoom = 0;
414 for (ImageryType type : ImageryType.values()) {
415 Matcher m = Pattern.compile(type.getUrlString()+"(?:\\[(?:(\\d+),)?(\\d+)\\])?:(.*)").matcher(url);
416 if(m.matches()) {
417 this.url = m.group(3);
418 this.imageryType = type;
419 if(m.group(2) != null) {
420 defaultMaxZoom = Integer.valueOf(m.group(2));
421 }
422 if(m.group(1) != null) {
423 defaultMinZoom = Integer.valueOf(m.group(1));
424 }
425 break;
426 }
427 }
428
429 if(serverProjections == null || serverProjections.isEmpty()) {
430 try {
431 serverProjections = new ArrayList<String>();
432 Matcher m = Pattern.compile(".*\\{PROJ\\(([^)}]+)\\)\\}.*").matcher(url.toUpperCase());
433 if(m.matches()) {
434 for(String p : m.group(1).split(","))
435 serverProjections.add(p);
436 }
437 } catch(Exception e) {
438 }
439 }
440 }
441
442 public String getName() {
443 return this.name;
444 }
445
446 public void setName(String name) {
447 this.name = name;
448 }
449
450 public String getUrl() {
451 return this.url;
452 }
453
454 public void setUrl(String url) {
455 this.url = url;
456 }
457
458 public boolean isDefaultEntry() {
459 return defaultEntry;
460 }
461
462 public void setDefaultEntry(boolean defaultEntry) {
463 this.defaultEntry = defaultEntry;
464 }
465
466 public String getCookies() {
467 return this.cookies;
468 }
469
470 public double getPixelPerDegree() {
471 return this.pixelPerDegree;
472 }
473
474 public int getMaxZoom() {
475 return this.defaultMaxZoom;
476 }
477
478 public int getMinZoom() {
479 return this.defaultMinZoom;
480 }
481
482 public String getEulaAcceptanceRequired() {
483 return eulaAcceptanceRequired;
484 }
485
486 public void setEulaAcceptanceRequired(String eulaAcceptanceRequired) {
487 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
488 }
489
490 public String getCountryCode() {
491 return countryCode;
492 }
493
494 public void setCountryCode(String countryCode) {
495 this.countryCode = countryCode;
496 }
497
498 public String getIcon() {
499 return icon;
500 }
501
502 public void setIcon(String icon) {
503 this.icon = icon;
504 }
505
506 /**
507 * Get the projections supported by the server. Only relevant for
508 * WMS-type ImageryInfo at the moment.
509 * @return null, if no projections have been specified; the list
510 * of supported projections otherwise.
511 */
512 public List<String> getServerProjections() {
513 if (serverProjections == null)
514 return Collections.emptyList();
515 return Collections.unmodifiableList(serverProjections);
516 }
517
518 public void setServerProjections(Collection<String> serverProjections) {
519 this.serverProjections = new ArrayList<String>(serverProjections);
520 }
521
522 public String getExtendedUrl() {
523 return imageryType.getUrlString() + (defaultMaxZoom != 0
524 ? "["+(defaultMinZoom != 0 ? defaultMinZoom+",":"")+defaultMaxZoom+"]" : "") + ":" + url;
525 }
526
527 public String getToolbarName()
528 {
529 String res = name;
530 if(pixelPerDegree != 0.0) {
531 res += "#PPD="+pixelPerDegree;
532 }
533 return res;
534 }
535
536 public String getMenuName()
537 {
538 String res = name;
539 if(pixelPerDegree != 0.0) {
540 res += " ("+pixelPerDegree+")";
541 }
542 return res;
543 }
544
545 public boolean hasAttribution()
546 {
547 return attributionText != null;
548 }
549
550 public void copyAttribution(ImageryInfo i)
551 {
552 this.attributionImage = i.attributionImage;
553 this.attributionImageURL = i.attributionImageURL;
554 this.attributionText = i.attributionText;
555 this.attributionLinkURL = i.attributionLinkURL;
556 this.termsOfUseText = i.termsOfUseText;
557 this.termsOfUseURL = i.termsOfUseURL;
558 }
559
560 /**
561 * Applies the attribution from this object to a TMSTileSource.
562 */
563 public void setAttribution(AbstractTileSource s) {
564 if (attributionText != null) {
565 if (attributionText.equals("osm")) {
566 s.setAttributionText(new Mapnik().getAttributionText(0, null, null));
567 } else {
568 s.setAttributionText(attributionText);
569 }
570 }
571 if (attributionLinkURL != null) {
572 if (attributionLinkURL.equals("osm")) {
573 s.setAttributionLinkURL(new Mapnik().getAttributionLinkURL());
574 } else {
575 s.setAttributionLinkURL(attributionLinkURL);
576 }
577 }
578 if (attributionImage != null) {
579 ImageIcon i = ImageProvider.getIfAvailable(null, attributionImage);
580 if (i != null) {
581 s.setAttributionImage(i.getImage());
582 }
583 }
584 if (attributionImageURL != null) {
585 s.setAttributionImageURL(attributionImageURL);
586 }
587 if (termsOfUseText != null) {
588 s.setTermsOfUseText(termsOfUseText);
589 }
590 if (termsOfUseURL != null) {
591 if (termsOfUseURL.equals("osm")) {
592 s.setTermsOfUseURL(new Mapnik().getTermsOfUseURL());
593 } else {
594 s.setTermsOfUseURL(termsOfUseURL);
595 }
596 }
597 }
598
599 public ImageryType getImageryType() {
600 return imageryType;
601 }
602
603 public void setImageryType(ImageryType imageryType) {
604 this.imageryType = imageryType;
605 }
606
607 /**
608 * Returns true if this layer's URL is matched by one of the regular
609 * expressions kept by the current OsmApi instance.
610 */
611 public boolean isBlacklisted() {
612 return OsmApi.getOsmApi().getCapabilities().isOnImageryBlacklist(this.url);
613 }
614}
Note: See TracBrowser for help on using the repository browser.