| 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.osm.MultipolygonBuilder;
|
|---|
| 26 | import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 31 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
|---|
| 32 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 33 |
|
|---|
| 34 | import relcontext.ChosenRelation;
|
|---|
| 35 | import relcontext.ChosenRelationListener;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Make a single polygon out of the multipolygon relation. The relation must have only outer members.
|
|---|
| 39 | * @author Zverik
|
|---|
| 40 | */
|
|---|
| 41 | public class ReconstructPolygonAction extends AbstractAction implements ChosenRelationListener {
|
|---|
| 42 | private ChosenRelation rel;
|
|---|
| 43 |
|
|---|
| 44 | private static final List<String> IRRELEVANT_KEYS = Arrays.asList(new String[] {
|
|---|
| 45 | "source", "created_by", "note"});
|
|---|
| 46 |
|
|---|
| 47 | public ReconstructPolygonAction(ChosenRelation rel) {
|
|---|
| 48 | super(tr("Reconstruct polygon"));
|
|---|
| 49 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "filter"));
|
|---|
| 50 | putValue(LONG_DESCRIPTION, "Reconstruct polygon from multipolygon relation");
|
|---|
| 51 | this.rel = rel;
|
|---|
| 52 | rel.addChosenRelationListener(this);
|
|---|
| 53 | setEnabled(isSuitableRelation(rel.get()));
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | @Override
|
|---|
| 57 | public void actionPerformed(ActionEvent e) {
|
|---|
| 58 | Relation r = rel.get();
|
|---|
| 59 | List<Way> ways = new ArrayList<>();
|
|---|
| 60 | boolean wont = false;
|
|---|
| 61 | for (RelationMember m : r.getMembers()) {
|
|---|
| 62 | if (m.isWay() ) {
|
|---|
| 63 | ways.add(m.getWay());
|
|---|
| 64 | } else {
|
|---|
| 65 | wont = true;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | if (wont) {
|
|---|
| 69 | JOptionPane.showMessageDialog(Main.parent, tr("Multipolygon must consist only of ways"), tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | MultipolygonBuilder mpc = new MultipolygonBuilder();
|
|---|
| 74 | String error = mpc.makeFromWays(ways);
|
|---|
| 75 | if (error != null) {
|
|---|
| 76 | JOptionPane.showMessageDialog(Main.parent, error);
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | if (!mpc.innerWays.isEmpty()) {
|
|---|
| 81 | JOptionPane.showMessageDialog(Main.parent, tr("Reconstruction of polygons can be done only from outer ways"), tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 82 | return;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | rel.clear();
|
|---|
| 86 | List<Way> newSelection = new ArrayList<>();
|
|---|
| 87 | List<Command> commands = new ArrayList<>();
|
|---|
| 88 | Command c = DeleteCommand.delete(Main.main.getEditLayer(), Collections.singleton(r), true, true);
|
|---|
| 89 | if (c == null )
|
|---|
| 90 | return;
|
|---|
| 91 | commands.add(c);
|
|---|
| 92 |
|
|---|
| 93 | for (JoinedPolygon p : mpc.outerWays) {
|
|---|
| 94 | // move all tags from relation and common tags from ways
|
|---|
| 95 | Map<String, String> tags = p.ways.get(0).getKeys();
|
|---|
| 96 | List<OsmPrimitive> relations = p.ways.get(0).getReferrers();
|
|---|
| 97 | Set<String> noTags = new HashSet<>(r.keySet());
|
|---|
| 98 | for (int i = 1; i < p.ways.size(); i++) {
|
|---|
| 99 | Way w = p.ways.get(i);
|
|---|
| 100 | for (String key : w.keySet()) {
|
|---|
| 101 | String value = w.get(key);
|
|---|
| 102 | if (!noTags.contains(key) && tags.containsKey(key) && !tags.get(key).equals(value)) {
|
|---|
| 103 | tags.remove(key);
|
|---|
| 104 | noTags.add(key);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | List<OsmPrimitive> referrers = w.getReferrers();
|
|---|
| 108 | for (Iterator<OsmPrimitive> ref1 = relations.iterator(); ref1.hasNext(); )
|
|---|
| 109 | if (!referrers.contains(ref1.next()) ) {
|
|---|
| 110 | ref1.remove();
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | tags.putAll(r.getKeys());
|
|---|
| 114 | tags.remove("type");
|
|---|
| 115 |
|
|---|
| 116 | // then delete ways that are not relevant (do not take part in other relations of have strange tags)
|
|---|
| 117 | Way candidateWay = null;
|
|---|
| 118 | for (Way w : p.ways) {
|
|---|
| 119 | if (w.getReferrers().equals(relations)) {
|
|---|
| 120 | // check tags that remain
|
|---|
| 121 | Set<String> keys = new HashSet<>(w.keySet());
|
|---|
| 122 | keys.removeAll(tags.keySet());
|
|---|
| 123 | keys.removeAll(IRRELEVANT_KEYS);
|
|---|
| 124 | if (keys.isEmpty()) {
|
|---|
| 125 | if (candidateWay == null ) {
|
|---|
| 126 | candidateWay = w;
|
|---|
| 127 | } else {
|
|---|
| 128 | if (candidateWay.isNew() && !w.isNew()) {
|
|---|
| 129 | // prefer ways that are already in the database
|
|---|
| 130 | Way tmp = w;
|
|---|
| 131 | w = candidateWay;
|
|---|
| 132 | candidateWay = tmp;
|
|---|
| 133 | }
|
|---|
| 134 | commands.add(new DeleteCommand(w));
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | // take the first way, put all nodes into it, making it a closed polygon
|
|---|
| 141 | Way result = candidateWay == null ? new Way() : new Way(candidateWay);
|
|---|
| 142 | result.setNodes(p.nodes);
|
|---|
| 143 | result.addNode(result.firstNode());
|
|---|
| 144 | result.setKeys(tags);
|
|---|
| 145 | newSelection.add(candidateWay == null ? result : candidateWay);
|
|---|
| 146 | commands.add(candidateWay == null ? new AddCommand(result) : new ChangeCommand(candidateWay, result));
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | Main.main.undoRedo.add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
|
|---|
| 150 | r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
|
|---|
| 151 | Main.main.getCurrentDataSet().setSelected(newSelection);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | @Override
|
|---|
| 155 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 156 | setEnabled(isSuitableRelation(newRelation));
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | private boolean isSuitableRelation(Relation newRelation) {
|
|---|
| 160 | if (newRelation == null || !"multipolygon".equals(newRelation.get("type")) || newRelation.getMembersCount() == 0 )
|
|---|
| 161 | return false;
|
|---|
| 162 | else {
|
|---|
| 163 | for (RelationMember m : newRelation.getMembers() )
|
|---|
| 164 | if ("inner".equals(m.getRole()) )
|
|---|
| 165 | return false;
|
|---|
| 166 | return true;
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|