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

martinelli at pi.ingv.it martinelli at pi.ingv.it
Wed Sep 20 03:29:27 PDT 2006


Thank you Markus.
Also the tip on the "serializer" worked perfectly, I used:
   org.postgis.binary.binaryWriter.writeHexed()

Just a note:
looking at the same package (org.postgis.binary) I also tried for the
"derializer" the following snippet:
    BinaryParser parser = new BinaryParser();
    Geometry geom = parser.parse((String)source);
and (at least "apparently") it worked without the need of looking for
SQLException, which is required for:
    Geometry geom = PGgeometry.geomFromString((String) source);
requires a try-catch.

Is there a reson for preferring the second one?

Thank you,
Francesco.


> 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
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
>





More information about the postgis-users mailing list