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:
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:
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.