Automate reporting in gvSIG using Scripting

Reporting can be a tedious and repetive task, which can be automated. After some questions came up related to this topic in the user list and due to the possible utility for the community, we have developed an script for showing the way to do this automation.

In this case we will use the jOpenDocument library, which will allow us to edit text files (odt) and spreadsheets (ods) of LibreOffice from our code.

We will use a template and a script ready to show what would be the final goal of this automation. In this example we have a script that shows information of the plots we have selected in an already selected layer. To do this, the script access to the information from various fields of the entity, and takes two images from our View, one of the full layer and the other one of the selected entity. The scale to be applied to the centered image over the entity, it will be the scale at that time on the View.

The outcome we look for is:

2015-09-28 12_05_27-Informess

For testing, I have prepared a .zip file with all you need. You can extract the file to C:\gvsig_informes\. The script is made for this path, if it´s another one, you should modify it at the beggining of the script.

First thing to do is to install the jOpenDocument library. On their web you can find documentation about the exampless and its API. For this example, I have used jOpenDocument 1.3 which is the latest stable version. We can download or copy from the already downloaded zip file . It is a .jar file (the one from the zip is jOpenDocument-1.3.jar), which will be copy to the folder \gvSIG\extensiones\org.gvsig.scripting.app.extension\lib of the gvSIG installation folder. Normally will be:

  • Win: C:\Program Files (x86)\gvSIG desktop 2.2.0\gvSIG\extensions\org.gvsig.scripting.app.extension\lib
  • Linux: \home\[user]\gvsig-desktop\gvSIG_2.2.0_final\gvSIG\extensions\org.gvsig.scripting.app.extension\lib

For the example, in gvSIG, we must have open in one View the layer manzanas_valencia.shp attached in the zip, and, have this layer selected in the Table of Contents(ToC) of the View (should be in bold). Also, we have to select the features to be used to generate the report.

From here, we should load our script in the Scripting Module, modify the four paths (if necessary) and run it:


from gvsig import *
import sys
from geom import *
from java.io import File
from org.jopendocument.dom.template import JavaScriptFileTemplate
from org.jdom import Namespace
from com.sun.jimi.core import Jimi
from org.gvsig.app import ApplicationLocator
import time

def main(*args):
    #Input
    pathTemplate = "C:\\gvsig_informes\\plantilla_informe_test2.odt"

    #Output
    # Use this format with an only bar
    pathOutputFile = r"C:/gvsig_informes/resultado/test_parcela%02d"
    pathEnvelope = r"C:/gvsig_informes/resultado/envelope.png"
    pathImageOut = r"C:/gvsig_informes/resultado/img%02d.png"
    
    #Start
    print "Informe parcelario"
    application = ApplicationLocator.getManager()
    layer = currentLayer()
    docvista = currentView()
    docwin = application.getDocumentWindow(docvista())
    
    #Get scale
    gsv = currentView().getMapContext().getScaleView()
    
    #Envelope global
    envelope = currentView().getMap().getLayers().getMapContext().getFullEnvelope()
    currentView().getMapContext().getViewPort().setEnvelope(envelope)
    time.sleep(2)
    img = docwin.getMapControl().getImage()
    Jimi.putImage(img,pathEnvelope)



            
    #Create images and odt
    n=0
    for f in layer.getSelection():
        print f.MASA, f.geometry()
        #Center view and set same scale
        geomf = f.geometry()
        centerView(geomf)
        currentView().getMapContext().setScaleView(gsv)
        time.sleep(2)
        #Imagen
        img = docwin.getMapControl().getImage()
        pathImage = pathImageOut % n
        Jimi.putImage(img,pathImage)

        # Create odt
        pathOutFile = pathOutputFile % n
        templateFile = pathTemplate #File(pathTemplate)
        outFile = File(pathOutFile)
        bcFile = pathImage
        template = JavaScriptFileTemplate(templateFile)

        values = f.getValues()
        template.setField("hoja", str(values['HOJA']))
        template.setField("area", str(values['AREA']))
        template.setField("coorx", f.COORX)
        template.setField("coory", f.COORY)
        ddoc = template.createDocument()
        ddoc.getDescendantByName("draw:frame","Imagen2").setAttribute("href", "file:///" + bcFile,Namespace.getNamespace("xlink", "http://www.w3.org/1999/xlink"))
        ddoc.getDescendantByName("draw:frame","Imagen1").setAttribute("href", "file:///" + pathEnvelope,Namespace.getNamespace("xlink", "http://www.w3.org/1999/xlink"))
        ddoc.saveAs(outFile)
        #template.saveAs(outFile)
        n += 1
        
def centerView(geomi):
        envelope = geomi.getEnvelope()
        currentView().getMapContext().getViewPort().setEnvelope(envelope)
        time.sleep(2)

Summary of the steps to follow:

  • Extract the zip file to C:/gvsig_informes (if is another one, modify in the script).
  • Copy the jOpenDocument-1.3.jar file to the folder \gvSIG\extensiones\org.gvsig.scripting.app.extension\lib of the gvSIG installation folder.
  • Load capa manzanas_valencia.shp layer in gvSIG
  • With the layer active (bold) in the TOC of the View
  • Select some features of the layer
  • Run the script

However, this script has still a couple of limitations:

  • The images of the template can only be replaced, that is, the image we take in gvSIG has the shape of the View frame, if it has different size will be deformed in the template. It has a solution if you edit the template with images that have been adapted to the appropriate size, or with programmation, cuts could be made in the image created.
  • The images are displayed as links to image files, therefore the image is not actually within the template, but is a link to it.
  • If the template contains special characters such as accents I get error when creating the document. Not sure why happens this error, maybe is a problem in coding or template or library.

If you like the post and have interest on it,we will post new ones, explaining the new steps taken in this for you to understand how to modify it, and examples of other types such as editing or calculation sheets ods or to export to PDF.

The jOpenDocument library opens up may possibilities. Any help or contribution will be welcome.

Hope you find it interesting.

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

2 Responses to Automate reporting in gvSIG using Scripting

  1. Pingback: Automate reporting in gvSIG using Scripting | GeoNe.ws

  2. Pingback: Editing Spreadsheets from gvSIG Scripting for reporting | gvSIG blog

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