source: josm/trunk/src/org/openstreetmap/josm/actions/SelectByInternalPointAction.java@ 13115

Last change on this file since 13115 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Map;
8import java.util.TreeMap;
9
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.osm.BBox;
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.RelationMember;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.data.projection.Projection;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.tools.Geometry;
21
22/**
23 * This allows to select a polygon/multipolygon by an internal point.
24 * @since 7144
25 */
26public final class SelectByInternalPointAction {
27
28 private SelectByInternalPointAction() {
29 // Hide public constructor for utility class
30 }
31
32 /**
33 * Returns the surrounding polygons/multipolygons 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 return getSurroundingObjects(MainApplication.getLayerManager().getEditDataSet(), internalPoint, false);
41 }
42
43 /**
44 * Returns the surrounding polygons/multipolygons ordered by their area size (from small to large)
45 * which contain the internal point.
46 *
47 * @param ds the data set
48 * @param internalPoint the internal point.
49 * @param includeMultipolygonWays whether to include multipolygon ways in the result (false by default)
50 * @return the surrounding polygons/multipolygons
51 * @since 11247
52 */
53 public static Collection<OsmPrimitive> getSurroundingObjects(DataSet ds, EastNorth internalPoint, boolean includeMultipolygonWays) {
54 if (ds == null) {
55 return Collections.emptySet();
56 }
57 final Node n = new Node(internalPoint);
58 final Map<Double, OsmPrimitive> found = new TreeMap<>();
59 for (Way w : ds.getWays()) {
60 if (w.isUsable() && w.isClosed() && w.isSelectable() && Geometry.nodeInsidePolygon(n, w.getNodes())) {
61 found.put(Geometry.closedWayArea(w), w);
62 }
63 }
64 Projection projection = MainApplication.getMap().mapView.getProjection();
65 for (Relation r : ds.getRelations()) {
66 if (r.isUsable() && r.isMultipolygon() && r.isSelectable() && Geometry.isNodeInsideMultiPolygon(n, r, null)) {
67 if (!includeMultipolygonWays) {
68 for (RelationMember m : r.getMembers()) {
69 if (m.isWay() && m.getWay().isClosed()) {
70 found.values().remove(m.getWay());
71 }
72 }
73 }
74 // estimate multipolygon size by its bounding box area
75 BBox bBox = r.getBBox();
76 EastNorth en1 = projection.latlon2eastNorth(bBox.getTopLeft());
77 EastNorth en2 = projection.latlon2eastNorth(bBox.getBottomRight());
78 double s = Math.abs((en1.east() - en2.east()) * (en1.north() - en2.north()));
79 found.put(s <= 0 ? 1e8 : s, r);
80 }
81 }
82 return found.values();
83 }
84
85 /**
86 * Returns the smallest surrounding polygon/multipolygon which contains the internal point.
87 *
88 * @param internalPoint the internal point.
89 * @return the smallest surrounding polygon/multipolygon
90 */
91 public static OsmPrimitive getSmallestSurroundingObject(EastNorth internalPoint) {
92 final Collection<OsmPrimitive> surroundingObjects = getSurroundingObjects(internalPoint);
93 return surroundingObjects.isEmpty() ? null : surroundingObjects.iterator().next();
94 }
95
96 /**
97 * Select a polygon or multipolygon by an internal point.
98 *
99 * @param internalPoint the internal point.
100 * @param doAdd whether to add selected polygon to the current selection.
101 * @param doRemove whether to remove the selected polygon from the current selection.
102 */
103 public static void performSelection(EastNorth internalPoint, boolean doAdd, boolean doRemove) {
104 final Collection<OsmPrimitive> surroundingObjects = getSurroundingObjects(internalPoint);
105 final DataSet ds = MainApplication.getLayerManager().getEditDataSet();
106 if (surroundingObjects.isEmpty()) {
107 return;
108 } else if (doRemove) {
109 final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
110 newSelection.removeAll(surroundingObjects);
111 ds.setSelected(newSelection);
112 } else if (doAdd) {
113 final Collection<OsmPrimitive> newSelection = new ArrayList<>(ds.getSelected());
114 newSelection.add(surroundingObjects.iterator().next());
115 ds.setSelected(newSelection);
116 } else {
117 ds.setSelected(surroundingObjects.iterator().next());
118 }
119 }
120}
Note: See TracBrowser for help on using the repository browser.