1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.LinkedHashSet;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.Set;
|
---|
21 | import java.util.TreeSet;
|
---|
22 | import java.util.stream.Collectors;
|
---|
23 |
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 | import javax.swing.SwingUtilities;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
|
---|
28 | import org.openstreetmap.josm.command.AddCommand;
|
---|
29 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
30 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
31 | import org.openstreetmap.josm.command.Command;
|
---|
32 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
33 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
34 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
35 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
36 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
37 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
38 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
39 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
40 | import org.openstreetmap.josm.data.osm.Way;
|
---|
41 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
42 | import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
|
---|
43 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
44 | import org.openstreetmap.josm.gui.Notification;
|
---|
45 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
46 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
---|
47 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
48 | import org.openstreetmap.josm.gui.dialogs.relation.sort.RelationSorter;
|
---|
49 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
50 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
51 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
52 | import org.openstreetmap.josm.tools.Pair;
|
---|
53 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
54 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
55 | import org.openstreetmap.josm.tools.Utils;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Create multipolygon from selected ways automatically.
|
---|
59 | *
|
---|
60 | * New relation with type=multipolygon is created.
|
---|
61 | *
|
---|
62 | * If one or more of ways is already in relation with type=multipolygon or the
|
---|
63 | * way is not closed, then error is reported and no relation is created.
|
---|
64 | *
|
---|
65 | * The "inner" and "outer" roles are guessed automatically. First, bbox is
|
---|
66 | * calculated for each way. then the largest area is assumed to be outside and
|
---|
67 | * the rest inside. In cases with one "outside" area and several cut-ins, the
|
---|
68 | * guess should be always good ... In more complex (multiple outer areas) or
|
---|
69 | * buggy (inner and outer ways intersect) scenarios the result is likely to be
|
---|
70 | * wrong.
|
---|
71 | */
|
---|
72 | public class CreateMultipolygonAction extends JosmAction {
|
---|
73 |
|
---|
74 | private final boolean update;
|
---|
75 | private static final int MAX_MEMBERS_TO_DOWNLOAD = 100;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Constructs a new {@code CreateMultipolygonAction}.
|
---|
79 | * @param update {@code true} if the multipolygon must be updated, {@code false} if it must be created
|
---|
80 | */
|
---|
81 | public CreateMultipolygonAction(final boolean update) {
|
---|
82 | super(getName(update), /* ICON */ "multipoly_create", getName(update),
|
---|
83 | /* at least three lines for each shortcut or the server extractor fails */
|
---|
84 | update ? Shortcut.registerShortcut("tools:multipoly_update",
|
---|
85 | tr("Tool: {0}", getName(true)),
|
---|
86 | KeyEvent.VK_B, Shortcut.CTRL_SHIFT)
|
---|
87 | : Shortcut.registerShortcut("tools:multipoly_create",
|
---|
88 | tr("Tool: {0}", getName(false)),
|
---|
89 | KeyEvent.VK_B, Shortcut.CTRL),
|
---|
90 | true, update ? "multipoly_update" : "multipoly_create", true);
|
---|
91 | this.update = update;
|
---|
92 | }
|
---|
93 |
|
---|
94 | private static String getName(boolean update) {
|
---|
95 | return update ? tr("Update multipolygon") : tr("Create multipolygon");
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static final class CreateUpdateMultipolygonTask implements Runnable {
|
---|
99 | private final Collection<Way> selectedWays;
|
---|
100 | private final Relation multipolygonRelation;
|
---|
101 |
|
---|
102 | private CreateUpdateMultipolygonTask(Collection<Way> selectedWays, Relation multipolygonRelation) {
|
---|
103 | this.selectedWays = selectedWays;
|
---|
104 | this.multipolygonRelation = multipolygonRelation;
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public void run() {
|
---|
109 | final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, multipolygonRelation);
|
---|
110 | if (commandAndRelation == null) {
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | final Command command = commandAndRelation.a;
|
---|
114 | final Relation relation = commandAndRelation.b;
|
---|
115 |
|
---|
116 | // to avoid EDT violations
|
---|
117 | SwingUtilities.invokeLater(() -> {
|
---|
118 | UndoRedoHandler.getInstance().add(command);
|
---|
119 |
|
---|
120 | // Use 'SwingUtilities.invokeLater' to make sure the relationListDialog
|
---|
121 | // knows about the new relation before we try to select it.
|
---|
122 | // (Yes, we are already in event dispatch thread. But DatasetEventManager
|
---|
123 | // uses 'SwingUtilities.invokeLater' to fire events so we have to do the same.)
|
---|
124 | SwingUtilities.invokeLater(() -> {
|
---|
125 | MainApplication.getMap().relationListDialog.selectRelation(relation);
|
---|
126 | if (Config.getPref().getBoolean("multipoly.show-relation-editor", false)) {
|
---|
127 | //Open relation edit window, if set up in preferences
|
---|
128 | RelationEditor editor = RelationEditor.getEditor(
|
---|
129 | MainApplication.getLayerManager().getEditLayer(), relation, null);
|
---|
130 | editor.setModal(true);
|
---|
131 | editor.setVisible(true);
|
---|
132 | } else {
|
---|
133 | MainApplication.getLayerManager().getEditLayer().setRecentRelation(relation);
|
---|
134 | }
|
---|
135 | });
|
---|
136 | });
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void actionPerformed(ActionEvent e) {
|
---|
142 | DataSet dataSet = getLayerManager().getEditDataSet();
|
---|
143 | if (dataSet == null) {
|
---|
144 | new Notification(
|
---|
145 | tr("No data loaded."))
|
---|
146 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
147 | .setDuration(Notification.TIME_SHORT)
|
---|
148 | .show();
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | final Collection<Way> selectedWays = dataSet.getSelectedWays();
|
---|
153 |
|
---|
154 | if (selectedWays.isEmpty()) {
|
---|
155 | // Sometimes it make sense creating multipoly of only one way (so it will form outer way)
|
---|
156 | // and then splitting the way later (so there are multiple ways forming outer way)
|
---|
157 | new Notification(
|
---|
158 | tr("You must select at least one way."))
|
---|
159 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
160 | .setDuration(Notification.TIME_SHORT)
|
---|
161 | .show();
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | final Collection<Relation> selectedRelations = dataSet.getSelectedRelations();
|
---|
166 | final Relation multipolygonRelation = update
|
---|
167 | ? getSelectedMultipolygonRelation(selectedWays, selectedRelations)
|
---|
168 | : null;
|
---|
169 |
|
---|
170 | if (update && multipolygonRelation == null)
|
---|
171 | return;
|
---|
172 | // download incomplete relation or incomplete members if necessary
|
---|
173 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
---|
174 | if (multipolygonRelation != null && editLayer != null && editLayer.isDownloadable()) {
|
---|
175 | if (!multipolygonRelation.isNew() && multipolygonRelation.isIncomplete()) {
|
---|
176 | MainApplication.worker
|
---|
177 | .submit(new DownloadRelationTask(Collections.singleton(multipolygonRelation), editLayer));
|
---|
178 | } else if (multipolygonRelation.hasIncompleteMembers()) {
|
---|
179 | // with complex relations the download of the full relation is much faster than download of almost all members, see #18341
|
---|
180 | SubclassFilteredCollection<IPrimitive, OsmPrimitive> incompleteMembers = Utils
|
---|
181 | .filteredCollection(DownloadSelectedIncompleteMembersAction.buildSetOfIncompleteMembers(
|
---|
182 | Collections.singleton(multipolygonRelation)), OsmPrimitive.class);
|
---|
183 |
|
---|
184 | if (incompleteMembers.size() <= MAX_MEMBERS_TO_DOWNLOAD) {
|
---|
185 | MainApplication.worker
|
---|
186 | .submit(new DownloadRelationMemberTask(multipolygonRelation, incompleteMembers, editLayer));
|
---|
187 | } else {
|
---|
188 | MainApplication.worker
|
---|
189 | .submit(new DownloadRelationTask(Collections.singleton(multipolygonRelation), editLayer));
|
---|
190 |
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | // create/update multipolygon relation
|
---|
195 | MainApplication.worker.submit(new CreateUpdateMultipolygonTask(selectedWays, multipolygonRelation));
|
---|
196 | }
|
---|
197 |
|
---|
198 | private static Relation getSelectedMultipolygonRelation(Collection<Way> selectedWays, Collection<Relation> selectedRelations) {
|
---|
199 | Relation candidate = null;
|
---|
200 | if (selectedRelations.size() == 1) {
|
---|
201 | candidate = selectedRelations.iterator().next();
|
---|
202 | if (!candidate.hasTag("type", "multipolygon"))
|
---|
203 | candidate = null;
|
---|
204 | } else if (!selectedWays.isEmpty()) {
|
---|
205 | for (final Way w : selectedWays) {
|
---|
206 | for (OsmPrimitive r : w.getReferrers()) {
|
---|
207 | if (r != candidate && !r.isDisabled() && r instanceof Relation && r.hasTag("type", "multipolygon")) {
|
---|
208 | if (candidate != null)
|
---|
209 | return null; // found another multipolygon relation
|
---|
210 | candidate = (Relation) r;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | return candidate;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Returns a {@link Pair} of the old multipolygon {@link Relation} (or null) and the newly created/modified multipolygon {@link Relation}.
|
---|
220 | * @param selectedWays selected ways
|
---|
221 | * @param selectedMultipolygonRelation selected multipolygon relation
|
---|
222 | * @return pair of old and new multipolygon relation if a difference was found, else the pair contains the old relation twice
|
---|
223 | */
|
---|
224 | public static Pair<Relation, Relation> updateMultipolygonRelation(Collection<Way> selectedWays, Relation selectedMultipolygonRelation) {
|
---|
225 |
|
---|
226 | // add ways of existing relation to include them in polygon analysis
|
---|
227 | Set<Way> ways = new HashSet<>(selectedWays);
|
---|
228 | ways.addAll(selectedMultipolygonRelation.getMemberPrimitives(Way.class));
|
---|
229 |
|
---|
230 | // even if no way was added the inner/outer roles might be different
|
---|
231 | MultipolygonTest mpTest = new MultipolygonTest();
|
---|
232 | Relation calculated = mpTest.makeFromWays(ways);
|
---|
233 | if (mpTest.getErrors().isEmpty()) {
|
---|
234 | return mergeRelationsMembers(selectedMultipolygonRelation, calculated);
|
---|
235 | }
|
---|
236 | showErrors(mpTest.getErrors());
|
---|
237 | return null; //could not make multipolygon.
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Merge members of multipolygon relation. Maintains the order of the old relation. May change roles,
|
---|
242 | * removes duplicate and non-way members and adds new members found in {@code calculated}.
|
---|
243 | * @param old old multipolygon relation
|
---|
244 | * @param calculated calculated multipolygon relation
|
---|
245 | * @return pair of old and new multipolygon relation if a difference was found, else the pair contains the old relation twice
|
---|
246 | */
|
---|
247 | private static Pair<Relation, Relation> mergeRelationsMembers(Relation old, Relation calculated) {
|
---|
248 | Set<RelationMember> merged = new LinkedHashSet<>();
|
---|
249 | boolean foundDiff = false;
|
---|
250 | int nonWayMember = 0;
|
---|
251 | // maintain order of members in updated relation
|
---|
252 | for (RelationMember oldMem :old.getMembers()) {
|
---|
253 | if (oldMem.isNode() || oldMem.isRelation()) {
|
---|
254 | nonWayMember++;
|
---|
255 | continue;
|
---|
256 | }
|
---|
257 | for (RelationMember newMem : calculated.getMembers()) {
|
---|
258 | if (newMem.getMember().equals(oldMem.getMember())) {
|
---|
259 | if (!newMem.getRole().equals(oldMem.getRole())) {
|
---|
260 | foundDiff = true;
|
---|
261 | }
|
---|
262 | foundDiff |= !merged.add(newMem); // detect duplicate members in old relation
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | if (nonWayMember > 0) {
|
---|
268 | foundDiff = true;
|
---|
269 | String msg = trn("Non-Way member removed from multipolygon", "Non-Way members removed from multipolygon", nonWayMember);
|
---|
270 | GuiHelper.runInEDT(() -> new Notification(msg).setIcon(JOptionPane.WARNING_MESSAGE).show());
|
---|
271 | }
|
---|
272 | foundDiff |= merged.addAll(calculated.getMembers());
|
---|
273 | if (!foundDiff) {
|
---|
274 | return Pair.create(old, old); // unchanged
|
---|
275 | }
|
---|
276 | Relation toModify = new Relation(old);
|
---|
277 | toModify.setMembers(new ArrayList<>(merged));
|
---|
278 | return Pair.create(old, toModify);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Returns a {@link Pair} null and the newly created/modified multipolygon {@link Relation}.
|
---|
283 | * @param selectedWays selected ways
|
---|
284 | * @param showNotif if {@code true}, shows a notification if an error occurs
|
---|
285 | * @return pair of null and new multipolygon relation
|
---|
286 | */
|
---|
287 | public static Pair<Relation, Relation> createMultipolygonRelation(Collection<Way> selectedWays, boolean showNotif) {
|
---|
288 | MultipolygonTest mpTest = new MultipolygonTest();
|
---|
289 | Relation calculated = mpTest.makeFromWays(selectedWays);
|
---|
290 | calculated.setMembers(RelationSorter.sortMembersByConnectivity(calculated.getMembers()));
|
---|
291 | if (mpTest.getErrors().isEmpty())
|
---|
292 | return Pair.create(null, calculated);
|
---|
293 | if (showNotif) {
|
---|
294 | showErrors(mpTest.getErrors());
|
---|
295 | }
|
---|
296 | return null; //could not make multipolygon.
|
---|
297 | }
|
---|
298 |
|
---|
299 | private static void showErrors(List<TestError> errors) {
|
---|
300 | if (!errors.isEmpty()) {
|
---|
301 | StringBuilder sb = new StringBuilder();
|
---|
302 | Set<String> errorMessages = new LinkedHashSet<>();
|
---|
303 | errors.forEach(e-> errorMessages.add(e.getMessage()));
|
---|
304 | Iterator<String> iter = errorMessages.iterator();
|
---|
305 | while (iter.hasNext()) {
|
---|
306 | sb.append(iter.next());
|
---|
307 | if (iter.hasNext())
|
---|
308 | sb.append('\n');
|
---|
309 | }
|
---|
310 | GuiHelper.runInEDT(() -> new Notification(sb.toString()).setIcon(JOptionPane.INFORMATION_MESSAGE).show());
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Returns a {@link Pair} of a multipolygon creating/modifying {@link Command} as well as the multipolygon {@link Relation}.
|
---|
316 | * @param selectedWays selected ways
|
---|
317 | * @param selectedMultipolygonRelation selected multipolygon relation
|
---|
318 | * @return pair of command and multipolygon relation
|
---|
319 | */
|
---|
320 | public static Pair<SequenceCommand, Relation> createMultipolygonCommand(Collection<Way> selectedWays,
|
---|
321 | Relation selectedMultipolygonRelation) {
|
---|
322 |
|
---|
323 | final Pair<Relation, Relation> rr = selectedMultipolygonRelation == null
|
---|
324 | ? createMultipolygonRelation(selectedWays, true)
|
---|
325 | : updateMultipolygonRelation(selectedWays, selectedMultipolygonRelation);
|
---|
326 | if (rr == null) {
|
---|
327 | return null;
|
---|
328 | }
|
---|
329 | boolean unchanged = rr.a == rr.b;
|
---|
330 | final Relation existingRelation = rr.a;
|
---|
331 | final Relation relation = rr.b;
|
---|
332 |
|
---|
333 | final List<Command> list = removeTagsFromWaysIfNeeded(relation);
|
---|
334 | final String commandName;
|
---|
335 | if (existingRelation == null) {
|
---|
336 | list.add(new AddCommand(selectedWays.iterator().next().getDataSet(), relation));
|
---|
337 | commandName = getName(false);
|
---|
338 | } else {
|
---|
339 | if (!unchanged) {
|
---|
340 | list.add(new ChangeCommand(existingRelation, relation));
|
---|
341 | }
|
---|
342 | if (list.isEmpty()) {
|
---|
343 | if (unchanged) {
|
---|
344 | MultipolygonTest mpTest = new MultipolygonTest();
|
---|
345 | mpTest.visit(existingRelation);
|
---|
346 | if (!mpTest.getErrors().isEmpty()) {
|
---|
347 | showErrors(mpTest.getErrors());
|
---|
348 | return null;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | GuiHelper.runInEDT(() -> new Notification(tr("Nothing changed")).setDuration(Notification.TIME_SHORT)
|
---|
353 | .setIcon(JOptionPane.INFORMATION_MESSAGE).show());
|
---|
354 | return null;
|
---|
355 | }
|
---|
356 | commandName = getName(true);
|
---|
357 | }
|
---|
358 | return Pair.create(new SequenceCommand(commandName, list), relation);
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Enable this action only if something is selected */
|
---|
362 | @Override
|
---|
363 | protected void updateEnabledState() {
|
---|
364 | updateEnabledStateOnCurrentSelection();
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Enable this action only if something is selected
|
---|
369 | *
|
---|
370 | * @param selection the current selection, gets tested for emptiness
|
---|
371 | */
|
---|
372 | @Override
|
---|
373 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
374 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
375 | if (ds == null || selection.isEmpty()) {
|
---|
376 | setEnabled(false);
|
---|
377 | } else if (update) {
|
---|
378 | setEnabled(getSelectedMultipolygonRelation(ds.getSelectedWays(), ds.getSelectedRelations()) != null);
|
---|
379 | } else {
|
---|
380 | setEnabled(!ds.getSelectedWays().isEmpty());
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | private static final List<String> DEFAULT_LINEAR_TAGS = Arrays.asList("barrier", "fence_type", "source");
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * This method removes tags/value pairs from inner and outer ways and put them on relation if necessary
|
---|
388 | * Function was extended in reltoolbox plugin by Zverikk and copied back to the core
|
---|
389 | * @param relation the multipolygon style relation to process
|
---|
390 | * @return a list of commands to execute
|
---|
391 | */
|
---|
392 | public static List<Command> removeTagsFromWaysIfNeeded(Relation relation) {
|
---|
393 | Map<String, String> values = new HashMap<>(relation.getKeys());
|
---|
394 |
|
---|
395 | List<Way> innerWays = new ArrayList<>();
|
---|
396 | List<Way> outerWays = new ArrayList<>();
|
---|
397 |
|
---|
398 | Set<String> conflictingKeys = new TreeSet<>();
|
---|
399 |
|
---|
400 | for (RelationMember m : relation.getMembers()) {
|
---|
401 |
|
---|
402 | if (m.hasRole() && "inner".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys()) {
|
---|
403 | innerWays.add(m.getWay());
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (m.hasRole() && "outer".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys()) {
|
---|
407 | Way way = m.getWay();
|
---|
408 | outerWays.add(way);
|
---|
409 |
|
---|
410 | for (String key : way.keySet()) {
|
---|
411 | if (!values.containsKey(key)) { //relation values take precedence
|
---|
412 | values.put(key, way.get(key));
|
---|
413 | } else if (!relation.hasKey(key) && !values.get(key).equals(way.get(key))) {
|
---|
414 | conflictingKeys.add(key);
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | // filter out empty key conflicts - we need second iteration
|
---|
421 | if (!Config.getPref().getBoolean("multipoly.alltags", false)) {
|
---|
422 | for (RelationMember m : relation.getMembers()) {
|
---|
423 | if (m.hasRole() && "outer".equals(m.getRole()) && m.isWay()) {
|
---|
424 | for (String key : values.keySet()) {
|
---|
425 | if (!m.getWay().hasKey(key) && !relation.hasKey(key)) {
|
---|
426 | conflictingKeys.add(key);
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | for (String key : conflictingKeys) {
|
---|
434 | values.remove(key);
|
---|
435 | }
|
---|
436 |
|
---|
437 | for (String linearTag : Config.getPref().getList("multipoly.lineartagstokeep", DEFAULT_LINEAR_TAGS)) {
|
---|
438 | values.remove(linearTag);
|
---|
439 | }
|
---|
440 |
|
---|
441 | if ("coastline".equals(values.get("natural")))
|
---|
442 | values.remove("natural");
|
---|
443 |
|
---|
444 | values.put("area", OsmUtils.TRUE_VALUE);
|
---|
445 |
|
---|
446 | List<Command> commands = new ArrayList<>();
|
---|
447 | boolean moveTags = Config.getPref().getBoolean("multipoly.movetags", true);
|
---|
448 |
|
---|
449 | for (Entry<String, String> entry : values.entrySet()) {
|
---|
450 | String key = entry.getKey();
|
---|
451 | String value = entry.getValue();
|
---|
452 | List<OsmPrimitive> affectedWays = innerWays.stream().filter(way -> value.equals(way.get(key))).collect(Collectors.toList());
|
---|
453 |
|
---|
454 | if (moveTags) {
|
---|
455 | // remove duplicated tags from outer ways
|
---|
456 | for (Way way : outerWays) {
|
---|
457 | if (way.hasKey(key)) {
|
---|
458 | affectedWays.add(way);
|
---|
459 | }
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | if (!affectedWays.isEmpty()) {
|
---|
464 | // reset key tag on affected ways
|
---|
465 | commands.add(new ChangePropertyCommand(affectedWays, key, null));
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | if (moveTags) {
|
---|
470 | // add those tag values to the relation
|
---|
471 | boolean fixed = false;
|
---|
472 | Relation r2 = new Relation(relation);
|
---|
473 | for (Entry<String, String> entry : values.entrySet()) {
|
---|
474 | String key = entry.getKey();
|
---|
475 | if (!r2.hasKey(key) && !"area".equals(key)) {
|
---|
476 | if (relation.isNew())
|
---|
477 | relation.put(key, entry.getValue());
|
---|
478 | else
|
---|
479 | r2.put(key, entry.getValue());
|
---|
480 | fixed = true;
|
---|
481 | }
|
---|
482 | }
|
---|
483 | if (fixed && !relation.isNew()) {
|
---|
484 | DataSet ds = relation.getDataSet();
|
---|
485 | if (ds == null) {
|
---|
486 | ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
487 | }
|
---|
488 | commands.add(new ChangeCommand(ds, relation, r2));
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | return commands;
|
---|
493 | }
|
---|
494 | }
|
---|