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

Last change on this file since 12269 was 11374, checked in by Don-vip, 7 years ago

sonar - squid:S00112 - Generic exceptions should never be thrown: define JosmRuntimeException

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