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