[postgis-users] Inserting Geometries into PostGres database - ???

Stephen Woodbridge woodbri at swoodbridge.com
Tue Sep 14 14:38:56 PDT 2010


On 9/14/2010 5:12 PM, CS wrote:
> Hi all. I am having problems inserting a geometry type into a database.
> I am using postgres 8.4 and whatever the latest postgis there is(i
> forgot now). Linux with glassfish is the server environment.
>
> I have seen multiple hardcoded examples about how to insert something in
> the VALUES clause but what if i have an array of polygons that cannot be
> hardcoded that is already determined? That is the coordinates and type
> of the shape is already decided. This lends to the question as to
> whether you can use a preparedStatement and setString to insert a
> geometry. This is what i need some help on.
>
>
> an example of a WKT for a certain cell that i have is:
>
> POLYGON ((-90.406 31.07360133086052, -90.406 31.34423828125,
> -90.09073538749632 31.34423828125, -90.09073538749632 31.07360133086052,
> -90.406 31.07360133086052))
>
> i want to be able to take this(as a string, if possible) and insert into
> the database using a prepared statement.
>
>
> PreparedStatement.setString(1, cells[cellIndex].getShape().getWkt());,
> where getWKT returns the string representation. Of course this doesnt
> work because the type of the column is a postgis geometry. So, again,
> how do you use a prepared statement to insert a WKT string representation?
>
>
> I have tried this:
>
> String tempString = cells[cellIndex].getShape().getWkt();
> String parameterInsert = "insert into Cell(result, report, modified,
> cellid, missionid, shape) values (?, ?, ?, ?, ?,
> GeomFromText(tempString,4326))";

You might try something like:

String parameterInsert = "insert into Cell(result, report, modified,
cellid, missionid, shape) values (?, ?, ?, ?, ?,
  GeomFromText(" + tempString + ",4326))";

And see if that works.

-Steve W

> where tempString would be the POLYGON above and i get the error:
> org.postgresql.util.PSQLException: ERROR: Invalid OGC WKT (does not
>
> start with P,L,M or G), which doesnt really make much sense to me.
>
>
> I try to put single quotes around each one like:
>
> String tempString = "'" + cells[cellIndex].getShape().getWkt() + "'";
>
> and i get the error that tempString as a column does not exist.
>
> There also seems to be some confusion on the postgis message boards
> whether a single quote is even needed. I was never able to come to a
> definite conclusion.
>
>
> I would appreciate help to all who could help. ive tried everything that
> i know at this point. Thanks a bunch in advance!
>
> CS
>
>
>
> _______________________________________________
> 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