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

Last change on this file since 3872 was 3826, checked in by framm, 13 years ago

Add a "blacklisted" property to imagery layers; set this property from a
compiled-in list of regular expressions; disable blacklisted entries in
the Imagery menu.

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.util.ArrayList;
5import java.util.Collection;
6
7/**
8 * Class that stores info about an image background layer.
9 *
10 * @author Frederik Ramm <frederik@remote.org>
11 */
12public class ImageryInfo implements Comparable<ImageryInfo> {
13
14 public enum ImageryType {
15 WMS("wms"),
16 TMS("tms"),
17 HTML("html"),
18 BING("bing");
19
20 private String urlString;
21 ImageryType(String urlString) {
22 this.urlString = urlString;
23 }
24 public String getUrlString() {
25 return urlString;
26 }
27 }
28
29 private final static String[] BLACKLIST_REGEXES = {
30 // These entries are for Google tile servers (names and IPV4 numbers)
31 ".*\\.google\\.com/.*",
32 ".*209\\.85\\.2\\d\\d.*",
33 ".*209\\.85\\.1[3-9]\\d.*",
34 ".*209\\.85\\.12[89].*"
35 };
36
37 String name;
38 String url = null;
39 String cookies = null;
40 public final String eulaAcceptanceRequired;
41 ImageryType imageryType = ImageryType.WMS;
42 double pixelPerDegree = 0.0;
43 int maxZoom = 0;
44 private boolean blacklisted = false;
45
46 public ImageryInfo(String name) {
47 this.name=name;
48 this.eulaAcceptanceRequired = null;
49 }
50
51 public ImageryInfo(String name, String url) {
52 this.name=name;
53 setUrl(url);
54 this.eulaAcceptanceRequired = null;
55 }
56
57 public ImageryInfo(String name, String url, String eulaAcceptanceRequired) {
58 this.name=name;
59 setUrl(url);
60 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
61 }
62
63 public ImageryInfo(String name, String url, String eulaAcceptanceRequired, String cookies) {
64 this.name=name;
65 setUrl(url);
66 this.cookies=cookies;
67 this.eulaAcceptanceRequired = eulaAcceptanceRequired;
68 }
69
70 public ImageryInfo(String name, String url, String cookies, double pixelPerDegree) {
71 this.name=name;
72 setUrl(url);
73 this.cookies=cookies;
74 this.pixelPerDegree=pixelPerDegree;
75 this.eulaAcceptanceRequired = null;
76 }
77
78 public ArrayList<String> getInfoArray() {
79 String e2 = null;
80 String e3 = null;
81 String e4 = null;
82 if(url != null && !url.isEmpty()) {
83 e2 = getFullUrl();
84 }
85 if(cookies != null && !cookies.isEmpty()) {
86 e3 = cookies;
87 }
88 if(imageryType == ImageryType.WMS || imageryType == ImageryType.HTML) {
89 if(pixelPerDegree != 0.0) {
90 e4 = String.valueOf(pixelPerDegree);
91 }
92 } else {
93 if(maxZoom != 0) {
94 e4 = String.valueOf(maxZoom);
95 }
96 }
97 if(e4 != null && e3 == null) {
98 e3 = "";
99 }
100 if(e3 != null && e2 == null) {
101 e2 = "";
102 }
103
104 ArrayList<String> res = new ArrayList<String>();
105 res.add(name);
106 if(e2 != null) {
107 res.add(e2);
108 }
109 if(e3 != null) {
110 res.add(e3);
111 }
112 if(e4 != null) {
113 res.add(e4);
114 }
115 return res;
116 }
117
118 public ImageryInfo(Collection<String> list) {
119 ArrayList<String> array = new ArrayList<String>(list);
120 this.name=array.get(0);
121 if(array.size() >= 2) {
122 setUrl(array.get(1));
123 }
124 if(array.size() >= 3) {
125 this.cookies=array.get(2);
126 }
127 if(array.size() >= 4) {
128 if (imageryType == ImageryType.WMS || imageryType == ImageryType.HTML) {
129 this.pixelPerDegree=Double.valueOf(array.get(3));
130 } else {
131 this.maxZoom=Integer.valueOf(array.get(3));
132 }
133 }
134 this.eulaAcceptanceRequired = null;
135 }
136
137 public ImageryInfo(ImageryInfo i) {
138 this.name=i.name;
139 this.url=i.url;
140 this.cookies=i.cookies;
141 this.imageryType=i.imageryType;
142 this.pixelPerDegree=i.pixelPerDegree;
143 this.eulaAcceptanceRequired = null;
144 }
145
146 @Override
147 public int compareTo(ImageryInfo in)
148 {
149 int i = name.compareTo(in.name);
150 if(i == 0) {
151 i = url.compareTo(in.url);
152 }
153 if(i == 0) {
154 i = Double.compare(pixelPerDegree, in.pixelPerDegree);
155 }
156 return i;
157 }
158
159 public boolean equalsBaseValues(ImageryInfo in)
160 {
161 return url.equals(in.url);
162 }
163
164 public void setPixelPerDegree(double ppd) {
165 this.pixelPerDegree = ppd;
166 }
167
168 public void setMaxZoom(int maxZoom) {
169 this.maxZoom = maxZoom;
170 }
171
172 public void setUrl(String url) {
173
174 // determine if URL is on blacklist and flag accordingly.
175 blacklisted = false;
176 for (String blacklistRegex : BLACKLIST_REGEXES) {
177 if (url.matches(blacklistRegex)) {
178 blacklisted = true;
179 System.err.println("layer '" + name + "' uses blacklisted URL");
180 break;
181 }
182 }
183
184 for (ImageryType type : ImageryType.values()) {
185 if (url.startsWith(type.getUrlString() + ":")) {
186 this.url = url.substring(type.getUrlString().length() + 1);
187 this.imageryType = type;
188 return;
189 }
190 }
191
192 // Default imagery type is WMS
193 this.url = url;
194 this.imageryType = ImageryType.WMS;
195 }
196
197 public String getName() {
198 return this.name;
199 }
200
201 public void setName(String name) {
202 this.name = name;
203 }
204
205 public String getUrl() {
206 return this.url;
207 }
208
209 public String getCookies() {
210 return this.cookies;
211 }
212
213 public double getPixelPerDegree() {
214 return this.pixelPerDegree;
215 }
216
217 public int getMaxZoom() {
218 return this.maxZoom;
219 }
220
221 public String getFullUrl() {
222 return imageryType.getUrlString() + ":" + url;
223 }
224
225 public String getToolbarName()
226 {
227 String res = name;
228 if(pixelPerDegree != 0.0) {
229 res += "#PPD="+pixelPerDegree;
230 }
231 return res;
232 }
233
234 public String getMenuName()
235 {
236 String res = name;
237 if(pixelPerDegree != 0.0) {
238 res += " ("+pixelPerDegree+")";
239 } else if(maxZoom != 0) {
240 res += " (z"+maxZoom+")";
241 }
242 return res;
243 }
244
245 public ImageryType getImageryType() {
246 return imageryType;
247 }
248
249 public static boolean isUrlWithPatterns(String url) {
250 return url != null && url.contains("{") && url.contains("}");
251 }
252
253 public boolean isBlacklisted() {
254 return blacklisted;
255 }
256}
Note: See TracBrowser for help on using the repository browser.