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

Last change on this file since 10314 was 9639, checked in by stoecker, 8 years ago

fix possible resource leaks

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.geom.Area;
5import java.io.IOException;
6import java.io.InputStream;
7import java.util.ArrayList;
8import java.util.Collection;
9
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.data.osm.BBox;
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.io.CachedFile;
15import org.openstreetmap.josm.io.IllegalDataException;
16import org.openstreetmap.josm.io.OsmReader;
17import org.openstreetmap.josm.tools.GeoPropertyIndex.GeoProperty;
18import org.openstreetmap.josm.tools.Geometry.PolygonIntersection;
19
20/**
21 * Look up, if there is right- or left-hand traffic at a certain place.
22 */
23public final class RightAndLefthandTraffic {
24
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()))
31 return Boolean.TRUE;
32 }
33 return Boolean.FALSE;
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)
42 return Boolean.TRUE;
43 if (is != PolygonIntersection.OUTSIDE)
44 return null;
45 }
46 return Boolean.FALSE;
47 }
48 }
49
50 private static volatile Collection<Area> leftHandTrafficPolygons;
51 private static volatile GeoPropertyIndex<Boolean> rlCache;
52
53 private RightAndLefthandTraffic() {
54 // Hide implicit public constructor for utility classes
55 }
56
57 /**
58 * Check if there is right-hand traffic at a certain location.
59 *
60 * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex}
61 * as most look-ups are read-only.
62 * @param ll the coordinates of the point
63 * @return true if there is right-hand traffic, false if there is left-hand traffic
64 */
65 public static synchronized boolean isRightHandTraffic(LatLon ll) {
66 if (leftHandTrafficPolygons == null) {
67 initialize();
68 }
69 return !rlCache.get(ll);
70 }
71
72 private static void initialize() {
73 leftHandTrafficPolygons = new ArrayList<>();
74 try (CachedFile cf = new CachedFile("resource://data/left-right-hand-traffic.osm");
75 InputStream is = cf.getInputStream()) {
76 DataSet data = OsmReader.parseDataSet(is, null);
77 for (Way w : data.getWays()) {
78 leftHandTrafficPolygons.add(Geometry.getAreaLatLon(w.getNodes()));
79 }
80 } catch (IOException | IllegalDataException ex) {
81 throw new RuntimeException(ex);
82 }
83 rlCache = new GeoPropertyIndex<>(new RLTrafficGeoProperty(), 24);
84 }
85}
Note: See TracBrowser for help on using the repository browser.