Select to view content in your preferred language

Python script problem - ListFields / Calculate Field

1976
2
07-13-2010 05:26 AM
EmmanuelBlondel
Emerging Contributor
Hi everybody,

I have some problem with a python script, maybe could you have an idea to help me..
I put the python script as attached file.
I am working with species distribution. In my script:
"Input_Sp" is the species distribution shapefile
"Input_Table'' is the result of IDENTITY between the Input_Sp to a grid shapefile
"PK_REL" is the new field which I want to calculate

I will explain it trying to be clear because it is a little complex!! Also I want to add that I am starting with Python (since a few days) so It is probably that my script seems to be a draft script...

- The name of a species shapefile is for example "d1_alb.shp" and the probability field in the attribute table is [D1_ALB], so, the probability field name is changing according to the species (the 3 alpha code is the species identifier)

In the identity result (Input_Table) I have two area fields: [AREA_K] (area of each polygon) and [AREA_I] (area of each initial gridcell)
and I need to perform this operation: [D1_ALB] * [AREA_K] / [AREA_I]

but the problem is that from one species to another, the 3alpha species code will change so for example if I want to calculate reallocation for an other species (example: HER) the expression will be:
[D1_HER] * [AREA_K] / [AREA_I]

In order to automate this, I have started to write a little script in PYTHON language, that I could integrate in the model. I am starting in Python programming and it is not so easy.
In this script I am using a function (gp.Listfields) that allows me to search and find the three fields in my input_table. For searching the two area fields there is no problem, but for searching the probability field, it is the same problem: I cannot write [D1_ALB] in the script, because the name of this field is changing. I tried to used a "%" character like in SQL (D1_%) to search the field which name starts with "D1_" but it does not run.. there is an error...
so my first question is:
--> With gp.ListFields, Is there a solution to search a field without having the whole field name?


To get around this problem, I had a idea which is consisting in «extracting» the 3alphacode from shapefile name and using it then for searching the [D1_3alphacode] in the Input Table. I lead to do that, but I don't lead to integrate it into the calculation expression (I put "????here there is my doubt????" in my script to indicate to you where is my problem):
I can calculate AREA_K / AREA_I but, though I lead to find the probability field in Input_Table, I don't lead to apply it in the calculation expression...--> this my second problem

please find the attached script. If you have some idea to solve thise problem, it would be very very helpful for me.

Thanks a lot in advance for looking at this issue...
Emmanuel
0 Kudos
2 Replies
SimonWillis
Emerging Contributor
Take a step back. If you are just starting python, bite off smaller chunks first.
Also, have you considered using one shapefile, or better, a featureclass?
It sounds like you need to use a function to calc your values and let it be fed by a variable based on the shapefile you are using.
Are the characters you are looking for always in the same start/ end position in the shapefile name?
If they are, maybe something like this. It's not perfect but you get the idea:


fc= "testABC.shp"
var1= "D1_" + fc[4:7] #this gives you var1='D1_ABC'
calcfield (var1) #now feed var1 to your function:

# calculate PK_REL field in fc with newval
def calcfield (val):
    newval =  val * [AREA_K] / [AREA_I]
    gp.CalculateField_management(fc, "PK_REL" newval, "VB") 
    gp.AddMessage("Successfully calculated field"+ '\n')
    return
0 Kudos
EmmanuelBlondel
Emerging Contributor
Thanks for your help, I tried to base my script on your advice. Nevertheless the formula could be applied. The problem that I had is that the variable (var1 in your example) was giving the name of the field whose name is changing according to the shapefile... and this variable could no be integrated in the formula for calculating the field. I finally solved the problem creating an other variable:
var = "["+var1+"]"
It appears that "[]" were needed for calculation.
Nevertheless, I could not calculate the new field in once time (var *[AREA_K]/[AREA_I]), with a message error like "TypeError: can't multiply sequence by non-int of type 'str' "
I went around this problem calculating first the new field PK_REL = var
then I updated itself by [PK_REL]*[AREA_K]/[AREA_I]. It was successful

Thanks for your help

end of thecode (with fieldname derived from feature class name):

probfield = gp.ListFields(Input_Table, fieldname).Next()
gp.Addmessage(probfield.Name)
areak = gp.ListFields(Input_Table,"AREA_K").Next()
areai = gp.ListFields(Input_Table,"AREA_I").Next()

if probfield and areak and areai:
        sp = "["+ probfield.Name +"]"
        gp.AddMessage("The species probability field is "+ sp)
        gp.AddField_management(Output_PK_REL,PK_REL, "Double")

        # two steps for calculating the field:
        #1.give the sp value to PK_REL
        gp.CalculateField_management(Output_PK_REL, PK_REL,sp,"VB")
        #2.update the PK_REL by the final formula
        gp.CalculateField_management(Output_PK_REL, PK_REL,"[PK_REL]*[AREA_K]/[AREA_I]","VB")
        gp.AddMessage("Successfully calculated field"+ '\n')
0 Kudos