1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Rectangle;
|
---|
5 | import java.awt.geom.Area;
|
---|
6 | import java.awt.geom.Path2D;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
10 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Implementation of simple boolean {@link GeoProperty}.
|
---|
16 | */
|
---|
17 | public class DefaultGeoProperty implements GeoProperty<Boolean> {
|
---|
18 |
|
---|
19 | private final Area area;
|
---|
20 | private LatLon random;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Create DefaultGeoProperty based on a collection of closed ways.
|
---|
24 | *
|
---|
25 | * @param ways the ways forming the area
|
---|
26 | */
|
---|
27 | public DefaultGeoProperty(Collection<Way> ways) {
|
---|
28 | Path2D path = new Path2D.Double();
|
---|
29 | path.setWindingRule(Path2D.WIND_EVEN_ODD);
|
---|
30 | for (Way w : ways) {
|
---|
31 | Geometry.buildPath2DLatLon(w.getNodes(), path);
|
---|
32 | }
|
---|
33 | this.area = new Area(path);
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Create DefaultGeoProperty based on a multipolygon relation.
|
---|
38 | *
|
---|
39 | * @param multipolygon the multipolygon
|
---|
40 | */
|
---|
41 | public DefaultGeoProperty(Relation multipolygon) {
|
---|
42 | this.area = Geometry.getAreaLatLon(multipolygon);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Boolean get(LatLon ll) {
|
---|
47 | return area.contains(ll.lon(), ll.lat());
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public Boolean get(BBox box) {
|
---|
52 | Area abox = new Area(box.toRectangle());
|
---|
53 | Geometry.PolygonIntersection is = Geometry.polygonIntersection(abox, area, 1e-10 /* using deg and not meters */);
|
---|
54 | switch (is) {
|
---|
55 | case FIRST_INSIDE_SECOND:
|
---|
56 | return Boolean.TRUE;
|
---|
57 | case OUTSIDE:
|
---|
58 | return Boolean.FALSE;
|
---|
59 | default:
|
---|
60 | return null;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Returns the area.
|
---|
66 | * @return the area
|
---|
67 | * @since 14484
|
---|
68 | */
|
---|
69 | public final Area getArea() {
|
---|
70 | return area;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Returns a random lat/lon in the area.
|
---|
75 | * @return a random lat/lon in the area
|
---|
76 | * @since 15359
|
---|
77 | */
|
---|
78 | public final synchronized LatLon getRandomLatLon() {
|
---|
79 | if (random == null) {
|
---|
80 | Rectangle r = area.getBounds();
|
---|
81 | double x, y;
|
---|
82 | do {
|
---|
83 | x = r.getX() + r.getWidth() * Math.random();
|
---|
84 | y = r.getY() + r.getHeight() * Math.random();
|
---|
85 | } while (!area.contains(x, y));
|
---|
86 |
|
---|
87 | random = new LatLon(y, x);
|
---|
88 | }
|
---|
89 | return random;
|
---|
90 | }
|
---|
91 | }
|
---|