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

Last change on this file since 33311 was 33265, checked in by donvip, 8 years ago

fix #josm14386 - StackOverflowError

File size: 8.4 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<>();
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 present
134 Map<String, String> tags = p.ways.get(0).getKeys();
135 if (!p.ways.get(0).hasAreaTags()) {
136 tags.clear();
137 }
138 List<OsmPrimitive> relations = p.ways.get(0).getReferrers();
139 Set<String> noTags = new HashSet<>(r.keySet());
140 for (int i = 1; i < p.ways.size(); i++) {
141 Way w = p.ways.get(i);
142 for (String key : w.keySet()) {
143 String value = w.get(key);
144 if (!noTags.contains(key) && tags.containsKey(key) && !tags.get(key).equals(value)) {
145 tags.remove(key);
146 noTags.add(key);
147 }
148 }
149 List<OsmPrimitive> referrers = w.getReferrers();
150 for (Iterator<OsmPrimitive> ref1 = relations.iterator(); ref1.hasNext();) {
151 if (!referrers.contains(ref1.next())) {
152 ref1.remove();
153 }
154 }
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 of have strange tags)
160 Way candidateWay = null;
161 for (Way w : p.ways) {
162 if (w.getReferrers().equals(relations)) {
163 // check tags that remain
164 Set<String> keys = new HashSet<>(w.keySet());
165 keys.removeAll(tags.keySet());
166 keys.removeAll(IRRELEVANT_KEYS);
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(result) : new ChangeCommand(candidateWay, result));
190 }
191
192 // only delete the relation if it hasn't been re-used
193 if (!relationReused) {
194 commands.add(relationDeleteCommand);
195 }
196
197 Main.main.undoRedo.add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
198 r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
199 Main.getLayerManager().getEditDataSet().setSelected(newSelection);
200 }
201
202 @Override
203 public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
204 setEnabled(isSuitableRelation(newRelation));
205 }
206
207 private boolean isSuitableRelation(Relation newRelation) {
208 if (newRelation == null || !"multipolygon".equals(newRelation.get("type")) || newRelation.getMembersCount() == 0)
209 return false;
210 else
211 return true;
212 }
213}
Note: See TracBrowser for help on using the repository browser.