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

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

see #9400 - see #10387 - see #12914 - initial implementation of boundaries file, replacing left-right-hand-traffic.osm, discourage contributors to use operator=RFF and operator=ERDF in France (territories rules must be manually eabled on existing installations)

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.Map;
10import java.util.Set;
11import java.util.concurrent.ConcurrentHashMap;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.SelectByInternalPointAction;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.BBox;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.io.CachedFile;
20import org.openstreetmap.josm.io.IllegalDataException;
21import org.openstreetmap.josm.io.OsmReader;
22import org.openstreetmap.josm.tools.GeoPropertyIndex.GeoProperty;
23
24/**
25 * Look up territories ISO3166 codes at a certain place.
26 */
27public 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}
Note: See TracBrowser for help on using the repository browser.