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

Last change on this file since 6310 was 6310, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Nested blocks of code should not be left empty

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