[postgis-users] How to instanziate the Geometry class from the geometry string?

Markus Schaber schabi at logix-tt.com
Wed Sep 20 01:54:35 PDT 2006


Hi, Martinelli,

martinelli at pi.ingv.it wrote:

> OJB requires the table fields to be defined as one of the types defined in
> java.sql.Types in its configuration file, therefore POINT is not allowed.
> Using VARCHAR I receive the geometry data string.

I'm afraid that you'll have to go the way of receiving them as VARCHAR,
unless OJB begins to support java.sql.Types.Other.

> However, to convert the geometry value received from the table I need to
> write the "serializer" to create the geometry instance (of class Point in
> my case).

> What method(s) I need to call to instanziate the proper class?
> Please consider I have only the geometry string returned by the database,
> and it is not in WKB or WKT format.

The geometry string from the database (should be a bunch of hex
characters) is Hexed EWKB, an downwards-compatible PostGIS extension (to
include SRIDs and measures) of the OpenGIS standard WKB (Well Known
Binary). It is converted to hex as the JDBC driver currently drives the
connection in text mode, the same as pgadmin3 or the psql command line.

As a side note, PostGIS 0.X delivered E(xtended) WKT formats, PostGIS
1.X will deliver pure EWKB when the PostgreSQL connection is driven in
binary mode.

The org.postgis.PGGeometry class has a method public static Geometry
geomFromString(String value) that will happily parse WKT, EWKT, HexWKB
and HexEWKB into the proper org.postgis.Geometry subclass.

If you compile PostGIS with JTS support, the org.postgis.jts.JTSGeometry
class geomFromString(String value) method will do the same with the
appropriate com.vividsolutions.jts.geom.Geometry subclass.

For the other way round (deserializer), you have the following
possibilities:
For org.postgis.Geometry:
- .toString() delivers EWKT.
- org.postgis.binary.binaryWriter.writeHexed() delivers HexEWKB.

For jts Geometries.
- .toSTring() delivers WKT.
- org.postgis.jts.JtsBinaryWriter.writeHexed() delivers HexEWKB.

The PostgreSQL backend will parse both EWKT and HexEWKB in text mode,
and WKB in binary mode.

HTH,
Markus
-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in Europe! www.ffii.org
www.nosoftwarepatents.org



More information about the postgis-users mailing list