From 6d1e8ee4cd476fa91b4cd7a45f878da78d6cf50e Mon Sep 17 00:00:00 2001 From: Martin Davis Date: Tue, 27 Oct 2020 12:25:59 -0700 Subject: [PATCH] Improve WKBWriter to write empty polygons with no rings Signed-off-by: Martin Davis --- .../org/locationtech/jts/io/WKBWriter.java | 9 ++++-- .../locationtech/jts/io/WKBWriterTest.java | 32 ++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java b/modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java index 69b303ff56..51b7b7b3ab 100644 --- a/modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java +++ b/modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java @@ -45,9 +45,7 @@ * @@ -363,6 +361,11 @@ private void writePolygon(Polygon poly, OutStream os) throws IOException { writeByteOrder(os); writeGeometryType(WKBConstants.wkbPolygon, poly, os); + //--- write empty polygons with no rings (OCG extension) + if (poly.isEmpty()) { + writeInt(0, os); + return; + } writeInt(poly.getNumInteriorRing() + 1, os); writeCoordinateSequence(poly.getExteriorRing().getCoordinateSequence(), true, os); for (int i = 0; i < poly.getNumInteriorRing(); i++) { diff --git a/modules/core/src/test/java/org/locationtech/jts/io/WKBWriterTest.java b/modules/core/src/test/java/org/locationtech/jts/io/WKBWriterTest.java index 547195199a..1eb5ff2a96 100644 --- a/modules/core/src/test/java/org/locationtech/jts/io/WKBWriterTest.java +++ b/modules/core/src/test/java/org/locationtech/jts/io/WKBWriterTest.java @@ -77,16 +77,40 @@ public void testSRID() throws Exception { } public void testPointEmpty2D() { - checkWKB("POINT EMPTY", 2, ByteOrderValues.LITTLE_ENDIAN, "0101000000000000000000F87F000000000000F87F" ); + checkWKB("POINT EMPTY", 2, "0101000000000000000000F87F000000000000F87F" ); } public void testPointEmpty3D() { - checkWKB("POINT EMPTY", 3, ByteOrderValues.LITTLE_ENDIAN, "0101000080000000000000F87F000000000000F87F000000000000F87F" ); + checkWKB("POINT EMPTY", 3, "0101000080000000000000F87F000000000000F87F000000000000F87F" ); } - void checkWKB(String wkt, int dimension, int byteOrder, String expectedWKBHex) { + public void testPolygonEmpty2DSRID() { + checkWKB("POLYGON EMPTY", 2, ByteOrderValues.LITTLE_ENDIAN, 4326, "0103000020E610000000000000" ); + } + + public void testPolygonEmpty2D() { + checkWKB("POLYGON EMPTY", 2, "010300000000000000" ); + } + + public void testPolygonEmpty3D() { + checkWKB("POLYGON EMPTY", 3, "010300008000000000" ); + } + + void checkWKB(String wkt, int dimension, String expectedWKBHex) { + checkWKB(wkt, dimension, ByteOrderValues.LITTLE_ENDIAN, -1, expectedWKBHex); + } + + void checkWKB(String wkt, int dimension, int byteOrder, int srid, String expectedWKBHex) { Geometry geom = read(wkt); - WKBWriter wkbWriter = new WKBWriter(dimension, byteOrder); + + // set SRID if not -1 + boolean includeSRID = false; + if (srid >= 0) { + includeSRID = true; + geom.setSRID(srid); + } + + WKBWriter wkbWriter = new WKBWriter(dimension, byteOrder, includeSRID); byte[] wkb = wkbWriter.write(geom); String wkbHex = WKBWriter.toHex(wkb);