Changeset 15952 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Territories.java
r15939 r15952 2 2 package org.openstreetmap.josm.tools; 3 3 4 import static java.util.Optional.ofNullable; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 … … 22 23 import javax.json.Json; 23 24 import javax.json.JsonArray; 24 import javax.json.JsonObject;25 25 import javax.json.JsonString; 26 26 import javax.json.JsonValue; … … 163 163 private static void initializeExternalData() { 164 164 taginfoGeofabrikCache = new TreeMap<>(); 165 try (CachedFile cf = new CachedFile(Config.getUrls().getJOSMWebsite() + "/remote/geofabrik-index-v1-nogeom.json"); 166 InputStream is = cf.getInputStream(); 167 JsonParser json = Json.createParser(is)) { 165 initializeExternalData(taginfoGeofabrikCache, "Geofabrik", 166 Config.getUrls().getJOSMWebsite() + "/remote/geofabrik-index-v1-nogeom.json"); 167 } 168 169 static void initializeExternalData(Map<String, TaginfoRegionalInstance> cache, String source, String path) { 170 try (CachedFile cf = new CachedFile(path); InputStream is = cf.getInputStream(); JsonParser json = Json.createParser(is)) { 168 171 while (json.hasNext()) { 169 172 Event event = json.next(); 170 173 if (event == Event.START_OBJECT) { 171 174 for (JsonValue feature : json.getObject().getJsonArray("features")) { 172 JsonObject props = feature.asJsonObject().getJsonObject("properties"); 173 if (props != null) { 174 JsonObject urls = props.getJsonObject("urls"); 175 if (urls != null) { 176 String taginfo = urls.getString(TAGINFO); 177 if (taginfo != null) { 178 JsonArray iso1 = props.getJsonArray(ISO3166_1_LC); 179 JsonArray iso2 = props.getJsonArray(ISO3166_2_LC); 180 if (iso1 != null) { 181 readExternalTaginfo(taginfo, iso1); 182 } else if (iso2 != null) { 183 readExternalTaginfo(taginfo, iso2); 184 } 185 } 175 ofNullable(feature.asJsonObject().getJsonObject("properties")).ifPresent(props -> 176 ofNullable(props.getJsonObject("urls")).ifPresent(urls -> 177 ofNullable(urls.getString(TAGINFO)).ifPresent(taginfo -> { 178 JsonArray iso1 = props.getJsonArray(ISO3166_1_LC); 179 JsonArray iso2 = props.getJsonArray(ISO3166_2_LC); 180 if (iso1 != null) { 181 readExternalTaginfo(cache, taginfo, iso1, source); 182 } else if (iso2 != null) { 183 readExternalTaginfo(cache, taginfo, iso2, source); 186 184 } 187 } 185 }))); 188 186 } 189 187 } 190 188 } 191 189 } catch (IOException | JsonParsingException e) { 192 Logging. trace(e);193 Logging.warn(tr("Failed to parse taginfo data geofabrik-index-v1-nogeom.json"));194 } 195 } 196 197 private static void readExternalTaginfo(String taginfo, JsonArray jsonCodes) { 190 Logging.debug(e); 191 Logging.warn(tr("Failed to parse external taginfo data at {0}: {1}", path, e.getMessage())); 192 } 193 } 194 195 private static void readExternalTaginfo(Map<String, TaginfoRegionalInstance> cache, String taginfo, JsonArray jsonCodes, String source) { 198 196 Set<String> isoCodes = jsonCodes.getValuesAs(JsonString.class).stream().map(JsonString::getString).collect(Collectors.toSet()); 199 isoCodes.forEach(s -> taginfoGeofabrikCache.put(s, new TaginfoRegionalInstance(taginfo, isoCodes,"Geofabrik")));197 isoCodes.forEach(s -> cache.put(s, new TaginfoRegionalInstance(taginfo, isoCodes, source))); 200 198 } 201 199 -
trunk/test/unit/org/openstreetmap/josm/tools/TerritoriesTest.java
r14138 r15952 2 2 package org.openstreetmap.josm.tools; 3 3 4 import static java.util.Collections.singleton; 4 5 import static org.junit.Assert.assertTrue; 6 import static org.junit.jupiter.api.Assertions.assertEquals; 7 8 import java.util.Arrays; 9 import java.util.HashSet; 10 import java.util.Map; 11 import java.util.Set; 12 import java.util.TreeMap; 5 13 6 14 import org.junit.Rule; 7 15 import org.junit.Test; 16 import org.openstreetmap.josm.TestUtils; 8 17 import org.openstreetmap.josm.data.coor.LatLon; 9 18 import org.openstreetmap.josm.testutils.JOSMTestRules; … … 45 54 } 46 55 } 56 57 /** 58 * Test of {@link Territories#initializeExternalData} - nominal case 59 */ 60 @Test 61 public void testTaginfoGeofabrik_nominal() { 62 Map<String, TaginfoRegionalInstance> cache = new TreeMap<>(); 63 Territories.initializeExternalData(cache, "foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom.json"); 64 assertEquals(5, cache.size()); 65 checkTaginfoInstance(cache.get("AF"), singleton("AF"), "https://taginfo.geofabrik.de/asia/afghanistan/"); 66 checkTaginfoInstance(cache.get("AL"), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/"); 67 checkTaginfoInstance(cache.get("CA-AB"), singleton("CA-AB"), "https://taginfo.geofabrik.de/north-america/canada/alberta/"); 68 Set<String> israelAndPalestine = new HashSet<>(Arrays.asList("PS", "IL")); 69 checkTaginfoInstance(cache.get("PS"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/"); 70 checkTaginfoInstance(cache.get("IL"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/"); 71 } 72 73 private static void checkTaginfoInstance(TaginfoRegionalInstance instance, Set<String> expectedIsoCodes, String expectedUrl) { 74 assertEquals(expectedIsoCodes, instance.getIsoCodes()); 75 assertEquals("foo", instance.getSuffix()); 76 assertEquals(expectedUrl, instance.getUrl()); 77 } 78 79 /** 80 * Test of {@link Territories#initializeExternalData} - broken contents 81 */ 82 @Test 83 public void testTaginfoGeofabrik_broken() { 84 Map<String, TaginfoRegionalInstance> cache = new TreeMap<>(); 85 Logging.clearLastErrorAndWarnings(); 86 Territories.initializeExternalData(cache, "foo", TestUtils.getTestDataRoot() + "taginfo/geofabrik-index-v1-nogeom-broken.json"); 87 assertTrue(cache.isEmpty()); 88 assertEquals("W: Failed to parse external taginfo data at test/data/taginfo/geofabrik-index-v1-nogeom-broken.json: " + 89 "Invalid token=EOF at (line no=3, column no=49, offset=97). Expected tokens are: " + 90 "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]", 91 Logging.getLastErrorAndWarnings().get(0)); 92 } 47 93 }
Note:
See TracChangeset
for help on using the changeset viewer.