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

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

see #10387 - see #12914 - add debug info for failing unit test, remove operator=RFF check as the tag does not exist anymore

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