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