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

Last change on this file since 8093 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

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