New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@turf/line-distance

Package Overview
Dependencies
Maintainers
4
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/line-distance - npm Package Compare versions

Comparing version 3.13.0 to 3.14.0

15

index.d.ts
/// <reference types="geojson" />
type LineStrings = GeoJSON.FeatureCollection<GeoJSON.LineString>;
type LineString = GeoJSON.Feature<GeoJSON.LineString>;
type MultiLineStrings = GeoJSON.FeatureCollection<GeoJSON.MultiLineString>;
type MultiLineString = GeoJSON.Feature<GeoJSON.MultiLineString>;
type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>;
type Polygon = GeoJSON.Feature<GeoJSON.Polygon>;
type MultiPolygons = GeoJSON.FeatureCollection<GeoJSON.MultiPolygon>;
type MultiPolygon = GeoJSON.Feature<GeoJSON.MultiPolygon>;
type LineStringFeatures = LineString | LineStrings | MultiLineString | MultiLineStrings | GeoJSON.LineString | GeoJSON.MultiLineString
type PolygonFeatures = Polygon | Polygons | MultiPolygon | MultiPolygons | GeoJSON.Polygon | GeoJSON.MultiPolygon
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>

@@ -17,4 +10,4 @@ /**

*/
declare function lineDistance(features: LineStringFeatures | PolygonFeatures, units?: string): number;
declare function lineDistance(features: Geometry | Feature | Features, units?: string): number;
declare namespace lineDistance { }
export = lineDistance;
var distance = require('@turf/distance');
var featureEach = require('@turf/meta').featureEach;
var coordReduce = require('@turf/meta').coordReduce;
var geomEach = require('@turf/meta').geomEach;
var flatten = require('@turf/flatten');
var lineString = require('@turf/helpers').lineString;
var point = require('@turf/helpers').point;

@@ -8,5 +13,5 @@

* @name lineDistance
* @param {Feature<(LineString|Polygon)>|FeatureCollection<(LineString|Polygon)>} line line to measure
* @param {Feature<(LineString|Polygon)>|FeatureCollection<(LineString|Polygon)>} geojson feature to measure
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers
* @return {number} length of the input line
* @returns {number} length feature
* @example

@@ -35,49 +40,72 @@ * var line = {

*/
module.exports = function lineDistance(line, units) {
if (line.type === 'FeatureCollection') {
return line.features.reduce(function (memo, feature) {
return memo + lineDistance(feature, units);
}, 0);
}
module.exports = function lineDistance(geojson, units) {
// Input Validation
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');
});
var geometry = line.type === 'Feature' ? line.geometry : line;
var d, i;
if (geometry.type === 'LineString') {
return length(geometry.coordinates, units);
} else if (geometry.type === 'Polygon' || geometry.type === 'MultiLineString') {
d = 0;
for (i = 0; i < geometry.coordinates.length; i++) {
d += length(geometry.coordinates[i], units);
}
return d;
} else if (geometry.type === 'MultiPolygon') {
d = 0;
for (i = 0; i < geometry.coordinates.length; i++) {
for (var j = 0; j < geometry.coordinates[i].length; j++) {
d += length(geometry.coordinates[i][j], units);
}
}
return d;
} else {
throw new Error('input must be a LineString, MultiLineString, ' +
'Polygon, or MultiPolygon Feature or Geometry (or a FeatureCollection ' +
'containing only those types)');
}
// Calculate distance from 2-vertex line segements
return segmentReduce(geojson, function (previousValue, segment) {
var coords = segment.geometry.coordinates;
var start = point(coords[0]);
var end = point(coords[1]);
return previousValue + distance(start, end, units);
}, 0);
};
function length(coords, units) {
var travelled = 0;
var prevCoords = point(coords[0]);
var curCoords = point(coords[0]);
var temp;
for (var i = 1; i < coords.length; i++) {
curCoords.geometry.coordinates = coords[i];
travelled += distance(prevCoords, curCoords, units);
temp = prevCoords;
prevCoords = curCoords;
curCoords = temp;
}
return travelled;
/**
* 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": "3.13.0",
"version": "3.14.0",
"description": "turf line-distance module",

@@ -34,9 +34,13 @@ "main": "index.js",

"devDependencies": {
"benchmark": "^1.0.0",
"tape": "^3.5.0"
"benchmark": "^2.1.3",
"load-json-file": "^2.0.0",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/distance": "^3.13.0",
"@turf/helpers": "^3.13.0"
"@turf/distance": "^3.14.0",
"@turf/flatten": "^3.14.0",
"@turf/helpers": "^3.13.0",
"@turf/meta": "^3.14.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc