cheap-ruler
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var Benchmark = require('benchmark'); | ||
@@ -12,3 +14,3 @@ | ||
suite | ||
.add('turf.bearing', function() { | ||
.add('turf.bearing', function () { | ||
for (var i = 0; i < lines.length; i++) { | ||
@@ -21,3 +23,3 @@ var line = lines[i]; | ||
}) | ||
.add('ruler.bearing', function() { | ||
.add('ruler.bearing', function () { | ||
for (var i = 0; i < lines.length; i++) { | ||
@@ -30,5 +32,5 @@ var line = lines[i]; | ||
}) | ||
.on('cycle', function(event) { | ||
console.log(String(event.target)); | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)); | ||
}) | ||
.run(); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var Benchmark = require('benchmark'); | ||
@@ -12,3 +14,3 @@ | ||
suite | ||
.add('turf.lineDistance', function() { | ||
.add('turf.lineDistance', function () { | ||
for (var i = 0; i < lines.length; i++) { | ||
@@ -18,3 +20,3 @@ turf.lineDistance(turf.linestring(lines[i])); | ||
}) | ||
.add('ruler.lineDistance', function() { | ||
.add('ruler.lineDistance', function () { | ||
for (var i = 0; i < lines.length; i++) { | ||
@@ -24,5 +26,5 @@ ruler.lineDistance(lines[i]); | ||
}) | ||
.on('cycle', function(event) { | ||
console.log(String(event.target)); | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)); | ||
}) | ||
.run(); |
39
index.js
@@ -76,3 +76,42 @@ 'use strict'; | ||
p[1] <= bbox[3]; | ||
}, | ||
pointOnLine(line, p) { | ||
var minDist = Infinity; | ||
var minX, minY; | ||
for (var i = 0; i < line.length - 1; i++) { | ||
var x = line[i][0]; | ||
var y = line[i][1]; | ||
var dx = (line[i + 1][0] - x) * this.e; | ||
var dy = line[i + 1][1] - y; | ||
if (dx !== 0 || dy !== 0) { | ||
var t = ((p[0] - x) * this.e * dx + (p[1] - y) * dy) / (dx * dx + dy * dy); | ||
if (t > 1) { | ||
x = line[i + 1][0]; | ||
y = line[i + 1][1]; | ||
} else if (t > 0) { | ||
x += dx * t / this.e; | ||
y += dy * t; | ||
} | ||
} | ||
dx = (p[0] - x) * this.e; | ||
dy = p[1] - y; | ||
var sqDist = dx * dx + dy * dy; | ||
if (sqDist < minDist) { | ||
minDist = sqDist; | ||
minX = x; | ||
minY = y; | ||
} | ||
} | ||
return [minX, minY]; | ||
} | ||
}; |
{ | ||
"name": "cheap-ruler", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A collection of fast approximations to common geographic measurements.", | ||
@@ -11,6 +11,8 @@ "main": "index.js", | ||
"eslint-config-mourner": "^2.0.1", | ||
"tape": "^4.5.1", | ||
"turf": "^2.0.2" | ||
}, | ||
"scripts": { | ||
"test": "eslint index.js" | ||
"pretest": "eslint index.js bench test/test.js", | ||
"test": "tape test/test.js" | ||
}, | ||
@@ -17,0 +19,0 @@ "eslintConfig": { |
@@ -1,6 +0,6 @@ | ||
# cheap-ruler | ||
# cheap-ruler [![Build Status](https://travis-ci.org/mapbox/cheap-ruler.svg?branch=master)](https://travis-ci.org/mapbox/cheap-ruler) | ||
A collection of fast approximations to common geographic measurements, along with some utility functions. | ||
Useful for speeding up analysis scripts when measuring things on a city scale | ||
by replacing expensive [Turf](http://turfjs.org/) calls in key places. | ||
Useful for speeding up analysis scripts when measuring things on a city scale, | ||
replacing [Turf](http://turfjs.org/) calls in key places. | ||
@@ -11,5 +11,10 @@ ## Usage | ||
var ruler = cheapRuler(35.05, 'miles'); | ||
var distance = ruler.distance([30.51, 50.32], [30.52, 50.312]); | ||
var lineLength = ruler.lineDistance(line.geometry.coordinates); | ||
var bbox = ruler.bufferPoint([30.5, 50.5], 0.01); | ||
``` | ||
**Note**: to get the full performance benefit, create the ruler object once per an area of calculation (such as a tile), and then reuse it as much as possible. | ||
### Creating a ruler object | ||
@@ -34,3 +39,3 @@ | ||
Given two points of the form `[x, y]`, returns the distance. Typically within 0.1% of `turf.distance` values but 20–25 times faster. | ||
Given two points of the form `[x, y]`, returns the distance. Typically within 0.1% of `turf.distance` values (on small distances and far away from poles), but 20–25 times faster. | ||
@@ -43,7 +48,12 @@ #### lineDistance(points) | ||
Returns the bearing between two points in angles. Typically within 0.001% of `turf.bearing` but 3–4 times faster. | ||
Returns the bearing between two points in angles. Typically within 0.01% of `turf.bearing` but 3–4 times faster. | ||
#### pointOnLine(line, p) | ||
Returns the closest point on the line from the given point. Line is an array of points. 80-90 times faster than `turf.pointOnLine`. | ||
#### bufferPoint(p, buffer) | ||
Given a point, returns a bounding box object (`[w, s, e, n]`) created from the given point buffered by a given distance. | ||
This is about _200 times faster_ than creating a bounding box with two diagonal `turf.destination` calls. | ||
@@ -50,0 +60,0 @@ ```js |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
22030
12
266
68
5
1