source: josm/trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java@ 11690

Last change on this file since 11690 was 11690, checked in by stoecker, 7 years ago

see #14470 - don't drop imagery when default list is empty

  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Map;
15import java.util.Objects;
16import java.util.Set;
17import java.util.TreeSet;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.io.CachedFile;
23import org.openstreetmap.josm.io.OfflineAccessException;
24import org.openstreetmap.josm.io.OnlineResource;
25import org.openstreetmap.josm.io.imagery.ImageryReader;
26import org.openstreetmap.josm.tools.Utils;
27import org.xml.sax.SAXException;
28
29/**
30 * Manages the list of imagery entries that are shown in the imagery menu.
31 */
32public class ImageryLayerInfo {
33
34 public static final ImageryLayerInfo instance = new ImageryLayerInfo();
35 /** List of all usable layers */
36 private final List<ImageryInfo> layers = new ArrayList<>();
37 /** List of layer ids of all usable layers */
38 private final Map<String, ImageryInfo> layerIds = new HashMap<>();
39 /** List of all available default layers */
40 private static final List<ImageryInfo> defaultLayers = new ArrayList<>();
41 /** List of all available default layers (including mirrors) */
42 private static final List<ImageryInfo> allDefaultLayers = new ArrayList<>();
43 /** List of all layer ids of available default layers (including mirrors) */
44 private static final Map<String, ImageryInfo> defaultLayerIds = new HashMap<>();
45
46 private static final String[] DEFAULT_LAYER_SITES = {
47 Main.getJOSMWebsite()+"/maps"
48 };
49
50 /**
51 * Returns the list of imagery layers sites.
52 * @return the list of imagery layers sites
53 * @since 7434
54 */
55 public static Collection<String> getImageryLayersSites() {
56 return Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
57 }
58
59 private ImageryLayerInfo() {
60 }
61
62 public ImageryLayerInfo(ImageryLayerInfo info) {
63 layers.addAll(info.layers);
64 }
65
66 public void clear() {
67 layers.clear();
68 layerIds.clear();
69 }
70
71 /**
72 * Loads the custom as well as default imagery entries.
73 * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
74 */
75 public void load(boolean fastFail) {
76 clear();
77 List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class);
78 if (entries != null) {
79 for (ImageryPreferenceEntry prefEntry : entries) {
80 try {
81 ImageryInfo i = new ImageryInfo(prefEntry);
82 add(i);
83 } catch (IllegalArgumentException e) {
84 Main.warn("Unable to load imagery preference entry:"+e);
85 }
86 }
87 Collections.sort(layers);
88 }
89 loadDefaults(false, true, fastFail);
90 }
91
92 /**
93 * Loads the available imagery entries.
94 *
95 * The data is downloaded from the JOSM website (or loaded from cache).
96 * Entries marked as "default" are added to the user selection, if not
97 * already present.
98 *
99 * @param clearCache if true, clear the cache and start a fresh download.
100 * @param quiet whether not the loading should be performed using a {@link PleaseWaitRunnable} in the background
101 * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
102 */
103 public void loadDefaults(boolean clearCache, boolean quiet, boolean fastFail) {
104 final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
105 if (quiet) {
106 loader.realRun();
107 loader.finish();
108 } else {
109 Main.worker.execute(new DefaultEntryLoader(clearCache, fastFail));
110 }
111 }
112
113 /**
114 * Loader/updater of the available imagery entries
115 */
116 class DefaultEntryLoader extends PleaseWaitRunnable {
117
118 private final boolean clearCache;
119 private final boolean fastFail;
120 private final List<ImageryInfo> newLayers = new ArrayList<>();
121 private ImageryReader reader;
122 private boolean canceled;
123
124 DefaultEntryLoader(boolean clearCache, boolean fastFail) {
125 super(tr("Update default entries"));
126 this.clearCache = clearCache;
127 this.fastFail = fastFail;
128 }
129
130 @Override
131 protected void cancel() {
132 canceled = true;
133 Utils.close(reader);
134 }
135
136 @Override
137 protected void realRun() {
138 for (String source : getImageryLayersSites()) {
139 if (canceled) {
140 return;
141 }
142 loadSource(source);
143 }
144 }
145
146 protected void loadSource(String source) {
147 boolean online = true;
148 try {
149 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(source, Main.getJOSMWebsite());
150 } catch (OfflineAccessException e) {
151 Main.warn(e, false);
152 online = false;
153 }
154 if (clearCache && online) {
155 CachedFile.cleanup(source);
156 }
157 try {
158 reader = new ImageryReader(source);
159 reader.setFastFail(fastFail);
160 Collection<ImageryInfo> result = reader.parse();
161 newLayers.addAll(result);
162 } catch (IOException ex) {
163 Main.error(ex, false);
164 } catch (SAXException ex) {
165 Main.error(ex);
166 }
167 }
168
169 @Override
170 protected void finish() {
171 defaultLayers.clear();
172 allDefaultLayers.clear();
173 defaultLayers.addAll(newLayers);
174 for (ImageryInfo layer : newLayers) {
175 allDefaultLayers.add(layer);
176 for (ImageryInfo sublayer : layer.getMirrors()) {
177 allDefaultLayers.add(sublayer);
178 }
179 }
180 defaultLayerIds.clear();
181 Collections.sort(defaultLayers);
182 Collections.sort(allDefaultLayers);
183 buildIdMap(allDefaultLayers, defaultLayerIds);
184 updateEntriesFromDefaults();
185 buildIdMap(layers, layerIds);
186 dropOldEntries();
187 }
188 }
189
190 /**
191 * Build the mapping of unique ids to {@link ImageryInfo}s.
192 * @param lst input list
193 * @param idMap output map
194 */
195 private static void buildIdMap(List<ImageryInfo> lst, Map<String, ImageryInfo> idMap) {
196 idMap.clear();
197 Set<String> notUnique = new HashSet<>();
198 for (ImageryInfo i : lst) {
199 if (i.getId() != null) {
200 if (idMap.containsKey(i.getId())) {
201 notUnique.add(i.getId());
202 Main.error("Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!",
203 i.getId(), i.getName(), idMap.get(i.getId()).getName());
204 continue;
205 }
206 idMap.put(i.getId(), i);
207 }
208 }
209 for (String i : notUnique) {
210 idMap.remove(i);
211 }
212 }
213
214 /**
215 * Update user entries according to the list of default entries.
216 */
217 public void updateEntriesFromDefaults() {
218 // add new default entries to the user selection
219 boolean changed = false;
220 Collection<String> knownDefaults = Main.pref.getCollection("imagery.layers.default");
221 Collection<String> newKnownDefaults = new TreeSet<>(knownDefaults);
222 for (ImageryInfo def : defaultLayers) {
223 if (def.isDefaultEntry()) {
224 boolean isKnownDefault = false;
225 for (String url : knownDefaults) {
226 if (isSimilar(url, def.getUrl())) {
227 isKnownDefault = true;
228 break;
229 }
230 }
231 boolean isInUserList = false;
232 if (!isKnownDefault) {
233 newKnownDefaults.add(def.getUrl());
234 for (ImageryInfo i : layers) {
235 if (isSimilar(def, i)) {
236 isInUserList = true;
237 break;
238 }
239 }
240 }
241 if (!isKnownDefault && !isInUserList) {
242 add(new ImageryInfo(def));
243 changed = true;
244 }
245 }
246 }
247 Main.pref.putCollection("imagery.layers.default", newKnownDefaults);
248
249 // automatically update user entries with same id as a default entry
250 for (int i = 0; i < layers.size(); i++) {
251 ImageryInfo info = layers.get(i);
252 if (info.getId() == null) {
253 continue;
254 }
255 ImageryInfo matchingDefault = defaultLayerIds.get(info.getId());
256 if (matchingDefault != null && !matchingDefault.equalsPref(info)) {
257 layers.set(i, matchingDefault);
258 Main.info(tr("Update imagery ''{0}''", info.getName()));
259 changed = true;
260 }
261 }
262
263 if (changed) {
264 save();
265 }
266 }
267
268 /**
269 * Drop entries with Id which do no longer exist (removed from defaults).
270 * @since 11527
271 */
272 public void dropOldEntries() {
273 List<String> drop = new ArrayList<>();
274
275 if (defaultLayerIds.isEmpty()) {
276 return;
277 }
278
279 for (Map.Entry<String, ImageryInfo> info : layerIds.entrySet()) {
280 if (!defaultLayerIds.containsKey(info.getKey())) {
281 remove(info.getValue());
282 drop.add(info.getKey());
283 Main.info(tr("Drop old imagery ''{0}''", info.getValue().getName()));
284 }
285 }
286
287 if (!drop.isEmpty()) {
288 for (String id : drop) {
289 layerIds.remove(id);
290 }
291 save();
292 }
293 }
294
295 private static boolean isSimilar(ImageryInfo iiA, ImageryInfo iiB) {
296 if (iiA == null)
297 return false;
298 if (!iiA.getImageryType().equals(iiB.getImageryType()))
299 return false;
300 if (iiA.getId() != null && iiB.getId() != null) return iiA.getId().equals(iiB.getId());
301 return isSimilar(iiA.getUrl(), iiB.getUrl());
302 }
303
304 // some additional checks to respect extended URLs in preferences (legacy workaround)
305 private static boolean isSimilar(String a, String b) {
306 return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
307 }
308
309 public void add(ImageryInfo info) {
310 layers.add(info);
311 }
312
313 public void remove(ImageryInfo info) {
314 layers.remove(info);
315 }
316
317 public void save() {
318 List<ImageryPreferenceEntry> entries = new ArrayList<>();
319 for (ImageryInfo info : layers) {
320 entries.add(new ImageryPreferenceEntry(info));
321 }
322 Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
323 }
324
325 /**
326 * List of usable layers
327 * @return unmodifiable list containing usable layers
328 */
329 public List<ImageryInfo> getLayers() {
330 return Collections.unmodifiableList(layers);
331 }
332
333 /**
334 * List of available default layers
335 * @return unmodifiable list containing available default layers
336 */
337 public List<ImageryInfo> getDefaultLayers() {
338 return Collections.unmodifiableList(defaultLayers);
339 }
340
341 /**
342 * List of all available default layers (including mirrors)
343 * @return unmodifiable list containing available default layers
344 * @since 11570
345 */
346 public List<ImageryInfo> getAllDefaultLayers() {
347 return Collections.unmodifiableList(allDefaultLayers);
348 }
349
350 public static void addLayer(ImageryInfo info) {
351 instance.add(info);
352 instance.save();
353 }
354
355 public static void addLayers(Collection<ImageryInfo> infos) {
356 for (ImageryInfo i : infos) {
357 instance.add(i);
358 }
359 instance.save();
360 Collections.sort(instance.layers);
361 }
362
363 /**
364 * Get unique id for ImageryInfo.
365 *
366 * This takes care, that no id is used twice (due to a user error)
367 * @param info the ImageryInfo to look up
368 * @return null, if there is no id or the id is used twice,
369 * the corresponding id otherwise
370 */
371 public String getUniqueId(ImageryInfo info) {
372 if (info.getId() != null && layerIds.get(info.getId()) == info) {
373 return info.getId();
374 }
375 return null;
376 }
377}
Note: See TracBrowser for help on using the repository browser.