From f5cf38cc57d60ccb7011299c775a8fd306170052 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Wed, 22 Nov 2017 10:55:49 -0800 Subject: [PATCH] Add extra geometry support for points-within-polygon --- packages/turf-points-within-polygon/README.md | 39 ++++++------ .../turf-points-within-polygon/index.d.ts | 8 +-- packages/turf-points-within-polygon/index.js | 59 +++++++++---------- .../turf-points-within-polygon/package.json | 3 +- packages/turf-points-within-polygon/test.js | 25 +++++++- packages/turf-points-within-polygon/types.ts | 20 +++++++ packages/turf-points-within-polygon/yarn.lock | 22 +++++++ scripts/generate-readmes | 3 + 8 files changed, 122 insertions(+), 57 deletions(-) create mode 100644 packages/turf-points-within-polygon/types.ts diff --git a/packages/turf-points-within-polygon/README.md b/packages/turf-points-within-polygon/README.md index 27e4bc6650..fde0ee6140 100644 --- a/packages/turf-points-within-polygon/README.md +++ b/packages/turf-points-within-polygon/README.md @@ -4,35 +4,34 @@ ## pointsWithinPolygon -Takes a set of [Points](http://geojson.org/geojson-spec.html#point) and a set of [(Multi)Polygons](http://geojson.org/geojson-spec.html#polygon) and returns the points that fall within the polygons. +Finds [Points](http://geojson.org/geojson-spec.html#point) that fall within [(Multi)Polygon(s)](http://geojson.org/geojson-spec.html#polygon). **Parameters** -- `points` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** input points -- `polygons` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<([Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>** input polygons +- `points` **(Feauture | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>)** Points as input search +- `polygons` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) | Geoemtry | [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** Points must be within these (Multi)Polygon(s) **Examples** ```javascript -var searchWithin = turf.featureCollection([ - turf.polygon([[ - [-46.653,-23.543], - [-46.634,-23.5346], - [-46.613,-23.543], - [-46.614,-23.559], - [-46.631,-23.567], - [-46.653,-23.560], - [-46.653,-23.543] - ]]) -]); -var points = turf.featureCollection([ - turf.point([-46.6318, -23.5523]), - turf.point([-46.6246, -23.5325]), - turf.point([-46.6062, -23.5513]), - turf.point([-46.663, -23.554]), - turf.point([-46.643, -23.557]) +var points = turf.points([ + [-46.6318, -23.5523], + [-46.6246, -23.5325], + [-46.6062, -23.5513], + [-46.663, -23.554], + [-46.643, -23.557] ]); +var searchWithin = turf.polygon([[ + [-46.653,-23.543], + [-46.634,-23.5346], + [-46.613,-23.543], + [-46.614,-23.559], + [-46.631,-23.567], + [-46.653,-23.560], + [-46.653,-23.543] +]]); + var ptsWithin = turf.pointsWithinPolygon(points, searchWithin); //addToMap diff --git a/packages/turf-points-within-polygon/index.d.ts b/packages/turf-points-within-polygon/index.d.ts index 929e242656..7729ec19ec 100644 --- a/packages/turf-points-within-polygon/index.d.ts +++ b/packages/turf-points-within-polygon/index.d.ts @@ -1,9 +1,9 @@ -import { FeatureCollection, Polygon, MultiPolygon, Point } from '@turf/helpers' +import { Feature, FeatureCollection, Polygon, MultiPolygon, Point } from '@turf/helpers' /** * http://turfjs.org/docs/#pointswithinpolygon */ -export default function pointsWithinPolygon( - points: FeatureCollection, - polygons: FeatureCollection +export default function pointsWithinPolygon( + points: Feature | FeatureCollection, + polygons: Feature | FeatureCollection | G ): FeatureCollection; diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index 49fefc1fa2..2bc9494f27 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -1,33 +1,33 @@ -import booleanPointInPolygon from '@turf/boolean-point-in-polygon'; +import pointInPolygon from '@turf/boolean-point-in-polygon'; import { featureCollection } from '@turf/helpers'; +import { geomEach, featureEach } from '@turf/meta'; /** - * Takes a set of {@link Points} and a set of {@link (Multi)Polygons} and returns the points that fall within the polygons. + * Finds {@link Points} that fall within {@link (Multi)Polygon(s)}. * * @name pointsWithinPolygon - * @param {FeatureCollection} points input points - * @param {FeatureCollection} polygons input polygons + * @param {Feauture|FeatureCollection} points Points as input search + * @param {FeatureCollection|Geoemtry|Feature} polygons Points must be within these (Multi)Polygon(s) * @returns {FeatureCollection} points that land within at least one polygon * @example - * var searchWithin = turf.featureCollection([ - * turf.polygon([[ - * [-46.653,-23.543], - * [-46.634,-23.5346], - * [-46.613,-23.543], - * [-46.614,-23.559], - * [-46.631,-23.567], - * [-46.653,-23.560], - * [-46.653,-23.543] - * ]]) - * ]); - * var points = turf.featureCollection([ - * turf.point([-46.6318, -23.5523]), - * turf.point([-46.6246, -23.5325]), - * turf.point([-46.6062, -23.5513]), - * turf.point([-46.663, -23.554]), - * turf.point([-46.643, -23.557]) + * var points = turf.points([ + * [-46.6318, -23.5523], + * [-46.6246, -23.5325], + * [-46.6062, -23.5513], + * [-46.663, -23.554], + * [-46.643, -23.557] * ]); * + * var searchWithin = turf.polygon([[ + * [-46.653,-23.543], + * [-46.634,-23.5346], + * [-46.613,-23.543], + * [-46.614,-23.559], + * [-46.631,-23.567], + * [-46.653,-23.560], + * [-46.653,-23.543] + * ]]); + * * var ptsWithin = turf.pointsWithinPolygon(points, searchWithin); * * //addToMap @@ -38,16 +38,13 @@ import { featureCollection } from '@turf/helpers'; * }); */ function pointsWithinPolygon(points, polygons) { - var pointsWithin = featureCollection([]); - for (var i = 0; i < polygons.features.length; i++) { - for (var j = 0; j < points.features.length; j++) { - var isInside = booleanPointInPolygon(points.features[j], polygons.features[i]); - if (isInside) { - pointsWithin.features.push(points.features[j]); - } - } - } - return pointsWithin; + var results = []; + geomEach(polygons, function (polygon) { + featureEach(points, function (point) { + if (pointInPolygon(point, polygon)) results.push(point); + }); + }); + return featureCollection(results); } export default pointsWithinPolygon; diff --git a/packages/turf-points-within-polygon/package.json b/packages/turf-points-within-polygon/package.json index 4edb31600d..3440f236f7 100644 --- a/packages/turf-points-within-polygon/package.json +++ b/packages/turf-points-within-polygon/package.json @@ -41,7 +41,8 @@ }, "dependencies": { "@turf/boolean-point-in-polygon": "^5.0.4", - "@turf/helpers": "^5.0.4" + "@turf/helpers": "^5.0.4", + "@turf/meta": "*" }, "@std/esm": { "esm": "js", diff --git a/packages/turf-points-within-polygon/test.js b/packages/turf-points-within-polygon/test.js index 48c0c87c6e..fd8a1f29ac 100644 --- a/packages/turf-points-within-polygon/test.js +++ b/packages/turf-points-within-polygon/test.js @@ -1,5 +1,5 @@ import test from 'tape'; -import { point } from '@turf/helpers'; +import { point, points } from '@turf/helpers'; import { polygon } from '@turf/helpers'; import { featureCollection } from '@turf/helpers'; import pointsWithinPolygon from '.'; @@ -34,3 +34,26 @@ test('turf-points-within-polygon', t => { t.ok(counted, 'returns a featurecollection'); t.equal(counted.features.length, 5, 'multiple points in multiple polygons'); }); + +test('turf-points-within-polygon -- support extra geometry', t => { + const pts = points([ + [-46.6318, -23.5523], + [-46.6246, -23.5325], + [-46.6062, -23.5513], + [-46.663, -23.554], + [-46.643, -23.557] + ]); + const searchWithin = polygon([[ + [-46.653,-23.543], + [-46.634,-23.5346], + [-46.613,-23.543], + [-46.614,-23.559], + [-46.631,-23.567], + [-46.653,-23.560], + [-46.653,-23.543] + ]]); + t.assert(pointsWithinPolygon(pts, searchWithin)); + t.assert(pointsWithinPolygon(pts.features[0], searchWithin)); + t.assert(pointsWithinPolygon(pts, searchWithin.geometry)); + t.end() +}) \ No newline at end of file diff --git a/packages/turf-points-within-polygon/types.ts b/packages/turf-points-within-polygon/types.ts new file mode 100644 index 0000000000..02f55ba6c1 --- /dev/null +++ b/packages/turf-points-within-polygon/types.ts @@ -0,0 +1,20 @@ +import pointsWithinPolygon from './' +import { points, polygon } from '@turf/helpers' + +const pts = points([ + [-46.6318, -23.5523], + [-46.6246, -23.5325], + [-46.6062, -23.5513], + [-46.663, -23.554], + [-46.643, -23.557] +]); +const searchWithin = polygon([[ + [-46.653,-23.543], + [-46.634,-23.5346], + [-46.613,-23.543], + [-46.614,-23.559], + [-46.631,-23.567], + [-46.653,-23.560], + [-46.653,-23.543] +]]); +const ptsWithin = pointsWithinPolygon(pts, searchWithin); diff --git a/packages/turf-points-within-polygon/yarn.lock b/packages/turf-points-within-polygon/yarn.lock index 3040805408..8e10502100 100644 --- a/packages/turf-points-within-polygon/yarn.lock +++ b/packages/turf-points-within-polygon/yarn.lock @@ -6,6 +6,28 @@ version "0.13.0" resolved "https://registry.yarnpkg.com/@std/esm/-/esm-0.13.0.tgz#b2ca2f7d96f50eec14bd14f0b8afc3138b2a40d4" +"@turf/boolean-point-in-polygon@^5.0.4": + version "5.0.4" + resolved "https://registry.yarnpkg.com/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-5.0.4.tgz#70702d8a67368512d1de8c69cb71f8c7ee3747ba" + dependencies: + "@turf/invariant" "^5.0.4" + +"@turf/helpers@^5.0.4": + version "5.0.4" + resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-5.0.4.tgz#e47a4e4f38dee3b47a3177a69de162d7a7a5837f" + +"@turf/invariant@^5.0.4": + version "5.0.4" + resolved "https://registry.yarnpkg.com/@turf/invariant/-/invariant-5.0.4.tgz#1cc305f4de7c0f1bfd9d7c420aac282051262b41" + dependencies: + "@turf/helpers" "^5.0.4" + +"@turf/meta@*": + version "5.0.4" + resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-5.0.4.tgz#b41d08f19d2ecc934805b6d713a663abd9f83213" + dependencies: + "@turf/helpers" "^5.0.4" + balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" diff --git a/scripts/generate-readmes b/scripts/generate-readmes index 32809a8924..8ca0ac23e2 100755 --- a/scripts/generate-readmes +++ b/scripts/generate-readmes @@ -16,18 +16,21 @@ const paths = { Points: 'http://geojson.org/geojson-spec.html#point', '(Multi)Point': 'http://geojson.org/geojson-spec.html#point', '(Multi)Points': 'http://geojson.org/geojson-spec.html#point', + '(Multi)Point(s)': 'http://geojson.org/geojson-spec.html#point', MultiPoint: 'http://geojson.org/geojson-spec.html#multipoint', MultiPoints: 'http://geojson.org/geojson-spec.html#multipoint', LineString: 'http://geojson.org/geojson-spec.html#linestring', LineStrings: 'http://geojson.org/geojson-spec.html#linestring', '(Multi)LineString': 'http://geojson.org/geojson-spec.html#linestring', '(Multi)LineStrings': 'http://geojson.org/geojson-spec.html#linestring', + '(Multi)LineString(s)': 'http://geojson.org/geojson-spec.html#linestring', MultiLineString: 'http://geojson.org/geojson-spec.html#multilinestring', MultiLineStrings: 'http://geojson.org/geojson-spec.html#multilinestring', Polygon: 'http://geojson.org/geojson-spec.html#polygon', Polygons: 'http://geojson.org/geojson-spec.html#polygon', '(Multi)Polygon': 'http://geojson.org/geojson-spec.html#polygon', '(Multi)Polygons': 'http://geojson.org/geojson-spec.html#polygon', + '(Multi)Polygon(s)': 'http://geojson.org/geojson-spec.html#polygon', MultiPolygon: 'http://geojson.org/geojson-spec.html#multipolygon', MultiPolygons: 'http://geojson.org/geojson-spec.html#multipolygon', Geometry: 'http://geojson.org/geojson-spec.html#geometry',