Necessitates Proper Modularity

# npm

Miss any of our Open RFC calls?Watch the recordings here! »

## cheap-ruler

3.0.1 • Public • Published

# cheap-ruler

A collection of very fast approximations to common geodesic measurements. Useful for performance-sensitive code that measures things on a city scale. Can be an order of magnitude faster than corresponding Turf methods.

The approximations are based on the WGS84 ellipsoid model of the Earth, projecting coordinates to a flat surface that approximates the ellipsoid around a certain latitude. For distances under 500 kilometers and not on the poles, the results are very precise — within 0.1% margin of error compared to Vincenti formulas, and usually much less for shorter distances.

## Usage

Note: to get the full performance benefit, create a ruler object only once per a general area of calculation, and then reuse it as much as possible. Don't create a new ruler for every calculation.

### Creating a ruler object

#### new CheapRuler(latitude[, units])

Creates a ruler object that will approximate measurements around the given latitude. Units are one of: `kilometers` (default), `miles`, `nauticalmiles`, `meters`, `yards`, `feet`, `inches`.

#### CheapRuler.fromTile(y, z[, units])

Creates a ruler object from tile coordinates (`y` and `z`).

### Ruler methods

#### distance(a, b)

Given two points of the form `[longitude, latitude]`, returns the distance.

#### bearing(a, b)

Returns the bearing between two points in angles.

#### destination(p, dist, bearing)

Returns a new point given distance and bearing from the starting point.

#### offset(p, dx, dy)

Returns a new point given easting and northing offsets from the starting point.

#### lineDistance(line)

Given a line (an array of points), returns the total line distance.

#### area(polygon)

Given a polygon (an array of rings, where each ring is an array of points), returns the area. Note that it returns the value in the specified units (square kilometers by default) rather than square meters as in `turf.area`.

#### pointToSegmentDistance(p, a, b)

Returns the distance from a point `p` to a line segment `a` to `b`.

#### along(line, dist)

Returns the point at a specified distance along the line.

#### pointOnLine(line, p)

Returns an object of the form `{point, index, t}`, where `point` is closest point on the line from the given point, `index` is the start index of the segment with the closest point, and `t` is a parameter from 0 to 1 that indicates where the closest point is on that segment.

#### 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).

#### lineSliceAlong(startDist, stopDist, line)

Returns a part of the given line between the start and the stop points indicated by distance along the line.

#### 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.

#### bufferBBox(bbox, buffer)

Given a bounding box, returns the box buffered by a given distance.

#### insideBBox(p, bbox)

Returns true if the given point is inside in the given bounding box, otherwise false.

### Units conversion

Multipliers for converting between units are also exposed in `CheapRuler.units`:

If you don't specify units when creating a ruler object, you can use these constants to convert return values (using multiplication) and input arguments (using division) to any units:

## Precision

A table that shows the margin of error for `ruler.distance` compared to `node-vincenty` (a state of the art distance formula):

lat 10° 20° 30° 40° 50° 60° 70° 80°
1km 0% 0% 0% 0% 0% 0% 0% 0% 0%
100km 0% 0% 0% 0% 0% 0% 0% 0.01% 0.03%
500km 0.01% 0.01% 0.01% 0.01% 0.02% 0.04% 0.08% 0.2% 0.83%
1000km 0.03% 0.03% 0.04% 0.06% 0.1% 0.17% 0.33% 0.8% 3.38%

Errors for all other methods are similar.

## Keywords

### Install

`npm i cheap-ruler`

3,397

3.0.1

ISC

41.2 kB

7

### Repository

github.com/mapbox/cheap-ruler