| [32395] | 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| [25670] | 2 | package relcontext.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| [30587] | 5 |
|
|---|
| [25670] | 6 | import java.awt.event.ActionEvent;
|
|---|
| [35602] | 7 | import java.awt.event.KeyEvent;
|
|---|
| [30587] | 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.Arrays;
|
|---|
| [36217] | 10 | import java.util.Collection;
|
|---|
| [30587] | 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 |
|
|---|
| [26882] | 17 | import javax.swing.JOptionPane;
|
|---|
| [30587] | 18 |
|
|---|
| [35602] | 19 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| [30587] | 20 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 21 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| [36217] | 22 | import org.openstreetmap.josm.command.ChangeMembersCommand;
|
|---|
| [30587] | 23 | import org.openstreetmap.josm.command.Command;
|
|---|
| 24 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 25 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| [34551] | 26 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| [33060] | 27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| [33694] | 28 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| [33530] | 29 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
|---|
| [30587] | 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;
|
|---|
| [33530] | 36 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| [35602] | 37 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| [30587] | 38 |
|
|---|
| [25670] | 39 | import relcontext.ChosenRelation;
|
|---|
| 40 | import relcontext.ChosenRelationListener;
|
|---|
| 41 |
|
|---|
| [26832] | 42 | /**
|
|---|
| 43 | * Make a single polygon out of the multipolygon relation. The relation must have only outer members.
|
|---|
| 44 | * @author Zverik
|
|---|
| 45 | */
|
|---|
| [35602] | 46 | public class ReconstructPolygonAction extends JosmAction implements ChosenRelationListener {
|
|---|
| [36217] | 47 | private final transient ChosenRelation rel;
|
|---|
| [32395] | 48 |
|
|---|
| [36102] | 49 | private static final List<String> IRRELEVANT_KEYS = Arrays.asList("source", "created_by", "note");
|
|---|
| [25670] | 50 |
|
|---|
| [36217] | 51 | /**
|
|---|
| 52 | * Reconstruct one or more polygons from multipolygon relation.
|
|---|
| 53 | * @param rel the multipolygon relation
|
|---|
| 54 | */
|
|---|
| [32395] | 55 | public ReconstructPolygonAction(ChosenRelation rel) {
|
|---|
| [35602] | 56 | super(tr("Reconstruct polygon"), "dialogs/filter", tr("Reconstruct polygon from multipolygon relation"),
|
|---|
| [35829] | 57 | Shortcut.registerShortcut("reltoolbox:reconstructpoly", tr("Relation Toolbox: {0}",
|
|---|
| 58 | tr("Reconstruct polygon from multipolygon relation")),
|
|---|
| [36217] | 59 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, false);
|
|---|
| [25670] | 60 | this.rel = rel;
|
|---|
| 61 | rel.addChosenRelationListener(this);
|
|---|
| [26882] | 62 | setEnabled(isSuitableRelation(rel.get()));
|
|---|
| [25670] | 63 | }
|
|---|
| 64 |
|
|---|
| [32395] | 65 | @Override
|
|---|
| 66 | public void actionPerformed(ActionEvent e) {
|
|---|
| [25670] | 67 | Relation r = rel.get();
|
|---|
| [33060] | 68 | boolean relationReused = false;
|
|---|
| [32395] | 69 | List<Way> ways = new ArrayList<>();
|
|---|
| 70 | boolean wont = false;
|
|---|
| 71 | for (RelationMember m : r.getMembers()) {
|
|---|
| [36142] | 72 | if (m.isWay()) {
|
|---|
| [32395] | 73 | ways.add(m.getWay());
|
|---|
| 74 | } else {
|
|---|
| 75 | wont = true;
|
|---|
| [30738] | 76 | }
|
|---|
| 77 | }
|
|---|
| [32395] | 78 | if (wont) {
|
|---|
| [34551] | 79 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
|---|
| [36217] | 80 | tr("Multipolygon must consist only of ways"),
|
|---|
| [36136] | 81 | tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
|
|---|
| [32395] | 82 | return;
|
|---|
| [30738] | 83 | }
|
|---|
| [32395] | 84 |
|
|---|
| 85 | MultipolygonBuilder mpc = new MultipolygonBuilder();
|
|---|
| 86 | String error = mpc.makeFromWays(ways);
|
|---|
| 87 | if (error != null) {
|
|---|
| [34551] | 88 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), error);
|
|---|
| [32395] | 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | rel.clear();
|
|---|
| 93 | List<Command> commands = new ArrayList<>();
|
|---|
| [33694] | 94 | Command relationDeleteCommand = DeleteCommand.delete(Collections.singleton(r), true, true);
|
|---|
| [33060] | 95 | if (relationDeleteCommand == null)
|
|---|
| [32395] | 96 | return;
|
|---|
| 97 |
|
|---|
| [33694] | 98 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
|---|
| [32395] | 99 | for (JoinedPolygon p : mpc.outerWays) {
|
|---|
| [33060] | 100 |
|
|---|
| [33265] | 101 | ArrayList<JoinedPolygon> myInnerWays = new ArrayList<>();
|
|---|
| [33060] | 102 | for (JoinedPolygon i : mpc.innerWays) {
|
|---|
| 103 | // if the first point of any inner ring is contained in this
|
|---|
| [33265] | 104 | // outer ring, then this inner ring belongs to us. This
|
|---|
| [33060] | 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()) {
|
|---|
| [33265] | 113 | // this ring has inner rings, so we leave a multipolygon in
|
|---|
| 114 | // place and don't reconstruct the rings.
|
|---|
| [36217] | 115 | List<RelationMember> members = new ArrayList<>();
|
|---|
| [36102] | 116 | Relation n;
|
|---|
| [33060] | 117 | for (Way w : p.ways) {
|
|---|
| [36217] | 118 | members.add(new RelationMember("outer", w));
|
|---|
| [33060] | 119 | }
|
|---|
| 120 | for (JoinedPolygon i : myInnerWays) {
|
|---|
| 121 | for (Way w : i.ways) {
|
|---|
| [36217] | 122 | members.add(new RelationMember("inner", w));
|
|---|
| [33060] | 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | if (relationReused) {
|
|---|
| [36217] | 126 | n = new Relation();
|
|---|
| 127 | n.setKeys(r.getKeys());
|
|---|
| 128 | n.setMembers(members);
|
|---|
| [33694] | 129 | commands.add(new AddCommand(ds, n));
|
|---|
| [33060] | 130 | } else {
|
|---|
| 131 | relationReused = true;
|
|---|
| [36217] | 132 | commands.add(new ChangeMembersCommand(r, members));
|
|---|
| [33060] | 133 | }
|
|---|
| 134 | continue;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| [32395] | 137 | // move all tags from relation and common tags from ways
|
|---|
| [33265] | 138 | // start with all tags from first way but only if area tags are present
|
|---|
| [32395] | 139 | Map<String, String> tags = p.ways.get(0).getKeys();
|
|---|
| [33060] | 140 | if (!p.ways.get(0).hasAreaTags()) {
|
|---|
| 141 | tags.clear();
|
|---|
| 142 | }
|
|---|
| [32395] | 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 | }
|
|---|
| [30738] | 153 | }
|
|---|
| [32395] | 154 | List<OsmPrimitive> referrers = w.getReferrers();
|
|---|
| [36102] | 155 | relations.removeIf(osmPrimitive -> !referrers.contains(osmPrimitive));
|
|---|
| [30738] | 156 | }
|
|---|
| [32395] | 157 | tags.putAll(r.getKeys());
|
|---|
| 158 | tags.remove("type");
|
|---|
| 159 |
|
|---|
| [36142] | 160 | // then delete ways that are not relevant (do not take part in other relations or have strange tags)
|
|---|
| [32395] | 161 | Way candidateWay = null;
|
|---|
| 162 | for (Way w : p.ways) {
|
|---|
| [36142] | 163 | if (w.getReferrers().size() == 1) {
|
|---|
| [32395] | 164 | // check tags that remain
|
|---|
| 165 | Set<String> keys = new HashSet<>(w.keySet());
|
|---|
| 166 | keys.removeAll(tags.keySet());
|
|---|
| [36102] | 167 | IRRELEVANT_KEYS.forEach(keys::remove);
|
|---|
| [32395] | 168 | if (keys.isEmpty()) {
|
|---|
| [32398] | 169 | if (candidateWay == null) {
|
|---|
| [32395] | 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 | }
|
|---|
| [36136] | 178 | commands.add(new DeleteCommand(w));
|
|---|
| [32395] | 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| [30738] | 182 | }
|
|---|
| [32395] | 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);
|
|---|
| [33694] | 189 | commands.add(candidateWay == null ? new AddCommand(ds, result) : new ChangeCommand(candidateWay, result));
|
|---|
| [30738] | 190 | }
|
|---|
| [32395] | 191 |
|
|---|
| [33060] | 192 | // only delete the relation if it hasn't been re-used
|
|---|
| 193 | if (!relationReused) {
|
|---|
| [36142] | 194 | // The relation needs to be deleted first, so that undo/redo continue to work properly
|
|---|
| 195 | commands.add(0, relationDeleteCommand);
|
|---|
| [33060] | 196 | }
|
|---|
| [34551] | 197 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
|
|---|
| [32395] | 198 | r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
|
|---|
| [36217] | 199 | Collection<? extends OsmPrimitive> newSelection = UndoRedoHandler.getInstance().getLastCommand().getParticipatingPrimitives();
|
|---|
| 200 | newSelection.removeIf(p -> p.isDeleted());
|
|---|
| [33694] | 201 | ds.setSelected(newSelection);
|
|---|
| [25670] | 202 | }
|
|---|
| 203 |
|
|---|
| [32395] | 204 | @Override
|
|---|
| 205 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 206 | setEnabled(isSuitableRelation(newRelation));
|
|---|
| [25670] | 207 | }
|
|---|
| [32395] | 208 |
|
|---|
| [36132] | 209 | private static boolean isSuitableRelation(Relation newRelation) {
|
|---|
| [36102] | 210 | return newRelation != null && "multipolygon".equals(newRelation.get("type")) && newRelation.getMembersCount() != 0;
|
|---|
| [26882] | 211 | }
|
|---|
| [25670] | 212 | }
|
|---|