[postgis-users] adding a geometry to a Postgis db
Kevin Neufeld
kneufeld at refractions.net
Sun Apr 26 23:41:08 PDT 2009
Hi Paul,
I do this all the time and find the JTS WKBWriter and WKBReader work well.
Your code could look something like this:
WKBWriter wkbWriter = new WKBWriter(3); // if writing in 3D
pstmt.setBytes(4, wkbWriter.write(geo));
Here is a simple example in reading and writing JTS geometries from PostGIS
Class.forName(“org.postgresql.Driver”);
Connection conn =
DriverManager.getConnection(“jdbc:postgresql://...”);
WKBReader wkbReader = new WKBReader();
WKBWriter wkbWriter = new WKBWriter();
String query =
"SELECT the_geom FROM my_spatial_table " +
"WHERE ST_Intersects(the_geom, ST_GeomFromWKB(?, 3005))");
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBytes(1, wkbWriter.write(myJTSPolygon));
ResultSet rs = pstmt.executeQuery();
while(rs.next) {
Geometry g = wkbReader.read(WKBReader.hexToBytes(
rs.getString(1)));
...
// Do stuff with Geometry from my_spatial_table
}
Cheers,
Kevin
Malm Paul wrote:
> Hi list,
> I have a geometry (com.vividsolutions.jts.geom.Geometry) which I would like to add as a geometry to the geometry colunm see below.
> how can I do this in java (I don´t know so much of databases)?
>
> PreparedStatement pstmt = null;
> String query = "insert into submap(submapid, mapid, blob, the_geom) values(?, ?, ?, ?)";
> pstmt = postGISdb.dbConnection.prepareStatement(query);
> pstmt.setInt(1, submapid);
> pstmt.setInt(2, mapId);
> pstmt.setBinaryStream(3, blob.getInputStream(), blob.toByteArray().length);
>
> //vividsolution geometry
> Geometry geo = submaps.get(submapNo).getSubmapsExtent();
> pstmt.set?????(4, ????);
> int rowCount = pstmt.executeUpdate();
>
>
> /kind regards,
> Paul
> _______________________________________________
> 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