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

martinelli at pi.ingv.it martinelli at pi.ingv.it
Fri Oct 6 02:55:01 PDT 2006


Hi Markus,
your tip wasn't applicable to the ODMG interface from OJB, but it gave me
the idea about the solution (it worked for Point class), setting the
jdbc-type="VARBINARY".
Here is the snippet in case someone may benefit from it:

public class Point2ClassConversion implements FieldConversion {
  private BinaryParser parser = new BinaryParser();
  private BinaryWriter writer = new BinaryWriter();
  public Object javaToSql(Object source) throws ConversionException {
    MyClass point = (MyClass)source;
    Point gisPoint = new Point(point.getLong(), javaPoint.getLat());
    gisPoint.setSrid(point.getSrid());

    byte[] sqlPoint = writer.writeBinary(gisPoint);

    return puntoSql;
  }
  public Object sqlToJava(Object source) throws ConversionException {
    byte[] sqlPoint = (byte[])source;
    StringBuffer buffer = new StringBuffer();

    for (int count=0; count<sqlPoint.length; count++) {
      buffer.append(new Character((char)sqlPoint[count]));
    }
    Geometry gisPoint = parser.parse(buffer.toString());

    MyClass point = new MyClass(((Point)geom).x, ((Point)geom).y);
    point.setSrid(((Point)geom).getSrid());

    return point;
  }
}

Regards,
Francesco.


> Hi, Francesco,
>
> martinelli at pi.ingv.it wrote:
>
>> during my last email I thought I got the solution, as method
>> org.postgis.binary.binaryWriter.writeHexed()
>>
>> returned the same string as I received from the database, and indeed the
>> same string as showed querying pgadmin3.
>>
>> Nevertheless, when I moved my sptes forward and tried to really connect
>> to
>> the databse for updating the geometry data, the following error came up:
>> org.postgresql.util.PSQLException: ERROR: column "the_geom" is of type
>> geometry but expression is of type character
>
> Did you wrap the String into an appropriate PGobject, and use
> setObject() to set it in the prepared statement?
>
> If you use setString(), the JDBC driver tells the server that the type
> is Text.
>
> Another possibility to cope with this is using an explicit cast in the
> statement, like:
>
> INSERT INTO mytable (the_geom) VALUES (?::geometry);
>
> 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