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

Last change on this file since 13628 was 13559, checked in by Don-vip, 6 years ago

extract DownloadPolicy / UploadPolicy to separate classes

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