| 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.awt.event.KeyEvent;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.Arrays;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.Collections;
|
|---|
| 12 | import java.util.HashSet;
|
|---|
| 13 | import java.util.List;
|
|---|
| 14 | import java.util.Map;
|
|---|
| 15 | import java.util.Set;
|
|---|
| 16 |
|
|---|
| 17 | import javax.swing.JOptionPane;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 20 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 21 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 22 | import org.openstreetmap.josm.command.ChangeMembersCommand;
|
|---|
| 23 | import org.openstreetmap.josm.command.Command;
|
|---|
| 24 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 25 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 26 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.MultipolygonBuilder;
|
|---|
| 31 | import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
|
|---|
| 32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 35 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 36 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 37 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 38 |
|
|---|
| 39 | import relcontext.ChosenRelation;
|
|---|
| 40 | import relcontext.ChosenRelationListener;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Make a single polygon out of the multipolygon relation. The relation must have only outer members.
|
|---|
| 44 | * @author Zverik
|
|---|
| 45 | */
|
|---|
| 46 | public class ReconstructPolygonAction extends JosmAction implements ChosenRelationListener {
|
|---|
| 47 | private final transient ChosenRelation rel;
|
|---|
| 48 |
|
|---|
| 49 | private static final List<String> IRRELEVANT_KEYS = Arrays.asList("source", "created_by", "note");
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Reconstruct one or more polygons from multipolygon relation.
|
|---|
| 53 | * @param rel the multipolygon relation
|
|---|
| 54 | */
|
|---|
| 55 | public ReconstructPolygonAction(ChosenRelation rel) {
|
|---|
| 56 | super(tr("Reconstruct polygon"), "dialogs/filter", tr("Reconstruct polygon from multipolygon relation"),
|
|---|
| 57 | Shortcut.registerShortcut("reltoolbox:reconstructpoly", tr("Relation Toolbox: {0}",
|
|---|
| 58 | tr("Reconstruct polygon from multipolygon relation")),
|
|---|
| 59 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, false);
|
|---|
| 60 | this.rel = rel;
|
|---|
| 61 | rel.addChosenRelationListener(this);
|
|---|
| 62 | setEnabled(isSuitableRelation(rel.get()));
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | @Override
|
|---|
| 66 | public void actionPerformed(ActionEvent e) {
|
|---|
| 67 | Relation r = rel.get();
|
|---|
| 68 | boolean relationReused = false;
|
|---|
| 69 | List<Way> ways = new ArrayList<>();
|
|---|
| 70 | boolean wont = false;
|
|---|
| 71 | for (RelationMember m : r.getMembers()) {
|
|---|
| 72 | if (m.isWay()) {
|
|---|
| 73 | ways.add(m.getWay());
|
|---|
| 74 | } else {
|
|---|
| 75 | wont = true;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | if (wont) {
|
|---|
| 79 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
|---|
| 80 | tr("Multipolygon must consist only of ways"),
|
|---|
| 81 | tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 82 | return;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | MultipolygonBuilder mpc = new MultipolygonBuilder();
|
|---|
| 86 | String error = mpc.makeFromWays(ways);
|
|---|
| 87 | if (error != null) {
|
|---|
| 88 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), error);
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | rel.clear();
|
|---|
| 93 | List<Command> commands = new ArrayList<>();
|
|---|
| 94 | Command relationDeleteCommand = DeleteCommand.delete(Collections.singleton(r), true, true);
|
|---|
| 95 | if (relationDeleteCommand == null)
|
|---|
| 96 | return;
|
|---|
| 97 |
|
|---|
| 98 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
|---|
| 99 | for (JoinedPolygon p : mpc.outerWays) {
|
|---|
| 100 |
|
|---|
| 101 | ArrayList<JoinedPolygon> myInnerWays = new ArrayList<>();
|
|---|
| 102 | for (JoinedPolygon i : mpc.innerWays) {
|
|---|
| 103 | // if the first point of any inner ring is contained in this
|
|---|
| 104 | // outer ring, then this inner ring belongs to us. This
|
|---|
| 105 | // assumption only works if multipolygons have valid geometries
|
|---|
| 106 | EastNorth en = i.ways.get(0).firstNode().getEastNorth();
|
|---|
| 107 | if (p.area.contains(en.east(), en.north())) {
|
|---|
| 108 | myInnerWays.add(i);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (!myInnerWays.isEmpty()) {
|
|---|
| 113 | // this ring has inner rings, so we leave a multipolygon in
|
|---|
| 114 | // place and don't reconstruct the rings.
|
|---|
| 115 | List<RelationMember> members = new ArrayList<>();
|
|---|
| 116 | Relation n;
|
|---|
| 117 | for (Way w : p.ways) {
|
|---|
| 118 | members.add(new RelationMember("outer", w));
|
|---|
| 119 | }
|
|---|
| 120 | for (JoinedPolygon i : myInnerWays) {
|
|---|
| 121 | for (Way w : i.ways) {
|
|---|
| 122 | members.add(new RelationMember("inner", w));
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | if (relationReused) {
|
|---|
| 126 | n = new Relation();
|
|---|
| 127 | n.setKeys(r.getKeys());
|
|---|
| 128 | n.setMembers(members);
|
|---|
| 129 | commands.add(new AddCommand(ds, n));
|
|---|
| 130 | } else {
|
|---|
| 131 | relationReused = true;
|
|---|
| 132 | commands.add(new ChangeMembersCommand(r, members));
|
|---|
| 133 | }
|
|---|
| 134 | continue;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // move all tags from relation and common tags from ways
|
|---|
| 138 | // start with all tags from first way but only if area tags are present
|
|---|
| 139 | Map<String, String> tags = p.ways.get(0).getKeys();
|
|---|
| 140 | if (!p.ways.get(0).hasAreaTags()) {
|
|---|
| 141 | tags.clear();
|
|---|
| 142 | }
|
|---|
| 143 | List<OsmPrimitive> relations = p.ways.get(0).getReferrers();
|
|---|
| 144 | Set<String> noTags = new HashSet<>(r.keySet());
|
|---|
| 145 | for (int i = 1; i < p.ways.size(); i++) {
|
|---|
| 146 | Way w = p.ways.get(i);
|
|---|
| 147 | for (String key : w.keySet()) {
|
|---|
| 148 | String value = w.get(key);
|
|---|
| 149 | if (!noTags.contains(key) && tags.containsKey(key) && !tags.get(key).equals(value)) {
|
|---|
| 150 | tags.remove(key);
|
|---|
| 151 | noTags.add(key);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | List<OsmPrimitive> referrers = w.getReferrers();
|
|---|
| 155 | relations.removeIf(osmPrimitive -> !referrers.contains(osmPrimitive));
|
|---|
| 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 or have strange tags)
|
|---|
| 161 | Way candidateWay = null;
|
|---|
| 162 | for (Way w : p.ways) {
|
|---|
| 163 | if (w.getReferrers().size() == 1) {
|
|---|
| 164 | // check tags that remain
|
|---|
| 165 | Set<String> keys = new HashSet<>(w.keySet());
|
|---|
| 166 | keys.removeAll(tags.keySet());
|
|---|
| 167 | IRRELEVANT_KEYS.forEach(keys::remove);
|
|---|
| 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 | 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 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
|
|---|
| 198 | r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
|
|---|
| 199 | Collection<? extends OsmPrimitive> newSelection = UndoRedoHandler.getInstance().getLastCommand().getParticipatingPrimitives();
|
|---|
| 200 | newSelection.removeIf(p -> p.isDeleted());
|
|---|
| 201 | ds.setSelected(newSelection);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | @Override
|
|---|
| 205 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 206 | setEnabled(isSuitableRelation(newRelation));
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | private static boolean isSuitableRelation(Relation newRelation) {
|
|---|
| 210 | return newRelation != null && "multipolygon".equals(newRelation.get("type")) && newRelation.getMembersCount() != 0;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|