using geoscript groovy

32
Using GeoScript Groovy Jared Erickson CUGOS February 2014

Upload: jared-erickson

Post on 10-May-2015

687 views

Category:

Technology


0 download

DESCRIPTION

Using GeoScript Groovy

TRANSCRIPT

Page 1: Using Geoscript Groovy

Using GeoScript Groovy

Jared Erickson CUGOS February 2014

Page 2: Using Geoscript Groovy

What is GeoScript?

• Geospatial Swiss Army Knife

• One scripting API Many Languages

Page 3: Using Geoscript Groovy

What is Groovy?

• Yes, there is a language called Groovy

• Dynamic JVM Language

• Strong Java Integration

• Closures, MetaProgramming, Scripting

Page 4: Using Geoscript Groovy

Uses• Scripts

• Shell

• Console

• GeoServer

• uDig

• Apps (GeoScript as a Library)

Page 5: Using Geoscript Groovy

How do I install it?• Install Java

• Install Groovy

• Download GeoScript Groovy and put the bin directory on your path

• https://github.com/jericks/geoscript-groovy/releases

Page 6: Using Geoscript Groovy

How do I learn it?

http://geoscript.org

http://geoscript.org/groovy/api/

Page 7: Using Geoscript Groovy

Command line toolsgeoscript-groovy = run scripts

geoscript-groovysh = interactive shell

geoscript-groovyConsole = mini ide

!

Page 8: Using Geoscript Groovy

Inline Scripts (geoscript-groovy)

geoscript-groovy -e "println new geoscript.layer.Shapefile('states.shp').toJSONString()" | mapfart !

geoscript-groovy -e "println new geoscript.layer.Shapefile('states.shp').bounds.geometry" !

geoscript-groovy -e "println new geoscript.layer.Shapefile('states.shp').count"

Page 9: Using Geoscript Groovy

Inline Scripts (geoscript-groovy)

geoscript-groovy -e "geoscript.render.Draw.draw(new geoscript.layer.Shapefile('states.shp'), out: 'image.png')" !

geoscript-groovy -e "new geoscript.layer.Shapefile('states.shp').eachFeature{ println it.geom.centroid}" | geom combine | geom buffer -d 1.5 !

echo "POINT (1 1)" | geom buffer -d 10 | geoscript-groovy -e "println geoscript.geom.Geometry.fromWKT(System.in.text).kml"

Page 10: Using Geoscript Groovy

Inline Scripts (geoscript-groovy)

• One liners

• Interact with other command line tools

• geom, mapfart, ogr, ect…

• Read Standard Input (System.in.text)

• Write to Standard Output (println)

Page 11: Using Geoscript Groovy

Scripts (geoscript-groovy)

1 import com.sun.grizzly.http.embed.GrizzlyWebServer! 2 import com.sun.grizzly.http.servlet.ServletAdapter! 3 ! 4 @Grab(group='com.sun.grizzly', module='grizzly-servlet-webserver', version='1.9.10')! 5 def start() {! 6 println("Starting web server...")! 7 def server = new GrizzlyWebServer(8080, "web")! 8 def servlet = new ServletAdapter()! 9 servlet.contextPath = "/geoscript"!10 servlet.servletInstance = new groovy.servlet.GroovyServlet()!11 server.addGrizzlyAdapter(servlet, ["/geoscript"] as String[])!12 server.start()!13 }!14 start()

web.groovy

Page 12: Using Geoscript Groovy

Scripts (geoscript-groovy)

1 import geoscript.geom.*!2 !3 def geom = Geometry.fromWKT(request.getParameter("geom"))!4 def distance = request.getParameter("d") as double!5 println(geom.buffer(distance).wkt)

web/buffer.groovy

1 import geoscript.geom.*!2 import geoscript.viewer.Viewer!3 !4 def geom = Geometry.fromWKT(request.getParameter("geom"))!5 def image = Viewer.drawToImage(geom, size: [400, 400])!6 response.contentType = "image/png"!7 javax.imageio.ImageIO.write(image, "png", response.outputStream)

web/draw.groovy

Page 13: Using Geoscript Groovy

Scripts (groovy)

1 @GrabResolver(name="opengeo", root="http://repo.opengeo.org")!2 @Grab("org.geoscript:geoscript-groovy:1.1")!3 import geoscript.geom.Point!4 !5 p = new Point(1,1)!6 println p.geoJSON!7

• Use GeoScript without installing GeoScript

• Just need Java and Groovy

• @GrabResolver and @Grab can download dependencies

Page 14: Using Geoscript Groovy

Scripts (geoscript-groovy)

• Larger multiline scripts

• Good for complex processing

• Record your workflow

• Can create command line tools, web services, or desktop apps

• Use @Grab to get dependencies

Page 15: Using Geoscript Groovy

Shell (geoscript-groovysh)

Groovy Shell (2.1.9, JVM: 1.7.0_45) Type 'help' or '\h' for help. ------------------------------------------------------------------------------- groovy:000> import geoscript.workspace.PostGIS ===> [import geoscript.workspace.PostGIS] groovy:000> postgis = new PostGIS("states",user: "jericks") ===> geoscript.workspace.PostGIS@7840df80 groovy:000> postgis.layers ===> [states] groovy:000> states = postgis.get("states") ===> states groovy:000> states.bounds ===> (-124.731422,24.955967,-66.969849,49.371735,EPSG:4326) groovy:000> states.count ===> 49 groovy:000> states.eachFeature{ println it.geom.centroid } POINT (-89.20368628698026 40.06397152717181) POINT (-77.01592888814594 38.90248929357207) POINT (-75.50090936853277 38.994999876971384) POINT (-80.61424804312078 38.64111336139042)

Page 16: Using Geoscript Groovy

groovy:000> states.schema.fields ===> [the_geom: MultiPolygon(EPSG:4326), STATE_NAME: String, STATE_FIPS: String, SUB_REGION: String, STATE_ABBR: String, LAND_KM: Double, WATER_KM: Double, PERSONS: Double, FAMILIES: Double, HOUSHOLD: Double, MALE: Double, FEMALE: Double, WORKERS: Double, DRVALONE: Double, CARPOOL: Double, PUBTRANS: Double, EMPLOYED: Double, UNEMPLOY: Double, SERVICE: Double, MANUAL: Double, P_MALE: Double, P_FEMALE: Double, SAMP_POP: Double] groovy:000> state_centroids = states.transform("state_centroids", [the_geom: "centroid(the_geom)", name: "STATE_NAME", abbreviation: "STATE_ABBR"]) ===> state_centroids groovy:000> state_centroids.count ===> 49 groovy:000> postgis ===> geoscript.workspace.PostGIS@7840df80 groovy:000> postgis.add(state_centroids) ===> state_centroids groovy:000> postgis.names ===> [state_centroids, states] groovy:000> postgis["state_centroids"].schema ===> state_centroids the_geom: Point(EPSG:4326), name: String, abbreviation: String

Shell (geoscript-groovysh)

Page 17: Using Geoscript Groovy

Shell (geoscript-groovysh)

• Good for trying things out

• Interact and explore all things geospatial

Page 18: Using Geoscript Groovy

Console (geoscript-groovyConsole)

Page 19: Using Geoscript Groovy

Console (geoscript-groovyConsole)

Page 20: Using Geoscript Groovy

Console geoscript-groovyConsole

Page 21: Using Geoscript Groovy

Console

• Good for longer scripts (but not too long)

• Excellent preview (View -> Visualize Script Results)

• Use it when rendering images, maps, geometries

Page 22: Using Geoscript Groovy

uDig

Page 23: Using Geoscript Groovy

uDig

• Done by Andrea Antonello and the UDig crew

• Part of the Spatial Toolbox

• http://udig.github.io/docs/user/getting_started/GeoScript%20Introduction.html

• http://prezi.com/wyopic4sinhg/geographic-scripting-in-udig-user-friendly-desktop-internet-gis/

• http://www.slideshare.net/moovida/04-geographic-scripting-in-udig-halfway-between-user-and-developer

Page 24: Using Geoscript Groovy

GeoServer• Web services

• Web Processing Services (WPS)

• Filter Functions (used in SLDs for styling your maps)

• Download groovy plugin from http://ares.opengeo.org/geoserver/2.4.x/community-latest/

• Extract jars to geoserver/WEB-INF/lib

• http://www.slideshare.net/JaredErickson/scripting-geoserver

Page 25: Using Geoscript Groovy

GeoServer

1 import geoscript.geom.Geometry! 2 ! 3 title = 'Buffer'! 4 description = 'Buffers a geometry'! 5 ! 6 inputs = [! 7 geom: [name: 'geom', title: 'The geometry to buffer', type: Geometry.class],! 8 distance: [name: 'distance', title: 'The buffer distance', type: Double.class]! 9 ]!10 !11 outputs = [!12 result: [name: 'result', title: 'The buffered geometry', type: Geometry.class]!13 ]!14 !15 def run(input) {!16 [result: input.geom.buffer(input.distance as double)]!17 }!18

$DATA_DIR/scripts/wps/buffer.groovy

Page 26: Using Geoscript Groovy

GeoServer

Page 27: Using Geoscript Groovy

As a libraryGeoscript is just another jar

<dependency>! <groupId>org.geoscript</groupId>! <artifactId>geoscript-groovy</artifactId>! <version>1.2</version>!</dependency>

<repositories>! <repository>! <id>opengeo</id>! <name>OpenGeo Maven Repository</name>! <url>http://repo.opengeo.org</url>! <snapshots>! <enabled>true</enabled>! </snapshots>! </repository>!</repositories>

Page 28: Using Geoscript Groovy

Gradle - App$ mkdir geo-gradle $ cd geo-gradle/ $ touch build.gradle $ vi build.gradle $ mkdir -p src/main/groovy/org/jce/geo $ touch src/main/groovy/org/jce/geo/App.groovy $ vi src/main/groovy/org/jce/geo/App.groovy $ gradle build

Page 29: Using Geoscript Groovy

Gradle - build.gradle 1 apply plugin: "groovy"! 2 apply plugin: "application"! 3 ! 4 version = 0.1! 5 mainClassName = "org.jce.geo.App"! 6 ! 7 repositories {! 8 maven {! 9 url "http://repo.opengeo.org"!10 }!11 maven {!12 url "http://download.osgeo.org/webdav/geotools/"!13 }!14 mavenCentral()!15 }!16 !17 dependencies {!18 compile "org.codehaus.groovy:groovy-all:2.1.9"!19 compile "org.geoscript:geoscript-groovy:1.2"!20 }

Page 30: Using Geoscript Groovy

Gradle - Code

1 package org.jce.geo! 2 ! 3 import geoscript.geom.Geometry! 4 import geoscript.proj.Projection! 5 ! 6 class App {! 7 static void main(String[] args) {! 8 Geometry geom = Geometry.fromString(args[0])! 9 Projection fromProj = new Projection(args[1])!10 Projection toProj = new Projection(args[2])!11 Geometry transformedGeom = fromProj.transform(geom, toProj)!12 println transformedGeom.wkt!13 }!14 }

src/main/groovy/org/jce/geo/App.groovy

Page 31: Using Geoscript Groovy

Using GeoScript Groovy

• Scripts

• Shell

• Console

• GeoServer

• uDig

• Apps

• Geometry

• Projection

• Layer

• Raster

• Style

• Render

Functionality Usages

Page 32: Using Geoscript Groovy

Thank you!