1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Objects;
|
---|
14 | import java.util.Set;
|
---|
15 | import java.util.TreeSet;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
|
---|
19 | import org.openstreetmap.josm.io.CachedFile;
|
---|
20 | import org.openstreetmap.josm.io.OfflineAccessException;
|
---|
21 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
22 | import org.openstreetmap.josm.io.imagery.ImageryReader;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Manages the list of imagery entries that are shown in the imagery menu.
|
---|
27 | */
|
---|
28 | public class ImageryLayerInfo {
|
---|
29 |
|
---|
30 | public static final ImageryLayerInfo instance = new ImageryLayerInfo();
|
---|
31 | private final List<ImageryInfo> layers = new ArrayList<>();
|
---|
32 | private final Map<String, ImageryInfo> layerIds = new HashMap<>();
|
---|
33 | private static final List<ImageryInfo> defaultLayers = new ArrayList<>();
|
---|
34 | private static final Map<String, ImageryInfo> defaultLayerIds = new HashMap<>();
|
---|
35 |
|
---|
36 | private static final String[] DEFAULT_LAYER_SITES = {
|
---|
37 | Main.getJOSMWebsite()+"/maps"
|
---|
38 | };
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Returns the list of imagery layers sites.
|
---|
42 | * @return the list of imagery layers sites
|
---|
43 | * @since 7434
|
---|
44 | */
|
---|
45 | public static Collection<String> getImageryLayersSites() {
|
---|
46 | return Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
|
---|
47 | }
|
---|
48 |
|
---|
49 | private ImageryLayerInfo() {
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ImageryLayerInfo(ImageryLayerInfo info) {
|
---|
53 | layers.addAll(info.layers);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void clear() {
|
---|
57 | layers.clear();
|
---|
58 | layerIds.clear();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void load() {
|
---|
62 | clear();
|
---|
63 | List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class);
|
---|
64 | if (entries != null) {
|
---|
65 | for (ImageryPreferenceEntry prefEntry : entries) {
|
---|
66 | try {
|
---|
67 | ImageryInfo i = new ImageryInfo(prefEntry);
|
---|
68 | add(i);
|
---|
69 | } catch (IllegalArgumentException e) {
|
---|
70 | Main.warn("Unable to load imagery preference entry:"+e);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | Collections.sort(layers);
|
---|
74 | }
|
---|
75 | loadDefaults(false);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Loads the available imagery entries.
|
---|
80 | *
|
---|
81 | * The data is downloaded from the JOSM website (or loaded from cache).
|
---|
82 | * Entries marked as "default" are added to the user selection, if not
|
---|
83 | * already present.
|
---|
84 | *
|
---|
85 | * @param clearCache if true, clear the cache and start a fresh download.
|
---|
86 | */
|
---|
87 | public void loadDefaults(boolean clearCache) {
|
---|
88 | defaultLayers.clear();
|
---|
89 | defaultLayerIds.clear();
|
---|
90 | for (String source : getImageryLayersSites()) {
|
---|
91 | boolean online = true;
|
---|
92 | try {
|
---|
93 | OnlineResource.JOSM_WEBSITE.checkOfflineAccess(source, Main.getJOSMWebsite());
|
---|
94 | } catch (OfflineAccessException e) {
|
---|
95 | Main.warn(e.getMessage());
|
---|
96 | online = false;
|
---|
97 | }
|
---|
98 | if (clearCache && online) {
|
---|
99 | CachedFile.cleanup(source);
|
---|
100 | }
|
---|
101 | try {
|
---|
102 | ImageryReader reader = new ImageryReader(source);
|
---|
103 | Collection<ImageryInfo> result = reader.parse();
|
---|
104 | defaultLayers.addAll(result);
|
---|
105 | } catch (IOException ex) {
|
---|
106 | Main.error(ex, false);
|
---|
107 | } catch (SAXException ex) {
|
---|
108 | Main.error(ex);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | while (defaultLayers.remove(null));
|
---|
112 | Collections.sort(defaultLayers);
|
---|
113 | buildIdMap(defaultLayers, defaultLayerIds);
|
---|
114 | updateEntriesFromDefaults();
|
---|
115 | buildIdMap(layers, layerIds);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Build the mapping of unique ids to {@link ImageryInfo}s.
|
---|
120 | * @param lst input list
|
---|
121 | * @param idMap output map
|
---|
122 | */
|
---|
123 | private static void buildIdMap(List<ImageryInfo> lst, Map<String, ImageryInfo> idMap) {
|
---|
124 | idMap.clear();
|
---|
125 | Set<String> notUnique = new HashSet<>();
|
---|
126 | for (ImageryInfo i : lst) {
|
---|
127 | if (i.getId() != null) {
|
---|
128 | if (idMap.containsKey(i.getId())) {
|
---|
129 | notUnique.add(i.getId());
|
---|
130 | Main.error("Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!",
|
---|
131 | i.getId(), i.getName(), idMap.get(i.getId()).getName());
|
---|
132 | continue;
|
---|
133 | }
|
---|
134 | idMap.put(i.getId(), i);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | for (String i : notUnique) {
|
---|
138 | idMap.remove(i);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Update user entries according to the list of default entries.
|
---|
144 | */
|
---|
145 | public void updateEntriesFromDefaults() {
|
---|
146 | // add new default entries to the user selection
|
---|
147 | boolean changed = false;
|
---|
148 | Collection<String> knownDefaults = Main.pref.getCollection("imagery.layers.default");
|
---|
149 | Collection<String> newKnownDefaults = new TreeSet<>(knownDefaults);
|
---|
150 | for (ImageryInfo def : defaultLayers) {
|
---|
151 | // temporary migration code, so all user preferences will get updated with new settings from JOSM site (can be removed ~Dez. 2015)
|
---|
152 | if (def.getNoTileHeaders() != null) {
|
---|
153 | for (ImageryInfo i: layers) {
|
---|
154 | if (isSimilar(def, i)) {
|
---|
155 | i.setNoTileHeaders(def.getNoTileHeaders());
|
---|
156 | changed = true;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (def.isDefaultEntry()) {
|
---|
162 | boolean isKnownDefault = false;
|
---|
163 | for (String url : knownDefaults) {
|
---|
164 | if (isSimilar(url, def.getUrl())) {
|
---|
165 | isKnownDefault = true;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | boolean isInUserList = false;
|
---|
170 | if (!isKnownDefault) {
|
---|
171 | newKnownDefaults.add(def.getUrl());
|
---|
172 | for (ImageryInfo i : layers) {
|
---|
173 | if (isSimilar(def, i)) {
|
---|
174 | isInUserList = true;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | if (!isKnownDefault && !isInUserList) {
|
---|
180 | add(new ImageryInfo(def));
|
---|
181 | changed = true;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | Main.pref.putCollection("imagery.layers.default", newKnownDefaults);
|
---|
186 |
|
---|
187 | // Add ids to user entries without id.
|
---|
188 | // Only do this the first time for each id, so the user can have
|
---|
189 | // custom entries that don't get updated automatically
|
---|
190 | Collection<String> addedIds = Main.pref.getCollection("imagery.layers.addedIds");
|
---|
191 | Collection<String> newAddedIds = new TreeSet<>(addedIds);
|
---|
192 | for (ImageryInfo info : layers) {
|
---|
193 | for (ImageryInfo def : defaultLayers) {
|
---|
194 | if (isSimilar(def, info)) {
|
---|
195 | if (def.getId() != null && !addedIds.contains(def.getId())) {
|
---|
196 | if (!defaultLayerIds.containsKey(def.getId())) {
|
---|
197 | // ignore ids used more than once (have been purged from the map)
|
---|
198 | continue;
|
---|
199 | }
|
---|
200 | newAddedIds.add(def.getId());
|
---|
201 | if (info.getId() == null) {
|
---|
202 | info.setId(def.getId());
|
---|
203 | changed = true;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 | Main.pref.putCollection("imagery.layers.addedIds", newAddedIds);
|
---|
210 |
|
---|
211 | // automatically update user entries with same id as a default entry
|
---|
212 | for (int i=0; i<layers.size(); i++) {
|
---|
213 | ImageryInfo info = layers.get(i);
|
---|
214 | if (info.getId() == null) {
|
---|
215 | continue;
|
---|
216 | }
|
---|
217 | ImageryInfo matchingDefault = defaultLayerIds.get(info.getId());
|
---|
218 | if (matchingDefault != null && !matchingDefault.equalsPref(info)) {
|
---|
219 | layers.set(i, matchingDefault);
|
---|
220 | changed = true;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (changed) {
|
---|
225 | save();
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private boolean isSimilar(ImageryInfo iiA, ImageryInfo iiB) {
|
---|
230 | if (iiA.getId() != null && iiB.getId() != null) return iiA.getId().equals(iiB.getId());
|
---|
231 | return isSimilar(iiA.getUrl(), iiB.getUrl());
|
---|
232 | }
|
---|
233 |
|
---|
234 | // some additional checks to respect extended URLs in preferences (legacy workaround)
|
---|
235 | private boolean isSimilar(String a, String b) {
|
---|
236 | return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
|
---|
237 | }
|
---|
238 |
|
---|
239 | public void add(ImageryInfo info) {
|
---|
240 | layers.add(info);
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void remove(ImageryInfo info) {
|
---|
244 | layers.remove(info);
|
---|
245 | }
|
---|
246 |
|
---|
247 | public void save() {
|
---|
248 | List<ImageryPreferenceEntry> entries = new ArrayList<>();
|
---|
249 | for (ImageryInfo info : layers) {
|
---|
250 | entries.add(new ImageryPreferenceEntry(info));
|
---|
251 | }
|
---|
252 | Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
|
---|
253 | }
|
---|
254 |
|
---|
255 | public List<ImageryInfo> getLayers() {
|
---|
256 | return Collections.unmodifiableList(layers);
|
---|
257 | }
|
---|
258 |
|
---|
259 | public List<ImageryInfo> getDefaultLayers() {
|
---|
260 | return Collections.unmodifiableList(defaultLayers);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public static void addLayer(ImageryInfo info) {
|
---|
264 | instance.add(info);
|
---|
265 | instance.save();
|
---|
266 | }
|
---|
267 |
|
---|
268 | public static void addLayers(Collection<ImageryInfo> infos) {
|
---|
269 | for (ImageryInfo i : infos) {
|
---|
270 | instance.add(i);
|
---|
271 | }
|
---|
272 | instance.save();
|
---|
273 | Collections.sort(instance.layers);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Get unique id for ImageryInfo.
|
---|
278 | *
|
---|
279 | * This takes care, that no id is used twice (due to a user error)
|
---|
280 | * @param info the ImageryInfo to look up
|
---|
281 | * @return null, if there is no id or the id is used twice,
|
---|
282 | * the corresponding id otherwise
|
---|
283 | */
|
---|
284 | public String getUniqueId(ImageryInfo info) {
|
---|
285 | if (info.getId() != null && layerIds.get(info.getId()) == info) {
|
---|
286 | return info.getId();
|
---|
287 | }
|
---|
288 | return null;
|
---|
289 | }
|
---|
290 | }
|
---|