@turf/line-chunk
Advanced tools
+18
-9
@@ -1,16 +0,25 @@ | ||
| import { | ||
| LineString, | ||
| LineStrings, | ||
| MultiLineString, | ||
| MultiLineStrings, | ||
| Units | ||
| } from '@turf/helpers' | ||
| /// <reference types="geojson" /> | ||
| type FeatureIn = LineString | LineStrings | MultiLineString | MultiLineStrings; | ||
| import {Units} from '@turf/helpers' | ||
| type LineString = GeoJSON.LineString; | ||
| type MultiLineString = GeoJSON.MultiLineString; | ||
| type GeometryObject = GeoJSON.GeometryObject; | ||
| type GeometryCollection = GeoJSON.GeometryCollection; | ||
| type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>; | ||
| type FeatureCollection<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>; | ||
| type Geoms = LineString | MultiLineString; | ||
| interface FeatureGeometryCollection extends GeoJSON.Feature<any> { | ||
| geometry: GeometryCollection | ||
| } | ||
| // Input & Output | ||
| type Input = Feature<Geoms> | FeatureCollection<Geoms> | Geoms | GeometryCollection | FeatureGeometryCollection; | ||
| type Output = FeatureCollection<LineString>; | ||
| /** | ||
| * http://turfjs.org/docs/#linechunk | ||
| */ | ||
| declare function lineChunk(featureIn: FeatureIn, segmentLength: number, unit?: Units): LineStrings; | ||
| declare function lineChunk(geojson: Input, segmentLength: number, unit?: Units, reverse?: boolean): Output; | ||
| declare namespace lineChunk {} | ||
| export = lineChunk; |
+19
-40
| var lineSliceAlong = require('@turf/line-slice-along'); | ||
| var lineDistance = require('@turf/line-distance'); | ||
| var featureCollection = require('@turf/helpers').featureCollection; | ||
| var featureEach = require('@turf/meta').featureEach; | ||
| var flatten = require('@turf/flatten'); | ||
| var flattenEach = require('@turf/meta').flattenEach; | ||
@@ -12,3 +11,3 @@ /** | ||
| * @name lineChunk | ||
| * @param {FeatureCollection|Feature<LineString|MultiLineString>} featureIn the lines to split | ||
| * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split | ||
| * @param {number} segmentLength how long to make each segment | ||
@@ -30,35 +29,19 @@ * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers | ||
| * //addToMap | ||
| * var addToMap = [line, chunk]; | ||
| * var addToMap = [chunk]; | ||
| */ | ||
| module.exports = function (featureIn, segmentLength, units, reverse) { | ||
| var outFeatures = []; | ||
| var debug = arguments['4']; // Hidden @param {boolean} Enable debug mode | ||
| module.exports = function (geojson, segmentLength, units, reverse) { | ||
| if (!geojson) throw new Error('geojson is required'); | ||
| if (segmentLength <= 0) throw new Error('segmentLength must be greater than 0'); | ||
| var results = []; | ||
| // Handles FeatureCollection | ||
| featureEach(featureIn, function (multiFeature) { | ||
| // Flatten each feature to simple LineString | ||
| flattenEach(geojson, function (feature) { | ||
| // reverses coordinates to start the first chunked segment at the end | ||
| if (reverse) feature.geometry.coordinates = feature.geometry.coordinates.reverse(); | ||
| // Handles MultiLineString | ||
| if (multiFeature.geometry.type === 'MultiLineString') { | ||
| multiFeature = flatten(multiFeature); | ||
| } | ||
| // All features are simple LineString | ||
| featureEach(multiFeature, function (feature) { | ||
| if (reverse) { | ||
| feature.geometry.coordinates = feature.geometry.coordinates.reverse(); | ||
| } | ||
| var lineSegments = sliceLineSegments(feature, segmentLength, units); | ||
| lineSegments.forEach(function (segment, index) { | ||
| if (debug === true) { | ||
| var r = (index % 2 === 0) ? 'F' : '0'; | ||
| var g = (index % 2 === 0) ? '0' : '0'; | ||
| var b = (index % 2 === 0) ? '0' : 'F'; | ||
| segment.properties['stroke'] = '#' + r + g + b; | ||
| segment.properties['stroke-width'] = 6; | ||
| } | ||
| outFeatures.push(segment); | ||
| }); | ||
| sliceLineSegments(feature, segmentLength, units, function (segment) { | ||
| results.push(segment); | ||
| }); | ||
| }); | ||
| return featureCollection(outFeatures); | ||
| return featureCollection(results); | ||
| }; | ||
@@ -73,20 +56,16 @@ | ||
| * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers | ||
| * @returns {Array<Feature<LineString>>} sliced lines | ||
| * @param {Function} callback iterate over sliced line segments | ||
| * @returns {void} | ||
| */ | ||
| function sliceLineSegments(line, segmentLength, units) { | ||
| var lineSegments = []; | ||
| function sliceLineSegments(line, segmentLength, units, callback) { | ||
| var lineLength = lineDistance(line, units); | ||
| // If the line is shorter than the segment length then the orginal line is returned. | ||
| if (lineLength <= segmentLength) { | ||
| return [line]; | ||
| } | ||
| if (lineLength <= segmentLength) return callback(line); | ||
| var numberOfSegments = Math.floor(lineLength / segmentLength) + 1; | ||
| for (var i = 0; i < numberOfSegments; i++) { | ||
| var outline = lineSliceAlong(line, segmentLength * i, segmentLength * (i + 1), units); | ||
| lineSegments.push(outline); | ||
| callback(outline, i); | ||
| } | ||
| return lineSegments; | ||
| } |
+12
-12
| { | ||
| "name": "@turf/line-chunk", | ||
| "version": "4.2.0", | ||
| "version": "4.3.0", | ||
| "description": "turf line-chunk module", | ||
@@ -31,21 +31,21 @@ "main": "index.js", | ||
| "contributors": [ | ||
| "Tim Channell", | ||
| "Rowan Winsemius", | ||
| "Denis Carriere <carriere.denis@gmail.com>" | ||
| "Tim Channell <@tcql>", | ||
| "Rowan Winsemius <@rowanwins>", | ||
| "Denis Carriere <@DenisCarriere>", | ||
| "Daniel Pulido <@dpmcmlxxvi>" | ||
| ], | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "@turf/truncate": "^4.2.0", | ||
| "benchmark": "^1.0.0", | ||
| "@turf/truncate": "^4.3.0", | ||
| "benchmark": "^2.1.4", | ||
| "load-json-file": "^2.0.0", | ||
| "tape": "^2.1.1", | ||
| "tape": "^4.6.3", | ||
| "write-json-file": "^2.0.0" | ||
| }, | ||
| "dependencies": { | ||
| "@turf/flatten": "^4.2.0", | ||
| "@turf/helpers": "^4.2.0", | ||
| "@turf/line-distance": "^4.2.0", | ||
| "@turf/line-slice-along": "^4.2.0", | ||
| "@turf/meta": "^4.2.0" | ||
| "@turf/helpers": "^4.3.0", | ||
| "@turf/line-distance": "^4.3.0", | ||
| "@turf/line-slice-along": "^4.3.0", | ||
| "@turf/meta": "^4.3.0" | ||
| } | ||
| } |
+2
-2
@@ -10,3 +10,3 @@ # @turf/line-chunk | ||
| - `featureIn` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** the lines to split | ||
| - `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** the lines to split | ||
| - `segmentLength` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how long to make each segment | ||
@@ -30,3 +30,3 @@ - `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** units can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`) | ||
| //addToMap | ||
| var addToMap = [line, chunk]; | ||
| var addToMap = [chunk]; | ||
| ``` | ||
@@ -33,0 +33,0 @@ |
4
-20%8058
-0.75%83
-12.63%- Removed
- Removed
Updated
Updated
Updated