Select to view content in your preferred language

Field Calculator Expression Arcpy

5136
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
WesMiller
Deactivated User

Are you writing this in code(which? ie python) or model builder? Are the polys all the same or varying sizes?

Edit: You may also want to move to a more appropriate place GeoNet Help is for help with GeoNet

HOWARDWILLIAMS
Deactivated User

Thanks Wes.

I am attempting to write this in Python, and it seems to work for most of

the script. I am using the PyCharm IDE.

I am trying to write a script that will execute a workflow that a

supervisor gave me. I will look into a spatial join, I wondered the same

thing myself, however I was trying to use the same methods as I was told to

follow.

Anyhow, I thought that this was a place to post arcpy questions, whoops

where do you suggest I post?

Thanks

0 Kudos
WesMiller
Deactivated User

You would want to move to python.

HOWARDWILLIAMS
Deactivated User

Thanks Wes!

0 Kudos
WesMiller
Deactivated User

Are all the polys the same size?

0 Kudos
DanPatterson_Retired
MVP Emeritus

tooo convoluted to dissect.  To expedite, it appears that you have a file which you have managed to get the results into the fields and now you are just having problems getting the field calculator expression to calculate the final field.  Do yourself a favour and do it manually once...get the correct functioning syntax from the Results window, then use it in your code.  see the attachment in my blog link for details "You are not allowed to use Modelbuilder": When Instructors need to get smarter

DanPatterson_Retired
MVP Emeritus

ps... do some code formatting...it will help others.  This link provides a compendium of recommendations

Code Formatting... the basics++

have fun

XanderBakker
Esri Esteemed Contributor

If I copy and paste the code to my IDE, I get some syntax errors for the indents.

A small tip on the years and range years lists. You could do this:

years = [str(year) for year in range(1999, 2014)]
range_years = ['{0}_{1}'.format(years[0], year) for year in years[1:]]

or like yours

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

Which yields:

['1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013']
['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']
HOWARDWILLIAMS
Deactivated User

Thanks for the suggestions.  I am sorry about the code formatting, I cut and pasted it from an IDE rather than a text editor and it got wired.  Anyway, I should have taken more time with snippet I posted.  Sorry I was getting frustrated.  Great suggestion on the years and range_years.  Thanks.  Really what I am struggling with the the expression that I have to write in field calculator.  It has been suggested that I do it manually first and I have tried, I can can not figure out the syntax for the expression.  Anyhow, thanks for the suggestion.

0 Kudos