3

It's been a few days that I'm struggling with WKB strings. I need to be able to parse it in order to get the equivalent geometry and extract points coordinates (X,Y,Z). I can't use PostGIS functions. The only java library that I found was the JTS Topology Suite, that i use as follow :

String wkb = "01ea030000020000009b4d3899fe95154153d97e8f43875941000000000000454003085bc23f9615411b4dc406578759410000000000004740"
byte[] aux = WKBReader.hexToBytes(wkb);
try {
    Geometry geom = new WKBReader().read(aux);
} catch (ParseException e) {
    e.printStackTrace();
    System.err.println("Bad WKB string.");
}

But the it gives me the following error :

com.vividsolutions.jts.io.ParseException: Unknown WKB type 234

234 is the decimal value of the hexadecimal string 'ea'. It's like the JTS Library was only looking at the frist 2 bytes instead of looking at the 4 bytes 'ea03', that correspond to 1002 in little endian (so a LineStringZ).

My question is then : does the JTS Topology Suite handle LineStringZ ? If not, why points can have a Z value ? And how can I parse it correctly ?

Thanks you for reading !

user3890394
  • 99
  • 1
  • 7

3 Answers3

0

JTs only supports 2D geometries, it does not support a Z value. Points only have X and Y in it.

bugmenot123
  • 1,069
  • 1
  • 18
  • 33
0

JTS does support 3D geometries at least in reading and writing. Support may be rather 2.5D than a real 3D but Z values are still carried on in the operations. The problem is that there are two ways for presenting XYZ, XYM, and XYZM geometries in WKB. JTS support the PostGIS EWKB variant as can be seem from the comment in the source code file https://sourceforge.net/p/jts-topo-suite/code/HEAD/tree/trunk/jts/java/src/com/vividsolutions/jts/io/WKBWriter.java

  • This implementation also supports the Extended WKB
  • standard. Extended WKB allows writing 3-dimensional coordinates
  • and including the geometry SRID value.
  • The presence of 3D coordinates is signified
  • by setting the high bit of the wkbType word.
  • The presence of an SRID is signified
  • by setting the third bit of the wkbType word.
  • EWKB format is upward compatible with the original SFS WKB format.

Your WKB is of the OGC variant which is defined in the OGC document http://portal.opengeospatial.org/files/?artifact_id=25355

JTS don't understand the four-digit geometry type codes. This mail thread gives some more information https://lists.osgeo.org/pipermail/geos-devel/2013-December/006757.html.

user30184
  • 116
  • 1
0

JTS Topology suite supports 3D data but in EWKB format. ISO WKB is not supported. If just so happens that you are using postgis, it supports EWKB. ST_GeomFromEWKB <-> ST_AsEWKB

Also if you are writing your EWKB with WKBWriter, don't forget to specify output dimmensions :

 WKBWriter wkbw = new WKBWriter(3);
Vincnetas
  • 146
  • 1
  • 4