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

Last change on this file since 15918 was 14720, checked in by GerdP, 6 years ago

fix #17224: Remove need for left-right-hand-traffic.osm

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Set;
8
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.Relation;
13import org.openstreetmap.josm.data.osm.RelationMember;
14import org.openstreetmap.josm.data.osm.Way;
15
16/**
17 * Look up, if there is right- or left-hand traffic at a certain place.
18 * See <a href="https://en.wikipedia.org/wiki/Left-_and_right-hand_traffic">Left- and right-hand traffic</a>
19 */
20public final class RightAndLefthandTraffic {
21
22 private static final String DRIVING_SIDE = "driving_side";
23 private static final String LEFT = "left";
24 private static final String RIGHT = "right";
25
26 private static volatile GeoPropertyIndex<Boolean> rlCache;
27
28 private RightAndLefthandTraffic() {
29 // Hide implicit public constructor for utility classes
30 }
31
32 /**
33 * Check if there is right-hand traffic at a certain location.
34 *
35 * @param ll the coordinates of the point
36 * @return true if there is right-hand traffic, false if there is left-hand traffic
37 */
38 public static synchronized boolean isRightHandTraffic(LatLon ll) {
39 Boolean value = rlCache.get(ll);
40 return value == null || !value;
41 }
42
43 /**
44 * Initializes Right and lefthand traffic data.
45 * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
46 */
47 public static synchronized void initialize() {
48 rlCache = new GeoPropertyIndex<>(computeLeftDrivingBoundaries(), 24);
49 }
50
51 private static DefaultGeoProperty computeLeftDrivingBoundaries() {
52 Collection<Way> ways = new ArrayList<>();
53 // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
54 DataSet data = Territories.getDataSet();
55 for (Way w : data.getWays()) {
56 if (LEFT.equals(w.get(DRIVING_SIDE))) {
57 addWayIfNotInner(ways, w);
58 }
59 }
60 for (Relation r : data.getRelations()) {
61 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
62 for (RelationMember rm : r.getMembers()) {
63 if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) {
64 addWayIfNotInner(ways, (Way) rm.getMember());
65 }
66 }
67 }
68 }
69 // Combine adjacent countries into a single polygon
70 return new DefaultGeoProperty(ways);
71 }
72
73 /**
74 * Adds w to ways, except if it is an inner way of another lefthand driving multipolygon,
75 * as Lesotho in South Africa and Cyprus village in British Cyprus base.
76 * @param ways ways
77 * @param w way
78 */
79 private static void addWayIfNotInner(Collection<Way> ways, Way w) {
80 Set<Way> s = Collections.singleton(w);
81 for (Relation r : OsmPrimitive.getParentRelations(s)) {
82 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) &&
83 "inner".equals(r.getMembersFor(s).iterator().next().getRole())) {
84 if (Logging.isDebugEnabled()) {
85 Logging.debug("Skipping {0} because inner part of {1}", w.get("name:en"), r.get("name:en"));
86 }
87 return;
88 }
89 }
90 ways.add(w);
91 }
92}
Note: See TracBrowser for help on using the repository browser.