Friday, May 15, 2015

Wrote script in groovy that converts XYZ space delimited data into CSB GeoJSON standard.

https://github.com/geoneubie/groovyScripts/blob/master/csb/xyzToGeoJSON.groovy

Wednesday, October 16, 2013

Getting Started with OpenShift Spatial (part 4)

Our last steps involve coding up a domain and controller class and then deploying the resulting war to OpenShift.
grails create-domain-class GeoEvent
import com.vividsolutions.jts.geom.Geometry
import com.vividsolutions.jts.geom.GeometryFactory
import com.vividsolutions.jts.geom.Coordinate

class GeoEvent {

  String name
  String status 
  String type
  double lat
  double lon

  Geometry shape
  
  static constraints = {
    shape nullable: true
  }

  def beforeInsert( ) {
    if ( lat!=null && lon!=null ) {
      shape = ( new GeometryFactory( ) ).createPoint( new Coordinate( lon.value, lat.value ) )
    }
  }

}
We'll use a simple scaffold based controller...
class GeoEventController {

    static scaffold = true

}
Finally compile test and deploy to OpenShift.
grails>compile
grails>run-app
grails>prod war
In our OpenShift managed repo copy the war generated above into webapps/ROOT.war
git commit -a -m 'latest update!'
git push

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!

Wednesday, October 2, 2013

Getting Started with OpenShift Spatial (Part 2)

Now let's remotely login into OpenShift using the ssh host name revealed by show-domain, and then enable postgis extensions.
rhc show-domain
ssh host
createlang -dspatialapp plpgsql
psql -dspatialapp -f /usr/share/pgsql/contrib/postgis-64.sql
psql -dspatialapp -f /usr/share/pgsql/contrib/spatial_ref_sys.sql
psql -dspatialapp
We should now be at the psql prompt. Let's create a new spatial table to store point events.
spatialapp=#
CREATE TABLE eventpoints
(
 gid serial NOT NULL,
 name text, -- name of the point
 type text, -- event type
 the_geom geometry,
 CONSTRAINT eventpoints_pk PRIMARY KEY (gid ),
 CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
 CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
 CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
)
WITH (
 OIDS=FALSE
);

CREATE INDEX eventpoints_spatial_idx ON eventpoints USING gist ( the_geom );
Finally let's insert our first record and then view it to confirm the entry.
Insert into eventpoints (name, type, the_geom) VALUES ('First point!', 'FIRE', ST_GeomFromText('POINT(-85.7302 37.5332)', 4326));
select * from eventpoints; 
gid |    name     | type |                      the_geom                      

-----+-------------+------+----------------------------------------------------

  1 | First point! | FIRE | 0101000020E610000082E2C798BB6E55C0151DC9E53FC44240
(1 row)

Getting Started with OpenShift Spatial (Part 1)

Getting started with OpenShift. I started on Windows 8 Professional 64 bit laptop, while there are instructions available here and there, I eventually installed a Ubuntu VM desktop and found it much easier to work with OpenShift this way. These steps assume linux OS.

First create an account here.
Next, install the RedHat Client rhc:
https://www.openshift.com/developers/rhc-client-tools-install
Run through setup and ssh configuration prompts.
Once the RedHat Client Tools are available, we are ready to begin our first spatial application!
We will begin by creating a webapp that will run under tomcat7 and connect to postgis enabled database.

1) rhc app create spatialapp Tomcat7 postgresql-8.4

Now let's check out what was created for us.

2) rhc show-domain

I should see a git URL, ssh host, postgres database with the same name as my webapp.

Groovy/Grails and Oracle Spatial (Part 2)

In part one we discussed building out a service class to perform our distance search. Now in part two, we'll build out the rest of the grails framework namely a GSP page to capture user input, a controller to process the form, and a GSP page to render the results as an html table.

Let's start with a controller to test that we can pass user input into our service class SearchByDistance.groovy. First, we'll want to inject the Oracle dataSource into our controller. To do this we need to edit our DataSource.groovy file in the conf folder.

DataSource.groovy:
dataSource {
pooled = true
driverClassName = "oracle.jdbc.OracleDriver"
dialect = "org.hibernate.dialect.Oracle10gDialect"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
url = "jdbc:oracle:thin:@myserver:1521:MYINSTANCE"
schema = "MYSCHEMA"
username = "myuser"
password = "mypassword"
}
}
// other environments to follow...
}


Now we can inject this into the controller by simply defining a dataSource. In the controller we also define a list action. The list action grabs user submitted params which we'll print out for debugging purposes, followed by submitting those parameters to our SearchByDistance service. Lastly we'll return a list of our domain object results by default to the list.gsp page.

package osmcreporter

class DailySummaryMappingController {

static allowedMethods = []

def dataSource // the groovy "dataSource" is auto-injected

def index = {
redirect(action: "list", params: params)
}

def list = {
println "params: " + params.sensor + "," + params.transectid + "," + params.distance
def dsrs = new SearchByDistanceService()
dsrs.setDbHandle(dataSource)
def observations = dsrs.serviceMethod(params.sensor, params.transectid, params.distance)
[ observations: observations] // return the results as model
}

}