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 {
|
582 | 582 | System.setProperty("josm.home", tmpdir.toString()); |
583 | 583 | DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback); |
584 | 584 | Territories.initialize(); |
585 | | RightAndLefthandTraffic.initialize(); |
586 | 585 | Files.createDirectories(options.imageDir); |
587 | 586 | } |
588 | 587 | } |
diff --git a/src/org/openstreetmap/josm/gui/MainInitialization.java b/src/org/openstreetmap/josm/gui/MainInitialization.java
index a145698f7..a1fd49357 100644
a
|
b
|
|
39 | 39 | import org.openstreetmap.josm.tools.OpenBrowser; |
40 | 40 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard; |
41 | 41 | import org.openstreetmap.josm.tools.PlatformManager; |
42 | | import org.openstreetmap.josm.tools.RightAndLefthandTraffic; |
43 | 42 | import org.openstreetmap.josm.tools.Shortcut; |
44 | 43 | import org.openstreetmap.josm.tools.Tag2Link; |
45 | 44 | import org.openstreetmap.josm.tools.Territories; |
… |
… |
public MainInitialization(MainApplication application) {
|
82 | 81 | // help shortcut |
83 | 82 | MainApplication.registerActionShortcut(MainApplication.menu.help, |
84 | 83 | 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 | }) |
88 | 85 | ); |
89 | 86 | } |
90 | 87 | |
… |
… |
public MainInitialization(MainApplication application) {
|
114 | 111 | Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e))); |
115 | 112 | } |
116 | 113 | }), |
117 | | new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize), |
| 114 | new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize), |
118 | 115 | new InitializationTask(tr("Initializing validator"), OsmValidator::initialize), |
119 | 116 | new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize), |
120 | 117 | new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize), |
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
|
|
43 | 43 | import org.openstreetmap.josm.tools.OptionParser; |
44 | 44 | import org.openstreetmap.josm.tools.OptionParser.OptionCount; |
45 | 45 | import org.openstreetmap.josm.tools.OptionParser.OptionParseException; |
46 | | import org.openstreetmap.josm.tools.RightAndLefthandTraffic; |
| 46 | import org.openstreetmap.josm.tools.Territories; |
47 | 47 | |
48 | 48 | /** |
49 | 49 | * Command line interface for rendering osm data to an image file. |
… |
… |
void initialize() {
|
438 | 438 | String projCode = Optional.ofNullable(argProjection).orElse("epsg:3857"); |
439 | 439 | ProjectionRegistry.setProjection(Projections.getProjectionByCode(projCode.toUpperCase(Locale.US))); |
440 | 440 | |
441 | | RightAndLefthandTraffic.initialize(); |
| 441 | Territories.initialize(); |
442 | 442 | } |
443 | 443 | |
444 | 444 | private Level getLogLevel() { |
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
|
|
46 | 46 | import org.openstreetmap.josm.gui.dialogs.LogShowDialog; |
47 | 47 | import org.openstreetmap.josm.gui.help.HelpUtil; |
48 | 48 | import org.openstreetmap.josm.gui.io.CustomConfigurator; |
49 | | import org.openstreetmap.josm.gui.layer.MainLayerManager; |
50 | 49 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
51 | 50 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting; |
52 | 51 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting; |
… |
… |
|
60 | 59 | import org.openstreetmap.josm.spi.preferences.StringSetting; |
61 | 60 | import org.openstreetmap.josm.tools.GBC; |
62 | 61 | import org.openstreetmap.josm.tools.Logging; |
63 | | import org.openstreetmap.josm.tools.Territories; |
64 | 62 | import org.openstreetmap.josm.tools.Utils; |
65 | 63 | |
66 | 64 | /** |
… |
… |
public void clear() {
|
89 | 87 | } |
90 | 88 | } |
91 | 89 | |
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 | | |
107 | 90 | private final class ResetPreferencesAction extends AbstractAction { |
108 | 91 | ResetPreferencesAction() { |
109 | 92 | super(tr("Reset preferences")); |
… |
… |
private JPopupMenu buildPopupMenu() {
|
391 | 374 | menu.addSeparator(); |
392 | 375 | menu.add(getProfileMenu()); |
393 | 376 | menu.addSeparator(); |
394 | | menu.add(new EditBoundariesAction()); |
395 | | menu.addSeparator(); |
396 | 377 | menu.add(new ResetPreferencesAction()); |
397 | 378 | return menu; |
398 | 379 | } |
diff --git a/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java b/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java
index a4372725a..4ed51e38e 100644
a
|
b
|
|
1 | 1 | // License: GPL. For details, see LICENSE file. |
2 | 2 | package org.openstreetmap.josm.tools; |
3 | 3 | |
4 | | import java.util.ArrayList; |
5 | 4 | import java.util.Collection; |
6 | 5 | import java.util.Collections; |
7 | 6 | import java.util.Set; |
8 | 7 | |
9 | 8 | import org.openstreetmap.josm.data.coor.LatLon; |
10 | | import org.openstreetmap.josm.data.osm.DataSet; |
11 | 9 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
12 | 10 | import org.openstreetmap.josm.data.osm.Relation; |
13 | | import org.openstreetmap.josm.data.osm.RelationMember; |
14 | 11 | import org.openstreetmap.josm.data.osm.Way; |
15 | 12 | |
16 | 13 | /** |
… |
… |
|
19 | 16 | */ |
20 | 17 | public final class RightAndLefthandTraffic { |
21 | 18 | |
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"; |
25 | 22 | |
26 | 23 | private static volatile GeoPropertyIndex<Boolean> rlCache; |
27 | 24 | |
… |
… |
public static synchronized boolean isRightHandTraffic(LatLon ll) {
|
42 | 39 | |
43 | 40 | /** |
44 | 41 | * Initializes Right and lefthand traffic data. |
| 42 | * @param geoProperty the property containing the traffic data |
45 | 43 | * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only. |
46 | 44 | */ |
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); |
71 | 47 | } |
72 | 48 | |
73 | 49 | /** |
… |
… |
private static DefaultGeoProperty computeLeftDrivingBoundaries() {
|
76 | 52 | * @param ways ways |
77 | 53 | * @param w way |
78 | 54 | */ |
79 | | private static void addWayIfNotInner(Collection<Way> ways, Way w) { |
| 55 | static void addWayIfNotInner(Collection<Way> ways, Way w) { |
80 | 56 | Set<Way> s = Collections.singleton(w); |
81 | 57 | for (Relation r : OsmPrimitive.getParentRelations(s)) { |
82 | 58 | if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE)) && |
diff --git a/src/org/openstreetmap/josm/tools/Territories.java b/src/org/openstreetmap/josm/tools/Territories.java
index a315e9f96..54db37894 100644
a
|
b
|
|
30 | 30 | |
31 | 31 | import org.openstreetmap.josm.data.coor.LatLon; |
32 | 32 | import org.openstreetmap.josm.data.osm.DataSet; |
| 33 | import org.openstreetmap.josm.data.osm.Node; |
33 | 34 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
34 | 35 | import org.openstreetmap.josm.data.osm.Relation; |
| 36 | import org.openstreetmap.josm.data.osm.RelationMember; |
35 | 37 | import org.openstreetmap.josm.data.osm.Way; |
| 38 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; |
36 | 39 | import org.openstreetmap.josm.io.CachedFile; |
37 | 40 | import org.openstreetmap.josm.io.IllegalDataException; |
38 | 41 | import org.openstreetmap.josm.io.OsmReader; |
… |
… |
|
52 | 55 | private static final String ISO3166_2_LC = ISO3166_2.toLowerCase(Locale.ENGLISH); |
53 | 56 | private static final String TAGINFO = "taginfo"; |
54 | 57 | |
55 | | private static DataSet dataSet; |
56 | | |
57 | 58 | private static volatile Map<String, GeoPropertyIndex<Boolean>> iso3166Cache; |
58 | 59 | private static volatile Map<String, TaginfoRegionalInstance> taginfoCache; |
59 | 60 | private static volatile Map<String, TaginfoRegionalInstance> taginfoGeofabrikCache; |
… |
… |
public static synchronized boolean isIso3166Code(String code, LatLon ll) {
|
98 | 99 | return Boolean.TRUE.equals(gpi.get(ll)); // avoid NPE, see #16491 |
99 | 100 | } |
100 | 101 | |
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 | | |
118 | 102 | /** |
119 | 103 | * Initializes territories. |
120 | 104 | * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only. |
… |
… |
public static synchronized void initialize() {
|
127 | 111 | private static void initializeInternalData() { |
128 | 112 | iso3166Cache = new HashMap<>(); |
129 | 113 | taginfoCache = new TreeMap<>(); |
| 114 | Collection<Way> traffic = new ArrayList<>(); |
130 | 115 | try (CachedFile cf = new CachedFile("resource://data/" + FILENAME); |
131 | 116 | 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 | } |
136 | 122 | String iso1 = osm.get(ISO3166_1); |
137 | 123 | String iso2 = osm.get(ISO3166_2); |
138 | 124 | if (iso1 != null || iso2 != null) { |
… |
… |
private static void initializeInternalData() {
|
154 | 140 | iso3166Cache.put(iso2, gpi); |
155 | 141 | } |
156 | 142 | } |
| 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 | } |
157 | 156 | } |
| 157 | RightAndLefthandTraffic.initialize(new DefaultGeoProperty(traffic)); |
| 158 | MultipolygonCache.getInstance().clear(dataSet); |
158 | 159 | } catch (IOException | IllegalDataException ex) { |
159 | 160 | throw new JosmRuntimeException(ex); |
160 | 161 | } |
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
|
|
64 | 64 | import org.openstreetmap.josm.tools.JosmRuntimeException; |
65 | 65 | import org.openstreetmap.josm.tools.Logging; |
66 | 66 | import org.openstreetmap.josm.tools.MemoryManagerTest; |
67 | | import org.openstreetmap.josm.tools.RightAndLefthandTraffic; |
68 | 67 | import org.openstreetmap.josm.tools.Territories; |
69 | 68 | import org.openstreetmap.josm.tools.bugreport.ReportedException; |
70 | 69 | import org.openstreetmap.josm.tools.date.DateUtils; |
… |
… |
|
99 | 98 | private boolean usePresets; |
100 | 99 | private boolean useHttps; |
101 | 100 | private boolean territories; |
102 | | private boolean rlTraffic; |
103 | 101 | private boolean metric; |
104 | 102 | private boolean main; |
105 | 103 | |
… |
… |
public JOSMTestRules territories() {
|
269 | 267 | * Use right and lefthand traffic dataset in this test. |
270 | 268 | * @return this instance, for easy chaining |
271 | 269 | * @since 12556 |
| 270 | * @deprecated Use {@link #territories} |
272 | 271 | */ |
| 272 | @Deprecated |
273 | 273 | public JOSMTestRules rlTraffic() { |
274 | 274 | territories(); |
275 | | rlTraffic = true; |
276 | 275 | return this; |
277 | 276 | } |
278 | 277 | |
… |
… |
protected void before() throws InitializationError, ReflectiveOperationException
|
552 | 551 | Territories.initialize(); |
553 | 552 | } |
554 | 553 | |
555 | | if (rlTraffic) { |
556 | | RightAndLefthandTraffic.initialize(); |
557 | | } |
558 | | |
559 | 554 | if (this.edtAssertionMockingRunnable != null) { |
560 | 555 | this.edtAssertionMockingRunnable.run(); |
561 | 556 | } |