turf-nearest
Advanced tools
Comparing version 1.0.2 to 3.0.0-canary.2f5f7167
@@ -5,4 +5,4 @@ var nearest = require('./'); | ||
var pt = JSON.parse(fs.readFileSync(__dirname+'/geojson/pt.geojson')); | ||
var pts = JSON.parse(fs.readFileSync(__dirname+'/geojson/pts.geojson')); | ||
var pt = JSON.parse(fs.readFileSync(__dirname+'/test/pt.geojson')); | ||
var pts = JSON.parse(fs.readFileSync(__dirname+'/test/pts.geojson')); | ||
@@ -20,2 +20,2 @@ var suite = new Benchmark.Suite('turf-nearest'); | ||
}) | ||
.run(); | ||
.run(); |
43
index.js
var distance = require('turf-distance'); | ||
/** | ||
* Takes a {@link Point} feature and a {@link FeatureCollection} of Point features and returns the Point feature from the FeatureCollection closest to the input point. | ||
* Takes a reference {@link Point|point} and a FeatureCollection of Features | ||
* with Point geometries and returns the | ||
* point from the FeatureCollection closest to the reference. This calculation | ||
* is geodesic. | ||
* | ||
* @module turf/nearest | ||
* @name nearest | ||
* @category classification | ||
* @param {Point} point the reference point | ||
* @param {FeatureCollection} against a FeatureCollection of Point features | ||
* @return {Feature} the closest Point feature in `against` to `point` | ||
* @param {Feature<Point>} point the reference point | ||
* @param {FeatureCollection<Point>} against input point set | ||
* @return {Feature<Point>} the closest point in the set to the reference point | ||
* @example | ||
@@ -61,22 +64,12 @@ * var point = { | ||
*/ | ||
module.exports = function(targetPoint, points){ | ||
var nearestPoint; | ||
var count = 0; | ||
var dist = Infinity; | ||
points.features.forEach(function(pt){ | ||
if(!nearestPoint){ | ||
nearestPoint = pt; | ||
var dist = distance(targetPoint, pt, 'miles'); | ||
nearestPoint.properties.distance = dist; | ||
module.exports = function (targetPoint, points) { | ||
var nearestPoint, minDist = Infinity; | ||
for (var i = 0; i < points.features.length; i++) { | ||
var distanceToPoint = distance(targetPoint, points.features[i], 'miles'); | ||
if (distanceToPoint < minDist) { | ||
nearestPoint = points.features[i]; | ||
minDist = distanceToPoint; | ||
} | ||
} | ||
else{ | ||
var dist = distance(targetPoint, pt, 'miles'); | ||
if(dist < nearestPoint.properties.distance){ | ||
nearestPoint = pt; | ||
nearestPoint.properties.distance = dist; | ||
} | ||
} | ||
}); | ||
delete nearestPoint.properties.distance; | ||
return nearestPoint; | ||
} | ||
return nearestPoint; | ||
}; |
{ | ||
"name": "turf-nearest", | ||
"version": "1.0.2", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "turf nearest module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "node test.js" | ||
}, | ||
@@ -28,9 +27,7 @@ "repository": { | ||
"benchmark": "^1.0.0", | ||
"tape": "^3.5.0", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
"tape": "^3.5.0" | ||
}, | ||
"dependencies": { | ||
"turf-distance": "^1.0.0" | ||
"turf-distance": "^3.0.0-canary.2f5f7167" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # turf-nearest | ||
Takes a Point feature and a FeatureCollection of Point features and returns the Point feature from the FeatureCollection closest to the input point. | ||
Takes a reference Point|point and a set of points and returns the point from the set closest to the reference. | ||
@@ -16,6 +16,6 @@ | ||
| parameter | type | description | | ||
| --------- | ----------------- | ------------------------------------- | | ||
| `point` | Point | the reference point | | ||
| `against` | FeatureCollection | a FeatureCollection of Point features | | ||
| parameter | type | description | | ||
| --------- | ---------------------------- | ------------------- | | ||
| `point` | Feature\.\<Point\> | the reference point | | ||
| `against` | FeatureCollection\.\<Point\> | input point set | | ||
@@ -26,9 +26,39 @@ | ||
```js | ||
var point = turf.point([28.965797, 41.010086]); | ||
point.properties['marker-color'] = '#0f0'; | ||
var against = turf.featurecollection([ | ||
turf.point([28.973865, 41.011122]), | ||
turf.point([28.948459, 41.024204]), | ||
turf.point([28.938674, 41.013324]) | ||
]); | ||
var point = { | ||
"type": "Feature", | ||
"properties": { | ||
"marker-color": "#0f0" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [28.965797, 41.010086] | ||
} | ||
}; | ||
var against = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [28.973865, 41.011122] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [28.948459, 41.024204] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [28.938674, 41.013324] | ||
} | ||
} | ||
] | ||
}; | ||
@@ -38,4 +68,7 @@ var nearest = turf.nearest(point, against); | ||
var result = turf.featurecollection( | ||
against.features.concat(point)); | ||
var resultFeatures = against.features.concat(point); | ||
var result = { | ||
"type": "FeatureCollection", | ||
"features": resultFeatures | ||
}; | ||
@@ -45,2 +78,5 @@ //=result | ||
**Returns** `Feature.<Point>`, the closest point in the set to the reference point | ||
## Installation | ||
@@ -60,1 +96,2 @@ | ||
@@ -6,4 +6,4 @@ var test = require('tape'); | ||
test('distance', function(t){ | ||
var pt = JSON.parse(fs.readFileSync(__dirname+'/geojson/pt.geojson')); | ||
var pts = JSON.parse(fs.readFileSync(__dirname+'/geojson/pts.geojson')); | ||
var pt = JSON.parse(fs.readFileSync(__dirname+'/test/pt.geojson')); | ||
var pts = JSON.parse(fs.readFileSync(__dirname+'/test/pts.geojson')); | ||
@@ -17,2 +17,2 @@ var closestPt = nearest(pt, pts); | ||
t.end(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
2
0
92
6579
7
102
1
1
+ Addedturf-distance@3.0.12(transitive)
+ Addedturf-helpers@3.0.12(transitive)
+ Addedturf-invariant@3.0.12(transitive)
- Removedturf-distance@1.1.0(transitive)
- Removedturf-invariant@1.0.3(transitive)