geometry-api-java icon indicating copy to clipboard operation
geometry-api-java copied to clipboard

Polygon exported to WKB with "wkbExportDefaults" has type of multipolygon

Open ljader opened this issue 7 years ago • 5 comments

OperatorExportToWkbLocal doesn't properly handle wkbExportDefaults - exported WKB has "multipolygon" (6) type written instead of "polygon" (3).

Here is reproducible example:

package ljader.test;

import java.nio.ByteBuffer;

import com.esri.core.geometry.OperatorExportToWkb;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.WkbExportFlags;

public class WkbExportPolygonWrongType {
	public static void main(String[] args) {
		ByteBuffer byteBuffer = OperatorExportToWkb.local().execute(WkbExportFlags.wkbExportDefaults, createPolygon1(), null);
		// WKB Polygon type is 3
		System.out.println(byteBuffer.getInt(1)); // outputs 6
	}

	public static Polygon createPolygon1() {
		Polygon poly = new Polygon();
		poly.startPath(0, 0);
		poly.lineTo(10, 10);
		poly.lineTo(0, 10);
		poly.lineTo(0, 0);
		return poly;
	}
}

ljader avatar Apr 11 '18 21:04 ljader

@ljader This is not a bug, because MultiPolygon can contain one Polygon. If you would like it to write Polygon type explicitly, provide WkbExportFlags.wkbExportPolygon flag. It does not try to write Polygon type automatically, exports MultiPolygon instead.

stolstov avatar Apr 11 '18 21:04 stolstov

Since I provided Polygon object to the export operation with "default" options, I expected the "polygon_wkb" would be returned - so when no flag is specified (default options), the polygon type is taken into account when writing WKB.

Note: OperatorImportFromWkbLocal when provided with Geometry.GeometryType.Unknown (and default import flags), but the type in WKB is poligon - the operator imports it as "single" polygon.

Here is little background for this ticket: I get byte[] from Hibernate from Oracle 11.2 SDO_GEOMETRY with "polygon_as_wkb" - then I import it as Geometry, do some operations on it like removing vertex, and I save it back to database as byte[].

The exported WKB contains "multipolygon" flag (which ends up in database in SDO_GEOMETRY structure) - so I'm affected by "bug" in SDO_BUFFER for Oracle 11.2, which cannot handle negative buffer properly for "polygon_with_multipolygon_flag" and returns null instead of buffered object.

In the end I will try "guess by myself" if I'm exporting polygon and I will provide the export flag, but for me it looks like the library could do it as well.

ljader avatar Apr 11 '18 22:04 ljader

@ljader Call getExteriorRingCount() on the polygon and check that it is less or equal to 1. Then write out WKB Polygon if it is.

stolstov avatar Apr 12 '18 01:04 stolstov

@stolstov thank you for the tip.

Side note: there is a typo / wrong import of WKT flags here: https://github.com/Esri/geometry-api-java/blob/master/src/main/java/com/esri/core/geometry/OperatorExportToWkbLocal.java#L173

ljader avatar Apr 12 '18 20:04 ljader

@ljader Thank you, I've submitted a PR to fix the typo: https://github.com/Esri/geometry-api-java/pull/171. There is no bug I think, but the typo is misleading.

stolstov avatar Apr 12 '18 20:04 stolstov