Ticket #25067: josm-geofabrik-final.patch
| File josm-geofabrik-final.patch, 6.8 KB (added by , 6 days ago) |
|---|
-
src/org/openstreetmap/josm/tools/Territories.java
201 201 ofNullable(feature.asJsonObject().getJsonObject("properties")).ifPresent(props -> 202 202 ofNullable(props.getJsonObject("urls")).flatMap(urls -> 203 203 ofNullable(urls.getString(TAGINFO))).ifPresent(taginfo -> { 204 Json Array iso1 = props.getJsonArray(ISO3166_1_LC);205 Json Array iso2 = props.getJsonArray(ISO3166_2_LC);204 JsonValue iso1 = props.get(ISO3166_1_LC); 205 JsonValue iso2 = props.get(ISO3166_2_LC); 206 206 if (iso1 != null) { 207 207 readExternalTaginfo(taginfoGeofabrikCache, taginfo, iso1, source); 208 208 } else if (iso2 != null) { … … 218 218 } 219 219 } 220 220 221 private static void readExternalTaginfo(Map<String, TaginfoRegionalInstance> cache, String taginfo, JsonArray jsonCodes, String source) { 222 Set<String> isoCodes = jsonCodes.getValuesAs(JsonString.class).stream().map(JsonString::getString).collect(Collectors.toSet()); 221 private static void readExternalTaginfo(Map<String, TaginfoRegionalInstance> cache, String taginfo, JsonValue jsonCodes, String source) { 222 Stream<String> codes; 223 if (jsonCodes instanceof JsonArray) { 224 codes = jsonCodes.asJsonArray().stream().filter(JsonString.class::isInstance) 225 .map(JsonString.class::cast).map(JsonString::getString); 226 } else if (jsonCodes instanceof JsonString) { 227 codes = Stream.of(((JsonString) jsonCodes).getString()); 228 } else { 229 return; 230 } 231 Set<String> isoCodes = codes.flatMap(s -> Arrays.stream(s.split(","))) 232 .map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toSet()); 223 233 isoCodes.forEach(s -> cache.put(s, new TaginfoRegionalInstance(taginfo, isoCodes, source))); 224 234 } 225 235 -
test/data/taginfo/geofabrik-index-v1-nogeom-strings.json
1 { 2 "type": "FeatureCollection", 3 "features": [ 4 { 5 "type": "Feature", 6 "properties": { 7 "id": "invalid-iso3166-1", 8 "iso3166-1:alpha2": 42, 9 "iso3166-2": ["CA-AB"], 10 "urls": { 11 "taginfo": "https://taginfo.geofabrik.de/invalid/" 12 } 13 } 14 }, 15 { 16 "type": "Feature", 17 "properties": { 18 "id": "empty-string", 19 "iso3166-1:alpha2": "", 20 "urls": { 21 "taginfo": "https://taginfo.geofabrik.de/empty-string/" 22 } 23 } 24 }, 25 { 26 "type": "Feature", 27 "properties": { 28 "id": "empty-array", 29 "iso3166-1:alpha2": [], 30 "urls": { 31 "taginfo": "https://taginfo.geofabrik.de/empty-array/" 32 } 33 } 34 }, 35 { 36 "type": "Feature", 37 "properties": { 38 "id": "albania", 39 "parent": "europe", 40 "iso3166-1:alpha2": "AL", 41 "name": "Albania", 42 "urls": { 43 "taginfo": "https://taginfo.geofabrik.de/europe/albania/" 44 } 45 } 46 }, 47 { 48 "type": "Feature", 49 "properties": { 50 "id": "guernsey-jersey", 51 "parent": "europe", 52 "iso3166-1:alpha2": " GG, JE ", 53 "name": "Guernsey and Jersey", 54 "urls": { 55 "taginfo": "https://taginfo.geofabrik.de/europe/guernsey-jersey/" 56 } 57 } 58 } 59 ] 60 } -
test/unit/org/openstreetmap/josm/tools/TerritoriesTest.java
3 3 4 4 import static java.util.Collections.singleton; 5 5 import static org.junit.jupiter.api.Assertions.assertEquals; 6 import static org.junit.jupiter.api.Assertions.assertFalse; 6 7 import static org.junit.jupiter.api.Assertions.assertNull; 7 8 import static org.junit.jupiter.api.Assertions.assertTrue; 8 9 … … 50 51 } 51 52 52 53 /** 53 * Test of {@link Territories#initializeExternalData} - nominal case54 * Test of {@link Territories#initializeExternalData} with ISO codes represented as arrays. 54 55 */ 55 56 @Test 56 void testTaginfoGeofabrik_ nominal() {57 void testTaginfoGeofabrik_arrays() { 57 58 Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom.json"); 58 59 Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache; 59 60 assertEquals(5, cache.size()); … … 68 69 checkTaginfoInstance(regionalTaginfo.iterator().next(), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/"); 69 70 } 70 71 72 /** 73 * Test of {@link Territories#initializeExternalData} with ISO codes represented as strings. 74 */ 75 @Test 76 void testTaginfoGeofabrik_strings() { 77 Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom-strings.json"); 78 Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache; 79 assertEquals(3, cache.size()); 80 assertFalse(cache.containsKey("CA-AB"), "ISO3166-1 must take precedence over ISO3166-2"); 81 assertFalse(cache.containsKey(""), "Empty ISO codes must not be cached"); 82 checkTaginfoInstance(cache.get("AL"), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/"); 83 Set<String> guernseyAndJersey = new HashSet<>(Arrays.asList("GG", "JE")); 84 checkTaginfoInstance(cache.get("GG"), guernseyAndJersey, "https://taginfo.geofabrik.de/europe/guernsey-jersey/"); 85 checkTaginfoInstance(cache.get("JE"), guernseyAndJersey, "https://taginfo.geofabrik.de/europe/guernsey-jersey/"); 86 } 87 71 88 private static void checkTaginfoInstance(TaginfoRegionalInstance instance, Set<String> expectedIsoCodes, String expectedUrl) { 72 89 assertEquals(expectedIsoCodes, instance.getIsoCodes()); 73 90 assertEquals("foo", instance.getSuffix());
