/
turf-line-dissolve.js
107 lines (107 loc) · 3.48 KB
/
turf-line-dissolve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var clone_1 = require("@turf/clone");
var helpers_1 = require("@turf/helpers");
var invariant_1 = require("@turf/invariant");
var meta_1 = require("@turf/meta");
/**
* Merges all connected (non-forking, non-junctioning) line strings into single lineStrings.
* [LineString] -> LineString|MultiLineString
*
* @param {FeatureCollection<LineString|MultiLineString>} geojson Lines to dissolve
* @param {Object} [options={}] Optional parameters
* @param {boolean} [options.mutate=false] Prevent input mutation
* @returns {Feature<LineString|MultiLineString>} Dissolved lines
*/
function lineDissolve(geojson, options) {
if (options === void 0) { options = {}; }
// Optional parameters
options = options || {};
if (!helpers_1.isObject(options)) {
throw new Error("options is invalid");
}
var mutate = options.mutate;
// Validation
if (invariant_1.getType(geojson) !== "FeatureCollection") {
throw new Error("geojson must be a FeatureCollection");
}
if (!geojson.features.length) {
throw new Error("geojson is empty");
}
// Clone geojson to avoid side effects
if (mutate === false || mutate === undefined) {
geojson = clone_1.default(geojson);
}
var result = [];
var lastLine = meta_1.lineReduce(geojson, function (previousLine, currentLine) {
// Attempt to merge this LineString with the other LineStrings, updating
// the reference as it is merged with others and grows.
var merged = mergeLineStrings(previousLine, currentLine);
// Accumulate the merged LineString
if (merged) {
return merged;
// Put the unmerged LineString back into the list
}
else {
result.push(previousLine);
return currentLine;
}
});
// Append the last line
if (lastLine) {
result.push(lastLine);
}
// Return null if no lines were dissolved
if (!result.length) {
return null;
// Return LineString if only 1 line was dissolved
}
else if (result.length === 1) {
return result[0];
// Return MultiLineString if multiple lines were dissolved with gaps
}
else {
return helpers_1.multiLineString(result.map(function (line) {
return line.coordinates;
}));
}
}
// [Number, Number] -> String
function coordId(coord) {
return coord[0].toString() + "," + coord[1].toString();
}
/**
* LineString, LineString -> LineString
*
* @private
* @param {Feature<LineString>} a line1
* @param {Feature<LineString>} b line2
* @returns {Feature<LineString>|null} Merged LineString
*/
function mergeLineStrings(a, b) {
var coords1 = a.geometry.coordinates;
var coords2 = b.geometry.coordinates;
var s1 = coordId(coords1[0]);
var e1 = coordId(coords1[coords1.length - 1]);
var s2 = coordId(coords2[0]);
var e2 = coordId(coords2[coords2.length - 1]);
// TODO: handle case where more than one of these is true!
var coords;
if (s1 === e2) {
coords = coords2.concat(coords1.slice(1));
}
else if (s2 === e1) {
coords = coords1.concat(coords2.slice(1));
}
else if (s1 === s2) {
coords = coords1.slice(1).reverse().concat(coords2);
}
else if (e1 === e2) {
coords = coords1.concat(coords2.reverse().slice(1));
}
else {
return null;
}
return helpers_1.lineString(coords);
}
exports.default = lineDissolve;