[Gvsig_english] Field Calculator: look for an expression saved in a file

Antonio Falciano afalciano at yahoo.it
Thu Sep 26 16:01:59 CEST 2013


Il 26/09/2013 14:10, phil_murcia ha scritto:
> I am trying to create a python expression to insert into the expression
> calculator that fills a column for me in the attribute table, but am
> struggling to find the correct syntax - or even successfully implement the
> simplest of python expressions. The table shows the compatibility of
> Land-use A with each instance of land use B, but I need to create a final
> column that can select the correct number from the correct column, depending
> on what is in the Land-use B column. An example of my attribute table is
> shown below.
>
> **Compatibility:**
>
>      ID  |  Land-use A |  Agr  |  Urb  | For  | Com |  Pub | Land-use B |
> Compat
>       1           N              1        4      3         0       2
> Agr
>       2           G              2        3      5         1       0
> Pub
>       3           K              4        2      3         5       1
> Urb
>       4           N              1        4      3         0       2
> Urb
>
> An example of my desired attribute table can be seen below:
>
> **Desired:**
>
>      ID  |  Land-use A |  Agr  |  Urb  | For  | Com |  Pub | Land-use B |
> Compat
>       1           N             1        4        3       0        2
> Agr              1
>       2           G             2        3        5       1        0
> Pub              0
>       3           K             4        2        3       5        1
> Urb              2
>       4           N             1        4        3       0        2
> Urb              4
>
> Perhaps it is possible to do this in the gvSIG expression calculator without
> import a .py file - but either way I can not seem to get the syntax right.
> Any help would be much appreciated.

Hi Phil,
you should define a Jython function in order to accomplish this task.
For instance:

def compat(agr,urb,for,com,pub,lub):
   if lub=="Agr":
     result=agr
   elif lub=="Urb":
     result=urb
   elif lub=="For":
     result=for
   elif lub=="Com":
     result=com
   elif lub=="Pub":
     result=pub
   else:
     result=-1
   return result

Save it in a file and process it as advanced expression in the field
calculator. Then select the header of Compat column and type the
following expression:

compat([Agr],[Urb],[For],[Com],[Pub])

It should work as you expect.

Cheers,
Antonio

-- 
Antonio Falciano
http://www.linkedin.com/in/antoniofalciano


More information about the Gvsig_internacional mailing list