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 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.Map.Entry;
|
---|
17 | import java.util.Set;
|
---|
18 | import java.util.TreeSet;
|
---|
19 |
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.SwingUtilities;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.command.AddCommand;
|
---|
25 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
26 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
27 | import org.openstreetmap.josm.command.Command;
|
---|
28 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
29 | import org.openstreetmap.josm.data.osm.MultipolygonCreate;
|
---|
30 | import org.openstreetmap.josm.data.osm.MultipolygonCreate.JoinedPolygon;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
33 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
34 | import org.openstreetmap.josm.data.osm.Way;
|
---|
35 | import org.openstreetmap.josm.gui.Notification;
|
---|
36 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
---|
37 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
38 | import org.openstreetmap.josm.tools.Pair;
|
---|
39 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
40 | import org.openstreetmap.josm.tools.Utils;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Create multipolygon from selected ways automatically.
|
---|
44 | *
|
---|
45 | * New relation with type=multipolygon is created
|
---|
46 | *
|
---|
47 | * If one or more of ways is already in relation with type=multipolygon or the
|
---|
48 | * way is not closed, then error is reported and no relation is created
|
---|
49 | *
|
---|
50 | * The "inner" and "outer" roles are guessed automatically. First, bbox is
|
---|
51 | * calculated for each way. then the largest area is assumed to be outside and
|
---|
52 | * the rest inside. In cases with one "outside" area and several cut-ins, the
|
---|
53 | * guess should be always good ... In more complex (multiple outer areas) or
|
---|
54 | * buggy (inner and outer ways intersect) scenarios the result is likely to be
|
---|
55 | * wrong.
|
---|
56 | */
|
---|
57 | public class CreateMultipolygonAction extends JosmAction {
|
---|
58 |
|
---|
59 | private final boolean update;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new {@code CreateMultipolygonAction}.
|
---|
63 | */
|
---|
64 | public CreateMultipolygonAction(final boolean update) {
|
---|
65 | super(getName(update), "multipoly_create", getName(update),
|
---|
66 | update
|
---|
67 | ? null
|
---|
68 | : Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", getName(false)), KeyEvent.VK_A, Shortcut.ALT_CTRL),
|
---|
69 | true, update ? "multipoly_update" : "multipoly_create", true);
|
---|
70 | this.update = update;
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static String getName(boolean update) {
|
---|
74 | return update ? tr("Update multipolygon") : tr("Create multipolygon");
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The action button has been clicked
|
---|
79 | *
|
---|
80 | * @param e Action Event
|
---|
81 | */
|
---|
82 | @Override
|
---|
83 | public void actionPerformed(ActionEvent e) {
|
---|
84 | if (!Main.main.hasEditLayer()) {
|
---|
85 | new Notification(
|
---|
86 | tr("No data loaded."))
|
---|
87 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
88 | .setDuration(Notification.TIME_SHORT)
|
---|
89 | .show();
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | final Collection<Way> selectedWays = Main.main.getCurrentDataSet().getSelectedWays();
|
---|
94 | final Collection<Relation> selectedRelations = Main.main.getCurrentDataSet().getSelectedRelations();
|
---|
95 |
|
---|
96 | if (selectedWays.size() < 1) {
|
---|
97 | // Sometimes it make sense creating multipoly of only one way (so it will form outer way)
|
---|
98 | // and then splitting the way later (so there are multiple ways forming outer way)
|
---|
99 | new Notification(
|
---|
100 | tr("You must select at least one way."))
|
---|
101 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
102 | .setDuration(Notification.TIME_SHORT)
|
---|
103 | .show();
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | final Relation multipolygonRelation = update
|
---|
108 | ? getSelectedMultipolygonRelation(selectedWays, selectedRelations)
|
---|
109 | : null;
|
---|
110 |
|
---|
111 | // runnable to create/update multipolygon relation
|
---|
112 | final Runnable createOrUpdateMultipolygonTask = new Runnable() {
|
---|
113 | @Override
|
---|
114 | public void run() {
|
---|
115 | final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, multipolygonRelation);
|
---|
116 | if (commandAndRelation == null) {
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | final Command command = commandAndRelation.a;
|
---|
120 | final Relation relation = commandAndRelation.b;
|
---|
121 |
|
---|
122 |
|
---|
123 | // to avoid EDT violations
|
---|
124 | SwingUtilities.invokeLater(new Runnable() {
|
---|
125 | @Override
|
---|
126 | public void run() {
|
---|
127 | Main.main.undoRedo.add(command);
|
---|
128 | }
|
---|
129 | });
|
---|
130 |
|
---|
131 | // Use 'SwingUtilities.invokeLater' to make sure the relationListDialog
|
---|
132 | // knows about the new relation before we try to select it.
|
---|
133 | // (Yes, we are already in event dispatch thread. But DatasetEventManager
|
---|
134 | // uses 'SwingUtilities.invokeLater' to fire events so we have to do
|
---|
135 | // the same.)
|
---|
136 | SwingUtilities.invokeLater(new Runnable() {
|
---|
137 | @Override
|
---|
138 | public void run() {
|
---|
139 | Main.map.relationListDialog.selectRelation(relation);
|
---|
140 | if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) {
|
---|
141 | //Open relation edit window, if set up in preferences
|
---|
142 | RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null);
|
---|
143 |
|
---|
144 | editor.setModal(true);
|
---|
145 | editor.setVisible(true);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | });
|
---|
149 | }
|
---|
150 | };
|
---|
151 |
|
---|
152 | // download incomplete relation if necessary
|
---|
153 | if (multipolygonRelation != null && (multipolygonRelation.isIncomplete() || multipolygonRelation.hasIncompleteMembers())) {
|
---|
154 | Main.worker.submit(new DownloadRelationTask(Collections.singleton(multipolygonRelation), Main.main.getEditLayer()));
|
---|
155 | }
|
---|
156 | // create/update multipolygon relation
|
---|
157 | Main.worker.submit(createOrUpdateMultipolygonTask);
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | private Relation getSelectedMultipolygonRelation() {
|
---|
162 | return getSelectedMultipolygonRelation(getCurrentDataSet().getSelectedWays(), getCurrentDataSet().getSelectedRelations());
|
---|
163 | }
|
---|
164 |
|
---|
165 | private static Relation getSelectedMultipolygonRelation(Collection<Way> selectedWays, Collection<Relation> selectedRelations) {
|
---|
166 | if (selectedRelations.size() == 1 && "multipolygon".equals(selectedRelations.iterator().next().get("type"))) {
|
---|
167 | return selectedRelations.iterator().next();
|
---|
168 | } else {
|
---|
169 | final HashSet<Relation> relatedRelations = new HashSet<Relation>();
|
---|
170 | for (final Way w : selectedWays) {
|
---|
171 | relatedRelations.addAll(Utils.filteredCollection(w.getReferrers(), Relation.class));
|
---|
172 | }
|
---|
173 | return relatedRelations.size() == 1 ? relatedRelations.iterator().next() : null;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Returns a {@link Pair} of the old multipolygon {@link Relation} (or null) and the newly created/modified multipolygon {@link Relation}.
|
---|
179 | */
|
---|
180 | public static Pair<Relation, Relation> updateMultipolygonRelation(Collection<Way> selectedWays, Relation selectedMultipolygonRelation) {
|
---|
181 |
|
---|
182 | // add ways of existing relation to include them in polygon analysis
|
---|
183 | selectedWays = new HashSet<Way>(selectedWays);
|
---|
184 | selectedWays.addAll(selectedMultipolygonRelation.getMemberPrimitives(Way.class));
|
---|
185 |
|
---|
186 | final MultipolygonCreate polygon = analyzeWays(selectedWays, true);
|
---|
187 | if (polygon == null) {
|
---|
188 | return null; //could not make multipolygon.
|
---|
189 | } else {
|
---|
190 | return Pair.create(selectedMultipolygonRelation, createRelation(polygon, new Relation(selectedMultipolygonRelation)));
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Returns a {@link Pair} null and the newly created/modified multipolygon {@link Relation}.
|
---|
196 | */
|
---|
197 | public static Pair<Relation, Relation> createMultipolygonRelation(Collection<Way> selectedWays, boolean showNotif) {
|
---|
198 |
|
---|
199 | final MultipolygonCreate polygon = analyzeWays(selectedWays, showNotif);
|
---|
200 | if (polygon == null) {
|
---|
201 | return null; //could not make multipolygon.
|
---|
202 | } else {
|
---|
203 | return Pair.create(null, createRelation(polygon, new Relation()));
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Returns a pair of a multipolygon creating/modifying {@link Command} as well as the multipolygon {@link Relation}.
|
---|
209 | */
|
---|
210 | public static Pair<SequenceCommand, Relation> createMultipolygonCommand(Collection<Way> selectedWays, Relation selectedMultipolygonRelation) {
|
---|
211 |
|
---|
212 | final Pair<Relation, Relation> rr = selectedMultipolygonRelation == null
|
---|
213 | ? createMultipolygonRelation(selectedWays, true)
|
---|
214 | : updateMultipolygonRelation(selectedWays, selectedMultipolygonRelation);
|
---|
215 | if (rr == null) {
|
---|
216 | return null;
|
---|
217 | }
|
---|
218 | final Relation existingRelation = rr.a;
|
---|
219 | final Relation relation = rr.b;
|
---|
220 |
|
---|
221 | final List<Command> list = removeTagsFromWaysIfNeeded(relation);
|
---|
222 | final String commandName;
|
---|
223 | if (existingRelation == null) {
|
---|
224 | list.add(new AddCommand(relation));
|
---|
225 | commandName = getName(false);
|
---|
226 | } else {
|
---|
227 | list.add(new ChangeCommand(existingRelation, relation));
|
---|
228 | commandName = getName(true);
|
---|
229 | }
|
---|
230 | return Pair.create(new SequenceCommand(commandName, list), relation);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Enable this action only if something is selected */
|
---|
234 | @Override protected void updateEnabledState() {
|
---|
235 | if (getCurrentDataSet() == null) {
|
---|
236 | setEnabled(false);
|
---|
237 | } else {
|
---|
238 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Enable this action only if something is selected
|
---|
244 | *
|
---|
245 | * @param selection the current selection, gets tested for emptyness
|
---|
246 | */
|
---|
247 | @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
|
---|
248 | if (update) {
|
---|
249 | setEnabled(getSelectedMultipolygonRelation() != null);
|
---|
250 | } else {
|
---|
251 | setEnabled(!getCurrentDataSet().getSelectedWays().isEmpty());
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * This method analyzes ways and creates multipolygon.
|
---|
257 | * @param selectedWays list of selected ways
|
---|
258 | * @return <code>null</code>, if there was a problem with the ways.
|
---|
259 | */
|
---|
260 | private static MultipolygonCreate analyzeWays(Collection < Way > selectedWays, boolean showNotif) {
|
---|
261 |
|
---|
262 | MultipolygonCreate pol = new MultipolygonCreate();
|
---|
263 | String error = pol.makeFromWays(selectedWays);
|
---|
264 |
|
---|
265 | if (error != null) {
|
---|
266 | if (showNotif) {
|
---|
267 | new Notification(error)
|
---|
268 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
269 | .show();
|
---|
270 | }
|
---|
271 | return null;
|
---|
272 | } else {
|
---|
273 | return pol;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Builds a relation from polygon ways.
|
---|
279 | * @param pol data storage class containing polygon information
|
---|
280 | * @return multipolygon relation
|
---|
281 | */
|
---|
282 | private static Relation createRelation(MultipolygonCreate pol, final Relation rel) {
|
---|
283 | // Create new relation
|
---|
284 | rel.put("type", "multipolygon");
|
---|
285 | // Add ways to it
|
---|
286 | for (JoinedPolygon jway:pol.outerWays) {
|
---|
287 | addMembers(jway, rel, "outer");
|
---|
288 | }
|
---|
289 |
|
---|
290 | for (JoinedPolygon jway:pol.innerWays) {
|
---|
291 | addMembers(jway, rel, "inner");
|
---|
292 | }
|
---|
293 | return rel;
|
---|
294 | }
|
---|
295 |
|
---|
296 | private static void addMembers(JoinedPolygon polygon, Relation rel, String role) {
|
---|
297 | final int count = rel.getMembersCount();
|
---|
298 | final HashSet<Way> ways = new HashSet<Way>(polygon.ways);
|
---|
299 | for (int i = 0; i < count; i++) {
|
---|
300 | final RelationMember m = rel.getMember(i);
|
---|
301 | if (ways.contains(m.getMember()) && !role.equals(m.getRole())) {
|
---|
302 | rel.setMember(i, new RelationMember(role, m.getMember()));
|
---|
303 | }
|
---|
304 | }
|
---|
305 | ways.removeAll(rel.getMemberPrimitives());
|
---|
306 | for (final Way way : ways) {
|
---|
307 | rel.addMember(new RelationMember(role, way));
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | static public final List<String> DEFAULT_LINEAR_TAGS = Arrays.asList(new String[] {"barrier", "source"});
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * This method removes tags/value pairs from inner and outer ways and put them on relation if necessary
|
---|
315 | * Function was extended in reltoolbox plugin by Zverikk and copied back to the core
|
---|
316 | * @param relation the multipolygon style relation to process
|
---|
317 | * @return a list of commands to execute
|
---|
318 | */
|
---|
319 | private static List<Command> removeTagsFromWaysIfNeeded( Relation relation ) {
|
---|
320 | Map<String, String> values = new HashMap<String, String>();
|
---|
321 |
|
---|
322 | if( relation.hasKeys() ) {
|
---|
323 | for( String key : relation.keySet() ) {
|
---|
324 | values.put(key, relation.get(key));
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | List<Way> innerWays = new ArrayList<Way>();
|
---|
329 | List<Way> outerWays = new ArrayList<Way>();
|
---|
330 |
|
---|
331 | Set<String> conflictingKeys = new TreeSet<String>();
|
---|
332 |
|
---|
333 | for( RelationMember m : relation.getMembers() ) {
|
---|
334 |
|
---|
335 | if( m.hasRole() && "inner".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys() ) {
|
---|
336 | innerWays.add(m.getWay());
|
---|
337 | }
|
---|
338 |
|
---|
339 | if( m.hasRole() && "outer".equals(m.getRole()) && m.isWay() && m.getWay().hasKeys() ) {
|
---|
340 | Way way = m.getWay();
|
---|
341 | outerWays.add(way);
|
---|
342 |
|
---|
343 | for( String key : way.keySet() ) {
|
---|
344 | if( !values.containsKey(key) ) { //relation values take precedence
|
---|
345 | values.put(key, way.get(key));
|
---|
346 | } else if( !relation.hasKey(key) && !values.get(key).equals(way.get(key)) ) {
|
---|
347 | conflictingKeys.add(key);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | // filter out empty key conflicts - we need second iteration
|
---|
354 | if( !Main.pref.getBoolean("multipoly.alltags", false) )
|
---|
355 | for( RelationMember m : relation.getMembers() )
|
---|
356 | if( m.hasRole() && m.getRole().equals("outer") && m.isWay() )
|
---|
357 | for( String key : values.keySet() )
|
---|
358 | if( !m.getWay().hasKey(key) && !relation.hasKey(key) )
|
---|
359 | conflictingKeys.add(key);
|
---|
360 |
|
---|
361 | for( String key : conflictingKeys )
|
---|
362 | values.remove(key);
|
---|
363 |
|
---|
364 | for( String linearTag : Main.pref.getCollection("multipoly.lineartagstokeep", DEFAULT_LINEAR_TAGS) )
|
---|
365 | values.remove(linearTag);
|
---|
366 |
|
---|
367 | if( values.containsKey("natural") && values.get("natural").equals("coastline") )
|
---|
368 | values.remove("natural");
|
---|
369 |
|
---|
370 | values.put("area", "yes");
|
---|
371 |
|
---|
372 | List<Command> commands = new ArrayList<Command>();
|
---|
373 | boolean moveTags = Main.pref.getBoolean("multipoly.movetags", true);
|
---|
374 |
|
---|
375 | for (Entry<String, String> entry : values.entrySet()) {
|
---|
376 | List<OsmPrimitive> affectedWays = new ArrayList<OsmPrimitive>();
|
---|
377 | String key = entry.getKey();
|
---|
378 | String value = entry.getValue();
|
---|
379 |
|
---|
380 | for (Way way : innerWays) {
|
---|
381 | if (way.hasKey(key) && (value.equals(way.get(key)))) {
|
---|
382 | affectedWays.add(way);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | if (moveTags) {
|
---|
387 | // remove duplicated tags from outer ways
|
---|
388 | for( Way way : outerWays ) {
|
---|
389 | if( way.hasKey(key) ) {
|
---|
390 | affectedWays.add(way);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (!affectedWays.isEmpty()) {
|
---|
396 | // reset key tag on affected ways
|
---|
397 | commands.add(new ChangePropertyCommand(affectedWays, key, null));
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | if (moveTags) {
|
---|
402 | // add those tag values to the relation
|
---|
403 |
|
---|
404 | boolean fixed = false;
|
---|
405 | Relation r2 = new Relation(relation);
|
---|
406 | for (Entry<String, String> entry : values.entrySet()) {
|
---|
407 | String key = entry.getKey();
|
---|
408 | if (!r2.hasKey(key) && !key.equals("area") ) {
|
---|
409 | if (relation.isNew())
|
---|
410 | relation.put(key, entry.getValue());
|
---|
411 | else
|
---|
412 | r2.put(key, entry.getValue());
|
---|
413 | fixed = true;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | if (fixed && !relation.isNew())
|
---|
417 | commands.add(new ChangeCommand(relation, r2));
|
---|
418 | }
|
---|
419 |
|
---|
420 | return commands;
|
---|
421 | }
|
---|
422 | }
|
---|