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

Last change on this file since 8256 was 8114, checked in by Don-vip, 9 years ago

fix #11197 - don't select filtered ways with double-click selection mode

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