Quick tip: add a function to the field calculator

If you are struggling on the field calculator of gvSIG because it lacks some chunk of code that would make your life easier, maybe you will find this interesting. On the field calculator you can evaluate external python scripts to add new functions to it. It’s easy, just type on a new text file something quite self-explanatory  like this (if you know the minimum basics of Python language):

def mysplit(myString, splitChar, position):
     try:
         val = myString.split(splitChar)[position - 1]
     except:
         val = -999
     return val

Then on the second tab of the calculator, locate your file and click on the "Evaluate" button. If no errors are reported then you can use your mysplit function on the field calculator in the same way you use the built-in ones. In my case I used this to parse a field with the typical "X,Y,Z" centroids that I couldn't parse with subString because they had different lengths so the Python string split was so convenient. This was as easy as just typing on the calculator to get the first coordinate:

toNumber(mySplit([LONGFIELD],",",1))

Easy, isn't it?

About Jorge

I'm a geogeek living near Valencia, Spain. I work on geomatics and I try to have fun with technology and free software.
This entry was posted in development, english, gvSIG Desktop and tagged , , . Bookmark the permalink.

One Response to Quick tip: add a function to the field calculator

  1. bitcoin says:

    Everything is very open with a very clear description of the challenges.
    It was definitely informative. Your website is very useful.
    Thanks for sharing!

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