2

I am trying to conduct a multicriteria evaluation, and it is necessary to check the compatibility of two different land-use types that I have in seperate layers. I am working in gvSIG 1.12, and have a unioned layer which contains polygons which are made up of land use A and B, included in the table is compatibility of Land-use A with each instance of B, but I need to create a final column that can select the correct number. 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

I am particularly struggling with getting the syntax correct for the Expression calculator. Should this be feasable using the preset commands in gvSIGs expression calculator or do I need to import the expression from a file (for instance a .py). In excel this is a simple if statement does the job but I am unable to execute this in gvSIG.

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
  • That's interesting, I'll forward your query to the mailing list. I'm not sure for the 1.12 field calculator, but that should be easy on the gvSIG 2.0 new scripting. – Jorge Sanz Sep 27 '13 at 8:32
  • Done, if anything useful is posted there we can place it here for future reference. – Jorge Sanz Sep 27 '13 at 8:51
  • I have found a solution to this problem, predominantly from help via the aforementioned mailing list. A Jython function is required, similar to this: def COMPAT(Agr,Urb,For,Com,Pub,LanduseB): if LanduseB=="Agr": result=Agr elif LanduseB=="Urb": result=Urb elif LanduseB=="For": result=For elif LanduseB=="Com": result=Com elif LanduseB=="Pub": result=Pub else: result=-1 return result This is loaded via the advanced tab in the expression calculator, and then in the general tab the following is entered: – user2547368 Sep 27 '13 at 12:12
  • COMPAT([Agr],[Urb],[For],[Com],[Pub],[LanduseB]) – user2547368 Sep 27 '13 at 12:13
  • IMHO the easiest way to accomplish this task is the simple expression in the Field Calculator. I answered to your question reporting both solutions I wrote in the gvSIG International mailing list. – Antonio Falciano Sep 29 '13 at 17:31
1

The problem can be solved in two alternative ways:

1. simple expression:

([Land-use B]=="Agr")*[Agr]+([Land-use B]=="Urb")*[Urb]+([Land-use B]=="For")*[For]+([Land-use B]=="Com")*[Com]+([Land-use B]=="Pub")*[Pub]

Note: result=0 if [Land-use B] is not equal to "Agr" or "Urb" or "For" or "Com" or "Pub".

2. advanced expression (via Jython scripting):

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

Note: result=0 in the 'else' case.

Save this Jython function in a script with .py extension (e.g. compat.py), select the header of Land-use B column in the attribute table, click on the Field Calculator, select the Advanced tab, load and process the script and finally type the following expression:

compat([Agr],[Urb],[For],[Com],[Pub],[Land-use B])
1

i try to do this with Scripting for gvSIG 2.0 Also i explain better this example in this post here

Some part is in english, but if you need more help i'll be glad to explain you better. The code should be like this, but i'm having one problem that gvSIG doesn't allow me to save the changes, that mean i can't close the edition mode in the table, i hope you don't have this problem, if anyone can help that would be great. This is why in the code appear the line #ERROR document.commit(), if you want to try just delete "#ERROR"

from gvsig import *

def main():
    #We work with our active document
    document = currentDocument()
    features = document.features()
    #Edit mode
    document.edit()

    #Features in our table
    for feature in features:
        feature.edit()
        #Column name
        var = feature.get("Land-use B")
        value = feature.get(var)
        #Here the column that we want modify
        feature.set("Compat",value)
        #Update the changes
        document.update(feature)

    #ERROR document.commit()
    print "END"

This script just will update the table when you run the script. It's different than having a field calculator but well, now you can choose whatever you prefer. Hope this is helpfull for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.