From 00ee2b5b67bcef02d9aa3b3a2339ec1e7426d16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Wed, 23 Aug 2023 21:45:35 +0200 Subject: [PATCH] Handle empty components in WKTParser (#503) --- src/org/locationtech/jts/io/WKTParser.js | 6 ++- test/manual/issues/499.js | 57 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 test/manual/issues/499.js diff --git a/src/org/locationtech/jts/io/WKTParser.js b/src/org/locationtech/jts/io/WKTParser.js index f6754e4e..0d4c6e0a 100644 --- a/src/org/locationtech/jts/io/WKTParser.js +++ b/src/org/locationtech/jts/io/WKTParser.js @@ -527,13 +527,15 @@ class Parser { parseGeometry_() { const factory = this.factory - const o2c = ordinates => new Coordinate(...ordinates) + const o2c = ordinates => ordinates ? new Coordinate(...ordinates) : new Coordinate() const ca2p = coordinates => { const rings = coordinates.map(a => factory.createLinearRing(a.map(o2c))) if (rings.length > 1) return factory.createPolygon(rings[0], rings.slice(1)) - else + else if (rings.length === 1) return factory.createPolygon(rings[0]) + else + return factory.createPolygon() } const token = this.token_ diff --git a/test/manual/issues/499.js b/test/manual/issues/499.js new file mode 100644 index 00000000..69a4b582 --- /dev/null +++ b/test/manual/issues/499.js @@ -0,0 +1,57 @@ +import GeometryFactory from '../../../src/org/locationtech/jts/geom/GeometryFactory.js' +import PrecisionModel from '../../../src/org/locationtech/jts/geom/PrecisionModel.js' +import WKTReader from '../../../src/org/locationtech/jts/io/WKTReader.js' + +const createGeometryWithGeometryFactory = (wkt, digits) => { + const gf = createGeometryFactory(digits) + const reader = new WKTReader(gf) + return reader.read(wkt) +} + +const createGeometryFactory = (digits) => { + const scale = Math.pow(10, digits) + const pm = new PrecisionModel(scale) + return new GeometryFactory(pm) +} + +it('createMultiPoint', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory('MULTIPOINT ((180 270), (330 270), (330 150), (180 150))', 2) +}) + +it('createMultiPointWithEmpty', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory('MULTIPOINT ((180 270), (330 270), (330 150), (180 150), EMPTY)', 2) +}) + +it('createMultiLineString', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory( + 'MULTILINESTRING ((100 300, 200 300), (200 300, 100 200), (100 200, 200 200))', + 2 + ) +}) + +it('createMultiLineStringWithEmpty', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory( + 'MULTILINESTRING ((100 300, 200 300), (200 300, 100 200), (100 200, 200 200), EMPTY)', + 2 + ) +}) + +it('createMultiPolygon', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory( + 'MULTIPOLYGON (((200 270, 260 270, 260 210, 200 210, 200 270)),((270 200, 330 200, 330 150, 270 150, 270 200)))', + 2 + ) +}) + +it('createMultiPolygonWithEmpty', () => { + const gf = createGeometryFactory(2) + const geom = createGeometryWithGeometryFactory( + 'MULTIPOLYGON (((200 270, 260 270, 260 210, 200 210, 200 270)),((270 200, 330 200, 330 150, 270 150, 270 200)), EMPTY)', + 2 + ) +}) \ No newline at end of file