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