Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra geometry support to @turf/points-within-polygon #1117

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions packages/turf-points-within-polygon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/turf-points-within-polygon/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<Point>,
polygons: FeatureCollection<Polygon | MultiPolygon>
export default function pointsWithinPolygon<G extends Polygon | MultiPolygon>(
points: Feature<Point> | FeatureCollection<Point>,
polygons: Feature<G> | FeatureCollection<G> | G
): FeatureCollection<Point>;
59 changes: 28 additions & 31 deletions packages/turf-points-within-polygon/index.js
Original file line number Diff line number Diff line change
@@ -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<Point>} points input points
* @param {FeatureCollection<Polygon|MultiPolygon>} polygons input polygons
* @param {Feauture|FeatureCollection<Point>} points Points as input search
* @param {FeatureCollection|Geoemtry|Feature<Polygon|MultiPolygon>} polygons Points must be within these (Multi)Polygon(s)
* @returns {FeatureCollection<Point>} 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
Expand All @@ -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;
3 changes: 2 additions & 1 deletion packages/turf-points-within-polygon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 24 additions & 1 deletion packages/turf-points-within-polygon/test.js
Original file line number Diff line number Diff line change
@@ -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 '.';
Expand Down Expand Up @@ -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()
})
20 changes: 20 additions & 0 deletions packages/turf-points-within-polygon/types.ts
Original file line number Diff line number Diff line change
@@ -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);
22 changes: 22 additions & 0 deletions packages/turf-points-within-polygon/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions scripts/generate-readmes
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down