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

Last change on this file since 34130 was 33694, checked in by donvip, 8 years ago

fix #josm15320

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