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