[7144] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[7828] | 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collection;
|
---|
[7921] | 6 | import java.util.Collections;
|
---|
[8338] | 7 | import java.util.Map;
|
---|
[7828] | 8 | import java.util.TreeMap;
|
---|
| 9 |
|
---|
[7144] | 10 | import org.openstreetmap.josm.Main;
|
---|
| 11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 12 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
[7921] | 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[7144] | 14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 19 | import org.openstreetmap.josm.tools.Geometry;
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
[8114] | 22 | * This allows to select a polygon/multipolygon by an internal point.
|
---|
| 23 | * @since 7144
|
---|
[7144] | 24 | */
|
---|
[9594] | 25 | public final class SelectByInternalPointAction {
|
---|
[7144] | 26 |
|
---|
[9594] | 27 | private SelectByInternalPointAction() {
|
---|
| 28 | // Hide public constructor for utility class
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[7144] | 31 | /**
|
---|
[8114] | 32 | * Returns the surrounding polygons/multipolygons
|
---|
[7144] | 33 | * ordered by their area size (from small to large)
|
---|
| 34 | * which contain the internal point.
|
---|
| 35 | *
|
---|
| 36 | * @param internalPoint the internal point.
|
---|
[8114] | 37 | * @return the surrounding polygons/multipolygons
|
---|
[7144] | 38 | */
|
---|
| 39 | public static Collection<OsmPrimitive> getSurroundingObjects(EastNorth internalPoint) {
|
---|
[10448] | 40 | final DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
[7921] | 41 | if (ds == null) {
|
---|
| 42 | return Collections.emptySet();
|
---|
| 43 | }
|
---|
[7144] | 44 | final Node n = new Node(internalPoint);
|
---|
[8338] | 45 | final Map<Double, OsmPrimitive> found = new TreeMap<>();
|
---|
[7921] | 46 | for (Way w : ds.getWays()) {
|
---|
[9594] | 47 | if (w.isUsable() && w.isClosed() && w.isSelectable() && Geometry.nodeInsidePolygon(n, w.getNodes())) {
|
---|
| 48 | found.put(Geometry.closedWayArea(w), w);
|
---|
[7144] | 49 | }
|
---|
| 50 | }
|
---|
[7921] | 51 | for (Relation r : ds.getRelations()) {
|
---|
[9594] | 52 | if (r.isUsable() && r.isMultipolygon() && r.isSelectable() && Geometry.isNodeInsideMultiPolygon(n, r, null)) {
|
---|
| 53 | for (RelationMember m : r.getMembers()) {
|
---|
| 54 | if (m.isWay() && m.getWay().isClosed()) {
|
---|
| 55 | found.values().remove(m.getWay());
|
---|
[7144] | 56 | }
|
---|
| 57 | }
|
---|
[9594] | 58 | // estimate multipolygon size by its bounding box area
|
---|
| 59 | BBox bBox = r.getBBox();
|
---|
| 60 | EastNorth en1 = Main.map.mapView.getProjection().latlon2eastNorth(bBox.getTopLeft());
|
---|
| 61 | EastNorth en2 = Main.map.mapView.getProjection().latlon2eastNorth(bBox.getBottomRight());
|
---|
| 62 | double s = Math.abs((en1.east() - en2.east()) * (en1.north() - en2.north()));
|
---|
| 63 | found.put(s <= 0 ? 1e8 : s, r);
|
---|
[7144] | 64 | }
|
---|
| 65 | }
|
---|
| 66 | return found.values();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
[8114] | 70 | * Returns the smallest surrounding polygon/multipolygon which contains the internal point.
|
---|
[7144] | 71 | *
|
---|
| 72 | * @param internalPoint the internal point.
|
---|
[8114] | 73 | * @return the smallest surrounding polygon/multipolygon
|
---|
[7144] | 74 | */
|
---|
| 75 | public static OsmPrimitive getSmallestSurroundingObject(EastNorth internalPoint) {
|
---|
| 76 | final Collection<OsmPrimitive> surroundingObjects = getSurroundingObjects(internalPoint);
|
---|
| 77 | return surroundingObjects.isEmpty() ? null : surroundingObjects.iterator().next();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
[7828] | 81 | * Select a polygon or multipolygon by an internal point.
|
---|
[7144] | 82 | *
|
---|
| 83 | * @param internalPoint the internal point.
|
---|
| 84 | * @param doAdd whether to add selected polygon to the current selection.
|
---|
| 85 | * @param doRemove whether to remove the selected polygon from the current selection.
|
---|
| 86 | */
|
---|
| 87 | public static void performSelection(EastNorth internalPoint, boolean doAdd, boolean doRemove) {
|
---|
| 88 | final Collection<OsmPrimitive> surroundingObjects = getSurroundingObjects(internalPoint);
|
---|
[10448] | 89 | final DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
[7144] | 90 | if (surroundingObjects.isEmpty()) {
|
---|
| 91 | return;
|
---|
| 92 | } else if (doRemove) {
|
---|
[9594] | 93 | final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
|
---|
[7144] | 94 | newSelection.removeAll(surroundingObjects);
|
---|
[9594] | 95 | ds.setSelected(newSelection);
|
---|
[7144] | 96 | } else if (doAdd) {
|
---|
[9594] | 97 | final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
|
---|
[7144] | 98 | newSelection.add(surroundingObjects.iterator().next());
|
---|
[9594] | 99 | ds.setSelected(newSelection);
|
---|
[7144] | 100 | } else {
|
---|
[9594] | 101 | ds.setSelected(surroundingObjects.iterator().next());
|
---|
[7144] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|