Last updated by daniel_henrique 7 months ago
grails install-plugin hibernate-spatial-postgresql
Last updated by daniel_henrique 7 months ago
Hibernate Spatial Provider for PostgreSQL/PostGIS.
It's a child plugin of
hibernate-spatial.
Usage example
Install the plugin
grails install-plugin hibernate-spatial-postgresql
Create a spatially enabled database
Steps 7, 8, 9 and 10 of
PostGIS Compile from Source and Installor
Creating PostGIS spatially-enabled databases from an in-built template
Configure the corresponding JDBC Driver dependency in BuildConfig.groovy
dependencies {
runtime 'postgresql:postgresql:8.4-702.jdbc4'
}Change the JDBC URL in DataSource(s).groovy
url = 'jdbc:postgresql://localhost/test_db'
In your domain classes, define properties of the type com.vividsolutions.jts.geom.Geometry (or subclasses)
// Place.groovy
package testimport com.vividsolutions.jts.geom.Pointclass Place { String description
Point location}Perform some tests using save, dynamic finders, HQL and Criteria
//import com.vividsolutions.jts.geom.GeometryFactory
//import com.vividsolutions.jts.geom.Coordinate
//import org.hibernatespatial.criterion.SpatialRestrictions
//import com.vividsolutions.jts.io.WKTReader Place.withNewTransaction {
Place p = new Place()
p.description = 'Desc'
p.location = geometryFactory.createPoint(new Coordinate(5d, 5d))
p.save(failOnError: true, flush: true)
} Place.withNewTransaction {
println(Place.createCriteria()
.add(SpatialRestrictions.within("location", reader.read('POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))')))
.list()) def filter = reader.read('POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))')
Query q = Place.createQuery("from Place where within(location, ?) = true")
q.setParameter(0, filter, GeometryUserType.TYPE)
println (q.list())
}
DomainClass.withNewTransaction is available through transaction-handling.
Last updated by admin 7 months ago
Last updated by admin 7 months ago