Hibernate Spatial PostgreSQL/PostGIS Grails Plugin

  • Authors : Daniel Henrique Alves Lima
0 vote
Dependency :
compile ":hibernate-spatial-postgresql:0.0.4"

Documentation Source

Summary

Installation

grails install-plugin hibernate-spatial-postgresql

Description

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 Install

or

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 test

import com.vividsolutions.jts.geom.Point

class 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.