turf-within
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -19,6 +19,10 @@ var within = require('./'); | ||
global.within = within; | ||
global.polyFC = polyFC; | ||
global.ptFC = ptFC; | ||
var suite = new Benchmark.Suite('turf-within'); | ||
suite | ||
.add('turf-within',function () { | ||
within(ptFC, polyFC); | ||
global.within(global.ptFC, global.polyFC); | ||
}) | ||
@@ -31,2 +35,2 @@ .on('cycle', function (event) { | ||
}) | ||
.run(); | ||
.run(); |
97
index.js
var inside = require('turf-inside'); | ||
var featureCollection = require('turf-featurecollection'); | ||
/** | ||
* Takes a {@link FeatureCollection} of {@link Point} features and a FeatureCollection of {@link Polygon} features and returns a FeatureCollection of Point features representing all points that fall within a collection of polygons. | ||
* | ||
* @module turf/within | ||
* @category joins | ||
* @param {FeatureCollection} points a FeatureCollection of {@link Point} features | ||
* @param {FeatureCollection} polygons a FeatureCollection of {@link Polygon} features | ||
* @return {FeatureCollection} a collection of all points that land | ||
* within at least one polygon | ||
* @example | ||
* var searchWithin = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[ | ||
* [-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 = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [-46.6318, -23.5523] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [-46.6246, -23.5325] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [-46.6062, -23.5513] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [-46.663, -23.554] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [-46.643, -23.557] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* | ||
* var ptsWithin = turf.within(points, searchWithin); | ||
* | ||
* //=points | ||
* | ||
* //=searchWithin | ||
* | ||
* //=ptsWithin | ||
*/ | ||
module.exports = function(ptFC, polyFC){ | ||
pointsWithin = featureCollection([]); | ||
polyFC.features.forEach(function(poly){ | ||
ptFC.features.forEach(function(pt){ | ||
var isInside = inside(pt, poly); | ||
var pointsWithin = featureCollection([]); | ||
for (var i = 0; i < polyFC.features.length; i++) { | ||
for (var j = 0; j < ptFC.features.length; j++) { | ||
var isInside = inside(ptFC.features[j], polyFC.features[i]); | ||
if(isInside){ | ||
pointsWithin.features.push(pt); | ||
pointsWithin.features.push(ptFC.features[j]); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
return pointsWithin; | ||
} | ||
}; |
{ | ||
"name": "turf-within", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "turf within module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js" | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/morganherlocker/turf-within.git" | ||
"url": "https://github.com/Turfjs/turf-within.git" | ||
}, | ||
@@ -23,15 +24,17 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/morganherlocker/turf-within/issues" | ||
"url": "https://github.com/Turfjs/turf-within/issues" | ||
}, | ||
"homepage": "https://github.com/morganherlocker/turf-within", | ||
"homepage": "https://github.com/Turfjs/turf-within", | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"tape": "^3.0.3", | ||
"turf-point": "^1.2.0", | ||
"turf-polygon": "^1.0.0" | ||
"tape": "^3.5.0", | ||
"turf-point": "^2.0.0", | ||
"turf-polygon": "^1.0.2", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
}, | ||
"dependencies": { | ||
"turf-featurecollection": "^1.0.0", | ||
"turf-inside": "^1.1.2" | ||
"turf-inside": "^1.1.3" | ||
} | ||
} |
@@ -1,47 +0,61 @@ | ||
turf-within | ||
=========== | ||
[![Build Status](https://travis-ci.org/Turfjs/turf-within.svg)](https://travis-ci.org/Turfjs/turf-within) | ||
# turf-within | ||
Returns a feature collection of points representing all points that fall withing a collection of polygons. | ||
[![build status](https://secure.travis-ci.org/Turfjs/turf-within.png)](http://travis-ci.org/Turfjs/turf-within) | ||
###Install | ||
turf within module | ||
```sh | ||
npm install turf-within | ||
``` | ||
###Parameters | ||
### `turf.within(points, polygons)` | ||
|name|description| | ||
|---|---| | ||
|points|A FeatureCollection of Point Features| | ||
|polygons|A FeatureCollection of Polygon Features| | ||
Returns a FeatureCollection of points representing all points that fall | ||
within a collection of polygons. | ||
###Usage | ||
### Parameters | ||
| parameter | type | description | | ||
| ---------- | ----------------- | ----------- | | ||
| `points` | FeatureCollection | | | ||
| `polygons` | FeatureCollection | | | ||
### Example | ||
```js | ||
within(points, polygons) | ||
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 ptsWithin = turf.within(points, searchWithin); | ||
//=points | ||
//=searchWithin | ||
//=ptsWithin | ||
``` | ||
###Example | ||
## Installation | ||
```js | ||
var within = require('turf-within') | ||
var point = require('turf-point') | ||
var polygon = require('turf-polygon') | ||
var featurecollection = require('turf-featurecollection') | ||
Requires [nodejs](http://nodejs.org/). | ||
```sh | ||
$ npm install turf-within | ||
``` | ||
var poly = polygon([[[10,0],[20,10],[20,20], [20,0]]]) | ||
var polyFC = featurecollection([poly]) | ||
var pt1 = point(1,1) | ||
var pt2 = point(1,3) | ||
var pt3 = point(14,2) | ||
var pt4 = point(13,1) | ||
var pt5 = point(19,7) | ||
var ptFC = featurecollection([pt1, pt2, pt3, pt4, pt5]) | ||
## Tests | ||
var ptsWithin = within(ptFC, polyFC) | ||
```sh | ||
$ npm test | ||
``` | ||
console.log(ptsWithin) // feature collection with 3 pts | ||
``` |
22
test.js
@@ -11,4 +11,4 @@ var test = require('tape'); | ||
// test with a single point | ||
var poly = polygon([[[0,0], [0,100], [100,100], [100,0]]]); | ||
var pt = point(50, 50); | ||
var poly = polygon([[[0,0], [0,100], [100,100], [100,0],[0,0]]]); | ||
var pt = point([50, 50]); | ||
var polyFC = featureCollection([poly]); | ||
@@ -23,11 +23,11 @@ var ptFC = featureCollection([pt]); | ||
// test with multiple points and multiple polygons | ||
var poly1 = polygon([[[0,0],[10,0],[10,10],[0,10]]]); | ||
var poly2 = polygon([[[10,0],[20,10],[20,20], [20,0]]]); | ||
var poly1 = polygon([[[0,0],[10,0],[10,10],[0,10],[0,0]]]); | ||
var poly2 = polygon([[[10,0],[20,10],[20,20], [20,0],[10,0]]]); | ||
var polyFC = featureCollection([poly1, poly2]); | ||
var pt1 = point(1,1, {population: 500}); | ||
var pt2 = point(1,3, {population: 400}); | ||
var pt3 = point(14,2, {population: 600}); | ||
var pt4 = point(13,1, {population: 500}); | ||
var pt5 = point(19,7, {population: 200}); | ||
var pt6 = point(100,7, {population: 200}); | ||
var pt1 = point([1,1], {population: 500}); | ||
var pt2 = point([1,3], {population: 400}); | ||
var pt3 = point([14,2], {population: 600}); | ||
var pt4 = point([13,1], {population: 500}); | ||
var pt5 = point([19,7], {population: 200}); | ||
var pt6 = point([100,7], {population: 200}); | ||
var ptFC = featureCollection([pt1, pt2, pt3, pt4, pt5, pt6]); | ||
@@ -38,2 +38,2 @@ | ||
t.equal(counted.features.length, 5, 'multiple points in multiple polygons'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8717
155
62
6
Updatedturf-inside@^1.1.3