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