Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
img
src/main
README.md
pom.xml

README.md

Groovy Console plugin

Introduction

The "Groovy console" makes Groovy language available to use in OrbisGIS. Groovy is a dynamic object-oriented programming language for the Java virtual machine (JVM). The language can be used to interact with OrbisGIS, create functions or write new applications.

Install

To install the "Groovy console", please use at least the OrbisGIS 5.1.X version.

Then, go to the menu "Tools/Manage plugins" and search "Groovy console" and press "Download & Start".

Go to the menu "Windows" and check the "Groovy console".

Groovy console installed

Implementation

The "Groovy console" implements the GroovyShell class that represents a shell capable of running arbitrary groovy scripts (see http://groovy-lang.org/api.html). To facilitate the use of the "Groovy console", two variables and one property are exposed to the shell.

Variables

Name : mc

Description : Refer to the current MapContext object. It allows the user to manage layers and styles.

Name : pm

Description : Allow the access to the RootProgressMonitor in OrbisGIS. User can canceled property and listeners.

Property

Name : sql

Description : Instance of the Groovy Sql object populated with the current OrbisGIS database connection. User are able to execute sql queries.

In actions

How to query a table available in OrbisGIS ?

Groovy console installed

Note : The print trace is piped to the OrbisGIS output window.

How to create a buffer on geometry and print the result?

sql.eachRow("select the_geom from BATI_INDIFFERENCIE LIMIT 1;"){ row ->
print row.the_geom.buffer(10)
}

How to print a result in a file?

def file_dump= new File('/tmp/dump_buffer.txt')
sql.eachRow("select the_geom from BATI_INDIFFERENCIE LIMIT 1;"){ row ->
file_dump << row.the_geom.buffer(10)
}

How to get the name of the first layer available in the TOC ?

print mc.getLayers()[0].getName()

How to create a layer based on a SQL query ?

import org.orbisgis.coremap.layerModel.ILayer;
sql.execute "Create table test as select * from BATI_INDIFFERENCIE limit 2"
ILayer layer = mc.createLayer("test");
mc.getLayerModel().addLayer(layer);

How to set a custom style to layer?

import org.orbisgis.coremap.layerModel.ILayer;
import org.orbisgis.coremap.renderer.se.*;

ILayer layer = mc.getLayers()[0]
layer.setStyle(0,new Style(layer, "/tmp/mypolygonstyle.se"));
mc.getLayerModel().addLayer(layer);

Acknowledgements

We would like to thank the Groovy community who provides this incredible scripting language.

You can’t perform that action at this time.