| 1 | import {default as polygonContains} from "./polygonContains.js";
|
|---|
| 2 | import {default as distance} from "./distance.js";
|
|---|
| 3 | import {epsilon2, radians} from "./math.js";
|
|---|
| 4 |
|
|---|
| 5 | var containsObjectType = {
|
|---|
| 6 | Feature: function(object, point) {
|
|---|
| 7 | return containsGeometry(object.geometry, point);
|
|---|
| 8 | },
|
|---|
| 9 | FeatureCollection: function(object, point) {
|
|---|
| 10 | var features = object.features, i = -1, n = features.length;
|
|---|
| 11 | while (++i < n) if (containsGeometry(features[i].geometry, point)) return true;
|
|---|
| 12 | return false;
|
|---|
| 13 | }
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | var containsGeometryType = {
|
|---|
| 17 | Sphere: function() {
|
|---|
| 18 | return true;
|
|---|
| 19 | },
|
|---|
| 20 | Point: function(object, point) {
|
|---|
| 21 | return containsPoint(object.coordinates, point);
|
|---|
| 22 | },
|
|---|
| 23 | MultiPoint: function(object, point) {
|
|---|
| 24 | var coordinates = object.coordinates, i = -1, n = coordinates.length;
|
|---|
| 25 | while (++i < n) if (containsPoint(coordinates[i], point)) return true;
|
|---|
| 26 | return false;
|
|---|
| 27 | },
|
|---|
| 28 | LineString: function(object, point) {
|
|---|
| 29 | return containsLine(object.coordinates, point);
|
|---|
| 30 | },
|
|---|
| 31 | MultiLineString: function(object, point) {
|
|---|
| 32 | var coordinates = object.coordinates, i = -1, n = coordinates.length;
|
|---|
| 33 | while (++i < n) if (containsLine(coordinates[i], point)) return true;
|
|---|
| 34 | return false;
|
|---|
| 35 | },
|
|---|
| 36 | Polygon: function(object, point) {
|
|---|
| 37 | return containsPolygon(object.coordinates, point);
|
|---|
| 38 | },
|
|---|
| 39 | MultiPolygon: function(object, point) {
|
|---|
| 40 | var coordinates = object.coordinates, i = -1, n = coordinates.length;
|
|---|
| 41 | while (++i < n) if (containsPolygon(coordinates[i], point)) return true;
|
|---|
| 42 | return false;
|
|---|
| 43 | },
|
|---|
| 44 | GeometryCollection: function(object, point) {
|
|---|
| 45 | var geometries = object.geometries, i = -1, n = geometries.length;
|
|---|
| 46 | while (++i < n) if (containsGeometry(geometries[i], point)) return true;
|
|---|
| 47 | return false;
|
|---|
| 48 | }
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | function containsGeometry(geometry, point) {
|
|---|
| 52 | return geometry && containsGeometryType.hasOwnProperty(geometry.type)
|
|---|
| 53 | ? containsGeometryType[geometry.type](geometry, point)
|
|---|
| 54 | : false;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | function containsPoint(coordinates, point) {
|
|---|
| 58 | return distance(coordinates, point) === 0;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | function containsLine(coordinates, point) {
|
|---|
| 62 | var ao, bo, ab;
|
|---|
| 63 | for (var i = 0, n = coordinates.length; i < n; i++) {
|
|---|
| 64 | bo = distance(coordinates[i], point);
|
|---|
| 65 | if (bo === 0) return true;
|
|---|
| 66 | if (i > 0) {
|
|---|
| 67 | ab = distance(coordinates[i], coordinates[i - 1]);
|
|---|
| 68 | if (
|
|---|
| 69 | ab > 0 &&
|
|---|
| 70 | ao <= ab &&
|
|---|
| 71 | bo <= ab &&
|
|---|
| 72 | (ao + bo - ab) * (1 - Math.pow((ao - bo) / ab, 2)) < epsilon2 * ab
|
|---|
| 73 | )
|
|---|
| 74 | return true;
|
|---|
| 75 | }
|
|---|
| 76 | ao = bo;
|
|---|
| 77 | }
|
|---|
| 78 | return false;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | function containsPolygon(coordinates, point) {
|
|---|
| 82 | return !!polygonContains(coordinates.map(ringRadians), pointRadians(point));
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | function ringRadians(ring) {
|
|---|
| 86 | return ring = ring.map(pointRadians), ring.pop(), ring;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | function pointRadians(point) {
|
|---|
| 90 | return [point[0] * radians, point[1] * radians];
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | export default function(object, point) {
|
|---|
| 94 | return (object && containsObjectType.hasOwnProperty(object.type)
|
|---|
| 95 | ? containsObjectType[object.type]
|
|---|
| 96 | : containsGeometry)(object, point);
|
|---|
| 97 | }
|
|---|