Changeset 15876 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2020-02-17T16:33:43+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r15751 r15876 370 370 destroyTaginfoNationalActions(); 371 371 if (!newSel.isEmpty()) { 372 for (Entry<String, String> e : Territories.getNationalTaginfoUrls( 373 newSel.iterator().next().getBBox().getCenter()).entrySet()) { 374 taginfoNationalActions.add(new TaginfoAction(tagTable, editHelper::getDataKey, editHelper::getDataValues, 375 membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), e.getValue(), e.getKey())); 376 } 372 Territories.getRegionalTaginfoUrls( 373 newSel.iterator().next().getBBox().getCenter()).values().stream().flatMap(List::stream).forEach( 374 taginfo -> taginfoNationalActions.add(new TaginfoAction(tagTable, editHelper::getDataKey, editHelper::getDataValues, 375 membershipTable, x -> (IRelation<?>) membershipData.getValueAt(x, 0), 376 taginfo.getUrl(), String.join("/", taginfo.getIsoCodes()) 377 + (taginfo.getSuffix() == null ? "" : " (" + taginfo.getSuffix() + ")"))) 378 ); 377 379 taginfoNationalActions.stream().map(membershipMenu::add).forEach(membershipMenuTagInfoNatItems::add); 378 380 taginfoNationalActions.stream().map(tagMenu::add).forEach(tagMenuTagInfoNatItems::add); -
trunk/src/org/openstreetmap/josm/tools/Territories.java
r15569 r15876 7 7 import java.io.InputStream; 8 8 import java.util.ArrayList; 9 import java.util.Arrays; 9 10 import java.util.Collection; 10 11 import java.util.Collections; 11 12 import java.util.HashMap; 13 import java.util.List; 14 import java.util.Locale; 12 15 import java.util.Map; 13 16 import java.util.Map.Entry; 17 import java.util.Optional; 14 18 import java.util.Set; 15 19 import java.util.TreeMap; 16 20 import java.util.stream.Collectors; 21 22 import javax.json.Json; 23 import javax.json.JsonArray; 24 import javax.json.JsonObject; 25 import javax.json.JsonString; 26 import javax.json.JsonValue; 27 import javax.json.stream.JsonParser; 28 import javax.json.stream.JsonParser.Event; 17 29 18 30 import org.openstreetmap.josm.data.coor.LatLon; … … 24 36 import org.openstreetmap.josm.io.IllegalDataException; 25 37 import org.openstreetmap.josm.io.OsmReader; 38 import org.openstreetmap.josm.spi.preferences.Config; 26 39 27 40 /** … … 35 48 private static final String ISO3166_1 = "ISO3166-1:alpha2"; 36 49 private static final String ISO3166_2 = "ISO3166-2"; 50 private static final String ISO3166_1_LC = ISO3166_1.toLowerCase(Locale.ENGLISH); 51 private static final String ISO3166_2_LC = ISO3166_2.toLowerCase(Locale.ENGLISH); 37 52 private static final String TAGINFO = "taginfo"; 38 53 … … 40 55 41 56 private static volatile Map<String, GeoPropertyIndex<Boolean>> iso3166Cache; 42 private static volatile Map<String, String> taginfoCache; 57 private static volatile Map<String, TaginfoRegionalInstance> taginfoCache; 58 private static volatile Map<String, TaginfoRegionalInstance> taginfoGeofabrikCache; 43 59 44 60 private Territories() { … … 104 120 */ 105 121 public static synchronized void initialize() { 122 initializeInternalData(); 123 initializeExternalData(); 124 } 125 126 private static void initializeInternalData() { 106 127 iso3166Cache = new HashMap<>(); 107 128 taginfoCache = new TreeMap<>(); … … 126 147 String taginfo = osm.get(TAGINFO); 127 148 if (taginfo != null) { 128 taginfoCache.put(iso1, taginfo);149 taginfoCache.put(iso1, new TaginfoRegionalInstance(taginfo, Collections.singleton(iso1))); 129 150 } 130 151 } … … 139 160 } 140 161 141 /** 142 * Returns a map of national taginfo instances for the given location. 162 private static void initializeExternalData() { 163 taginfoGeofabrikCache = new TreeMap<>(); 164 try (CachedFile cf = new CachedFile(Config.getUrls().getJOSMWebsite() + "/remote/geofabrik-index-v1-nogeom.json"); 165 InputStream is = cf.getInputStream(); 166 JsonParser json = Json.createParser(is)) { 167 while (json.hasNext()) { 168 Event event = json.next(); 169 if (event == Event.START_OBJECT) { 170 for (JsonValue feature : json.getObject().getJsonArray("features")) { 171 JsonObject props = feature.asJsonObject().getJsonObject("properties"); 172 if (props != null) { 173 JsonObject urls = props.getJsonObject("urls"); 174 if (urls != null) { 175 String taginfo = urls.getString(TAGINFO); 176 if (taginfo != null) { 177 JsonArray iso1 = props.getJsonArray(ISO3166_1_LC); 178 JsonArray iso2 = props.getJsonArray(ISO3166_2_LC); 179 if (iso1 != null) { 180 readExternalTaginfo(taginfo, iso1); 181 } else if (iso2 != null) { 182 readExternalTaginfo(taginfo, iso2); 183 } 184 } 185 } 186 } 187 } 188 } 189 } 190 } catch (IOException e) { 191 throw new JosmRuntimeException(e); 192 } 193 } 194 195 private static void readExternalTaginfo(String taginfo, JsonArray jsonCodes) { 196 Set<String> isoCodes = jsonCodes.getValuesAs(JsonString.class).stream().map(JsonString::getString).collect(Collectors.toSet()); 197 isoCodes.forEach(s -> taginfoGeofabrikCache.put(s, new TaginfoRegionalInstance(taginfo, isoCodes, "Geofabrik"))); 198 } 199 200 /** 201 * Returns a map of regional taginfo instances for the given location. 143 202 * @param ll lat/lon where to look. 144 * @return a map of national taginfo instances for the given location (code / url)145 * @since 15 565146 */ 147 public static Map<String, String> getNationalTaginfoUrls(LatLon ll) {148 Map<String, String> result = new TreeMap<>();203 * @return a map of regional taginfo instances for the given location (code / url) 204 * @since 15876 205 */ 206 public static Map<String, List<TaginfoRegionalInstance>> getRegionalTaginfoUrls(LatLon ll) { 207 Map<String, List<TaginfoRegionalInstance>> result = new TreeMap<>(); 149 208 if (iso3166Cache != null) { 150 209 for (String code : iso3166Cache.entrySet().parallelStream().distinct() … … 152 211 .map(Entry<String, GeoPropertyIndex<Boolean>>::getKey) 153 212 .collect(Collectors.toSet())) { 154 String taginfo = taginfoCache.get(code);155 if (taginfo != null) {156 result.put(code, taginfo);213 for (Map<String, TaginfoRegionalInstance> cache : Arrays.asList(taginfoCache, taginfoGeofabrikCache)) { 214 Optional.ofNullable(cache.get(code)).ifPresent( 215 taginfo -> result.computeIfAbsent(code, c -> new ArrayList<>()).add(taginfo)); 157 216 } 158 217 }
Note:
See TracChangeset
for help on using the changeset viewer.