1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.geom.Area;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
8 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Implementation of simple boolean {@link GeoProperty}.
|
---|
14 | */
|
---|
15 | public class DefaultGeoProperty implements GeoProperty<Boolean> {
|
---|
16 |
|
---|
17 | private final Area area;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Create DefaultGeoProperty based on a collection of closed ways.
|
---|
21 | *
|
---|
22 | * @param ways the ways forming the area
|
---|
23 | */
|
---|
24 | public DefaultGeoProperty(Collection<Way> ways) {
|
---|
25 | Area area = null;
|
---|
26 | for (Way w : ways) {
|
---|
27 | Area wayArea = Geometry.getAreaLatLon(w.getNodes());
|
---|
28 | if (area == null) {
|
---|
29 | area = wayArea;
|
---|
30 | } else {
|
---|
31 | area.add(wayArea);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | this.area = area;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Create DefaultGeoProperty based on a multipolygon relation.
|
---|
39 | *
|
---|
40 | * @param multipolygon the multipolygon
|
---|
41 | */
|
---|
42 | public DefaultGeoProperty(Relation multipolygon) {
|
---|
43 | this.area = Geometry.getAreaLatLon(multipolygon);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Boolean get(LatLon ll) {
|
---|
48 | return area.contains(ll.lon(), ll.lat());
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public Boolean get(BBox box) {
|
---|
53 | Area abox = new Area(box.toRectangle());
|
---|
54 | Geometry.PolygonIntersection is = Geometry.polygonIntersection(abox, area, 1e-10 /* using deg and not meters */);
|
---|
55 | switch (is) {
|
---|
56 | case FIRST_INSIDE_SECOND:
|
---|
57 | return Boolean.TRUE;
|
---|
58 | case OUTSIDE:
|
---|
59 | return Boolean.FALSE;
|
---|
60 | default:
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|