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

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

fix NPE that occurs with Taginfo script

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