Changeset 14720 in josm
- Timestamp:
- 2019-01-23T06:42:40+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java
r14032 r14720 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.io.File;5 import java.io.IOException;6 import java.io.InputStream;7 import java.io.OutputStreamWriter;8 import java.io.PrintWriter;9 import java.io.Writer;10 import java.nio.charset.StandardCharsets;11 import java.nio.file.Files;12 import java.nio.file.InvalidPathException;13 import java.nio.file.Paths;14 4 import java.util.ArrayList; 15 5 import java.util.Collection; 16 6 import java.util.Collections; 17 import java.util.List;18 7 import java.util.Set; 19 8 20 import org.openstreetmap.josm.actions.JoinAreasAction;21 import org.openstreetmap.josm.actions.JoinAreasAction.JoinAreasResult;22 import org.openstreetmap.josm.actions.JoinAreasAction.Multipolygon;23 import org.openstreetmap.josm.command.PurgeCommand;24 9 import org.openstreetmap.josm.data.coor.LatLon; 25 10 import org.openstreetmap.josm.data.osm.DataSet; 26 import org.openstreetmap.josm.data.osm.DownloadPolicy;27 11 import org.openstreetmap.josm.data.osm.OsmPrimitive; 28 12 import org.openstreetmap.josm.data.osm.Relation; 29 13 import org.openstreetmap.josm.data.osm.RelationMember; 30 import org.openstreetmap.josm.data.osm.UploadPolicy;31 14 import org.openstreetmap.josm.data.osm.Way; 32 import org.openstreetmap.josm.io.IllegalDataException;33 import org.openstreetmap.josm.io.OsmReader;34 import org.openstreetmap.josm.io.OsmWriter;35 import org.openstreetmap.josm.io.OsmWriterFactory;36 import org.openstreetmap.josm.spi.preferences.Config;37 15 38 16 /** … … 68 46 */ 69 47 public static synchronized void initialize() { 70 Collection<Way> optimizedWays = loadOptimizedBoundaries(); 71 if (optimizedWays.isEmpty()) { 72 optimizedWays = computeOptimizedBoundaries(); 73 try { 74 saveOptimizedBoundaries(optimizedWays); 75 } catch (IOException | SecurityException e) { 76 Logging.log(Logging.LEVEL_ERROR, "Unable to save optimized boundaries", e); 77 } 78 } 79 rlCache = new GeoPropertyIndex<>(new DefaultGeoProperty(optimizedWays), 24); 48 rlCache = new GeoPropertyIndex<>(computeLeftDrivingBoundaries(), 24); 80 49 } 81 50 82 private static Collection<Way> computeOptimizedBoundaries() {51 private static DefaultGeoProperty computeLeftDrivingBoundaries() { 83 52 Collection<Way> ways = new ArrayList<>(); 84 Collection<OsmPrimitive> toPurge = new ArrayList<>();85 53 // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states) 86 54 DataSet data = Territories.getDataSet(); 87 Collection<Relation> allRelations = data.getRelations(); 88 Collection<Way> allWays = data.getWays(); 89 for (Way w : allWays) { 55 for (Way w : data.getWays()) { 90 56 if (LEFT.equals(w.get(DRIVING_SIDE))) { 91 57 addWayIfNotInner(ways, w); 92 58 } 93 59 } 94 for (Relation r : allRelations) {60 for (Relation r : data.getRelations()) { 95 61 if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) { 96 62 for (RelationMember rm : r.getMembers()) { … … 101 67 } 102 68 } 103 toPurge.addAll(allRelations);104 toPurge.addAll(allWays);105 toPurge.removeAll(ways);106 // Remove ways from parent relations for following optimizations107 for (Relation r : OsmPrimitive.getParentRelations(ways)) {108 r.setMembers(null);109 }110 // Remove all tags to avoid any conflict111 for (Way w : ways) {112 w.removeAll();113 }114 // Purge all other ways and relations so dataset only contains lefthand traffic data115 PurgeCommand.build(toPurge, null).executeCommand();116 69 // Combine adjacent countries into a single polygon 117 Collection<Way> optimizedWays = new ArrayList<>(); 118 List<Multipolygon> areas = JoinAreasAction.collectMultipolygons(ways); 119 if (areas != null) { 120 try { 121 JoinAreasResult result = new JoinAreasAction(false).joinAreas(areas); 122 if (result.hasChanges()) { 123 for (Multipolygon mp : result.getPolygons()) { 124 optimizedWays.add(mp.getOuterWay()); 125 } 126 } 127 } catch (UserCancelException ex) { 128 Logging.warn(ex); 129 } catch (JosmRuntimeException ex) { 130 // Workaround to #10511 / #14185. To remove when #10511 is solved 131 Logging.error(ex); 132 } 133 } 134 if (optimizedWays.isEmpty()) { 135 // Problem: don't optimize 136 Logging.warn("Unable to join left-driving countries polygons"); 137 optimizedWays.addAll(ways); 138 } 139 return optimizedWays; 70 return new DefaultGeoProperty(ways); 140 71 } 141 72 … … 159 90 ways.add(w); 160 91 } 161 162 private static void saveOptimizedBoundaries(Collection<Way> optimizedWays) throws IOException {163 DataSet ds = optimizedWays.iterator().next().getDataSet();164 File file = new File(Config.getDirs().getCacheDirectory(true), "left-right-hand-traffic.osm");165 try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8);166 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, ds.getVersion())167 ) {168 w.header(DownloadPolicy.NORMAL, UploadPolicy.DISCOURAGED);169 w.writeContent(ds);170 w.footer();171 }172 }173 174 private static Collection<Way> loadOptimizedBoundaries() {175 try (InputStream is = Files.newInputStream(Paths.get(176 Config.getDirs().getCacheDirectory(false).getPath(), "left-right-hand-traffic.osm"))) {177 return OsmReader.parseDataSet(is, null).getWays();178 } catch (IllegalDataException | IOException | InvalidPathException | SecurityException ex) {179 Logging.trace(ex);180 return Collections.emptyList();181 }182 }183 92 }
Note:
See TracChangeset
for help on using the changeset viewer.