From 1853198ce153a10b243a925308eb944c7b4da79e Mon Sep 17 00:00:00 2001 From: LiamAttClarke Date: Sun, 3 Nov 2024 14:14:59 -0500 Subject: [PATCH] Add support for multiple polygons in a path --- demo/main-bundle.js | 41 ++++++++------- dist/transformers/path.js | 41 ++++++++------- dist/transformers/path.js.map | 2 +- docs/functions/convertSVG.html | 2 +- docs/functions/getSVGMetadata.html | 2 +- docs/interfaces/convertsvgoutput.html | 4 +- src/transformers/composite-polygon.ts | 32 ------------ src/transformers/path.ts | 73 ++++++++++++++++++--------- 8 files changed, 99 insertions(+), 98 deletions(-) delete mode 100644 src/transformers/composite-polygon.ts diff --git a/demo/main-bundle.js b/demo/main-bundle.js index 3d63a04..9e93ce1 100644 --- a/demo/main-bundle.js +++ b/demo/main-bundle.js @@ -532,30 +532,32 @@ var svgPathParser = require("svg-path-parser"); var utils_1 = require("../utils"); var Vector2_1 = require("../Vector2"); var mathUtils = require("../math-utils"); -var pathTransformer = function (input, svgMeta, options) { - var polygons = []; - var lineStrings = []; - var points = []; +function parseSVGPath(pathD, transform, svgMeta, options) { + var output = { + polygons: [], + lineStrings: [], + points: [], + }; var currentLineString = []; var previousCurveHandle = null; - var pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(input.attributes.d)); + var pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(pathD)); pathCommands.forEach(function (pathCommand, i) { var previousCommand = (i > 0) ? pathCommands[i - 1] : null; if (pathCommand.code === 'M') { var command = pathCommand; if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } currentLineString = [ - (0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, input.attributes.transform), + (0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, transform), ]; } else if (['L', 'V', 'H'].indexOf(pathCommand.code) !== -1) { var command = pathCommand; - currentLineString.push((0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, input.attributes.transform)); + currentLineString.push((0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, transform)); } else if (['C', 'S'].indexOf(pathCommand.code) !== -1) { var command = pathCommand; @@ -569,7 +571,7 @@ var pathTransformer = function (input, svgMeta, options) { } var p2_1 = new Vector2_1.default(command.x2, command.y2); var p3_1 = new Vector2_1.default(command.x, command.y); - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnCubicBezierCurve(p0_1, p1_1, p2_1, p3_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnCubicBezierCurve(p0_1, p1_1, p2_1, p3_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p2_1; } @@ -584,7 +586,7 @@ var pathTransformer = function (input, svgMeta, options) { p1_2 = ['Q', 'T'].indexOf(previousCommand.code) !== -1 ? p0_2.add(p0_2.subtract(previousCurveHandle)) : p0_2; } var p2_2 = new Vector2_1.default(command.x, command.y); - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnQuadraticBezierCurve(p0_2, p1_2, p2_2, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnQuadraticBezierCurve(p0_2, p1_2, p2_2, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p1_2; } @@ -597,21 +599,25 @@ var pathTransformer = function (input, svgMeta, options) { var xAxisRotation_1 = -command.xAxisRotation; var largeArc_1 = command.largeArc; var sweep_1 = !command.sweep; - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnEllipticalArc(p0_3, p1_3, rx_1, ry_1, xAxisRotation_1, largeArc_1, sweep_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnEllipticalArc(p0_3, p1_3, rx_1, ry_1, xAxisRotation_1, largeArc_1, sweep_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); } else if (pathCommand.code === 'Z') { currentLineString.push(currentLineString[0]); - polygons.push(currentLineString); + output.polygons.push(currentLineString); currentLineString = []; } }); if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } + return output; +} +var pathTransformer = function (input, svgMeta, options) { + var _a = parseSVGPath(input.attributes.d, input.attributes.transform, svgMeta, options), points = _a.points, lineStrings = _a.lineStrings, polygons = _a.polygons; var features = []; var properties = options.propertyMapper ? options.propertyMapper(input) : null; if (points.length) { @@ -635,15 +641,14 @@ var pathTransformer = function (input, svgMeta, options) { }); } if (polygons.length) { - var polygonFeatures = polygons.map(function (polygon) { + polygons.forEach(function (polygon) { var id = options.idMapper ? options.idMapper(input) : null; var geometry = { type: 'Polygon', coordinates: [polygon], }; - return (0, utils_1.createFeature)(geometry, id, properties); + features.push((0, utils_1.createFeature)(geometry, id, properties)); }); - features.push(polygonFeatures[0]); } return { features: features, diff --git a/dist/transformers/path.js b/dist/transformers/path.js index 5481d9b..da8ebed 100644 --- a/dist/transformers/path.js +++ b/dist/transformers/path.js @@ -4,30 +4,32 @@ var svgPathParser = require("svg-path-parser"); var utils_1 = require("../utils"); var Vector2_1 = require("../Vector2"); var mathUtils = require("../math-utils"); -var pathTransformer = function (input, svgMeta, options) { - var polygons = []; - var lineStrings = []; - var points = []; +function parseSVGPath(pathD, transform, svgMeta, options) { + var output = { + polygons: [], + lineStrings: [], + points: [], + }; var currentLineString = []; var previousCurveHandle = null; - var pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(input.attributes.d)); + var pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(pathD)); pathCommands.forEach(function (pathCommand, i) { var previousCommand = (i > 0) ? pathCommands[i - 1] : null; if (pathCommand.code === 'M') { var command = pathCommand; if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } currentLineString = [ - (0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, input.attributes.transform), + (0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, transform), ]; } else if (['L', 'V', 'H'].indexOf(pathCommand.code) !== -1) { var command = pathCommand; - currentLineString.push((0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, input.attributes.transform)); + currentLineString.push((0, utils_1.svgPointToCoordinate)(new Vector2_1.default(command.x, command.y), svgMeta, options, transform)); } else if (['C', 'S'].indexOf(pathCommand.code) !== -1) { var command = pathCommand; @@ -41,7 +43,7 @@ var pathTransformer = function (input, svgMeta, options) { } var p2_1 = new Vector2_1.default(command.x2, command.y2); var p3_1 = new Vector2_1.default(command.x, command.y); - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnCubicBezierCurve(p0_1, p1_1, p2_1, p3_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnCubicBezierCurve(p0_1, p1_1, p2_1, p3_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p2_1; } @@ -56,7 +58,7 @@ var pathTransformer = function (input, svgMeta, options) { p1_2 = ['Q', 'T'].indexOf(previousCommand.code) !== -1 ? p0_2.add(p0_2.subtract(previousCurveHandle)) : p0_2; } var p2_2 = new Vector2_1.default(command.x, command.y); - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnQuadraticBezierCurve(p0_2, p1_2, p2_2, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnQuadraticBezierCurve(p0_2, p1_2, p2_2, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p1_2; } @@ -69,21 +71,25 @@ var pathTransformer = function (input, svgMeta, options) { var xAxisRotation_1 = -command.xAxisRotation; var largeArc_1 = command.largeArc; var sweep_1 = !command.sweep; - var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnEllipticalArc(p0_3, p1_3, rx_1, ry_1, xAxisRotation_1, largeArc_1, sweep_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, input.attributes.transform); }); + var curvePoints = mathUtils.drawCurve(function (t) { return mathUtils.pointOnEllipticalArc(p0_3, p1_3, rx_1, ry_1, xAxisRotation_1, largeArc_1, sweep_1, t); }, options.subdivideThreshold).map(function (p) { return (0, utils_1.svgPointToCoordinate)(p, svgMeta, options, transform); }); currentLineString = currentLineString.concat(curvePoints); } else if (pathCommand.code === 'Z') { currentLineString.push(currentLineString[0]); - polygons.push(currentLineString); + output.polygons.push(currentLineString); currentLineString = []; } }); if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } + return output; +} +var pathTransformer = function (input, svgMeta, options) { + var _a = parseSVGPath(input.attributes.d, input.attributes.transform, svgMeta, options), points = _a.points, lineStrings = _a.lineStrings, polygons = _a.polygons; var features = []; var properties = options.propertyMapper ? options.propertyMapper(input) : null; if (points.length) { @@ -107,15 +113,14 @@ var pathTransformer = function (input, svgMeta, options) { }); } if (polygons.length) { - var polygonFeatures = polygons.map(function (polygon) { + polygons.forEach(function (polygon) { var id = options.idMapper ? options.idMapper(input) : null; var geometry = { type: 'Polygon', coordinates: [polygon], }; - return (0, utils_1.createFeature)(geometry, id, properties); + features.push((0, utils_1.createFeature)(geometry, id, properties)); }); - features.push(polygonFeatures[0]); } return { features: features, diff --git a/dist/transformers/path.js.map b/dist/transformers/path.js.map index 7dae7e2..ae793b2 100644 --- a/dist/transformers/path.js.map +++ b/dist/transformers/path.js.map @@ -1 +1 @@ -{"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/transformers/path.ts"],"names":[],"mappings":";;AAAA,+CAAiD;AAEjD,kCAA+D;AAC/D,sCAAiC;AACjC,yCAA2C;AAmB3C,IAAM,eAAe,GAAuB,UAAC,KAAK,EAAE,OAAO,EAAE,OAAO;IAClE,IAAM,QAAQ,GAAiB,EAAE,CAAC;IAClC,IAAM,WAAW,GAAiB,EAAE,CAAC;IACrC,IAAM,MAAM,GAAe,EAAE,CAAC;IAC9B,IAAI,iBAAiB,GAAe,EAAE,CAAC;IACvC,IAAI,mBAAmB,GAAY,IAAI,CAAC;IACxC,IAAM,YAAY,GAAG,aAAa,CAAC,YAAY,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5F,YAAY,CAAC,OAAO,CAAC,UAAC,WAAW,EAAE,CAAC;QAClC,IAAM,eAAe,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAA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,IAAM,QAAQ,GAAoB;gBAChC,IAAI,EAAE,SAAS;gBACf,WAAW,EAAE,CAAC,OAAO,CAAC;aACvB,CAAC;YACF,QAAQ,CAAC,IAAI,CAAC,IAAA,qBAAa,EAAC,QAAQ,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC;QACzD,CAAC,CAAC,CAAC;KACJ;IACD,OAAO;QACL,QAAQ,UAAA;QACR,QAAQ,EAAE,EAAE;KACb,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"} \ No newline at end of file diff --git a/docs/functions/convertSVG.html b/docs/functions/convertSVG.html index 7905cf1..4c12701 100644 --- a/docs/functions/convertSVG.html +++ b/docs/functions/convertSVG.html @@ -1 +1 @@ -convertSVG | svg-plotter

Function convertSVG

+convertSVG | svg-plotter

Function convertSVG

diff --git a/docs/functions/getSVGMetadata.html b/docs/functions/getSVGMetadata.html index 0177787..df12b2a 100644 --- a/docs/functions/getSVGMetadata.html +++ b/docs/functions/getSVGMetadata.html @@ -1 +1 @@ -getSVGMetadata | svg-plotter

Function getSVGMetadata

  • Parameters

    • parsedSVG: INode

    Returns SVGMetaData

+getSVGMetadata | svg-plotter

Function getSVGMetadata

  • Parameters

    • parsedSVG: INode

    Returns SVGMetaData

diff --git a/docs/interfaces/convertsvgoutput.html b/docs/interfaces/convertsvgoutput.html index 6a74514..00a2b87 100644 --- a/docs/interfaces/convertsvgoutput.html +++ b/docs/interfaces/convertsvgoutput.html @@ -1,3 +1,3 @@ -ConvertSVGOutput | svg-plotter

Interface ConvertSVGOutput

interface ConvertSVGOutput {
    errors: string[];
    geojson: FeatureCollection<Geometry, {
        [name: string]: any;
    }>;
}

Properties

errors +ConvertSVGOutput | svg-plotter

Interface ConvertSVGOutput

interface ConvertSVGOutput {
    errors: string[];
    geojson: FeatureCollection<Geometry, {
        [name: string]: any;
    }>;
}

Properties

Properties

errors: string[]
geojson: FeatureCollection<Geometry, {
    [name: string]: any;
}>
+

Properties

errors: string[]
geojson: FeatureCollection<Geometry, {
    [name: string]: any;
}>
diff --git a/src/transformers/composite-polygon.ts b/src/transformers/composite-polygon.ts deleted file mode 100644 index 91161d7..0000000 --- a/src/transformers/composite-polygon.ts +++ /dev/null @@ -1,32 +0,0 @@ -// function compositePolygons(polygonFeatures, properties) { -// if (!polygonFeatures) { -// throw new Error('PolygonFeatures is a required argument.') -// } else if (polygonFeatures.length < 2) { -// throw new Error('PolygonFeatures must contain 2 or more polygon features.') -// } -// const multiPolygonCoordinates = [] -// const capturedPolygonIndeces = [] -// for (let i = 0; i < polygonFeatures.length; i++) { -// if (capturedPolygonIndeces.includes(i)) { continue } -// capturedPolygonIndeces.push(i) -// const currentPolygon = polygonFeatures[i] -// const polygonGroup = [i] -// for (let j = i + 1; j < polygonFeatures.length; j++) { -// if (capturedPolygonIndeces.includes(j)) { continue } -// const nextPolygon = polygonFeatures[j] -// if (geojsonUtils.arePolygonsNested(currentPolygon, nextPolygon)) { -// capturedPolygonIndeces.push(j) -// polygonGroup.push(j) -// } -// } -// // Construct polygon -// let polygonCoordinates = [] -// polygonGroup.forEach(index => { -// polygonCoordinates = polygonCoordinates.concat( -// polygonFeatures[index].geometry.coordinates, -// ) -// }) -// multiPolygonCoordinates.push(polygonCoordinates) -// } -// return mapToFeature('MultiPolygon', multiPolygonCoordinates, properties) -// } diff --git a/src/transformers/path.ts b/src/transformers/path.ts index ae4b09b..6aa467d 100644 --- a/src/transformers/path.ts +++ b/src/transformers/path.ts @@ -1,5 +1,5 @@ import * as svgPathParser from 'svg-path-parser'; -import { SVGNodeTransformer } from '../types'; +import { ConvertSVGOptions, SVGMetaData, SVGNodeTransformer } from '../types'; import { createFeature, svgPointToCoordinate } from '../utils'; import Vector2 from '../Vector2'; import * as mathUtils from '../math-utils'; @@ -20,29 +20,44 @@ interface QuadraticCurveToCommand extends svgPathParser.QuadraticCurveToCommand y0: number; } -/** Reference: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths */ -const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { - const polygons: number[][][] = []; - const lineStrings: number[][][] = []; - const points: number[][] = []; +interface ParsedSVGPath { + polygons: number[][][]; + lineStrings: number[][][]; + points: number[][]; +} + +function parseSVGPath( + pathD: string, + transform: string, + svgMeta: SVGMetaData, + options: ConvertSVGOptions, +): ParsedSVGPath { + const output: ParsedSVGPath = { + polygons: [], + lineStrings: [], + points: [], + }; + let currentLineString: number[][] = []; let previousCurveHandle: Vector2 = null; - const pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(input.attributes.d)); + + const pathCommands = svgPathParser.makeAbsolute(svgPathParser.parseSVG(pathD)); + pathCommands.forEach((pathCommand, i) => { const previousCommand = (i > 0) ? pathCommands[i - 1] : null; if (pathCommand.code === 'M') { const command = pathCommand as svgPathParser.MoveToCommand; if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } currentLineString = [ svgPointToCoordinate( new Vector2(command.x, command.y), svgMeta, options, - input.attributes.transform, + transform, ), ]; } else if (['L', 'V', 'H'].indexOf(pathCommand.code) !== -1) { @@ -51,7 +66,7 @@ const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { new Vector2(command.x, command.y), svgMeta, options, - input.attributes.transform, + transform, )); } else if (['C', 'S'].indexOf(pathCommand.code) !== -1) { const command = pathCommand as CurveToCommand; @@ -67,7 +82,7 @@ const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { const curvePoints = mathUtils.drawCurve( (t: number) => mathUtils.pointOnCubicBezierCurve(p0, p1, p2, p3, t), options.subdivideThreshold, - ).map((p) => svgPointToCoordinate(p, svgMeta, options, input.attributes.transform)); + ).map((p) => svgPointToCoordinate(p, svgMeta, options, transform)); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p2; } else if (['Q', 'T'].indexOf(pathCommand.code) !== -1) { @@ -83,7 +98,7 @@ const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { const curvePoints = mathUtils.drawCurve( (t: number) => mathUtils.pointOnQuadraticBezierCurve(p0, p1, p2, t), options.subdivideThreshold, - ).map((p) => svgPointToCoordinate(p, svgMeta, options, input.attributes.transform)); + ).map((p) => svgPointToCoordinate(p, svgMeta, options, transform)); currentLineString = currentLineString.concat(curvePoints); previousCurveHandle = p1; } else if (pathCommand.code === 'A') { @@ -100,21 +115,34 @@ const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { p0, p1, rx, ry, xAxisRotation, largeArc, sweep, t, ), options.subdivideThreshold, - ).map((p) => svgPointToCoordinate(p, svgMeta, options, input.attributes.transform)); + ).map((p) => svgPointToCoordinate(p, svgMeta, options, transform)); currentLineString = currentLineString.concat(curvePoints); } else if (pathCommand.code === 'Z') { currentLineString.push(currentLineString[0]); - polygons.push(currentLineString); + output.polygons.push(currentLineString); currentLineString = []; } }); if (currentLineString.length === 1) { - points.push(currentLineString[0]); + output.points.push(currentLineString[0]); } else if (currentLineString.length > 1) { - lineStrings.push(currentLineString); + output.lineStrings.push(currentLineString); } + + return output; +} + +/** Reference: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths */ +const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { + const { + points, + lineStrings, + polygons, + } = parseSVGPath(input.attributes.d, input.attributes.transform, svgMeta, options); + const features: GeoJSON.Feature[] = []; const properties = options.propertyMapper ? options.propertyMapper(input) : null; + if (points.length) { points.forEach((point) => { const id = options.idMapper ? options.idMapper(input) : null; @@ -135,21 +163,16 @@ const pathTransformer: SVGNodeTransformer = (input, svgMeta, options) => { features.push(createFeature(geometry, id, properties)); }); } + // TODO: Map >1 polygons to a GeoJSON MultiPolygon feature if (polygons.length) { - const polygonFeatures = polygons.map((polygon) => { + polygons.forEach((polygon) => { const id = options.idMapper ? options.idMapper(input) : null; const geometry: GeoJSON.Polygon = { type: 'Polygon', coordinates: [polygon], }; - return createFeature(geometry, id, properties); + features.push(createFeature(geometry, id, properties)); }); - features.push(polygonFeatures[0]); - // features.push( - // polygonFeatures.length > 1 - // ? compositePolygons(polygonFeatures, properties) - // : polygonFeatures[0], - // ); } return { features,