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