| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.tools;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.geom.Area;
|
|---|
| 5 | import java.awt.geom.Path2D;
|
|---|
| 6 | import java.util.Collection;
|
|---|
| 7 |
|
|---|
| 8 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 9 | import org.openstreetmap.josm.data.osm.BBox;
|
|---|
| 10 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 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 |
|
|---|
| 18 | private final Area area;
|
|---|
| 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) {
|
|---|
| 26 | Path2D path = new Path2D.Double();
|
|---|
| 27 | path.setWindingRule(Path2D.WIND_EVEN_ODD);
|
|---|
| 28 | for (Way w : ways) {
|
|---|
| 29 | Geometry.buildPath2DLatLon(w.getNodes(), path);
|
|---|
| 30 | }
|
|---|
| 31 | this.area = new Area(path);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 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 |
|
|---|
| 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());
|
|---|
| 51 | Geometry.PolygonIntersection is = Geometry.polygonIntersection(abox, area, 1e-10 /* using deg and not meters */);
|
|---|
| 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 | }
|
|---|