From 73c10b00aef8452231f58d60b53e20b2e319891d Mon Sep 17 00:00:00 2001 From: Andreas Voegele Date: Mon, 15 Mar 2021 15:47:03 +0100 Subject: [PATCH] Ignore empty gx:coord elements in KML files --- src/ol/format/KML.js | 24 ++++++++++++++++-------- test/browser/spec/ol/format/kml.test.js | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index 977a6dce3f..6f5a893b65 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -65,7 +65,7 @@ import {transformGeometryWithOptions} from './Feature.js'; /** * @typedef {Object} GxTrackObject - * @property {Array} flatCoordinates Flat coordinates. + * @property {Array>} coordinates Coordinates. * @property {Array} whens Whens. */ @@ -1466,7 +1466,7 @@ function gxCoordParser(node, objectStack) { const gxTrackObject = /** @type {GxTrackObject} */ (objectStack[objectStack.length - 1]); - const flatCoordinates = gxTrackObject.flatCoordinates; + const coordinates = gxTrackObject.coordinates; const s = getAllTextContent(node, false); const re = /^\s*([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s*$/i; const m = re.exec(s); @@ -1474,9 +1474,9 @@ function gxCoordParser(node, objectStack) { const x = parseFloat(m[1]); const y = parseFloat(m[2]); const z = parseFloat(m[3]); - flatCoordinates.push(x, y, z, 0); + coordinates.push([x, y, z]); } else { - flatCoordinates.push(0, 0, 0, 0); + coordinates.push([]); } } @@ -1530,7 +1530,7 @@ const GX_TRACK_PARSERS = makeStructureNS( function readGxTrack(node, objectStack) { const gxTrackObject = pushParseAndPop( /** @type {GxTrackObject} */ ({ - flatCoordinates: [], + coordinates: [], whens: [], }), GX_TRACK_PARSERS, @@ -1540,14 +1540,22 @@ function readGxTrack(node, objectStack) { if (!gxTrackObject) { return undefined; } - const flatCoordinates = gxTrackObject.flatCoordinates; + const flatCoordinates = []; + const coordinates = gxTrackObject.coordinates; const whens = gxTrackObject.whens; for ( - let i = 0, ii = Math.min(flatCoordinates.length, whens.length); + let i = 0, ii = Math.min(coordinates.length, whens.length); i < ii; ++i ) { - flatCoordinates[4 * i + 3] = whens[i]; + if (coordinates[i].length == 3) { + flatCoordinates.push( + coordinates[i][0], + coordinates[i][1], + coordinates[i][2], + whens[i] + ); + } } return new LineString(flatCoordinates, GeometryLayout.XYZM); } diff --git a/test/browser/spec/ol/format/kml.test.js b/test/browser/spec/ol/format/kml.test.js index 470688cd07..872e6ea91e 100644 --- a/test/browser/spec/ol/format/kml.test.js +++ b/test/browser/spec/ol/format/kml.test.js @@ -1603,9 +1603,11 @@ describe('ol.format.KML', function () { ' 2014-01-06T19:38:55Z' + ' 2014-01-06T19:39:03Z' + ' 2014-01-06T19:39:10Z' + + ' 2014-01-06T19:39:17Z' + ' 8.1 46.1 1909.9' + ' 8.2 46.2 1925.2' + ' 8.3 46.3 1926.2' + + ' ' + ' ' + ' ' + '';