Scripting with Groovy

Thanks to its official “Groovy Console” plugin, OrbisGIS is able to execute instructions written in Groovy programming language.

To see how to install this plugin, you are invited to consult this page or just check for “Groovy console” in the Plugins manager.

Groovy console

Here, the user is able to:

  • Write & execute ( Execute SQL instruction icon ) instructions,
  • Execute only selected instructions ( Execute Selected SQL instruction icon :width: 16 pt ),
  • Load & Save .groovy files,
  • Search & Replace words (with advanced options) ( Search icon ),

Use cases

Include SQL into Groovy script

Below is an example of how to use SQL instructions into Groovy script. Here we just want to retrieve the coordinates of a point, which is stored in a layer called “TEST”.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 import groovy.sql.Sql;

 // Execute SQL request
 sql.execute("DROP TABLE IF EXISTS TEST")
 sql.execute("CREATE TABLE TEST AS SELECT 'POINT(1 3 10)'::geometry THE_GEOM")

 // Fetch results and print point coordinates
 // row["THE_GEOM"] is an instance of
 // http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/geom/Point.html
 sql.eachRow("SELECT * FROM TEST") { row ->
     pt = row["THE_GEOM"].getCoordinate()
     print(pt.x+" "+pt.y+" "+pt.z)
 }

The result is displayed in the Output Console:

1.0 3.0 10.0

To see more about Groovy, you can consult this tutorial from the official Groovy documentation.