source: josm/trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java@ 11330

Last change on this file since 11330 was 11264, checked in by bastiK, 7 years ago

refactor GeoProperty implementation of RightAndLefthandTraffic to generic separate class DefaultGeoProperty (see #10387)

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStreamWriter;
10import java.io.PrintWriter;
11import java.io.Writer;
12import java.nio.charset.StandardCharsets;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.List;
17import java.util.Set;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.JoinAreasAction;
21import org.openstreetmap.josm.actions.JoinAreasAction.JoinAreasResult;
22import org.openstreetmap.josm.actions.JoinAreasAction.Multipolygon;
23import org.openstreetmap.josm.actions.PurgeAction;
24import org.openstreetmap.josm.data.coor.LatLon;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.data.osm.Relation;
28import org.openstreetmap.josm.data.osm.RelationMember;
29import org.openstreetmap.josm.data.osm.Way;
30import org.openstreetmap.josm.io.IllegalDataException;
31import org.openstreetmap.josm.io.OsmReader;
32import org.openstreetmap.josm.io.OsmWriter;
33import org.openstreetmap.josm.io.OsmWriterFactory;
34
35/**
36 * Look up, if there is right- or left-hand traffic at a certain place.
37 */
38public final class RightAndLefthandTraffic {
39
40 private static final String DRIVING_SIDE = "driving_side";
41 private static final String LEFT = "left";
42 private static final String RIGHT = "right";
43
44 private static volatile GeoPropertyIndex<Boolean> rlCache;
45
46 private RightAndLefthandTraffic() {
47 // Hide implicit public constructor for utility classes
48 }
49
50 /**
51 * Check if there is right-hand traffic at a certain location.
52 *
53 * @param ll the coordinates of the point
54 * @return true if there is right-hand traffic, false if there is left-hand traffic
55 */
56 public static synchronized boolean isRightHandTraffic(LatLon ll) {
57 return !rlCache.get(ll);
58 }
59
60 /**
61 * Initializes Right and lefthand traffic data.
62 * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
63 */
64 public static synchronized void initialize() {
65 Collection<Way> optimizedWays = loadOptimizedBoundaries();
66 if (optimizedWays.isEmpty()) {
67 optimizedWays = computeOptimizedBoundaries();
68 saveOptimizedBoundaries(optimizedWays);
69 }
70 rlCache = new GeoPropertyIndex<>(new DefaultGeoProperty(optimizedWays), 24);
71 }
72
73 private static Collection<Way> computeOptimizedBoundaries() {
74 Collection<Way> ways = new ArrayList<>();
75 Collection<OsmPrimitive> toPurge = new ArrayList<>();
76 // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
77 DataSet data = Territories.getDataSet();
78 Collection<Relation> allRelations = data.getRelations();
79 Collection<Way> allWays = data.getWays();
80 for (Way w : allWays) {
81 if (LEFT.equals(w.get(DRIVING_SIDE))) {
82 addWayIfNotInner(ways, w);
83 }
84 }
85 for (Relation r : allRelations) {
86 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
87 for (RelationMember rm : r.getMembers()) {
88 if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) {
89 addWayIfNotInner(ways, (Way) rm.getMember());
90 }
91 }
92 }
93 }
94 toPurge.addAll(allRelations);
95 toPurge.addAll(allWays);
96 toPurge.removeAll(ways);
97 // Remove ways from parent relations for following optimizations
98 for (Relation r : OsmPrimitive.getParentRelations(ways)) {
99 r.setMembers(null);
100 }
101 // Remove all tags to avoid any conflict
102 for (Way w : ways) {
103 w.removeAll();
104 }
105 // Purge all other ways and relations so dataset only contains lefthand traffic data
106 new PurgeAction().getPurgeCommand(toPurge).executeCommand();
107 // Combine adjacent countries into a single polygon
108 Collection<Way> optimizedWays = new ArrayList<>();
109 List<Multipolygon> areas = JoinAreasAction.collectMultipolygons(ways);
110 if (areas != null) {
111 try {
112 JoinAreasResult result = new JoinAreasAction().joinAreas(areas);
113 if (result.hasChanges) {
114 for (Multipolygon mp : result.polygons) {
115 optimizedWays.add(mp.outerWay);
116 }
117 }
118 } catch (UserCancelException ex) {
119 Main.warn(ex);
120 }
121 }
122 if (optimizedWays.isEmpty()) {
123 // Problem: don't optimize
124 Main.warn("Unable to join left-driving countries polygons");
125 optimizedWays.addAll(ways);
126 }
127 return optimizedWays;
128 }
129
130 /**
131 * Adds w to ways, except if it is an inner way of another lefthand driving multipolygon,
132 * as Lesotho in South Africa and Cyprus village in British Cyprus base.
133 * @param ways ways
134 * @param w way
135 */
136 private static void addWayIfNotInner(Collection<Way> ways, Way w) {
137 Set<Way> s = Collections.singleton(w);
138 for (Relation r : OsmPrimitive.getParentRelations(s)) {
139 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) &&
140 "inner".equals(r.getMembersFor(s).iterator().next().getRole())) {
141 if (Main.isDebugEnabled()) {
142 Main.debug("Skipping " + w.get("name:en") + " because inner part of " + r.get("name:en"));
143 }
144 return;
145 }
146 }
147 ways.add(w);
148 }
149
150 private static void saveOptimizedBoundaries(Collection<Way> optimizedWays) {
151 DataSet ds = optimizedWays.iterator().next().getDataSet();
152 File file = new File(Main.pref.getCacheDirectory(), "left-right-hand-traffic.osm");
153 try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
154 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, ds.getVersion())
155 ) {
156 w.header(false);
157 w.writeContent(ds);
158 w.footer();
159 } catch (IOException ex) {
160 throw new RuntimeException(ex);
161 }
162 }
163
164 private static Collection<Way> loadOptimizedBoundaries() {
165 try (InputStream is = new FileInputStream(new File(Main.pref.getCacheDirectory(), "left-right-hand-traffic.osm"))) {
166 return OsmReader.parseDataSet(is, null).getWays();
167 } catch (IllegalDataException | IOException ex) {
168 Main.trace(ex);
169 return Collections.emptyList();
170 }
171 }
172}
Note: See TracBrowser for help on using the repository browser.