@turf/line-distance
Advanced tools
Comparing version 4.5.2 to 4.6.0
/// <reference types="geojson" /> | ||
type Geometry = GeoJSON.Polygon | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.MultiPolygon | ||
type Feature = GeoJSON.Feature<GeoJSON.Polygon | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.MultiPolygon> | ||
type Features = GeoJSON.FeatureCollection<GeoJSON.Polygon | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.MultiPolygon> | ||
import {Units} from '@turf/helpers' | ||
type Input = GeoJSON.Feature<any> | GeoJSON.FeatureCollection<any> | GeoJSON.GeometryObject | GeoJSON.GeometryCollection | ||
@@ -10,4 +9,4 @@ /** | ||
*/ | ||
declare function lineDistance(features: Geometry | Feature | Features, units?: string): number; | ||
declare function lineDistance(geojson: Input, units?: Units): number; | ||
declare namespace lineDistance { } | ||
export = lineDistance; |
97
index.js
@@ -1,40 +0,18 @@ | ||
var flatten = require('@turf/flatten'); | ||
var distance = require('@turf/distance'); | ||
var meta = require('@turf/meta'); | ||
var geomEach = meta.geomEach; | ||
var featureEach = meta.featureEach; | ||
var coordReduce = meta.coordReduce; | ||
var helpers = require('@turf/helpers'); | ||
var point = helpers.point; | ||
var lineString = helpers.lineString; | ||
var segmentReduce = require('@turf/meta').segmentReduce; | ||
/** | ||
* Takes a {@link LineString} or {@link Polygon} and measures its length in the specified units. | ||
* Takes a {@link GeoJSON} and measures its length in the specified units, {@link (Multi)Point|Point}'s distance are ignored. | ||
* | ||
* @name lineDistance | ||
* @param {Feature<(LineString|Polygon)>|FeatureCollection<(LineString|Polygon)>} geojson feature to measure | ||
* @param {FeatureCollection|Feature|Geometry} geojson GeoJSON to measure | ||
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers | ||
* @returns {number} length feature | ||
* @returns {number} length of GeoJSON | ||
* @example | ||
* var line = { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "LineString", | ||
* "coordinates": [ | ||
* [-77.031669, 38.878605], | ||
* [-77.029609, 38.881946], | ||
* [-77.020339, 38.884084], | ||
* [-77.025661, 38.885821], | ||
* [-77.021884, 38.889563], | ||
* [-77.019824, 38.892368] | ||
* ] | ||
* } | ||
* }; | ||
* | ||
* var line = turf.lineString([[115, -32], [131, -22], [143, -25], [150, -34]]); | ||
* var length = turf.lineDistance(line, 'miles'); | ||
* | ||
* //addToMap | ||
* var addToMap = [line]; | ||
* line.properties.distance = length; | ||
* var addToMap = [line]; | ||
*/ | ||
@@ -44,6 +22,2 @@ module.exports = function lineDistance(geojson, units) { | ||
if (!geojson) throw new Error('geojson is required'); | ||
geomEach(geojson, function (geometry) { | ||
if (geometry.type === 'Point') throw new Error('geojson cannot be a Point'); | ||
if (geometry.type === 'MultiPoint') throw new Error('geojson cannot be a MultiPoint'); | ||
}); | ||
@@ -53,61 +27,4 @@ // Calculate distance from 2-vertex line segements | ||
var coords = segment.geometry.coordinates; | ||
var start = point(coords[0]); | ||
var end = point(coords[1]); | ||
return previousValue + distance(start, end, units); | ||
return previousValue + distance(coords[0], coords[1], units); | ||
}, 0); | ||
}; | ||
/** | ||
* Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach() | ||
* | ||
* @private | ||
* @param {FeatureCollection|Feature<any>} geojson any GeoJSON | ||
* @param {Function} callback a method that takes (currentSegment, currentIndex) | ||
* @returns {void} | ||
* @example | ||
* var polygon = { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]] | ||
* } | ||
* } | ||
* turf.segmentEach(polygon, function (segment) { | ||
* //= segment | ||
* }); | ||
*/ | ||
function segmentEach(geojson, callback) { | ||
var count = 0; | ||
featureEach(geojson, function (multiFeature) { | ||
featureEach(flatten(multiFeature), function (feature) { | ||
coordReduce(feature, function (previousCoords, currentCoords) { | ||
var line = lineString([previousCoords, currentCoords], feature.properties); | ||
callback(line, count); | ||
count++; | ||
return currentCoords; | ||
}); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce() | ||
* | ||
* @private | ||
* @param {FeatureCollection|Feature<any>} geojson any GeoJSON | ||
* @param {Function} callback a method that takes (previousValue, currentSegment, currentIndex) | ||
* @param {*} [initialValue] Value to use as the first argument to the first call of the callback. | ||
* @returns {void} | ||
*/ | ||
function segmentReduce(geojson, callback, initialValue) { | ||
var previousValue = initialValue; | ||
segmentEach(geojson, function (currentSegment, currentIndex) { | ||
if (currentIndex === 0 && initialValue === undefined) { | ||
previousValue = currentSegment; | ||
} else { | ||
previousValue = callback(previousValue, currentSegment, currentIndex); | ||
} | ||
}); | ||
return previousValue; | ||
} |
{ | ||
"name": "@turf/line-distance", | ||
"version": "4.5.2", | ||
"version": "4.6.0", | ||
"description": "turf line-distance module", | ||
@@ -28,2 +28,6 @@ "main": "index.js", | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Denis Carriere <@DenisCarriere>", | ||
"Tom MacWright <@tmcw>" | ||
], | ||
"license": "MIT", | ||
@@ -41,7 +45,6 @@ "bugs": { | ||
"dependencies": { | ||
"@turf/distance": "^4.5.2", | ||
"@turf/flatten": "^4.5.2", | ||
"@turf/helpers": "^4.5.2", | ||
"@turf/meta": "^4.5.2" | ||
"@turf/distance": "^4.6.0", | ||
"@turf/helpers": "^4.6.0", | ||
"@turf/meta": "^4.6.0" | ||
} | ||
} |
@@ -5,7 +5,7 @@ # @turf/line-distance | ||
Takes a [LineString](http://geojson.org/geojson-spec.html#linestring) or [Polygon](http://geojson.org/geojson-spec.html#polygon) and measures its length in the specified units. | ||
Takes a [GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects) and measures its length in the specified units, [Point]((Multi)Point)'s distance are ignored. | ||
**Parameters** | ||
- `geojson` **([Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))>)** feature to measure | ||
- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry))** GeoJSON to measure | ||
- `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`) | ||
@@ -16,26 +16,11 @@ | ||
```javascript | ||
var line = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[-77.031669, 38.878605], | ||
[-77.029609, 38.881946], | ||
[-77.020339, 38.884084], | ||
[-77.025661, 38.885821], | ||
[-77.021884, 38.889563], | ||
[-77.019824, 38.892368] | ||
] | ||
} | ||
}; | ||
var line = turf.lineString([[115, -32], [131, -22], [143, -25], [150, -34]]); | ||
var length = turf.lineDistance(line, 'miles'); | ||
//addToMap | ||
var addToMap = [line]; | ||
line.properties.distance = length; | ||
var addToMap = [line]; | ||
``` | ||
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** length feature | ||
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** length of GeoJSON | ||
@@ -42,0 +27,0 @@ <!-- This file is automatically generated. Please don't edit it directly: |
3
5185
35
49
- Removed@turf/flatten@^4.5.2
- Removed@turf/flatten@4.7.3(transitive)
Updated@turf/distance@^4.6.0
Updated@turf/helpers@^4.6.0
Updated@turf/meta@^4.6.0