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

Last change on this file since 17333 was 15359, checked in by Don-vip, 5 years ago

see #17058, fix #18097 - fix MapCSSTagChecker.getLocation

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Rectangle;
5import java.awt.geom.Area;
6import java.awt.geom.Path2D;
7import java.util.Collection;
8
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.osm.BBox;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.Way;
13
14/**
15 * Implementation of simple boolean {@link GeoProperty}.
16 */
17public class DefaultGeoProperty implements GeoProperty<Boolean> {
18
19 private final Area area;
20 private LatLon random;
21
22 /**
23 * Create DefaultGeoProperty based on a collection of closed ways.
24 *
25 * @param ways the ways forming the area
26 */
27 public DefaultGeoProperty(Collection<Way> ways) {
28 Path2D path = new Path2D.Double();
29 path.setWindingRule(Path2D.WIND_EVEN_ODD);
30 for (Way w : ways) {
31 Geometry.buildPath2DLatLon(w.getNodes(), path);
32 }
33 this.area = new Area(path);
34 }
35
36 /**
37 * Create DefaultGeoProperty based on a multipolygon relation.
38 *
39 * @param multipolygon the multipolygon
40 */
41 public DefaultGeoProperty(Relation multipolygon) {
42 this.area = Geometry.getAreaLatLon(multipolygon);
43 }
44
45 @Override
46 public Boolean get(LatLon ll) {
47 return area.contains(ll.lon(), ll.lat());
48 }
49
50 @Override
51 public Boolean get(BBox box) {
52 Area abox = new Area(box.toRectangle());
53 Geometry.PolygonIntersection is = Geometry.polygonIntersection(abox, area, 1e-10 /* using deg and not meters */);
54 switch (is) {
55 case FIRST_INSIDE_SECOND:
56 return Boolean.TRUE;
57 case OUTSIDE:
58 return Boolean.FALSE;
59 default:
60 return null;
61 }
62 }
63
64 /**
65 * Returns the area.
66 * @return the area
67 * @since 14484
68 */
69 public final Area getArea() {
70 return area;
71 }
72
73 /**
74 * Returns a random lat/lon in the area.
75 * @return a random lat/lon in the area
76 * @since 15359
77 */
78 public final synchronized LatLon getRandomLatLon() {
79 if (random == null) {
80 Rectangle r = area.getBounds();
81 double x, y;
82 do {
83 x = r.getX() + r.getWidth() * Math.random();
84 y = r.getY() + r.getHeight() * Math.random();
85 } while (!area.contains(x, y));
86
87 random = new LatLon(y, x);
88 }
89 return random;
90 }
91}
Note: See TracBrowser for help on using the repository browser.