source: josm/trunk/src/org/openstreetmap/josm/tools/Territories.java@ 14484

Last change on this file since 14484 was 14484, checked in by Don-vip, 5 years ago

fix #17058 - Cannot add assertMatch/assertNoMatch declarations with inside() selector

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.Map;
13import java.util.Set;
14
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.io.CachedFile;
21import org.openstreetmap.josm.io.IllegalDataException;
22import org.openstreetmap.josm.io.OsmReader;
23
24/**
25 * Look up territories ISO3166 codes at a certain place.
26 */
27public final class Territories {
28
29 /** Internal OSM filename */
30 public static final String FILENAME = "boundaries.osm";
31
32 private static final String ISO3166_1 = "ISO3166-1:alpha2";
33 private static final String ISO3166_2 = "ISO3166-2";
34
35 private static DataSet dataSet;
36
37 private static volatile Map<String, GeoPropertyIndex<Boolean>> iso3166Cache;
38
39 private Territories() {
40 // Hide implicit public constructor for utility classes
41 }
42
43 /**
44 * Get all known ISO3166-1 and ISO3166-2 codes.
45 *
46 * @return the ISO3166-1 and ISO3166-2 codes for the given location
47 */
48 public static synchronized Set<String> getKnownIso3166Codes() {
49 return iso3166Cache.keySet();
50 }
51
52 /**
53 * Returns the {@link GeoPropertyIndex} for the given ISO3166-1 or ISO3166-2 code.
54 * @param code the ISO3166-1 or ISO3166-2 code
55 * @return the {@link GeoPropertyIndex} for the given {@code code}
56 * @since 14484
57 */
58 public static GeoPropertyIndex<Boolean> getGeoPropertyIndex(String code) {
59 return iso3166Cache.get(code);
60 }
61
62 /**
63 * Determine, if a point is inside a territory with the given ISO3166-1
64 * or ISO3166-2 code.
65 *
66 * @param code the ISO3166-1 or ISO3166-2 code
67 * @param ll the coordinates of the point
68 * @return true, if the point is inside a territory with the given code
69 */
70 public static synchronized boolean isIso3166Code(String code, LatLon ll) {
71 GeoPropertyIndex<Boolean> gpi = iso3166Cache.get(code);
72 if (gpi == null) {
73 Logging.warn(tr("Unknown territory id: {0}", code));
74 return false;
75 }
76 return Boolean.TRUE.equals(gpi.get(ll)); // avoid NPE, see #16491
77 }
78
79 /**
80 * Returns the territories dataset.
81 * @return the territories dataset
82 */
83 public static synchronized DataSet getDataSet() {
84 return new DataSet(dataSet);
85 }
86
87 /**
88 * Initializes territories.
89 * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
90 */
91 public static synchronized void initialize() {
92 iso3166Cache = new HashMap<>();
93 try (CachedFile cf = new CachedFile("resource://data/" + FILENAME);
94 InputStream is = cf.getInputStream()) {
95 dataSet = OsmReader.parseDataSet(is, null);
96 Collection<OsmPrimitive> candidates = new ArrayList<>(dataSet.getWays());
97 candidates.addAll(dataSet.getRelations());
98 for (OsmPrimitive osm : candidates) {
99 String iso1 = osm.get(ISO3166_1);
100 String iso2 = osm.get(ISO3166_2);
101 if (iso1 != null || iso2 != null) {
102 GeoProperty<Boolean> gp;
103 if (osm instanceof Way) {
104 gp = new DefaultGeoProperty(Collections.singleton((Way) osm));
105 } else {
106 gp = new DefaultGeoProperty((Relation) osm);
107 }
108 GeoPropertyIndex<Boolean> gpi = new GeoPropertyIndex<>(gp, 24);
109 if (iso1 != null) {
110 iso3166Cache.put(iso1, gpi);
111 }
112 if (iso2 != null) {
113 iso3166Cache.put(iso2, gpi);
114 }
115 }
116 }
117 } catch (IOException | IllegalDataException ex) {
118 throw new JosmRuntimeException(ex);
119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.