Wednesday, October 16, 2013

Gettting Started with OpenShift Spatial (Part 3)

With a spatial database available, let's try using grails to connect to it. To do that we'll create a new grails app and add the libraries we need to BuildConfig.groovy. First add the repositories that contain the libraries.
repositories {
  inherits true // Whether to inherit repository definitions from plugins

  grailsPlugins()
  grailsHome()
  grailsCentral()

  mavenLocal()
  mavenCentral()
 
  // Hibernate Spatial repo
  mavenRepo "http://www.hibernatespatial.org/repository"
}
Because the postgis libs are hosted in external maven repo we'll load these into our local mavenRepo.
Dowload the two libraries to your local tmp dir and use the install-file command to load into maven. I found a binary of of postgres-jdbc here: http://jdbc.postgresql.org/download/postgresql-9.2-1003.jdbc4.jar
and the postgis-jdbc here: http://postgis.refractions.net/download/postgis-jdbc-2.1.0SVN.jar.
mvn install:install-file -Dfile=/tmp/postgresql-9.2-1003.jdbc4.jar /
  -DgroupId=org.postgres -DartifactId=postgresql -Dversion=9.2-1003 /
  -Dpackaging=jar

mvn install:install-file -Dfile=/tmp/postgis-jdbc-2.1.0SVN.jar -DgroupId=org.postgis /
  -DartifactId=postgis-jdbc -Dversion=2.1 -Dpackaging=jar
Now we can add the dependencies we need.
dependencies {
  // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
  runtime "org.postgres:postgresql:9.2-1003" //local repo addition

  // Hibernate Spatial dependencies
  runtime "org.postgis:postgis-jdbc:2.1" //local repo addition 
  runtime "org.hibernatespatial:hibernate-spatial-postgis:1.1.1"
  compile "com.vividsolutions:jts:1.13"
 
}
Next we update our DataSource.groovy.
dataSource {
  pooled = true
  dialect = org.hibernatespatial.postgis.PostgisDialect
  driverClassName = "org.postgresql.Driver" 
}

production {
  dataSource {
    dbCreate = "update"
    url = "jdbc:postgresql://${dbHost}:${dbPort}/spatialapp";
    username = "myuser";
    password = "******";
  }
}
Now we are ready to code up a domain object, see next post!

No comments: