1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.OutputStreamWriter;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.io.Writer;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.actions.JoinAreasAction;
|
---|
20 | import org.openstreetmap.josm.actions.JoinAreasAction.JoinAreasResult;
|
---|
21 | import org.openstreetmap.josm.actions.JoinAreasAction.Multipolygon;
|
---|
22 | import org.openstreetmap.josm.command.PurgeCommand;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
27 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
29 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
30 | import org.openstreetmap.josm.io.OsmReader;
|
---|
31 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
32 | import org.openstreetmap.josm.io.OsmWriterFactory;
|
---|
33 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Look up, if there is right- or left-hand traffic at a certain place.
|
---|
37 | */
|
---|
38 | public final class RightAndLefthandTraffic {
|
---|
39 |
|
---|
40 | private static final String DRIVING_SIDE = "driving_side";
|
---|
41 | private static final String LEFT = "left";
|
---|
42 | private static final String RIGHT = "right";
|
---|
43 |
|
---|
44 | private static volatile GeoPropertyIndex<Boolean> rlCache;
|
---|
45 |
|
---|
46 | private RightAndLefthandTraffic() {
|
---|
47 | // Hide implicit public constructor for utility classes
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Check if there is right-hand traffic at a certain location.
|
---|
52 | *
|
---|
53 | * @param ll the coordinates of the point
|
---|
54 | * @return true if there is right-hand traffic, false if there is left-hand traffic
|
---|
55 | */
|
---|
56 | public static synchronized boolean isRightHandTraffic(LatLon ll) {
|
---|
57 | return !rlCache.get(ll);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Initializes Right and lefthand traffic data.
|
---|
62 | * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
|
---|
63 | */
|
---|
64 | public static synchronized void initialize() {
|
---|
65 | Collection<Way> optimizedWays = loadOptimizedBoundaries();
|
---|
66 | if (optimizedWays.isEmpty()) {
|
---|
67 | optimizedWays = computeOptimizedBoundaries();
|
---|
68 | saveOptimizedBoundaries(optimizedWays);
|
---|
69 | }
|
---|
70 | rlCache = new GeoPropertyIndex<>(new DefaultGeoProperty(optimizedWays), 24);
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static Collection<Way> computeOptimizedBoundaries() {
|
---|
74 | Collection<Way> ways = new ArrayList<>();
|
---|
75 | Collection<OsmPrimitive> toPurge = new ArrayList<>();
|
---|
76 | // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
|
---|
77 | DataSet data = Territories.getDataSet();
|
---|
78 | Collection<Relation> allRelations = data.getRelations();
|
---|
79 | Collection<Way> allWays = data.getWays();
|
---|
80 | for (Way w : allWays) {
|
---|
81 | if (LEFT.equals(w.get(DRIVING_SIDE))) {
|
---|
82 | addWayIfNotInner(ways, w);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | for (Relation r : allRelations) {
|
---|
86 | if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
|
---|
87 | for (RelationMember rm : r.getMembers()) {
|
---|
88 | if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) {
|
---|
89 | addWayIfNotInner(ways, (Way) rm.getMember());
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | toPurge.addAll(allRelations);
|
---|
95 | toPurge.addAll(allWays);
|
---|
96 | toPurge.removeAll(ways);
|
---|
97 | // Remove ways from parent relations for following optimizations
|
---|
98 | for (Relation r : OsmPrimitive.getParentRelations(ways)) {
|
---|
99 | r.setMembers(null);
|
---|
100 | }
|
---|
101 | // Remove all tags to avoid any conflict
|
---|
102 | for (Way w : ways) {
|
---|
103 | w.removeAll();
|
---|
104 | }
|
---|
105 | // Purge all other ways and relations so dataset only contains lefthand traffic data
|
---|
106 | PurgeCommand.build(toPurge, null).executeCommand();
|
---|
107 | // Combine adjacent countries into a single polygon
|
---|
108 | Collection<Way> optimizedWays = new ArrayList<>();
|
---|
109 | List<Multipolygon> areas = JoinAreasAction.collectMultipolygons(ways);
|
---|
110 | if (areas != null) {
|
---|
111 | try {
|
---|
112 | JoinAreasResult result = new JoinAreasAction(false).joinAreas(areas);
|
---|
113 | if (result.hasChanges()) {
|
---|
114 | for (Multipolygon mp : result.getPolygons()) {
|
---|
115 | optimizedWays.add(mp.getOuterWay());
|
---|
116 | }
|
---|
117 | }
|
---|
118 | } catch (UserCancelException ex) {
|
---|
119 | Logging.warn(ex);
|
---|
120 | } catch (JosmRuntimeException ex) {
|
---|
121 | // Workaround to #10511 / #14185. To remove when #10511 is solved
|
---|
122 | Logging.error(ex);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | if (optimizedWays.isEmpty()) {
|
---|
126 | // Problem: don't optimize
|
---|
127 | Logging.warn("Unable to join left-driving countries polygons");
|
---|
128 | optimizedWays.addAll(ways);
|
---|
129 | }
|
---|
130 | return optimizedWays;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Adds w to ways, except if it is an inner way of another lefthand driving multipolygon,
|
---|
135 | * as Lesotho in South Africa and Cyprus village in British Cyprus base.
|
---|
136 | * @param ways ways
|
---|
137 | * @param w way
|
---|
138 | */
|
---|
139 | private static void addWayIfNotInner(Collection<Way> ways, Way w) {
|
---|
140 | Set<Way> s = Collections.singleton(w);
|
---|
141 | for (Relation r : OsmPrimitive.getParentRelations(s)) {
|
---|
142 | if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) &&
|
---|
143 | "inner".equals(r.getMembersFor(s).iterator().next().getRole())) {
|
---|
144 | if (Logging.isDebugEnabled()) {
|
---|
145 | Logging.debug("Skipping {0} because inner part of {1}", w.get("name:en"), r.get("name:en"));
|
---|
146 | }
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | ways.add(w);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private static void saveOptimizedBoundaries(Collection<Way> optimizedWays) {
|
---|
154 | DataSet ds = optimizedWays.iterator().next().getDataSet();
|
---|
155 | File file = new File(Config.getDirs().getCacheDirectory(true), "left-right-hand-traffic.osm");
|
---|
156 | try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
---|
157 | OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, ds.getVersion())
|
---|
158 | ) {
|
---|
159 | w.header(DataSet.UploadPolicy.DISCOURAGED);
|
---|
160 | w.writeContent(ds);
|
---|
161 | w.footer();
|
---|
162 | } catch (IOException ex) {
|
---|
163 | throw new JosmRuntimeException(ex);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | private static Collection<Way> loadOptimizedBoundaries() {
|
---|
168 | try (InputStream is = new FileInputStream(new File(
|
---|
169 | Config.getDirs().getCacheDirectory(false), "left-right-hand-traffic.osm"))) {
|
---|
170 | return OsmReader.parseDataSet(is, null).getWays();
|
---|
171 | } catch (IllegalDataException | IOException ex) {
|
---|
172 | Logging.trace(ex);
|
---|
173 | return Collections.emptyList();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|