Skip to content

Commit

Permalink
Handle empty components in WKTParser (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Aug 23, 2023
1 parent 02bd059 commit 00ee2b5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/org/locationtech/jts/io/WKTParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
57 changes: 57 additions & 0 deletions test/manual/issues/499.js
Original file line number Diff line number Diff line change
@@ -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
)
})

0 comments on commit 00ee2b5

Please sign in to comment.