1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.TreeMap;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
12 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
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 | /**
|
---|
22 | * This allows to select a polygon/multipolygon by an internal point.
|
---|
23 | * @since 7144
|
---|
24 | */
|
---|
25 | public final class SelectByInternalPointAction {
|
---|
26 |
|
---|
27 | private SelectByInternalPointAction() {
|
---|
28 | // Hide public constructor for utility class
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Returns the surrounding polygons/multipolygons
|
---|
33 | * ordered by their area size (from small to large)
|
---|
34 | * which contain the internal point.
|
---|
35 | *
|
---|
36 | * @param internalPoint the internal point.
|
---|
37 | * @return the surrounding polygons/multipolygons
|
---|
38 | */
|
---|
39 | public static Collection<OsmPrimitive> getSurroundingObjects(EastNorth internalPoint) {
|
---|
40 | final DataSet ds = JosmAction.getCurrentDataSet();
|
---|
41 | if (ds == null) {
|
---|
42 | return Collections.emptySet();
|
---|
43 | }
|
---|
44 | final Node n = new Node(internalPoint);
|
---|
45 | final Map<Double, OsmPrimitive> found = new TreeMap<>();
|
---|
46 | for (Way w : ds.getWays()) {
|
---|
47 | if (w.isUsable() && w.isClosed() && w.isSelectable() && Geometry.nodeInsidePolygon(n, w.getNodes())) {
|
---|
48 | found.put(Geometry.closedWayArea(w), w);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | for (Relation r : ds.getRelations()) {
|
---|
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());
|
---|
56 | }
|
---|
57 | }
|
---|
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);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return found.values();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Returns the smallest surrounding polygon/multipolygon which contains the internal point.
|
---|
71 | *
|
---|
72 | * @param internalPoint the internal point.
|
---|
73 | * @return the smallest surrounding polygon/multipolygon
|
---|
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 | /**
|
---|
81 | * Select a polygon or multipolygon by an internal point.
|
---|
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);
|
---|
89 | final DataSet ds = JosmAction.getCurrentDataSet();
|
---|
90 | if (surroundingObjects.isEmpty()) {
|
---|
91 | return;
|
---|
92 | } else if (doRemove) {
|
---|
93 | final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
|
---|
94 | newSelection.removeAll(surroundingObjects);
|
---|
95 | ds.setSelected(newSelection);
|
---|
96 | } else if (doAdd) {
|
---|
97 | final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
|
---|
98 | newSelection.add(surroundingObjects.iterator().next());
|
---|
99 | ds.setSelected(newSelection);
|
---|
100 | } else {
|
---|
101 | ds.setSelected(surroundingObjects.iterator().next());
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|