Ticket #18907: 18907.patch

File 18907.patch, 15.3 KB (added by simon04, 5 years ago)
  • scripts/TagInfoExtract.java

    commit 59feb376093a782eb0fae018e7cfe28b04ea3222
    Author: Simon Legner <Simon.Legner@gmail.com>
    Date:   2020-03-10 12:27:12 +0100
    
        fix #18907 - Initialize Territories+RightAndLefthandTraffic together, remove "Edit boundaries", save memory
    
    diff --git a/scripts/TagInfoExtract.java b/scripts/TagInfoExtract.java
    index 901072ecb..eab619448 100644
    a b private void init() throws IOException {  
    582582        System.setProperty("josm.home", tmpdir.toString());
    583583        DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
    584584        Territories.initialize();
    585         RightAndLefthandTraffic.initialize();
    586585        Files.createDirectories(options.imageDir);
    587586    }
    588587}
  • src/org/openstreetmap/josm/gui/MainInitialization.java

    diff --git a/src/org/openstreetmap/josm/gui/MainInitialization.java b/src/org/openstreetmap/josm/gui/MainInitialization.java
    index a145698f7..a1fd49357 100644
    a b  
    3939import org.openstreetmap.josm.tools.OpenBrowser;
    4040import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    4141import org.openstreetmap.josm.tools.PlatformManager;
    42 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
    4342import org.openstreetmap.josm.tools.Shortcut;
    4443import org.openstreetmap.josm.tools.Tag2Link;
    4544import org.openstreetmap.josm.tools.Territories;
    public MainInitialization(MainApplication application) {  
    8281                // help shortcut
    8382                MainApplication.registerActionShortcut(MainApplication.menu.help,
    8483                        Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT));
    85             }),
    86             // This needs to be done before RightAndLefthandTraffic::initialize is called
    87             new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize)
     84            })
    8885        );
    8986    }
    9087
    public MainInitialization(MainApplication application) {  
    114111                        Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e)));
    115112                    }
    116113                }),
    117             new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize),
     114            new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize),
    118115            new InitializationTask(tr("Initializing validator"), OsmValidator::initialize),
    119116            new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize),
    120117            new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize),
  • src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java b/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java
    index ac565d763..7973ee370 100644
    a b  
    4343import org.openstreetmap.josm.tools.OptionParser;
    4444import org.openstreetmap.josm.tools.OptionParser.OptionCount;
    4545import org.openstreetmap.josm.tools.OptionParser.OptionParseException;
    46 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
     46import org.openstreetmap.josm.tools.Territories;
    4747
    4848/**
    4949 * Command line interface for rendering osm data to an image file.
    void initialize() {  
    438438        String projCode = Optional.ofNullable(argProjection).orElse("epsg:3857");
    439439        ProjectionRegistry.setProjection(Projections.getProjectionByCode(projCode.toUpperCase(Locale.US)));
    440440
    441         RightAndLefthandTraffic.initialize();
     441        Territories.initialize();
    442442    }
    443443
    444444    private Level getLogLevel() {
  • src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java b/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
    index 5339e127d..1ad501304 100644
    a b  
    4646import org.openstreetmap.josm.gui.dialogs.LogShowDialog;
    4747import org.openstreetmap.josm.gui.help.HelpUtil;
    4848import org.openstreetmap.josm.gui.io.CustomConfigurator;
    49 import org.openstreetmap.josm.gui.layer.MainLayerManager;
    5049import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    5150import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
    5251import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     
    6059import org.openstreetmap.josm.spi.preferences.StringSetting;
    6160import org.openstreetmap.josm.tools.GBC;
    6261import org.openstreetmap.josm.tools.Logging;
    63 import org.openstreetmap.josm.tools.Territories;
    6462import org.openstreetmap.josm.tools.Utils;
    6563
    6664/**
    public void clear() {  
    8987        }
    9088    }
    9189
    92     private static final class EditBoundariesAction extends AbstractAction {
    93         EditBoundariesAction() {
    94             super(tr("Edit boundaries"));
    95         }
    96 
    97         @Override
    98         public void actionPerformed(ActionEvent ae) {
    99             DataSet dataSet = Territories.getOriginalDataSet();
    100             MainLayerManager layerManager = MainApplication.getLayerManager();
    101             if (layerManager.getLayersOfType(OsmDataLayer.class).stream().noneMatch(l -> dataSet.equals(l.getDataSet()))) {
    102                 layerManager.addLayer(new UnclearableOsmDataLayer(dataSet, tr("Internal JOSM boundaries")));
    103             }
    104         }
    105     }
    106 
    10790    private final class ResetPreferencesAction extends AbstractAction {
    10891        ResetPreferencesAction() {
    10992            super(tr("Reset preferences"));
    private JPopupMenu buildPopupMenu() {  
    391374        menu.addSeparator();
    392375        menu.add(getProfileMenu());
    393376        menu.addSeparator();
    394         menu.add(new EditBoundariesAction());
    395         menu.addSeparator();
    396377        menu.add(new ResetPreferencesAction());
    397378        return menu;
    398379    }
  • src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java

    diff --git a/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java b/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java
    index a4372725a..4ed51e38e 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
    33
    4 import java.util.ArrayList;
    54import java.util.Collection;
    65import java.util.Collections;
    76import java.util.Set;
    87
    98import org.openstreetmap.josm.data.coor.LatLon;
    10 import org.openstreetmap.josm.data.osm.DataSet;
    119import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1210import org.openstreetmap.josm.data.osm.Relation;
    13 import org.openstreetmap.josm.data.osm.RelationMember;
    1411import org.openstreetmap.josm.data.osm.Way;
    1512
    1613/**
     
    1916 */
    2017public final class RightAndLefthandTraffic {
    2118
    22     private static final String DRIVING_SIDE = "driving_side";
    23     private static final String LEFT = "left";
    24     private static final String RIGHT = "right";
     19    static final String DRIVING_SIDE = "driving_side";
     20    static final String LEFT = "left";
     21    static final String RIGHT = "right";
    2522
    2623    private static volatile GeoPropertyIndex<Boolean> rlCache;
    2724
    public static synchronized boolean isRightHandTraffic(LatLon ll) {  
    4239
    4340    /**
    4441     * Initializes Right and lefthand traffic data.
     42     * @param geoProperty the property containing the traffic data
    4543     * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
    4644     */
    47     public static synchronized void initialize() {
    48         rlCache = new GeoPropertyIndex<>(computeLeftDrivingBoundaries(), 24);
    49     }
    50 
    51     private static DefaultGeoProperty computeLeftDrivingBoundaries() {
    52         Collection<Way> ways = new ArrayList<>();
    53         // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
    54         DataSet data = Territories.getDataSet();
    55         for (Way w : data.getWays()) {
    56             if (LEFT.equals(w.get(DRIVING_SIDE))) {
    57                 addWayIfNotInner(ways, w);
    58             }
    59         }
    60         for (Relation r : data.getRelations()) {
    61             if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
    62                 for (RelationMember rm : r.getMembers()) {
    63                     if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) {
    64                         addWayIfNotInner(ways, (Way) rm.getMember());
    65                     }
    66                 }
    67             }
    68         }
    69         // Combine adjacent countries into a single polygon
    70         return new DefaultGeoProperty(ways);
     45    static synchronized void initialize(DefaultGeoProperty geoProperty) {
     46        rlCache = new GeoPropertyIndex<>(geoProperty, 24);
    7147    }
    7248
    7349    /**
    private static DefaultGeoProperty computeLeftDrivingBoundaries() {  
    7652     * @param ways ways
    7753     * @param w way
    7854     */
    79     private static void addWayIfNotInner(Collection<Way> ways, Way w) {
     55    static void addWayIfNotInner(Collection<Way> ways, Way w) {
    8056        Set<Way> s = Collections.singleton(w);
    8157        for (Relation r : OsmPrimitive.getParentRelations(s)) {
    8258            if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) &&
  • src/org/openstreetmap/josm/tools/Territories.java

    diff --git a/src/org/openstreetmap/josm/tools/Territories.java b/src/org/openstreetmap/josm/tools/Territories.java
    index a315e9f96..54db37894 100644
    a b  
    3030
    3131import org.openstreetmap.josm.data.coor.LatLon;
    3232import org.openstreetmap.josm.data.osm.DataSet;
     33import org.openstreetmap.josm.data.osm.Node;
    3334import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3435import org.openstreetmap.josm.data.osm.Relation;
     36import org.openstreetmap.josm.data.osm.RelationMember;
    3537import org.openstreetmap.josm.data.osm.Way;
     38import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
    3639import org.openstreetmap.josm.io.CachedFile;
    3740import org.openstreetmap.josm.io.IllegalDataException;
    3841import org.openstreetmap.josm.io.OsmReader;
     
    5255    private static final String ISO3166_2_LC = ISO3166_2.toLowerCase(Locale.ENGLISH);
    5356    private static final String TAGINFO = "taginfo";
    5457
    55     private static DataSet dataSet;
    56 
    5758    private static volatile Map<String, GeoPropertyIndex<Boolean>> iso3166Cache;
    5859    private static volatile Map<String, TaginfoRegionalInstance> taginfoCache;
    5960    private static volatile Map<String, TaginfoRegionalInstance> taginfoGeofabrikCache;
    public static synchronized boolean isIso3166Code(String code, LatLon ll) {  
    9899        return Boolean.TRUE.equals(gpi.get(ll)); // avoid NPE, see #16491
    99100    }
    100101
    101     /**
    102      * Returns the original territories dataset. Be extra cautious when manipulating it!
    103      * @return the original territories dataset
    104      * @since 15565
    105      */
    106     public static synchronized DataSet getOriginalDataSet() {
    107         return dataSet;
    108     }
    109 
    110     /**
    111      * Returns a copy of the territories dataset.
    112      * @return a copy of the territories dataset
    113      */
    114     public static synchronized DataSet getDataSet() {
    115         return new DataSet(dataSet);
    116     }
    117 
    118102    /**
    119103     * Initializes territories.
    120104     * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
    public static synchronized void initialize() {  
    127111    private static void initializeInternalData() {
    128112        iso3166Cache = new HashMap<>();
    129113        taginfoCache = new TreeMap<>();
     114        Collection<Way> traffic = new ArrayList<>();
    130115        try (CachedFile cf = new CachedFile("resource://data/" + FILENAME);
    131116                InputStream is = cf.getInputStream()) {
    132             dataSet = OsmReader.parseDataSet(is, null);
    133             Collection<OsmPrimitive> candidates = new ArrayList<>(dataSet.getWays());
    134             candidates.addAll(dataSet.getRelations());
    135             for (OsmPrimitive osm : candidates) {
     117            DataSet dataSet = OsmReader.parseDataSet(is, null);
     118            for (OsmPrimitive osm : dataSet.allPrimitives()) {
     119                if (osm instanceof Node) {
     120                    continue;
     121                }
    136122                String iso1 = osm.get(ISO3166_1);
    137123                String iso2 = osm.get(ISO3166_2);
    138124                if (iso1 != null || iso2 != null) {
    private static void initializeInternalData() {  
    154140                        iso3166Cache.put(iso2, gpi);
    155141                    }
    156142                }
     143                // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
     144                if (RightAndLefthandTraffic.LEFT.equals(osm.get(RightAndLefthandTraffic.DRIVING_SIDE))) {
     145                    if (osm instanceof Way) {
     146                        RightAndLefthandTraffic.addWayIfNotInner(traffic, ((Way) osm));
     147                    } else if (osm instanceof Relation && osm.isMultipolygon()) {
     148                        for (RelationMember rm : ((Relation) osm).getMembers()) {
     149                            if (rm.isWay() && "outer".equals(rm.getRole())
     150                                    && !RightAndLefthandTraffic.RIGHT.equals(rm.getMember().get(RightAndLefthandTraffic.DRIVING_SIDE))) {
     151                                RightAndLefthandTraffic.addWayIfNotInner(traffic, (Way) rm.getMember());
     152                            }
     153                        }
     154                    }
     155                }
    157156            }
     157            RightAndLefthandTraffic.initialize(new DefaultGeoProperty(traffic));
     158            MultipolygonCache.getInstance().clear(dataSet);
    158159        } catch (IOException | IllegalDataException ex) {
    159160            throw new JosmRuntimeException(ex);
    160161        }
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
    index 9161e3fa9..f41f80283 100644
    a b  
    6464import org.openstreetmap.josm.tools.JosmRuntimeException;
    6565import org.openstreetmap.josm.tools.Logging;
    6666import org.openstreetmap.josm.tools.MemoryManagerTest;
    67 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
    6867import org.openstreetmap.josm.tools.Territories;
    6968import org.openstreetmap.josm.tools.bugreport.ReportedException;
    7069import org.openstreetmap.josm.tools.date.DateUtils;
     
    9998    private boolean usePresets;
    10099    private boolean useHttps;
    101100    private boolean territories;
    102     private boolean rlTraffic;
    103101    private boolean metric;
    104102    private boolean main;
    105103
    public JOSMTestRules territories() {  
    269267     * Use right and lefthand traffic dataset in this test.
    270268     * @return this instance, for easy chaining
    271269     * @since 12556
     270     * @deprecated Use {@link #territories}
    272271     */
     272    @Deprecated
    273273    public JOSMTestRules rlTraffic() {
    274274        territories();
    275         rlTraffic = true;
    276275        return this;
    277276    }
    278277
    protected void before() throws InitializationError, ReflectiveOperationException  
    552551            Territories.initialize();
    553552        }
    554553
    555         if (rlTraffic) {
    556             RightAndLefthandTraffic.initialize();
    557         }
    558 
    559554        if (this.edtAssertionMockingRunnable != null) {
    560555            this.edtAssertionMockingRunnable.run();
    561556        }