Ignore empty gx:coord elements in KML files

This commit is contained in:
Andreas Voegele
2021-03-15 15:47:03 +01:00
committed by Andreas Vögele
parent ab8a16a8a7
commit 73c10b00ae
2 changed files with 18 additions and 8 deletions

View File

@@ -65,7 +65,7 @@ import {transformGeometryWithOptions} from './Feature.js';
/** /**
* @typedef {Object} GxTrackObject * @typedef {Object} GxTrackObject
* @property {Array<number>} flatCoordinates Flat coordinates. * @property {Array<Array<number>>} coordinates Coordinates.
* @property {Array<number>} whens Whens. * @property {Array<number>} whens Whens.
*/ */
@@ -1466,7 +1466,7 @@ function gxCoordParser(node, objectStack) {
const gxTrackObject = const gxTrackObject =
/** @type {GxTrackObject} */ /** @type {GxTrackObject} */
(objectStack[objectStack.length - 1]); (objectStack[objectStack.length - 1]);
const flatCoordinates = gxTrackObject.flatCoordinates; const coordinates = gxTrackObject.coordinates;
const s = getAllTextContent(node, false); 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 re = /^\s*([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s*$/i;
const m = re.exec(s); const m = re.exec(s);
@@ -1474,9 +1474,9 @@ function gxCoordParser(node, objectStack) {
const x = parseFloat(m[1]); const x = parseFloat(m[1]);
const y = parseFloat(m[2]); const y = parseFloat(m[2]);
const z = parseFloat(m[3]); const z = parseFloat(m[3]);
flatCoordinates.push(x, y, z, 0); coordinates.push([x, y, z]);
} else { } else {
flatCoordinates.push(0, 0, 0, 0); coordinates.push([]);
} }
} }
@@ -1530,7 +1530,7 @@ const GX_TRACK_PARSERS = makeStructureNS(
function readGxTrack(node, objectStack) { function readGxTrack(node, objectStack) {
const gxTrackObject = pushParseAndPop( const gxTrackObject = pushParseAndPop(
/** @type {GxTrackObject} */ ({ /** @type {GxTrackObject} */ ({
flatCoordinates: [], coordinates: [],
whens: [], whens: [],
}), }),
GX_TRACK_PARSERS, GX_TRACK_PARSERS,
@@ -1540,14 +1540,22 @@ function readGxTrack(node, objectStack) {
if (!gxTrackObject) { if (!gxTrackObject) {
return undefined; return undefined;
} }
const flatCoordinates = gxTrackObject.flatCoordinates; const flatCoordinates = [];
const coordinates = gxTrackObject.coordinates;
const whens = gxTrackObject.whens; const whens = gxTrackObject.whens;
for ( for (
let i = 0, ii = Math.min(flatCoordinates.length, whens.length); let i = 0, ii = Math.min(coordinates.length, whens.length);
i < ii; i < ii;
++i ++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); return new LineString(flatCoordinates, GeometryLayout.XYZM);
} }

View File

@@ -1603,9 +1603,11 @@ describe('ol.format.KML', function () {
' <when>2014-01-06T19:38:55Z</when>' + ' <when>2014-01-06T19:38:55Z</when>' +
' <when>2014-01-06T19:39:03Z</when>' + ' <when>2014-01-06T19:39:03Z</when>' +
' <when>2014-01-06T19:39:10Z</when>' + ' <when>2014-01-06T19:39:10Z</when>' +
' <when>2014-01-06T19:39:17Z</when>' +
' <gx:coord>8.1 46.1 1909.9</gx:coord>' + ' <gx:coord>8.1 46.1 1909.9</gx:coord>' +
' <gx:coord>8.2 46.2 1925.2</gx:coord>' + ' <gx:coord>8.2 46.2 1925.2</gx:coord>' +
' <gx:coord>8.3 46.3 1926.2</gx:coord>' + ' <gx:coord>8.3 46.3 1926.2</gx:coord>' +
' <gx:coord/>' +
' </gx:Track>' + ' </gx:Track>' +
' </Placemark>' + ' </Placemark>' +
'</kml>'; '</kml>';