-
Notifications
You must be signed in to change notification settings - Fork 944
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
New module @turf/nearest-point-to-line
#939
Changes from 4 commits
474e72f
4a12e50
2867e83
c241e16
6fbe42f
63e4969
b681ff6
0e24609
a6238d9
c04ccd5
162b94d
e825fb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 TurfJS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# @turf/nearest-point-to-line | ||
|
||
# nearestPointToLine | ||
|
||
Returns the closest [point](http://geojson.org/geojson-spec.html#point), of a [collection](http://geojson.org/geojson-spec.html#feature-collection-objects) of points, to a [line](http://geojson.org/geojson-spec.html#linestring). | ||
The returned point has a `dist` property indicating its distance to the line. | ||
|
||
**Parameters** | ||
|
||
- `points` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** collection | ||
- `line` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** Feature | ||
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** can be degrees, radians, miles, or kilometers (optional, default `kilometers`) | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var points = featureCollection([point([0, 0]), point([0.5, 0.5])]); | ||
var line = lineString([[1,1], [-1,1]]); | ||
|
||
var nearest = turf.nearestPointToLine(points, line); | ||
|
||
//addToMap | ||
var addToMap = [nearest, line]; | ||
``` | ||
|
||
Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** the closest point | ||
|
||
<!-- This file is automatically generated. Please don't edit it directly: | ||
if you find an error, edit the source file (likely index.js), and re-run | ||
./scripts/generate-readmes in the turf project. --> | ||
|
||
--- | ||
|
||
This module is part of the [Turfjs project](http://turfjs.org/), an open source | ||
module collection dedicated to geographic algorithms. It is maintained in the | ||
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create | ||
PRs and issues. | ||
|
||
### Installation | ||
|
||
Install this module individually: | ||
|
||
```sh | ||
$ npm install @turf/nearest-point-to-line | ||
``` | ||
|
||
Or install the Turf module that includes it as a function: | ||
|
||
```sh | ||
$ npm install @turf/turf | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const load = require('load-json-file'); | ||
const Benchmark = require('benchmark'); | ||
const nearestPointToLine = require('./'); | ||
|
||
/** | ||
* Benchmark Results | ||
* | ||
* fiji: 2.973ms | ||
* on-line: 0.758ms | ||
* one: 0.549ms | ||
* resolute: 0.349ms | ||
* two: 0.358ms | ||
* | ||
* fiji x 36,409 ops/sec ±4.14% (69 runs sampled) | ||
* on-line x 14,044 ops/sec ±4.92% (69 runs sampled) | ||
* one x 65,604 ops/sec ±11.55% (64 runs sampled) | ||
* resolute x 44,962 ops/sec ±6.76% (67 runs sampled) | ||
* two x 42,690 ops/sec ±2.34% (76 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-nearest-point-to-line'); | ||
glob.sync(path.join(__dirname, 'test', 'in', '*.geojson')).forEach(filepath => { | ||
const {name} = path.parse(filepath); | ||
const geojson = load.sync(filepath); | ||
const [points, line] = geojson.features; | ||
console.time(name); | ||
nearestPointToLine(points, line); | ||
console.timeEnd(name); | ||
suite.add(name, () => nearestPointToLine(points, line)); | ||
}); | ||
|
||
suite | ||
.on('cycle', e => console.log(String(e.target))) | ||
.on('complete', () => {}) | ||
.run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference types="geojson" /> | ||
|
||
type Points = GeoJSON.FeatureCollection<GeoJSON.Point>; | ||
type LineString = GeoJSON.LineString; | ||
type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point; | ||
|
||
/** | ||
* http://turfjs.org/docs/#nearestpointtoline | ||
*/ | ||
declare function nearestPointToLine(points: Points, line: LineString): Point; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
declare namespace nearestPointToLine { } | ||
export = nearestPointToLine; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var meta = require('@turf/meta'); | ||
var invariant = require('@turf/invariant'); | ||
var pointToLineDistance = require('@turf/point-to-line-distance'); | ||
var featureOf = invariant.featureOf; | ||
var featureEach = meta.featureEach; | ||
var collectionOf = invariant.collectionOf; | ||
|
||
/** | ||
* Returns the closest {@link Point|point}, of a {@link FeatureCollection|collection} of points, to a {@link LineString|line}. | ||
* The returned point has a `dist` property indicating its distance to the line. | ||
* | ||
* @name nearestPointToLine | ||
* @param {FeatureCollection<Point>} points collection | ||
* @param {Feature<LineString>} line Feature | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also support LineString as a Geometry? /**
* @param {Feature<LineString>|LineString} line Line Feature |
||
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My only comment would be saying that this is for the output property. So perhaps |
||
* @returns {Feature<Point>} the closest point | ||
* @example | ||
* var points = featureCollection([point([0, 0]), point([0.5, 0.5])]); | ||
* var line = lineString([[1,1], [-1,1]]); | ||
* | ||
* var nearest = turf.nearestPointToLine(points, line); | ||
* | ||
* //addToMap | ||
* var addToMap = [nearest, line]; | ||
*/ | ||
module.exports = function (points, line, units) { | ||
// validation | ||
if (!points) throw new Error('points is required'); | ||
else collectionOf(points, 'Point', 'points'); | ||
if (!line) throw new Error('line is required'); | ||
else featureOf(line, 'LineString', 'line'); | ||
|
||
var dist = Infinity; | ||
var pt = null; | ||
featureEach(points, function (point) { | ||
var d = pointToLineDistance(point, line, units); | ||
if (d < dist) { | ||
dist = d; | ||
pt = point; | ||
} | ||
}); | ||
|
||
pt.properties.dist = dist; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd call the output property Perhaps this should be an upstream change in Just my personal preference |
||
return pt; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "@turf/nearest-point-to-line", | ||
"version": "4.0.0", | ||
"description": "turf nearest-point-to-line module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Turfjs/turf.git" | ||
}, | ||
"keywords": [ | ||
"turf", | ||
"geojson", | ||
"gis", | ||
"near", | ||
"nearest-point-to-line" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"@turf/helpers": "4.7.1", | ||
"benchmark": "^2.1.4", | ||
"write-json-file": "^2.2.0", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"@turf/point-to-line-distance": "4.7.1", | ||
"@turf/meta": "4.7.1", | ||
"@turf/invariant": "4.7.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const fs = require('fs'); | ||
const test = require('tape'); | ||
const path = require('path'); | ||
const load = require('load-json-file'); | ||
const write = require('write-json-file'); | ||
const {featureCollection, point, lineString, round} = require('@turf/helpers'); | ||
const nearestPointToLine = require('./'); | ||
|
||
const directories = { | ||
in: path.join(__dirname, 'test', 'in') + path.sep, | ||
out: path.join(__dirname, 'test', 'out') + path.sep | ||
}; | ||
|
||
const fixtures = fs.readdirSync(directories.in).map(filename => { | ||
return { | ||
filename, | ||
name: path.parse(filename).name, | ||
geojson: load.sync(directories.in + filename) | ||
}; | ||
}); | ||
|
||
test('turf-nearest-point-to-line', t => { | ||
for (const {filename, name, geojson} of fixtures) { | ||
const [points, line] = geojson.features; | ||
let {units} = geojson.properties || {}; | ||
const nearest = nearestPointToLine(points, line, units); | ||
nearest.properties.dist = round(nearest.properties.dist, 13); | ||
nearest.properties = Object.assign(nearest.properties, { | ||
'marker-color': '#F00', | ||
'marker-size': 'large', | ||
'marker-symbol': 'star' | ||
}); | ||
const results = featureCollection([nearest, line]); | ||
|
||
if (process.env.REGEN) write.sync(directories.out + filename, results); | ||
t.deepEqual(results, load.sync(directories.out + filename), name); | ||
} | ||
t.end(); | ||
}); | ||
|
||
|
||
test('turf-nearest-point-to-line -- throws', t => { | ||
const points = featureCollection([point([0, 0]), point([0, 1])]); | ||
const line = lineString([[1,1], [-1,1]]); | ||
|
||
t.throws(() => nearestPointToLine(null, line), /points is required/, 'missing points'); | ||
t.throws(() => nearestPointToLine(points, null), /line is required/, 'missing line'); | ||
|
||
t.throws(() => nearestPointToLine(points, line, 'invalid'), /units is invalid/, 'invalid units'); | ||
t.throws(() => nearestPointToLine(points, points), /Invalid input to line, Feature with geometry required/, 'invalid line'); | ||
t.throws(() => nearestPointToLine(line, line), /Invalid input to points, FeatureCollection required/, 'invalid points'); | ||
|
||
t.end(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
180.60699462890625, | ||
-16.62303335009946 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
181.0986328125, | ||
-17.027898881942694 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
179.10186767578125, | ||
-17.413546114374437 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
180.516357421875, | ||
-17.287709050621917 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
179.37103271484375, | ||
-16.691447830122993 | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
180.25817871093747, | ||
-16.95435146120393 | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
179.68139648437497, | ||
-16.607241925191516 | ||
], | ||
[ | ||
180.6427001953125, | ||
-16.935960102864705 | ||
], | ||
[ | ||
179.58251953125, | ||
-17.07253857905758 | ||
], | ||
[ | ||
180.29937744140625, | ||
-17.460712710429785 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would only allow Geometry LineString and not a
Feature<LineString>
:Correct definition for both Geometry & Feature support: