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 @@
*
* - Point: a
WKBPoint
with NaN
ordinate values
* - LineString: a
WKBLineString
with zero points
- * - Polygon: currently output as a
WKBPolygon
with one LinearRing
with zero points.
- * Note: This is different to other systems. It will change to a WKBPolygon
with zero LinearRing
s.
- *
+ * - Polygon: a
WKBPolygon
with zero rings
* - Multi geometries: a
WKBMulti
geometry of appropriate type with zero elements
* - GeometryCollections: a
WKBGeometryCollection
with zero elements
*
@@ -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);