Last updated by Tim Pigden 3 years ago
MySQL GIS/Geometry with Grails
Setting up grails to use GIS geometry is pretty simple (I will do a plugin sometime). So while I wait for 1.1 beta2 to download I thought I'd quickly describe the bits you need and the steps.I assume you already know what GIS capabilities exist in MySql. If not consult the MySql website.The OGC Open Geopatial Consortium publish a standard Geometry model amongst a plethora of documents which describes geometry elements and coordinate transformations for storing and manipulating geospatial data.To access this from a Grails project you need a few things:- A recent version of MySql (4.1 or above) with appropriate driver
- The JTS Topology Suite - an open (LGPL) library of geometry primitives and operations see http://www.vividsolutions.com/jts/main.htm
- A set of hibernate mapping UserType classes from http://www.hibernatespatial.org
import com.vividsolutions.jts.geom.Polygon import org.hibernatespatial.GeometryUserTypepublic class MyPoly { String name Polygon poly static mapping = { poly type: GeometryUserType }}
import com.vividsolutions.jts.geom.* import org.hibernate.type.* import org.hibernate.Query import org.hibernatespatial.GeometryUserType class GeometryService { def sessionFactory boolean transactional = false // performs query which takes polygon as single position 0 argument def doWithPolygon(String queryString, Polygon polygon) { def session = sessionFactory.currentSession Query query = session.createQuery(queryString) Type geometryType = new CustomType(GeometryUserType.class, null) query.setParameter(0, polygon, geometryType) return query.list() } }
def pointsWithin = geometryService.doWithPolygon("from MyPoint mp where within(mp.point, ?) = true", myPoly.poly)public class MyPoint { String name Point point static mapping = { point type: GeometryUserType } String toString() { return "$name (${point.x}, ${point.y}) " } }



