cheap-ruler
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -95,3 +95,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.cheapRuler = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var minDist = Infinity; | ||
var minX, minY; | ||
var minX, minY, minI, minT; | ||
@@ -127,8 +127,42 @@ for (var i = 0; i < line.length - 1; i++) { | ||
minY = y; | ||
minI = i; | ||
minT = t; | ||
} | ||
} | ||
return [minX, minY]; | ||
return { | ||
point: [minX, minY], | ||
index: minI, | ||
t: minT | ||
}; | ||
}, | ||
lineSlice: function (start, stop, line) { | ||
var p1 = this.pointOnLine(line, start); | ||
var p2 = this.pointOnLine(line, stop); | ||
if (p1.index > p2.index || (p1.index === p2.index && p1.t > p2.t)) { | ||
var tmp = p1; | ||
p1 = p2; | ||
p2 = tmp; | ||
} | ||
var slice = [p1.point]; | ||
var l = p1.index + 1; | ||
var r = p2.index; | ||
if (!equals(line[l], slice[0]) && l <= r) | ||
slice.push(line[l]); | ||
for (var i = l + 1; i <= r; i++) { | ||
slice.push(line[i]); | ||
} | ||
if (!equals(line[r], p2.point)) | ||
slice.push(p2.point); | ||
return slice; | ||
}, | ||
along: function (line, dist) { | ||
@@ -159,6 +193,19 @@ var sum = 0; | ||
return line[line.length - 1]; | ||
}, | ||
destination: function (p, dist, bearing) { | ||
var a = (90 - bearing) * Math.PI / 180; | ||
var d = dist / this.d; | ||
return [ | ||
p[0] + d * Math.cos(a) / this.e, | ||
p[1] + d * Math.sin(a) | ||
]; | ||
} | ||
}; | ||
function equals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1]; | ||
} | ||
},{}]},{},[1])(1) | ||
}); |
51
index.js
@@ -94,3 +94,3 @@ 'use strict'; | ||
var minDist = Infinity; | ||
var minX, minY; | ||
var minX, minY, minI, minT; | ||
@@ -126,8 +126,42 @@ for (var i = 0; i < line.length - 1; i++) { | ||
minY = y; | ||
minI = i; | ||
minT = t; | ||
} | ||
} | ||
return [minX, minY]; | ||
return { | ||
point: [minX, minY], | ||
index: minI, | ||
t: minT | ||
}; | ||
}, | ||
lineSlice: function (start, stop, line) { | ||
var p1 = this.pointOnLine(line, start); | ||
var p2 = this.pointOnLine(line, stop); | ||
if (p1.index > p2.index || (p1.index === p2.index && p1.t > p2.t)) { | ||
var tmp = p1; | ||
p1 = p2; | ||
p2 = tmp; | ||
} | ||
var slice = [p1.point]; | ||
var l = p1.index + 1; | ||
var r = p2.index; | ||
if (!equals(line[l], slice[0]) && l <= r) | ||
slice.push(line[l]); | ||
for (var i = l + 1; i <= r; i++) { | ||
slice.push(line[i]); | ||
} | ||
if (!equals(line[r], p2.point)) | ||
slice.push(p2.point); | ||
return slice; | ||
}, | ||
along: function (line, dist) { | ||
@@ -158,3 +192,16 @@ var sum = 0; | ||
return line[line.length - 1]; | ||
}, | ||
destination: function (p, dist, bearing) { | ||
var a = (90 - bearing) * Math.PI / 180; | ||
var d = dist / this.d; | ||
return [ | ||
p[0] + d * Math.cos(a) / this.e, | ||
p[1] + d * Math.sin(a) | ||
]; | ||
} | ||
}; | ||
function equals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1]; | ||
} |
{ | ||
"name": "cheap-ruler", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "A collection of fast approximations to common geographic measurements.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,7 +70,2 @@ # cheap-ruler [![Build Status](https://travis-ci.org/mapbox/cheap-ruler.svg?branch=master)](https://travis-ci.org/mapbox/cheap-ruler) | ||
#### pointOnLine(line, p) | ||
Returns the closest point on the line from the given point. | ||
80–90 times faster than `turf.pointOnLine`. | ||
#### along(line, dist) | ||
@@ -81,2 +76,18 @@ | ||
#### destination(p, dist, bearing) | ||
Returns a new point given distance and bearing from the starting point. | ||
6–7 times faster than `turf.destination`. | ||
#### pointOnLine(line, p) | ||
Returns an object of the form `{point, index}` where `point` is closest point on the line from the given point, | ||
and `index` is the start index of the segment with the closest point. | ||
70–75 times faster than `turf.pointOnLine`. | ||
#### lineSlice(start, stop, line) | ||
Returns a part of the given line between the start and the stop points (or their closest points on the line). | ||
50–60 times faster than `turf.lineSlice`. | ||
#### bufferPoint(p, buffer) | ||
@@ -101,3 +112,3 @@ | ||
- NPM: `npm install chep-ruler` | ||
- Browser build: https://npmcdn.com/cheap-ruler@1.2.0/cheap-ruler.js | ||
- NPM: `npm install cheap-ruler` | ||
- Browser build (CDN): https://npmcdn.com/cheap-ruler@1.3.0/cheap-ruler.js |
@@ -51,2 +51,21 @@ 'use strict'; | ||
test('lineSlice', function (t) { | ||
for (var i = 0; i < lines.length; i++) { | ||
if (i === 46) continue; // skip due to Turf bug https://github.com/Turfjs/turf/issues/351 | ||
var line = lines[i]; | ||
var dist = ruler.lineDistance(line); | ||
var start = ruler.along(line, dist * 0.3); | ||
var stop = ruler.along(line, dist * 0.7); | ||
var expected = ruler.lineDistance(turf.lineSlice( | ||
turf.point(start), turf.point(stop), turf.linestring(line)).geometry.coordinates); | ||
var actual = ruler.lineDistance(ruler.lineSlice(start, stop, line)); | ||
assertErr(t, expected, actual, 0.001, 'lineSlice length'); | ||
} | ||
t.pass('lineSlice length within 0.1%'); | ||
t.end(); | ||
}); | ||
test('area', function (t) { | ||
@@ -74,2 +93,14 @@ for (var i = 0; i < lines.length; i++) { | ||
test('destination', function (t) { | ||
for (var i = 0; i < points.length; i++) { | ||
var bearing = (i % 360) - 180; | ||
var expected = turf.destination(turf.point(points[i]), 1.0, bearing, 'kilometers').geometry.coordinates; | ||
var actual = ruler.destination(points[i], 1.0, bearing); | ||
assertErr(t, expected[0], actual[0], 3e-7, 'destination longitude'); | ||
assertErr(t, expected[1], actual[1], 3e-7, 'destination latitude'); | ||
} | ||
t.pass('destination within 3e-7'); | ||
t.end(); | ||
}); | ||
test('bufferPoint', function (t) { | ||
@@ -89,5 +120,5 @@ for (var i = 0; i < points.length; i++) { | ||
test('pointOnLine', function (t) { | ||
// not Turf comparison because pointOnLatter is bugged https://github.com/Turfjs/turf/issues/344 | ||
// not Turf comparison because pointOnLine is bugged https://github.com/Turfjs/turf/issues/344 | ||
var line = [[-77.031669, 38.878605], [-77.029609, 38.881946]]; | ||
var p = ruler.pointOnLine(line, [-77.034076, 38.882017]); | ||
var p = ruler.pointOnLine(line, [-77.034076, 38.882017]).point; | ||
t.same(p, [-77.03051972665213, 38.88046894284234]); | ||
@@ -94,0 +125,0 @@ t.end(); |
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
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
37527
18
683
112