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

Last change on this file since 33060 was 33060, checked in by frederik, 9 years ago

Make "reconstruct relation" option available for polygons with inner rings. Any outer ring that has an inner ring will be placed in a relation of its own and not reconstructed. This allows the reconstruction of large multipolygon relations with multiple outer rings, only some of which have an inner ring - at least those without can be turned into a way.

Also, copy tags from the first way only if that way has area tags, fixing a bug where highway or waterway tags would be copied from the first way onto the relation.

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