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

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

Sonar/FindBugs - Performance - Method concatenates strings using + in a loop (forgot this one)

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