Class WKBReader
- java.lang.Object
-
- org.locationtech.jts.io.WKBReader
-
public class WKBReader extends java.lang.Object
Reads aGeometry
from a byte stream in Well-Known Binary format. Supports use of anInStream
, which allows easy use with arbitrary byte stream sources.This class reads the format describe in
WKBWriter
. It partially handles the Extended WKB format used by PostGIS, by parsing and storing optional SRID values. If a SRID is not specified in an element geometry, it is inherited from the parent's SRID. The default SRID value is 0.Although not defined in the WKB specification, empty points are handled if they are represented as a Point with
NaN
X and Y ordinates.The reader repairs structurally-invalid input (specifically, LineStrings and LinearRings which contain too few points have vertices added, and non-closed rings are closed).
The reader handles most errors caused by malformed or malicious WKB data. It checks for obviously excessive values of the fields
numElems
,numRings
, andnumCoords
. It also checks that the reader does not read beyond the end of the data supplied. AParseException
is thrown if this situation is detected.This class is designed to support reuse of a single instance to read multiple geometries. This class is not thread-safe; each thread should create its own instance.
As of version 1.15, the reader can read geometries following the OGC 06-103r4 Simple Features Access 1.2.1 specification, which aligns with the ISO 19125 standard. This format is used by Spatialite and Geopackage.
The difference between PostGIS EWKB format and the new ISO/OGC specification is that Z and M coordinates are detected with a bit mask on the higher byte in the former case (0x80 for Z and 0x40 for M) while new OGC specification use specific int ranges for 2D geometries, Z geometries (2D code+1000), M geometries (2D code+2000) and ZM geometries (2D code+3000).
Note that the
WKBWriter
is not changed and still writes the PostGIS EWKB geometry format.- See Also:
for a formal format specification
-
-
Constructor Summary
Constructors Constructor Description WKBReader()
WKBReader(GeometryFactory geometryFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static byte[]
hexToBytes(java.lang.String hex)
Converts a hexadecimal string to a byte array.Geometry
read(byte[] bytes)
Reads a singleGeometry
in WKB format from a byte array.Geometry
read(InStream is)
-
-
-
Constructor Detail
-
WKBReader
public WKBReader()
-
WKBReader
public WKBReader(GeometryFactory geometryFactory)
-
-
Method Detail
-
hexToBytes
public static byte[] hexToBytes(java.lang.String hex)
Converts a hexadecimal string to a byte array. The hexadecimal digit symbols are case-insensitive.- Parameters:
hex
- a string containing hex digits- Returns:
- an array of bytes with the value of the hex string
-
read
public Geometry read(byte[] bytes) throws ParseException
Reads a singleGeometry
in WKB format from a byte array.- Parameters:
bytes
- the byte array to read from- Returns:
- the geometry read
- Throws:
ParseException
- if the WKB is ill-formed
-
read
public Geometry read(InStream is) throws java.io.IOException, ParseException
- Parameters:
is
- the stream to read from- Returns:
- the Geometry read
- Throws:
java.io.IOException
- if the underlying stream creates an errorParseException
- if the WKB is ill-formed
-
-