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

Last change on this file since 17335 was 16321, checked in by simon04, 4 years ago

fix #18907 - Initialize Territories+RightAndLefthandTraffic together, remove "Edit boundaries" to save memory (unless started with --debug)

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