From 0311ef53d7c024c135fc5341a3a01ae3df67b4c2 Mon Sep 17 00:00:00 2001 From: Rich Fecher Date: Tue, 11 Nov 2025 10:24:08 -0500 Subject: [PATCH] fix a bug with polylines exporting a part per coordinate There is a bug exporting polylines and polygons where it creates a part per coordinate (so a line that should be continuous with 50 points and 1 part would export as 50 parts with 1 point per part) --- src/poly.js | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/poly.js b/src/poly.js index 4ba8c2de..71c3fa16 100644 --- a/src/poly.js +++ b/src/poly.js @@ -81,18 +81,36 @@ module.exports.extent = function(coordinates) { }; function parts(geometries, TYPE) { - var no = 1; - if (TYPE === types.geometries.POLYGON || TYPE === types.geometries.POLYLINE) { - no = geometries.reduce(function (no, coords) { - no += coords.length; - if (Array.isArray(coords[0][0][0])) { // multi - no += coords.reduce(function (no, rings) { - return no + rings.length - 1; // minus outer - }, 0); - } - return no; + var no = 1; + if (TYPE === types.geometries.POLYGON || TYPE === types.geometries.POLYLINE) { + no = geometries.reduce(function (no, coords) { + no += coords.length; + if (Array.isArray(coords[0][0][0])) { // multi + no += coords.reduce(function (no, rings) { + return no + rings.length - 1; // minus outer }, 0); - } + // For LineString: coords is [[x1,y1], [x2,y2], ...] - this is 1 part + // For MultiLineString: coords is [[[x1,y1], [x2,y2]], [[x3,y3], [x4,y4]]] - multiple parts + // For Polygon: coords is [[[outer ring]], [[hole1]], [[hole2]]] - multiple parts + if (Array.isArray(coords[0]) && Array.isArray(coords[0][0])) { + // Check if this is a multi-geometry or polygon with holes + if (Array.isArray(coords[0][0][0])) { + // Multi-geometry: each element is a separate part + no += coords.length; + // For multi-polygons, also count inner rings + no += coords.reduce(function (innerNo, rings) { + return innerNo + rings.length - 1; // minus outer ring + }, 0); + } else { + // Simple LineString or Polygon outer ring: 1 part + no += 1; + } + } else { + // Fallback: 1 part + no += 1; + } + return no; + }, 0); return no; }