Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

turf-nearest

Package Overview
Dependencies
Maintainers
9
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-nearest - npm Package Compare versions

Comparing version 1.0.2 to 3.0.0-canary.2f5f7167

6

bench.js

@@ -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();
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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc