gvSIG 2.0: Scripting, exploit your gvSIG

One of the novelties of the 2.0 version of gvSIG is the inclusion of a small environment for development and execution of scripts that interact with the application. These scripts allow us to automate small tasks or add some functionality that we need and that we can implement.

The language chosen by the gvSIG project to give support to scripting is Jython , an implementation of Python language that is ‘run’ in the Java Virtual Machine (JVM), allowing it to be fully integrated with gvSIG.

The default gvSIG installation has no base plugin to support the scripting, so the first thing we have to do is install it, you can consult how to do so in the gvSIG add-ons manager documentation.

The scripting plugin provides two tools, an editor and organizer of our scripts, Scripting Composer, and one that launches them, Scripting Launcher. These two tools are available in the Tools -> Scripting menu, and in two buttons on gvSIG the toolbar.

images/scripting-composer.png

As can be seen in the image the Composer is divided into 3 main zones, a file browser where we can see the scripts that are available, an edition area, and a notification area where we can see the incidents that occur during the execution of our scripts.

One of the advantages that the scripting extensions offers that is integrated in gvSIG is that it allows us to access the documents that we have loaded into the application and interact with them, which together with the re-factoring of the 2.0 gvSIG APIS and the libraries that have been created we can create scripts easily. We can see this through an example.

Suppose we have a loaded layer whose data structure has a field called ELEVATION and we want to obtain the maximum and minimum values ​​of this field. To do this we must go over all of the features of the layer and check the values ​​of this field, store the maximum and minimum value and display in a window.

from gvsig import *
from commonsdialog import *

def main(): 
  layer = currentLayer()
  if layer == None:
    msgbox("The layer should be charged and
              selected.", "NOTIFICATION", 1)
    return

  emax = 0.0
  emin = 0.0

  for feature in layer.features():
    if feature.ELEVATION > emax :
      emax = feature.ELEVATION
    if feature.ELEVATION < emin or emin ==0.0:
      emin = feature.ELEVATION
  msgbox("máximum Elevation=%s and minimum=%s" % (emax, emin),
         "Elevation", 0)

The result of running the script is can be seen in the below image:

images/script-ejecuted.png

We hope you enjoyed it and that it will inspire you to start coding your own scripts in Python and if so, and if you become somehow pythonicos maybe you would like to read the zen of python or better yet read your own script running in gvSIG. Will you tell us how?

This entry was posted in development, english, gvSIG Desktop, scripting. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s