cheap-ruler
Advanced tools
Comparing version 1.1.0 to 1.2.0
16
index.js
@@ -40,2 +40,16 @@ 'use strict'; | ||
area: function (polygon) { | ||
var sum = 0; | ||
for (var i = 0; i < polygon.length; i++) { | ||
var ring = polygon[i]; | ||
for (var j = 0, len = ring.length, k = len - 1; j < len; k = j++) { | ||
sum += (ring[j][0] - ring[k][0]) * (ring[j][1] + ring[k][1]) * (i ? -1 : 1); | ||
} | ||
} | ||
return (Math.abs(sum) / 2) * this.e * this.d * this.d; | ||
}, | ||
bearing: function (a, b) { | ||
@@ -79,3 +93,3 @@ var dx = (b[0] - a[0]) * this.e; | ||
pointOnLine(line, p) { | ||
pointOnLine: function (line, p) { | ||
var minDist = Infinity; | ||
@@ -82,0 +96,0 @@ var minX, minY; |
{ | ||
"name": "cheap-ruler", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A collection of fast approximations to common geographic measurements.", | ||
@@ -9,2 +9,3 @@ "main": "index.js", | ||
"benchmark": "^2.1.0", | ||
"browserify": "^13.0.0", | ||
"eslint": "^2.7.0", | ||
@@ -17,3 +18,5 @@ "eslint-config-mourner": "^2.0.1", | ||
"pretest": "eslint index.js bench test/test.js", | ||
"test": "tape test/test.js" | ||
"test": "tape test/test.js", | ||
"build": "browserify index.js -s cheapRuler > cheap-ruler.js", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -20,0 +23,0 @@ "eslintConfig": { |
@@ -17,2 +17,5 @@ # cheap-ruler [![Build Status](https://travis-ci.org/mapbox/cheap-ruler.svg?branch=master)](https://travis-ci.org/mapbox/cheap-ruler) | ||
For a city scale (a few dozen miles) and far away from poles, | ||
the results are typically within 0.1% of corresponding Turf functions. | ||
**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. | ||
@@ -39,15 +42,35 @@ | ||
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. | ||
Given two points of the form `[x, y]`, returns the distance. | ||
20–25 times faster than `turf.distance`. | ||
#### lineDistance(points) | ||
Given an array of points, returns the total line distance. Typically within 0.1% of `turf.lineDistance` values but 20–25 times faster. | ||
Given an array of points, returns the total line distance. | ||
20–25 times faster than `turf.lineDistance`. | ||
#### area(polygon) | ||
Given a polygon (an array of rings, where each ring is an array of points), returns the area. | ||
3–4 times faster than `turf.area`. Note that it returns the value in the specified units | ||
(square kilometers by default) rather than square meters as in `turf.area`. | ||
```js | ||
var area = ruler.area([[ | ||
[-67.031, 50.458], | ||
[-67.031, 50.534], | ||
[-66.929, 50.534], | ||
[-66.929, 50.458], | ||
[-67.031, 50.458] | ||
]]); | ||
``` | ||
#### bearing(a, b) | ||
Returns the bearing between two points in angles. Typically within 0.01% of `turf.bearing` but 3–4 times faster. | ||
Returns the bearing between two points in angles. | ||
3–4 times faster than `turf.bearing`. | ||
#### 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`. | ||
Returns the closest point on the line from the given point. Line is an array of points. | ||
80–90 times faster than `turf.pointOnLine`. | ||
@@ -57,3 +80,3 @@ #### 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. | ||
About _200 times faster_ than creating a bounding box with two diagonal `turf.destination` calls. | ||
@@ -60,0 +83,0 @@ ```js |
@@ -38,2 +38,14 @@ 'use strict'; | ||
test('area', function (t) { | ||
for (var i = 0; i < lines.length; i++) { | ||
if (lines[i].length < 3) continue; | ||
var poly = turf.polygon([lines[i].concat([lines[i][0]])]); | ||
var expected = turf.area(poly) / 1e6; | ||
var actual = ruler.area([lines[i]]); | ||
assertErr(t, expected, actual, 0.0002, 'area'); | ||
} | ||
t.pass('area within 0.02%'); | ||
t.end(); | ||
}); | ||
test('bearing', function (t) { | ||
@@ -40,0 +52,0 @@ for (var i = 0; i < points.length - 1; i++) { |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
28364
14
424
91
0
6
2