001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import java.util.Collection; 005import java.util.Collections; 006import java.util.Set; 007 008import org.openstreetmap.josm.data.coor.LatLon; 009import org.openstreetmap.josm.data.osm.OsmPrimitive; 010import org.openstreetmap.josm.data.osm.Relation; 011import org.openstreetmap.josm.data.osm.RelationMember; 012import org.openstreetmap.josm.data.osm.Way; 013 014/** 015 * Look up, if there is right- or left-hand traffic at a certain place. 016 * See <a href="https://en.wikipedia.org/wiki/Left-_and_right-hand_traffic">Left- and right-hand traffic</a> 017 */ 018public final class RightAndLefthandTraffic { 019 020 private static final String DRIVING_SIDE = "driving_side"; 021 private static final String LEFT = "left"; 022 private static final String RIGHT = "right"; 023 024 private static volatile GeoPropertyIndex<Boolean> rlCache; 025 026 private RightAndLefthandTraffic() { 027 // Hide implicit public constructor for utility classes 028 } 029 030 /** 031 * Check if there is right-hand traffic at a certain location. 032 * 033 * @param ll the coordinates of the point 034 * @return true if there is right-hand traffic, false if there is left-hand traffic 035 */ 036 public static synchronized boolean isRightHandTraffic(LatLon ll) { 037 Boolean value = rlCache.get(ll); 038 return value == null || !value; 039 } 040 041 /** 042 * Initializes Right and lefthand traffic data. 043 * @param geoProperty the property containing the traffic data 044 * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only. 045 */ 046 static synchronized void initialize(DefaultGeoProperty geoProperty) { 047 rlCache = new GeoPropertyIndex<>(geoProperty, 24); 048 } 049 050 static void appendLeftDrivingBoundaries(OsmPrimitive osm, Collection<Way> ways) { 051 // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states) 052 if (LEFT.equals(osm.get(DRIVING_SIDE))) { 053 if (osm instanceof Way) { 054 Way w = (Way) osm; 055 addWayIfNotInner(ways, w); 056 } else if (osm instanceof Relation && osm.isMultipolygon()) { 057 Relation r = (Relation) osm; 058 for (RelationMember rm : r.getMembers()) { 059 if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) { 060 addWayIfNotInner(ways, (Way) rm.getMember()); 061 } 062 } 063 } 064 } 065 } 066 067 /** 068 * Adds w to ways, except if it is an inner way of another lefthand driving multipolygon, 069 * as Lesotho in South Africa and Cyprus village in British Cyprus base. 070 * @param ways ways 071 * @param w way 072 */ 073 private static void addWayIfNotInner(Collection<Way> ways, Way w) { 074 Set<Way> s = Collections.singleton(w); 075 for (Relation r : OsmPrimitive.getParentRelations(s)) { 076 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) && 077 "inner".equals(r.getMembersFor(s).iterator().next().getRole())) { 078 if (Logging.isDebugEnabled()) { 079 Logging.debug("Skipping {0} because inner part of {1}", w.get("name:en"), r.get("name:en")); 080 } 081 return; 082 } 083 } 084 ways.add(w); 085 } 086}