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

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

enable new PMD rule AvoidFileStream - see https://pmd.github.io/pmd-6.0.0/pmd_rules_java_performance.html#avoidfilestream / https://bugs.openjdk.java.net/browse/JDK-8080225 for details

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