Comparing version 1.0.0 to 1.0.1
57
index.js
@@ -1,21 +0,56 @@ | ||
inside = require('turf-inside'); | ||
var inside = require('turf-inside'); | ||
/** | ||
* Takes a {@link FeatureCollection} of {@link Point} features and a FeatureCollection of {@link Polygon} features and performs a spatial join. | ||
* | ||
* @module turf/tag | ||
* @category joins | ||
* @param {FeatureCollection} points a FeatureCollection of {@link Point} features | ||
* @param {FeatureCollection} polygons a FeatureCollection of {@link Polygon} features | ||
* @param {String} polyId property in `polygons` to add to joined Point features | ||
* @param {String} containingPolyId property in `points` in which to store joined property from `polygons | ||
* @return {FeatureCollection} a FeatureCollection of point features | ||
* @example | ||
* var bbox = [0, 0, 50, 50]; | ||
* // create a triangular grid of polygons | ||
* var triangleGrid = turf.tin(turf.grid(bbox, 10)); | ||
* triangleGrid.features.forEach(function(f) { | ||
* f.properties.fill = '#' + | ||
* (~~(Math.random() * 16)).toString(16) + | ||
* (~~(Math.random() * 16)).toString(16) + | ||
* (~~(Math.random() * 16)).toString(16); | ||
* f.properties.stroke = 0; | ||
* f.properties['fill-opacity'] = 1; | ||
* }); | ||
* var randomPoints = turf.random('point', 30, { | ||
* bbox: bbox | ||
* }); | ||
* var both = turf.featurecollection( | ||
* triangleGrid.features.concat(randomPoints.features)); | ||
* | ||
* //=both | ||
* | ||
* var tagged = turf.tag(randomPoints, triangleGrid, | ||
* 'fill', 'marker-color'); | ||
* | ||
* //=tagged | ||
*/ | ||
module.exports = function(points, polygons, field, outField){ | ||
points.features.forEach(function(pt){ | ||
if(!pt.properties){ | ||
// prevent mutations | ||
points = JSON.parse(JSON.stringify(points)); | ||
polygons = JSON.parse(JSON.stringify(polygons)); | ||
points.features.forEach(function(pt) { | ||
if (!pt.properties) { | ||
pt.properties = {}; | ||
} | ||
polygons.features.forEach(function(poly){ | ||
if(!pt.properties[outField]){ | ||
polygons.features.forEach(function(poly) { | ||
if (pt.properties[outField] === undefined) { | ||
var isInside = inside(pt, poly); | ||
if(isInside){ | ||
if (isInside) { | ||
pt.properties[outField] = poly.properties[field]; | ||
} | ||
else{ | ||
pt.properties[outField] = null; | ||
} | ||
} | ||
}); | ||
}) | ||
}); | ||
return points; | ||
} | ||
}; |
{ | ||
"name": "turf-tag", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "turf tag module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "node test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/morganherlocker/turf-tag.git" | ||
"url": "https://github.com/Turfjs/turf-tag.git" | ||
}, | ||
@@ -26,12 +27,14 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/morganherlocker/turf-tag/issues" | ||
"url": "https://github.com/Turfjs/turf-tag/issues" | ||
}, | ||
"homepage": "https://github.com/morganherlocker/turf-tag", | ||
"homepage": "https://github.com/Turfjs/turf-tag", | ||
"dependencies": { | ||
"turf-inside": "^1.1.2" | ||
"turf-inside": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"tape": "^3.0.3" | ||
"tape": "^3.5.0", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
} | ||
} |
@@ -1,43 +0,64 @@ | ||
turf-tag | ||
======== | ||
[![Build Status](https://travis-ci.org/Turfjs/turf-tag.svg)](https://travis-ci.org/Turfjs/turf-tag) | ||
# turf-tag | ||
Performs a spatial join on a set of points from a set of polygons. | ||
[![build status](https://secure.travis-ci.org/Turfjs/turf-tag.png)](http://travis-ci.org/Turfjs/turf-tag) | ||
###Install | ||
turf tag module | ||
```sh | ||
npm install turf-inside | ||
``` | ||
###Parameters | ||
### `turf.tag(points, polygons, polyId, containingPolyId)` | ||
|name|description| | ||
|---|---| | ||
|points|A FeatureCollection of Point Features| | ||
|polygons|A FeatureCollection of Polygon Features| | ||
|polyID|id field on the polygons| | ||
|containingPolyID|id field to tag onto points that are in a corresponding polygon| | ||
Takes a FeatureCollection of Point features and a FeatureCollection of Polygon features and performs a spatial join. | ||
###Usage | ||
### Parameters | ||
| parameter | type | description | | ||
| ------------------ | ----------------- | --------------------------------------------------------------------- | | ||
| `points` | FeatureCollection | a FeatureCollection of Point features | | ||
| `polygons` | FeatureCollection | a FeatureCollection of Polygon features | | ||
| `polyId` | String | property in `polygons` to add to joined Point features | | ||
| `containingPolyId` | String | property in `points` in which to store joined property from `polygons | | ||
### Example | ||
```js | ||
tag(points, polygons, 'polyID', 'containingPolyID') | ||
var bbox = [0, 0, 50, 50]; | ||
// create a triangular grid of polygons | ||
var triangleGrid = turf.tin(turf.grid(bbox, 10)); | ||
triangleGrid.features.forEach(function(f) { | ||
f.properties.fill = '#' + | ||
(~~(Math.random() * 16)).toString(16) + | ||
(~~(Math.random() * 16)).toString(16) + | ||
(~~(Math.random() * 16)).toString(16); | ||
f.properties.stroke = 0; | ||
f.properties['fill-opacity'] = 1; | ||
}); | ||
var randomPoints = turf.random('point', 30, { | ||
bbox: bbox | ||
}); | ||
var both = turf.featurecollection( | ||
triangleGrid.features.concat(randomPoints.features)); | ||
//=both | ||
var tagged = turf.tag(randomPoints, triangleGrid, | ||
'fill', 'marker-color'); | ||
//=tagged | ||
``` | ||
###Example | ||
## Installation | ||
```js | ||
var tag = require('turf-tag') | ||
var fs = require('fs') | ||
Requires [nodejs](http://nodejs.org/). | ||
t.load('./testIn/tagPoints.geojson', function(err, points){ | ||
t.load('./testIn/tagPolygons.geojson', function(err, polygons){ | ||
```sh | ||
$ npm install turf-tag | ||
``` | ||
var pts = JSON.parse(fs.readFileSync('/path/to/pts.geojson')) | ||
var polys = JSON.parse(fs.readFileSync('/path/to/polys.geojson')) | ||
## Tests | ||
var tagged = tag(points, polygons, 'polyID', 'containingPolyID') | ||
```sh | ||
$ npm test | ||
``` | ||
console.log(taggedPoints) | ||
``` |
20
test.js
@@ -6,20 +6,16 @@ var test = require('tape'); | ||
test('tag', function(t){ | ||
t.plan(3); | ||
var points = JSON.parse(fs.readFileSync(__dirname + '/geojson/tagPoints.geojson')); | ||
var polygons = JSON.parse(fs.readFileSync(__dirname + '/geojson/tagPolygons.geojson')); | ||
var points = JSON.parse(fs.readFileSync('./geojson/tagPoints.geojson')); | ||
var polygons = JSON.parse(fs.readFileSync('./geojson/tagPolygons.geojson')); | ||
var taggedPoints = tag(points, polygons, 'polyID', 'containingPolyID'); | ||
t.ok(taggedPoints.features, 'features should be ok'); | ||
t.equal(taggedPoints.features.length, points.features.length, | ||
t.equal(taggedPoints.features.length, points.features.length, | ||
'tagged points should have the same length as the input points'); | ||
var count = 0; | ||
taggedPoints.features.forEach(function(pt){ | ||
if(pt.properties.containingPolyID === 4){ | ||
count++; | ||
} | ||
}); | ||
var count = taggedPoints.features.filter(function(pt){ | ||
return (pt.properties.containingPolyID === 4); | ||
}).length; | ||
t.equal(count, 6, 'polygon 4 should have tagged 6 points'); | ||
}); | ||
t.end(); | ||
}); |
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
15302
86
65
4
Updatedturf-inside@^1.1.3