As you may know, gvpy is a gvsig library that help us with the execution of geoprocesses. Thus, geoprocesses added in our gvSIG Toolbox can be executed from a script with a single line of code.
For the new gvSIG 2.4, we introduced a small change is this geoprocess execution that will allow us to work with objects in a way that before it wasn’t possible. For example, some geoprocesses (like “profile”) have a graphical output, a object created from the library jfreechart. With this new functionality, is possible capture this object and process it.
An example: we have the task to create a big cuantity of profiles located around a road, as it appears in the next image.
We want to save all this profiles in a folder so we have to use the profile geoprocesses multiple times, one for each profile. This is a basic script just for one line.
# encoding: utf-8 import gvsig from gvsig.libs import gvpy reload(gvpy) from org.jfree.chart import ChartUtilities def main(*args): #Remove this lines and add here your code route = gvsig.currentView().getLayer("perfil_huesca") dem = gvsig.currentView().getLayer("MDTHuesca") r = gvpy.runalg( "profile",route,dem,"",False,"") aJFreeChart = r[0].getChart() from java.io import FileOutputStream out = FileOutputStream("C:/temp/imagen1.png") ChartUtilities.writeChartAsPNG(out, aJFreeChart, 500, #aChartPanel.getWidth(), 200)#aChartPanel.getHeight());
Now, we want to repeat this process for each profile (a different selected line feature in the initial layer) and saving the graphical output in a folder.
The script would be like this:
# encoding: utf-8 import gvsig from gvsig.libs import gvpy from org.jfree.chart import ChartUtilities from java.io import FileOutputStream import os def main(*args): route = gvsig.currentView().getLayer("Secciones transversales") dem = gvsig.currentView().getLayer("MDTHuesca") features = route.features() selection = route.getSelection() idfeature = 0 for i in features: idfeature+=1 selection.select(i) r = gvpy.runalg( "profile",route,dem,"",False,"",ADDLAYER=False) aJFreeChart = r[0].getChart() filename = gvsig.getTempFile("imagen_"+str(idfeature),".png", "C:\\temp") #filename = os.path.join("C:\\temp", "imagen_"+str(n)+".png") out = FileOutputStream(filename) ChartUtilities.writeChartAsPNG(out, aJFreeChart, 500, #aChartPanel.getWidth(), 200)#aChartPanel.getHeight()); out.close() selection.deselectAll() return
If you change the parameter ADDLAYER to True, for each process execution the new created layer will be added to the View.
Now, we can check in our folder the output of all the profile geoprocess executed during the script.
This script is just a small example and it could be improved. If you make any improvement don’t hesitate and contact with us so we could publish the new example in the blog for share it with the community.
Hope you find useful this post!
Pingback: Towards gvSIG 2.4: Functionalities improved in gvpy – GeoNe.ws
Pingback: gvSIG Desktop 2.4 is already available | gvSIG blog