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

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

fixed #10387 - efficiency for "inside(...)" function in MapCSS

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