Entering the world of GIS
At my current project we store information about buildings in a database. A part of that information is geographic information about the location of the building. This information is sent in a SOAP message and the geographic part is formatted according to the GML standard. This information (both geographic and ‘normal’ data) is persisted in the database (PostgreSQL with PostGIS) by using Hibernate.
Until now we didn’t have to validate this geographic data so we just stored it in the database by using a custom FieldType in Hibernate (just the most important parts are shown here):
-
import org.hibernate.HibernateException;
-
import org.hibernate.usertype.EnhancedUserType;
-
import org.postgis.PGgeometry;
-
-
/**
-
* This class is the Hibernate UserType class,
-
* for more information see the hibernate docs at
-
* http://www.hibernate.org
-
*
-
* @author Rob Augustinus, OpenSolutions
-
*/
-
public class GeometryType implements EnhancedUserType {
-
-
-
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
-
return (PGgeometry) rs.getObject(names[0]);
-
}
-
-
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
-
if (value == null) {
-
} else {
-
statement.setObject(index, value);
-
}
-
}
-
-
public Class returnedClass() {
-
return PGgeometry.class;
-
}
-
-
public int[] sqlTypes() {
-
}
-
}
So to persist the data of a building we create a Model object ‘Building’ which has one field ‘Geometry’ that is of the type ‘GeometryType’.
But, as usual, the demands of the application change and it now looks like we will have to perform some validations on the geographic data, prefferable before we store it in the database. So I was asked if this was possible and how it should be done.
The standard for this kind of geometric validations is JTS (JTS Topology Suite) API. After downloading an implementation I got to the next problem. The XML message we receive is translated to our Hibernate model object which is using the type open.gis.PGgeometry as a wrapper around the org.opengis.Geometry class. Now in the new situation I have to translate the openGIS Geometry type to the JTS implementation to be able to perform the necessary validations. I started to write some code myself, because I couldn’t find a clear answer for this on the internet. I used the WKT (Well Known Text) option of both types. You will get something like this:
-
org.postgis.Geometry postGisGeometry1 = org.postgis.PGgeometry.geomFromString(“LINESTRING (0 0, 30 30)”);
-
org.postgis.Geometry postGisGeometry2 = org.postgis.PGgeometry.geomFromString(“LINESTRING (30 0, 0 30)”);
-
-
com.vividsolutions.jts.geom.Geometry jtsGeometry1 = new WKTReader().read( postGisGeometry1.toString());
-
com.vividsolutions.jts.geom.Geometry jtsGeometry2 = new WKTReader().read( postGisGeometry2.toString());
-
But writing every coordinate to a String and read that back into another object isn’t an acceptable solution. So I dug some more and ended up with the beta code of the OpenGIS JDBC driver. I compiled it, jarred it added it to my project. With this JDBC-extension the resultSet getObject() will not return an org.opengis.PGGeometry object but the JTS implementation, org.postgis.jts.JtsGeometry. If you then perform a getGeometry at this object you will get a JTS object. See this code:
-
-
while (rs.next()) {
-
-
Geometry geo = ((JtsGeometry)obj).getGeometry();
-
}
With this JDBC extension in place we can modify our custom-made GeometryType and work with JTS types directly. No more String conversions!
To make this last example work you will need to add this
JTS-jdbc.jar to your classpath and you will have to modify your database-url to ‘jdbc:postgres_jts:’ instead of ‘jdbc:postgresql’. The source code of this jar can be found here and is still in beta! So use it at your own risk ![]()
Posted: March 4th, 2007 under General, Hibernate.
Comments: 1

Comment from mehran
Time: 13 October 2008, 11:55 am
hello..i m mehran from iran ..so impressed by your site…would you please introduce me the good site for open gis source - samples and training and also good source of gml - vrml eai - postgis