| 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.Collections;
|
|---|
| 11 | import java.util.HashSet;
|
|---|
| 12 | import java.util.Iterator;
|
|---|
| 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.Command;
|
|---|
| 23 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 24 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 25 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.MultipolygonBuilder;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
|
|---|
| 31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 32 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 35 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 36 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 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 ChosenRelation rel;
|
|---|
| 48 |
|
|---|
| 49 | private static final List<String> IRRELEVANT_KEYS = Arrays.asList(new String[] {
|
|---|
| 50 | "source", "created_by", "note"});
|
|---|
| 51 |
|
|---|
| 52 | public ReconstructPolygonAction(ChosenRelation rel) {
|
|---|
| 53 | super(tr("Reconstruct polygon"), "dialogs/filter", tr("Reconstruct polygon from multipolygon relation"),
|
|---|
| 54 | Shortcut.registerShortcut("reltoolbox:reconstructpoly", tr("Relation Toolbox: {0}", tr("Reconstruct polygon from multipolygon relation")),
|
|---|
| 55 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
|
|---|
| 56 | this.rel = rel;
|
|---|
| 57 | rel.addChosenRelationListener(this);
|
|---|
| 58 | setEnabled(isSuitableRelation(rel.get()));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | @Override
|
|---|
| 62 | public void actionPerformed(ActionEvent e) {
|
|---|
| 63 | Relation r = rel.get();
|
|---|
| 64 | boolean relationReused = false;
|
|---|
| 65 | List<Way> ways = new ArrayList<>();
|
|---|
| 66 | boolean wont = false;
|
|---|
| 67 | for (RelationMember m : r.getMembers()) {
|
|---|
| 68 | if (m.isWay()) {
|
|---|
| 69 | ways.add(m.getWay());
|
|---|
| 70 | } else {
|
|---|
| 71 | wont = true;
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | if (wont) {
|
|---|
| 75 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
|---|
| 76 | tr("Multipolygon must consist only of ways"), tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | MultipolygonBuilder mpc = new MultipolygonBuilder();
|
|---|
| 81 | String error = mpc.makeFromWays(ways);
|
|---|
| 82 | if (error != null) {
|
|---|
| 83 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), error);
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | rel.clear();
|
|---|
| 88 | List<OsmPrimitive> newSelection = new ArrayList<>();
|
|---|
| 89 | List<Command> commands = new ArrayList<>();
|
|---|
| 90 | Command relationDeleteCommand = DeleteCommand.delete(Collections.singleton(r), true, true);
|
|---|
| 91 | if (relationDeleteCommand == null)
|
|---|
| 92 | return;
|
|---|
| 93 |
|
|---|
| 94 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
|---|
| 95 | for (JoinedPolygon p : mpc.outerWays) {
|
|---|
| 96 |
|
|---|
| 97 | ArrayList<JoinedPolygon> myInnerWays = new ArrayList<>();
|
|---|
| 98 | for (JoinedPolygon i : mpc.innerWays) {
|
|---|
| 99 | // if the first point of any inner ring is contained in this
|
|---|
| 100 | // outer ring, then this inner ring belongs to us. This
|
|---|
| 101 | // assumption only works if multipolygons have valid geometries
|
|---|
| 102 | EastNorth en = i.ways.get(0).firstNode().getEastNorth();
|
|---|
| 103 | if (p.area.contains(en.east(), en.north())) {
|
|---|
| 104 | myInnerWays.add(i);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (!myInnerWays.isEmpty()) {
|
|---|
| 109 | // this ring has inner rings, so we leave a multipolygon in
|
|---|
| 110 | // place and don't reconstruct the rings.
|
|---|
| 111 | Relation n = null;
|
|---|
| 112 | if (relationReused) {
|
|---|
| 113 | n = new Relation();
|
|---|
| 114 | n.setKeys(r.getKeys());
|
|---|
| 115 | } else {
|
|---|
| 116 | n = new Relation(r);
|
|---|
| 117 | n.setMembers(null);
|
|---|
| 118 | }
|
|---|
| 119 | for (Way w : p.ways) {
|
|---|
| 120 | n.addMember(new RelationMember("outer", w));
|
|---|
| 121 | }
|
|---|
| 122 | for (JoinedPolygon i : myInnerWays) {
|
|---|
| 123 | for (Way w : i.ways) {
|
|---|
| 124 | n.addMember(new RelationMember("inner", w));
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | if (relationReused) {
|
|---|
| 128 | commands.add(new AddCommand(ds, n));
|
|---|
| 129 | } else {
|
|---|
| 130 | relationReused = true;
|
|---|
| 131 | commands.add(new ChangeCommand(r, n));
|
|---|
| 132 | }
|
|---|
| 133 | newSelection.add(n);
|
|---|
| 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 | for (Iterator<OsmPrimitive> ref1 = relations.iterator(); ref1.hasNext();) {
|
|---|
| 156 | if (!referrers.contains(ref1.next())) {
|
|---|
| 157 | ref1.remove();
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | tags.putAll(r.getKeys());
|
|---|
| 162 | tags.remove("type");
|
|---|
| 163 |
|
|---|
| 164 | // then delete ways that are not relevant (do not take part in other relations of have strange tags)
|
|---|
| 165 | Way candidateWay = null;
|
|---|
| 166 | for (Way w : p.ways) {
|
|---|
| 167 | if (w.getReferrers().equals(relations)) {
|
|---|
| 168 | // check tags that remain
|
|---|
| 169 | Set<String> keys = new HashSet<>(w.keySet());
|
|---|
| 170 | keys.removeAll(tags.keySet());
|
|---|
| 171 | keys.removeAll(IRRELEVANT_KEYS);
|
|---|
| 172 | if (keys.isEmpty()) {
|
|---|
| 173 | if (candidateWay == null) {
|
|---|
| 174 | candidateWay = w;
|
|---|
| 175 | } else {
|
|---|
| 176 | if (candidateWay.isNew() && !w.isNew()) {
|
|---|
| 177 | // prefer ways that are already in the database
|
|---|
| 178 | Way tmp = w;
|
|---|
| 179 | w = candidateWay;
|
|---|
| 180 | candidateWay = tmp;
|
|---|
| 181 | }
|
|---|
| 182 | commands.add(new DeleteCommand(w));
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | // take the first way, put all nodes into it, making it a closed polygon
|
|---|
| 189 | Way result = candidateWay == null ? new Way() : new Way(candidateWay);
|
|---|
| 190 | result.setNodes(p.nodes);
|
|---|
| 191 | result.addNode(result.firstNode());
|
|---|
| 192 | result.setKeys(tags);
|
|---|
| 193 | newSelection.add(candidateWay == null ? result : candidateWay);
|
|---|
| 194 | commands.add(candidateWay == null ? new AddCommand(ds, result) : new ChangeCommand(candidateWay, result));
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | // only delete the relation if it hasn't been re-used
|
|---|
| 198 | if (!relationReused) {
|
|---|
| 199 | commands.add(relationDeleteCommand);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
|
|---|
| 203 | r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
|
|---|
| 204 | ds.setSelected(newSelection);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | @Override
|
|---|
| 208 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 209 | setEnabled(isSuitableRelation(newRelation));
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | private boolean isSuitableRelation(Relation newRelation) {
|
|---|
| 213 | if (newRelation == null || !"multipolygon".equals(newRelation.get("type")) || newRelation.getMembersCount() == 0)
|
|---|
| 214 | return false;
|
|---|
| 215 | else
|
|---|
| 216 | return true;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|