source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/ReconstructPolygonAction.java@ 36142

Last change on this file since 36142 was 36142, checked in by taylor.smock, 2 years ago

See #23170: Better behavior around undo/redo, only delete ways with one referrer

File size: 8.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package relcontext.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Map;
14import java.util.Set;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.command.AddCommand;
20import org.openstreetmap.josm.command.ChangeCommand;
21import org.openstreetmap.josm.command.Command;
22import org.openstreetmap.josm.command.DeleteCommand;
23import org.openstreetmap.josm.command.SequenceCommand;
24import org.openstreetmap.josm.data.UndoRedoHandler;
25import org.openstreetmap.josm.data.coor.EastNorth;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
28import org.openstreetmap.josm.data.osm.MultipolygonBuilder;
29import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
30import org.openstreetmap.josm.data.osm.OsmPrimitive;
31import org.openstreetmap.josm.data.osm.Relation;
32import org.openstreetmap.josm.data.osm.RelationMember;
33import org.openstreetmap.josm.data.osm.Way;
34import org.openstreetmap.josm.gui.MainApplication;
35import org.openstreetmap.josm.tools.Shortcut;
36
37import relcontext.ChosenRelation;
38import relcontext.ChosenRelationListener;
39
40/**
41 * Make a single polygon out of the multipolygon relation. The relation must have only outer members.
42 * @author Zverik
43 */
44public class ReconstructPolygonAction extends JosmAction implements ChosenRelationListener {
45 private final ChosenRelation rel;
46
47 private static final List<String> IRRELEVANT_KEYS = Arrays.asList("source", "created_by", "note");
48
49 public ReconstructPolygonAction(ChosenRelation rel) {
50 super(tr("Reconstruct polygon"), "dialogs/filter", tr("Reconstruct polygon from multipolygon relation"),
51 Shortcut.registerShortcut("reltoolbox:reconstructpoly", tr("Relation Toolbox: {0}",
52 tr("Reconstruct polygon from multipolygon relation")),
53 KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
54 this.rel = rel;
55 rel.addChosenRelationListener(this);
56 setEnabled(isSuitableRelation(rel.get()));
57 }
58
59 @Override
60 public void actionPerformed(ActionEvent e) {
61 Relation r = rel.get();
62 boolean relationReused = false;
63 List<Way> ways = new ArrayList<>();
64 boolean wont = false;
65 for (RelationMember m : r.getMembers()) {
66 if (m.isWay()) {
67 ways.add(m.getWay());
68 } else {
69 wont = true;
70 }
71 }
72 if (wont) {
73 JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
74 tr("Multipolygon must consist only of ways with one referring relation"),
75 tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
76 return;
77 }
78
79 MultipolygonBuilder mpc = new MultipolygonBuilder();
80 String error = mpc.makeFromWays(ways);
81 if (error != null) {
82 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), error);
83 return;
84 }
85
86 rel.clear();
87 List<OsmPrimitive> newSelection = new ArrayList<>();
88 List<Command> commands = new ArrayList<>();
89 Command relationDeleteCommand = DeleteCommand.delete(Collections.singleton(r), true, true);
90 if (relationDeleteCommand == null)
91 return;
92
93 DataSet ds = MainApplication.getLayerManager().getEditDataSet();
94 for (JoinedPolygon p : mpc.outerWays) {
95
96 ArrayList<JoinedPolygon> myInnerWays = new ArrayList<>();
97 for (JoinedPolygon i : mpc.innerWays) {
98 // if the first point of any inner ring is contained in this
99 // outer ring, then this inner ring belongs to us. This
100 // assumption only works if multipolygons have valid geometries
101 EastNorth en = i.ways.get(0).firstNode().getEastNorth();
102 if (p.area.contains(en.east(), en.north())) {
103 myInnerWays.add(i);
104 }
105 }
106
107 if (!myInnerWays.isEmpty()) {
108 // this ring has inner rings, so we leave a multipolygon in
109 // place and don't reconstruct the rings.
110 Relation n;
111 if (relationReused) {
112 n = new Relation();
113 n.setKeys(r.getKeys());
114 } else {
115 n = new Relation(r);
116 n.setMembers(null);
117 }
118 for (Way w : p.ways) {
119 n.addMember(new RelationMember("outer", w));
120 }
121 for (JoinedPolygon i : myInnerWays) {
122 for (Way w : i.ways) {
123 n.addMember(new RelationMember("inner", w));
124 }
125 }
126 if (relationReused) {
127 commands.add(new AddCommand(ds, n));
128 } else {
129 relationReused = true;
130 commands.add(new ChangeCommand(r, n));
131 }
132 newSelection.add(n);
133 continue;
134 }
135
136 // move all tags from relation and common tags from ways
137 // start with all tags from first way but only if area tags are present
138 Map<String, String> tags = p.ways.get(0).getKeys();
139 if (!p.ways.get(0).hasAreaTags()) {
140 tags.clear();
141 }
142 List<OsmPrimitive> relations = p.ways.get(0).getReferrers();
143 Set<String> noTags = new HashSet<>(r.keySet());
144 for (int i = 1; i < p.ways.size(); i++) {
145 Way w = p.ways.get(i);
146 for (String key : w.keySet()) {
147 String value = w.get(key);
148 if (!noTags.contains(key) && tags.containsKey(key) && !tags.get(key).equals(value)) {
149 tags.remove(key);
150 noTags.add(key);
151 }
152 }
153 List<OsmPrimitive> referrers = w.getReferrers();
154 relations.removeIf(osmPrimitive -> !referrers.contains(osmPrimitive));
155 }
156 tags.putAll(r.getKeys());
157 tags.remove("type");
158
159 // then delete ways that are not relevant (do not take part in other relations or have strange tags)
160 Way candidateWay = null;
161 for (Way w : p.ways) {
162 if (w.getReferrers().size() == 1) {
163 // check tags that remain
164 Set<String> keys = new HashSet<>(w.keySet());
165 keys.removeAll(tags.keySet());
166 IRRELEVANT_KEYS.forEach(keys::remove);
167 if (keys.isEmpty()) {
168 if (candidateWay == null) {
169 candidateWay = w;
170 } else {
171 if (candidateWay.isNew() && !w.isNew()) {
172 // prefer ways that are already in the database
173 Way tmp = w;
174 w = candidateWay;
175 candidateWay = tmp;
176 }
177 commands.add(new DeleteCommand(w));
178 }
179 }
180 }
181 }
182
183 // take the first way, put all nodes into it, making it a closed polygon
184 Way result = candidateWay == null ? new Way() : new Way(candidateWay);
185 result.setNodes(p.nodes);
186 result.addNode(result.firstNode());
187 result.setKeys(tags);
188 newSelection.add(candidateWay == null ? result : candidateWay);
189 commands.add(candidateWay == null ? new AddCommand(ds, result) : new ChangeCommand(candidateWay, result));
190 }
191
192 // only delete the relation if it hasn't been re-used
193 if (!relationReused) {
194 // The relation needs to be deleted first, so that undo/redo continue to work properly
195 commands.add(0, relationDeleteCommand);
196 }
197
198 UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
199 r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
200 ds.setSelected(newSelection);
201 }
202
203 @Override
204 public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
205 setEnabled(isSuitableRelation(newRelation));
206 }
207
208 private static boolean isSuitableRelation(Relation newRelation) {
209 return newRelation != null && "multipolygon".equals(newRelation.get("type")) && newRelation.getMembersCount() != 0;
210 }
211}
Note: See TracBrowser for help on using the repository browser.