Select to view content in your preferred language

Field Calculator Expression Arcpy

5635
11
01-27-2016 03:25 PM
HOWARDWILLIAMS
Deactivated User

So I am writing a script to perform a Union on multiple feature data sets. 

Feature data sets:  R1ADS1999Damage, R1ADS2000Damage.....R1ADS2013Damage  (these are all polygon feature classes with the same attribute fields years 1999-2013)

I need to join the first (1999) year to the next year, naming the joined feature class R1ADS1999-2000Damage, and then add three new fields called: TPA_1999_2000; Acres_1999_2000 and; NoTrees_1999_2000.  Each new field is calculated with a unique (field calculator) expression.  I will provide these calculations in a moment.  Once the fields are added and the values calculated the newly created unioned feature class, R1ADS1999-2000Damage, needs to be unioned to R1ADS2001Damage, the next chronological year. Fields called  TPA_1999_2001; Acres_1999_2001 and; NoTrees_1999_2001  need to be added and calculated.  This pattern continues until the final year 2013.

TPA_1999_2000 = Is the sum of field TPA_1999 + TPA_2000, after the next union the TPA_1999_2001= TPA_1999_2000+TPA_2001 this pattern continues for after all unions.

Acres_1999_2000= Is a recalculation of geometry after the union, in Acres. This is the same for all subsequent unions.

No_Trees_1999_2000= Is TPA_1999_2000 * Acres_1999_2000. After the next union the NoTrees_1999_2001= TPA_1999_2001*Acres1999_2001

I have managed to get the union and adding fields part of the script done, see below.  I got stuck on writing the Field Calculator expression for the TPA_1999_20XX. 

It seems that I need an expression that sums all of the fields that begin with TPA_  something like

TPA_1999_2000= sum([!TPA_*!)   However this does not work. 

So here is the code I have come up with so far until got stuck:

##################################################

R1="R1ADS"
Dam="Damage"
years=['1999','2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013']

fields=['TPA_','Acres_','NoTrees_']

RangeYear= ['1999_2000','1999_2001','1999_2002','1999_2003','1999_2004','1999_2005','1999_2006','1999_2007','1999_2008','1999_2009','1999_2010','1999_2011','1999_2012','1999_2013']

expression=['TPA_ expression', 'Acres_expression', 'NoTree_expession'] ### Note these are place holders for the correct expressions that I need help with.

arcpy.Union_analysis([R1+years[0]+Dam,R1+years[1]+Dam],R1+years[0]+"_"+years[1]+Dam)

for i in range(0,13) :

  arcpy.Union_analysis([R1+years[0]+"_"+years[i+1]+Dam,R1+years[i+2]+Dam],R1+years[0]+"_"+years[i+2]+Dam)

  print years[0]+'_'+years[i+2]+" union done"

         for j in range(0,3):

                 arcpy.AddField_management(R1+years[0]+'_'+years[i+2]+Dam, fields+RangeYear, "FLOAT","","","")

                 arcpy.CalculateField_Management(R1+years[0]+'_'+years[i+2]+Dam, fields+RangeYear,expression)

I would appreciate any help you can give, please do not worry about insulting me I am new to programming.

Thanks

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

Xander... Yes good... there is a variant that interweaves two lists or portions of a single list too.

>>> years = ['1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006','2007', '2008']
>>> text = [z[0] +"_"+ pair[1] for z in zip(years[:-1], years[1:])]

>>> text
['1999_2000', '2000_2001', '2001_2002', '2002_2003', '2003_2004', '2004_2005', '2005_2006', '2006_2007', '2007_2008']

OR

>>> text = ["{}_{}".format(z[0], z[1]) for z in zip(years[:-1], years[1:])] 

but I do like using the new formatting options so that you don't have to care about casting between strings or text if you don't want to.

HOWARDWILLIAMS
Deactivated User

Thanks, it got weird when I cut and pasted it from my IDE, sorry about that.  I will fix it up tomorrow. 

Cheers

Howard

0 Kudos