From 114d333dd790999c5041f23b2a18c125a65a8ce3 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 17:27:35 -0700 Subject: [PATCH 01/13] pointsWithinPolygon: add multipoint support --- packages/turf-points-within-polygon/index.js | 6 +- packages/turf-points-within-polygon/test.js | 90 +++++++++++++++++++- 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index 69b2a9bf0f..31b5b5cfa8 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -1,6 +1,6 @@ import pointInPolygon from "@turf/boolean-point-in-polygon"; import { featureCollection } from "@turf/helpers"; -import { geomEach, featureEach } from "@turf/meta"; +import { geomEach, featureEach, coordEach } from "@turf/meta"; /** * Finds {@link Points} that fall within {@link (Multi)Polygon(s)}. @@ -42,7 +42,9 @@ function pointsWithinPolygon(points, polygons) { featureEach(points, function (point) { var contained = false; geomEach(polygons, function (polygon) { - if (pointInPolygon(point, polygon)) contained = true; + coordEach(point, function (pointCoord) { + if (pointInPolygon(pointCoord, polygon)) contained = true; + }); }); if (contained) { results.push(point); diff --git a/packages/turf-points-within-polygon/test.js b/packages/turf-points-within-polygon/test.js index 0f967a9fdb..984d14f233 100644 --- a/packages/turf-points-within-polygon/test.js +++ b/packages/turf-points-within-polygon/test.js @@ -1,10 +1,10 @@ import test from "tape"; -import { point, points } from "@turf/helpers"; +import { multiPoint, point, points } from "@turf/helpers"; import { polygon } from "@turf/helpers"; import { featureCollection } from "@turf/helpers"; import pointsWithinPolygon from "./index"; -test("turf-points-within-polygon", (t) => { +test("turf-points-within-polygon -- single point", (t) => { t.plan(4); // test with a single point @@ -59,7 +59,91 @@ test("turf-points-within-polygon", (t) => { t.equal(counted.features.length, 5, "multiple points in multiple polygons"); }); -test("turf-points-within-polygon -- support extra geometry", (t) => { +test("turf-points-within-polygon -- single multipoint", (t) => { + t.plan(8); + + const poly1 = polygon([ + [ + [0, 0], + [0, 100], + [100, 100], + [100, 0], + [0, 0], + ], + ]); + + const mpt1 = multiPoint([[50, 50]]); // inside poly1 + const mpt2 = multiPoint([[1000, 1000]]); // outside poly1 + const mpt3 = multiPoint([ + [50, 50], + [1000, 1000], + ]); // inside and outside poly1 + const mpt1FC = featureCollection([mpt1]); + const polyFC = featureCollection([poly1]); + + // multipoint within + const mpWithin = pointsWithinPolygon(mpt1, polyFC); + t.ok( + mpWithin && mpWithin.type === "FeatureCollection", + "returns a featurecollection" + ); + t.equal(mpWithin.features.length, 1, "1 multipoint in 1 polygon"); + t.equal( + mpWithin.features[0].geometry.type, + "MultiPoint", + "1 multipoint with correct type" + ); + + // multipoint fc within + const fcWithin = pointsWithinPolygon(mpt1FC, polyFC); + t.ok( + fcWithin && fcWithin.type === "FeatureCollection", + "returns a featurecollection" + ); + t.equal(fcWithin.features.length, 1, "1 multipoint in 1 polygon"); + + // multipoint not within + const mpNotWithin = pointsWithinPolygon(mpt2, polyFC); + t.ok( + mpNotWithin && mpNotWithin.type === "FeatureCollection", + "returns an empty featurecollection" + ); + t.equal(mpNotWithin.features.length, 0, "0 multipoint in 1 polygon"); + + // multipoint with point coords both within and not within + const mpPartWithin = pointsWithinPolygon(mpt3, polyFC); + t.ok(mpPartWithin, "returns a featurecollection"); + // maintains the whole multipoint, including coordinates that are outside the polygon(s) + t.equal( + mpPartWithin.features[0].geometry.coordinates.length, + 2, + "1 multipoint with 2 points in polygon" + ); + + // multiple multipoints and multiple polygons + + const poly2 = polygon([ + [ + [10, 0], + [20, 10], + [20, 20], + [20, 0], + [10, 0], + ], + ]); + var mptFC = featureCollection([mpt1, mpt2, mpt3]); + var poly2FC = featureCollection([poly1, poly2]); + + var fcMultiWithin = pointsWithinPolygon(mptFC, poly2FC); + t.ok(fcMultiWithin, "returns a featurecollection"); + t.equal( + fcMultiWithin.features.length, + 2, + "multiple points in multiple polygons" + ); +}); + +test("turf-points-within-polygon -- support extra point geometry", (t) => { const pts = points([ [-46.6318, -23.5523], [-46.6246, -23.5325], From bf08807c73fbd66bf86d3624de2ce41e427c1768 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 20:30:21 -0700 Subject: [PATCH 02/13] add multipoint coord filtering --- packages/turf-points-within-polygon/index.js | 29 +++++++++++++++----- packages/turf-points-within-polygon/test.js | 19 ++++++++----- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index 31b5b5cfa8..58c04575b4 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -1,5 +1,5 @@ import pointInPolygon from "@turf/boolean-point-in-polygon"; -import { featureCollection } from "@turf/helpers"; +import { featureCollection, multiPoint } from "@turf/helpers"; import { geomEach, featureEach, coordEach } from "@turf/meta"; /** @@ -41,13 +41,28 @@ function pointsWithinPolygon(points, polygons) { var results = []; featureEach(points, function (point) { var contained = false; - geomEach(polygons, function (polygon) { - coordEach(point, function (pointCoord) { - if (pointInPolygon(pointCoord, polygon)) contained = true; + if (point.geometry.type === "Point") { + geomEach(polygons, function (polygon) { + if (pointInPolygon(point, polygon)) contained = true; }); - }); - if (contained) { - results.push(point); + if (contained) { + results.push(point); + } + } else if (point.geometry.type === "MultiPoint") { + let pointsWithin = []; + geomEach(polygons, function (polygon) { + coordEach(point, function (pointCoord) { + if (pointInPolygon(pointCoord, polygon)) { + contained = true; + pointsWithin.push(pointCoord); + } + }); + }); + if (contained) { + results.push(multiPoint(pointsWithin)); + } + } else { + throw new Error("Input geometry must be a Point or MultiPoint"); } }); return featureCollection(results); diff --git a/packages/turf-points-within-polygon/test.js b/packages/turf-points-within-polygon/test.js index 984d14f233..4fd2195b0b 100644 --- a/packages/turf-points-within-polygon/test.js +++ b/packages/turf-points-within-polygon/test.js @@ -60,7 +60,7 @@ test("turf-points-within-polygon -- single point", (t) => { }); test("turf-points-within-polygon -- single multipoint", (t) => { - t.plan(8); + t.plan(12); const poly1 = polygon([ [ @@ -73,10 +73,10 @@ test("turf-points-within-polygon -- single multipoint", (t) => { ]); const mpt1 = multiPoint([[50, 50]]); // inside poly1 - const mpt2 = multiPoint([[1000, 1000]]); // outside poly1 + const mpt2 = multiPoint([[150, 150]]); // outside poly1 const mpt3 = multiPoint([ [50, 50], - [1000, 1000], + [150, 150], ]); // inside and outside poly1 const mpt1FC = featureCollection([mpt1]); const polyFC = featureCollection([poly1]); @@ -113,11 +113,16 @@ test("turf-points-within-polygon -- single multipoint", (t) => { // multipoint with point coords both within and not within const mpPartWithin = pointsWithinPolygon(mpt3, polyFC); t.ok(mpPartWithin, "returns a featurecollection"); - // maintains the whole multipoint, including coordinates that are outside the polygon(s) + const partCoords = mpPartWithin.features[0].geometry.coordinates; t.equal( - mpPartWithin.features[0].geometry.coordinates.length, - 2, - "1 multipoint with 2 points in polygon" + partCoords.length, + 1, + "multipoint result should have 1 remaining coord that was within polygon" + ); + t.equal( + partCoords[0][0] === 50 && partCoords[0][1] === 50, + true, + "remaining coord should have expected values" ); // multiple multipoints and multiple polygons From 0133fafb06d78391848d1db9802cb8b12ee45c48 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 21:14:20 -0700 Subject: [PATCH 03/13] update jsdoc --- packages/turf-points-within-polygon/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index 58c04575b4..32f0ffadc4 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -3,12 +3,12 @@ import { featureCollection, multiPoint } from "@turf/helpers"; import { geomEach, featureEach, coordEach } from "@turf/meta"; /** - * Finds {@link Points} that fall within {@link (Multi)Polygon(s)}. + * Finds {@link Points} or {@link MultiPoint} coordinates that fall within {@link (Multi)Polygon(s)}. * * @name pointsWithinPolygon - * @param {Feature|FeatureCollection} points Points as input search - * @param {FeatureCollection|Geometry|Feature} polygons Points must be within these (Multi)Polygon(s) - * @returns {FeatureCollection} points that land within at least one polygon + * @param {Feature|FeatureCollection} points Points or MultiPoints as input search + * @param {FeatureCollection|Geometry|Feature} polygons Polygon(s) or MultiPolygon(s) to check if points are within + * @returns {FeatureCollection} Points that land within at least one polygon * @example * var points = turf.points([ * [-46.6318, -23.5523], From cda7a14c480adadbfb4c9eca07872a9e6213ef19 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 21:16:43 -0700 Subject: [PATCH 04/13] doc consolidate --- packages/turf-points-within-polygon/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index 32f0ffadc4..d8923966c0 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -6,9 +6,9 @@ import { geomEach, featureEach, coordEach } from "@turf/meta"; * Finds {@link Points} or {@link MultiPoint} coordinates that fall within {@link (Multi)Polygon(s)}. * * @name pointsWithinPolygon - * @param {Feature|FeatureCollection} points Points or MultiPoints as input search - * @param {FeatureCollection|Geometry|Feature} polygons Polygon(s) or MultiPolygon(s) to check if points are within - * @returns {FeatureCollection} Points that land within at least one polygon + * @param {Feature|FeatureCollection} points (Multi)Point(s) as input search + * @param {FeatureCollection|Geometry|Feature} polygons (Multi)Polygon(s) to check if points are within + * @returns {FeatureCollection} (Multi)Points that land within at least one polygon * @example * var points = turf.points([ * [-46.6318, -23.5523], From 72a2f6f96dc4e540e59e16a15f08acc7300b3a92 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 21:26:17 -0700 Subject: [PATCH 05/13] tighten up comparison --- packages/turf-points-within-polygon/test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/turf-points-within-polygon/test.js b/packages/turf-points-within-polygon/test.js index 4fd2195b0b..56887033f4 100644 --- a/packages/turf-points-within-polygon/test.js +++ b/packages/turf-points-within-polygon/test.js @@ -120,7 +120,8 @@ test("turf-points-within-polygon -- single multipoint", (t) => { "multipoint result should have 1 remaining coord that was within polygon" ); t.equal( - partCoords[0][0] === 50 && partCoords[0][1] === 50, + partCoords[0][0] === mpt3.geometry.coordinates[0][0] && + partCoords[0][1] === mpt3.geometry.coordinates[0][1], true, "remaining coord should have expected values" ); From 5f30fb2df7297e76e1f9ffd48507936e42804d20 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 21:39:39 -0700 Subject: [PATCH 06/13] update types --- .../turf-points-within-polygon/index.d.ts | 5 ++++- packages/turf-points-within-polygon/types.ts | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/turf-points-within-polygon/index.d.ts b/packages/turf-points-within-polygon/index.d.ts index 794819b9d7..61e12972d3 100644 --- a/packages/turf-points-within-polygon/index.d.ts +++ b/packages/turf-points-within-polygon/index.d.ts @@ -3,6 +3,7 @@ import { FeatureCollection, Polygon, MultiPolygon, + MultiPoint, Point, Properties, } from "@turf/helpers"; @@ -14,6 +15,8 @@ export default function pointsWithinPolygon< G extends Polygon | MultiPolygon, P = Properties >( - points: Feature | FeatureCollection, + points: + | Feature + | FeatureCollection, polygons: Feature | FeatureCollection | G ): FeatureCollection; diff --git a/packages/turf-points-within-polygon/types.ts b/packages/turf-points-within-polygon/types.ts index 7253a2f1e6..a7a8679b9d 100644 --- a/packages/turf-points-within-polygon/types.ts +++ b/packages/turf-points-within-polygon/types.ts @@ -1,5 +1,5 @@ import pointsWithinPolygon from "./"; -import { points, polygon } from "@turf/helpers"; +import { points, polygon, multiPoint, featureCollection } from "@turf/helpers"; const pts = points([ [-46.6318, -23.5523], @@ -8,6 +8,22 @@ const pts = points([ [-46.663, -23.554], [-46.643, -23.557], ]); +const mpt1 = multiPoint( + [ + [50, 50], + [100, 100], + ], + {} +); +const mpt2 = multiPoint( + [ + [75, 75], + [150, 150], + ], + {} +); +const mpts = featureCollection([mpt1, mpt2]); + const searchWithin = polygon([ [ [-46.653, -23.543], @@ -20,3 +36,4 @@ const searchWithin = polygon([ ], ]); const ptsWithin = pointsWithinPolygon(pts, searchWithin); +const mptsWithin = pointsWithinPolygon(mpts, searchWithin); From 83aa26f945bbe8caecadad97450bbccee89676b2 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 21:42:45 -0700 Subject: [PATCH 07/13] fix return type --- packages/turf-points-within-polygon/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/turf-points-within-polygon/index.d.ts b/packages/turf-points-within-polygon/index.d.ts index 61e12972d3..b8f3addb63 100644 --- a/packages/turf-points-within-polygon/index.d.ts +++ b/packages/turf-points-within-polygon/index.d.ts @@ -19,4 +19,4 @@ export default function pointsWithinPolygon< | Feature | FeatureCollection, polygons: Feature | FeatureCollection | G -): FeatureCollection; +): FeatureCollection; From e515adbfe771e4edaab5cf57f647d5362738e253 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 22:18:11 -0700 Subject: [PATCH 08/13] change let to var for linter --- packages/turf-points-within-polygon/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index d8923966c0..eed85ff990 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -49,7 +49,7 @@ function pointsWithinPolygon(points, polygons) { results.push(point); } } else if (point.geometry.type === "MultiPoint") { - let pointsWithin = []; + var pointsWithin = []; geomEach(polygons, function (polygon) { coordEach(point, function (pointCoord) { if (pointInPolygon(pointCoord, polygon)) { From 5ced88380d80be03d64f0225e4bb5421be8a5dcb Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Mon, 28 Jun 2021 22:35:14 -0700 Subject: [PATCH 09/13] add point geom generic --- packages/turf-points-within-polygon/index.d.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/turf-points-within-polygon/index.d.ts b/packages/turf-points-within-polygon/index.d.ts index b8f3addb63..d3a31d3ad9 100644 --- a/packages/turf-points-within-polygon/index.d.ts +++ b/packages/turf-points-within-polygon/index.d.ts @@ -12,11 +12,10 @@ import { * http://turfjs.org/docs/#pointswithinpolygon */ export default function pointsWithinPolygon< + F extends Point | MultiPoint, G extends Polygon | MultiPolygon, P = Properties >( - points: - | Feature - | FeatureCollection, + points: Feature | FeatureCollection, polygons: Feature | FeatureCollection | G -): FeatureCollection; +): FeatureCollection; From 93fcd965a76054255d7a9ed52b778f6456ec9bcb Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Tue, 29 Jun 2021 08:20:21 -0700 Subject: [PATCH 10/13] doc fixes --- packages/turf-points-within-polygon/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index eed85ff990..f60bd67e40 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -3,12 +3,12 @@ import { featureCollection, multiPoint } from "@turf/helpers"; import { geomEach, featureEach, coordEach } from "@turf/meta"; /** - * Finds {@link Points} or {@link MultiPoint} coordinates that fall within {@link (Multi)Polygon(s)}. + * Finds {@link Points} or {@link MultiPoint} coordinate positions that fall within {@link (Multi)Polygon(s)}. * * @name pointsWithinPolygon * @param {Feature|FeatureCollection} points (Multi)Point(s) as input search * @param {FeatureCollection|Geometry|Feature} polygons (Multi)Polygon(s) to check if points are within - * @returns {FeatureCollection} (Multi)Points that land within at least one polygon + * @returns {FeatureCollection} (Multi)Points that land within at least one polygon * @example * var points = turf.points([ * [-46.6318, -23.5523], From 100207e07aa7267ee0ef85fffcb9404deb5d5069 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Tue, 6 Jul 2021 07:36:23 -0700 Subject: [PATCH 11/13] add mixed example --- packages/turf-points-within-polygon/types.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/turf-points-within-polygon/types.ts b/packages/turf-points-within-polygon/types.ts index a7a8679b9d..2085c244b8 100644 --- a/packages/turf-points-within-polygon/types.ts +++ b/packages/turf-points-within-polygon/types.ts @@ -1,5 +1,12 @@ import pointsWithinPolygon from "./"; -import { points, polygon, multiPoint, featureCollection } from "@turf/helpers"; +import { + points, + polygon, + multiPoint, + featureCollection, + Point, + MultiPoint, +} from "@turf/helpers"; const pts = points([ [-46.6318, -23.5523], @@ -37,3 +44,7 @@ const searchWithin = polygon([ ]); const ptsWithin = pointsWithinPolygon(pts, searchWithin); const mptsWithin = pointsWithinPolygon(mpts, searchWithin); + +// Accepts a mixture of Point and MultiPoint +const mix = featureCollection([...pts.features, mpt1]); +const mixWithin = pointsWithinPolygon(mix, searchWithin); From b17c8e801367dd784b3a0d75f0d3dd75b0fabf97 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Tue, 6 Jul 2021 07:38:07 -0700 Subject: [PATCH 12/13] update jsdoc --- packages/turf-points-within-polygon/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/turf-points-within-polygon/index.js b/packages/turf-points-within-polygon/index.js index f60bd67e40..75cbde298c 100644 --- a/packages/turf-points-within-polygon/index.js +++ b/packages/turf-points-within-polygon/index.js @@ -6,9 +6,9 @@ import { geomEach, featureEach, coordEach } from "@turf/meta"; * Finds {@link Points} or {@link MultiPoint} coordinate positions that fall within {@link (Multi)Polygon(s)}. * * @name pointsWithinPolygon - * @param {Feature|FeatureCollection} points (Multi)Point(s) as input search + * @param {Feature|FeatureCollection} points Point(s) or MultiPoint(s) as input search * @param {FeatureCollection|Geometry|Feature} polygons (Multi)Polygon(s) to check if points are within - * @returns {FeatureCollection} (Multi)Points that land within at least one polygon + * @returns {FeatureCollection} Point(s) or MultiPoint(s) with positions that land within at least one polygon. The geometry type will match what was passsed in * @example * var points = turf.points([ * [-46.6318, -23.5523], From 347d5498c524197caaac1607cf4d937bbf9d0219 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Tue, 6 Jul 2021 08:05:58 -0700 Subject: [PATCH 13/13] add mixed test --- packages/turf-points-within-polygon/test.js | 55 +++++++++++++++------ 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/packages/turf-points-within-polygon/test.js b/packages/turf-points-within-polygon/test.js index 56887033f4..62826e7940 100644 --- a/packages/turf-points-within-polygon/test.js +++ b/packages/turf-points-within-polygon/test.js @@ -4,7 +4,7 @@ import { polygon } from "@turf/helpers"; import { featureCollection } from "@turf/helpers"; import pointsWithinPolygon from "./index"; -test("turf-points-within-polygon -- single point", (t) => { +test("turf-points-within-polygon -- point", (t) => { t.plan(4); // test with a single point @@ -59,10 +59,10 @@ test("turf-points-within-polygon -- single point", (t) => { t.equal(counted.features.length, 5, "multiple points in multiple polygons"); }); -test("turf-points-within-polygon -- single multipoint", (t) => { +test("turf-points-within-polygon -- multipoint", (t) => { t.plan(12); - const poly1 = polygon([ + var poly1 = polygon([ [ [0, 0], [0, 100], @@ -72,17 +72,17 @@ test("turf-points-within-polygon -- single multipoint", (t) => { ], ]); - const mpt1 = multiPoint([[50, 50]]); // inside poly1 - const mpt2 = multiPoint([[150, 150]]); // outside poly1 - const mpt3 = multiPoint([ + var mpt1 = multiPoint([[50, 50]]); // inside poly1 + var mpt2 = multiPoint([[150, 150]]); // outside poly1 + var mpt3 = multiPoint([ [50, 50], [150, 150], ]); // inside and outside poly1 - const mpt1FC = featureCollection([mpt1]); - const polyFC = featureCollection([poly1]); + var mpt1FC = featureCollection([mpt1]); + var polyFC = featureCollection([poly1]); // multipoint within - const mpWithin = pointsWithinPolygon(mpt1, polyFC); + var mpWithin = pointsWithinPolygon(mpt1, polyFC); t.ok( mpWithin && mpWithin.type === "FeatureCollection", "returns a featurecollection" @@ -95,7 +95,7 @@ test("turf-points-within-polygon -- single multipoint", (t) => { ); // multipoint fc within - const fcWithin = pointsWithinPolygon(mpt1FC, polyFC); + var fcWithin = pointsWithinPolygon(mpt1FC, polyFC); t.ok( fcWithin && fcWithin.type === "FeatureCollection", "returns a featurecollection" @@ -103,7 +103,7 @@ test("turf-points-within-polygon -- single multipoint", (t) => { t.equal(fcWithin.features.length, 1, "1 multipoint in 1 polygon"); // multipoint not within - const mpNotWithin = pointsWithinPolygon(mpt2, polyFC); + var mpNotWithin = pointsWithinPolygon(mpt2, polyFC); t.ok( mpNotWithin && mpNotWithin.type === "FeatureCollection", "returns an empty featurecollection" @@ -111,9 +111,9 @@ test("turf-points-within-polygon -- single multipoint", (t) => { t.equal(mpNotWithin.features.length, 0, "0 multipoint in 1 polygon"); // multipoint with point coords both within and not within - const mpPartWithin = pointsWithinPolygon(mpt3, polyFC); + var mpPartWithin = pointsWithinPolygon(mpt3, polyFC); t.ok(mpPartWithin, "returns a featurecollection"); - const partCoords = mpPartWithin.features[0].geometry.coordinates; + var partCoords = mpPartWithin.features[0].geometry.coordinates; t.equal( partCoords.length, 1, @@ -128,7 +128,7 @@ test("turf-points-within-polygon -- single multipoint", (t) => { // multiple multipoints and multiple polygons - const poly2 = polygon([ + var poly2 = polygon([ [ [10, 0], [20, 10], @@ -149,6 +149,33 @@ test("turf-points-within-polygon -- single multipoint", (t) => { ); }); +test("turf-points-within-polygon -- point and multipoint", (t) => { + t.plan(4); + + var poly = polygon([ + [ + [0, 0], + [0, 100], + [100, 100], + [100, 0], + [0, 0], + ], + ]); + var polyFC = featureCollection([poly]); + + var pt = point([50, 50]); + var mpt1 = multiPoint([[50, 50]]); // inside poly1 + var mpt2 = multiPoint([[150, 150]]); // outside poly1 + var mixedFC = featureCollection([pt, mpt1, mpt2]); + + var counted = pointsWithinPolygon(mixedFC, polyFC); + + t.ok(counted, "returns a featurecollection"); + t.equal(counted.features.length, 2, "1 point and 1 multipoint in 1 polygon"); + t.equal(counted.features[0].geometry.type, "Point"); + t.equal(counted.features[1].geometry.type, "MultiPoint"); +}); + test("turf-points-within-polygon -- support extra point geometry", (t) => { const pts = points([ [-46.6318, -23.5523],