Changeset 14720 in josm for trunk


Ignore:
Timestamp:
2019-01-23T06:42:40+01:00 (6 years ago)
Author:
GerdP
Message:

fix #17224: Remove need for left-right-hand-traffic.osm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java

    r14032 r14720  
    22package org.openstreetmap.josm.tools;
    33
    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;
    144import java.util.ArrayList;
    155import java.util.Collection;
    166import java.util.Collections;
    17 import java.util.List;
    187import java.util.Set;
    198
    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;
    249import org.openstreetmap.josm.data.coor.LatLon;
    2510import org.openstreetmap.josm.data.osm.DataSet;
    26 import org.openstreetmap.josm.data.osm.DownloadPolicy;
    2711import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2812import org.openstreetmap.josm.data.osm.Relation;
    2913import org.openstreetmap.josm.data.osm.RelationMember;
    30 import org.openstreetmap.josm.data.osm.UploadPolicy;
    3114import 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;
    3715
    3816/**
     
    6846     */
    6947    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);
    8049    }
    8150
    82     private static Collection<Way> computeOptimizedBoundaries() {
     51    private static DefaultGeoProperty computeLeftDrivingBoundaries() {
    8352        Collection<Way> ways = new ArrayList<>();
    84         Collection<OsmPrimitive> toPurge = new ArrayList<>();
    8553        // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
    8654        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()) {
    9056            if (LEFT.equals(w.get(DRIVING_SIDE))) {
    9157                addWayIfNotInner(ways, w);
    9258            }
    9359        }
    94         for (Relation r : allRelations) {
     60        for (Relation r : data.getRelations()) {
    9561            if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
    9662                for (RelationMember rm : r.getMembers()) {
     
    10167            }
    10268        }
    103         toPurge.addAll(allRelations);
    104         toPurge.addAll(allWays);
    105         toPurge.removeAll(ways);
    106         // Remove ways from parent relations for following optimizations
    107         for (Relation r : OsmPrimitive.getParentRelations(ways)) {
    108             r.setMembers(null);
    109         }
    110         // Remove all tags to avoid any conflict
    111         for (Way w : ways) {
    112             w.removeAll();
    113         }
    114         // Purge all other ways and relations so dataset only contains lefthand traffic data
    115         PurgeCommand.build(toPurge, null).executeCommand();
    11669        // 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);
    14071    }
    14172
     
    15990        ways.add(w);
    16091    }
    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     }
    18392}
Note: See TracChangeset for help on using the changeset viewer.