source: josm/trunk/src/org/openstreetmap/josm/tools/DefaultGeoProperty.java@ 13468

Last change on this file since 13468 was 11361, checked in by bastiK, 7 years ago

see #10387 - use path winding rule instead of Area.subtract

  • Property svn:eol-style set to native
File size: 1.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.geom.Area;
5import java.awt.geom.Path2D;
6import java.util.Collection;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9import org.openstreetmap.josm.data.osm.BBox;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Way;
12
13/**
14 * Implementation of simple boolean {@link GeoProperty}.
15 */
16public 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}
Note: See TracBrowser for help on using the repository browser.