1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.geom.Area;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
10 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.io.CachedFile;
|
---|
14 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
15 | import org.openstreetmap.josm.io.OsmReader;
|
---|
16 | import org.openstreetmap.josm.tools.GeoPropertyIndex.GeoProperty;
|
---|
17 | import org.openstreetmap.josm.tools.Geometry.PolygonIntersection;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Look up, if there is right- or left-hand traffic at a certain place.
|
---|
21 | */
|
---|
22 | public class RightAndLefthandTraffic {
|
---|
23 |
|
---|
24 | private static class RLTrafficGeoProperty implements GeoProperty<Boolean> {
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public Boolean get(LatLon ll) {
|
---|
28 | for (Area a : leftHandTrafficPolygons) {
|
---|
29 | if (a.contains(ll.lon(), ll.lat()))
|
---|
30 | return true;
|
---|
31 | }
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public Boolean get(BBox box) {
|
---|
37 | Area abox = new Area(box.toRectangle());
|
---|
38 | for (Area a : leftHandTrafficPolygons) {
|
---|
39 | PolygonIntersection is = Geometry.polygonIntersection(abox, a, 1e-10 /* using deg and not meters */);
|
---|
40 | if (is == PolygonIntersection.FIRST_INSIDE_SECOND)
|
---|
41 | return true;
|
---|
42 | if (is != PolygonIntersection.OUTSIDE)
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static Collection<Area> leftHandTrafficPolygons;
|
---|
50 | private static GeoPropertyIndex<Boolean> rlCache;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Check if there is right-hand traffic at a certain location.
|
---|
54 | *
|
---|
55 | * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex}
|
---|
56 | * as most look-ups are read-only.
|
---|
57 | * @param ll the coordinates of the point
|
---|
58 | * @return true if there is right-hand traffic, false if there is left-hand traffic
|
---|
59 | */
|
---|
60 | public static synchronized boolean isRightHandTraffic(LatLon ll) {
|
---|
61 | if (leftHandTrafficPolygons == null) {
|
---|
62 | initialize();
|
---|
63 | }
|
---|
64 | return !rlCache.get(ll);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static void initialize() {
|
---|
68 | leftHandTrafficPolygons = new ArrayList<>();
|
---|
69 | try (InputStream is = new CachedFile("resource://data/left-right-hand-traffic.osm").getInputStream()) {
|
---|
70 | DataSet data = OsmReader.parseDataSet(is, null);
|
---|
71 | for (Way w : data.getWays()) {
|
---|
72 | leftHandTrafficPolygons.add(Geometry.getAreaLatLon(w.getNodes()));
|
---|
73 | }
|
---|
74 | } catch (IOException | IllegalDataException ex) {
|
---|
75 | throw new RuntimeException(ex);
|
---|
76 | }
|
---|
77 | rlCache = new GeoPropertyIndex<Boolean>(new RLTrafficGeoProperty(), 24);
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|