POST
|
Xander, I think I figured it out when I call expression , it places the expression inside double quotation marks. However if I write the expression in without the double quotation marks it works. exp="'!TPA_{0}!+!TPA_{1}!','!shape.area@acres!','!TPA_{2}! * !Acres_{2}!'"
exp2= exp.format(years[0],years[1],RangeYear[0],RangeYear[0])
expression=exp2.split(',')
arcpy.CalculateField_management(R1+RangeYear[0]+Dam,fields[0]+RangeYear[0],expression[0],"PYTHON") #Does not work
arcpy.CalculateField_management(R1+RangeYear[0]+Dam,fields[0]+RangeYear[0],'!TPA_1999!+!TPA_2000!',"PYTHON") #Does work
... View more
02-04-2016
02:12 PM
|
0
|
1
|
675
|
POST
|
Xander, Thanks so much I have already learned a ton from you! What a help you have been! I have almost got it but am getting an error message saying "The value type is incompatible with the field type. [TPA_1999_2000]" here is the code: Any idea why?@Xandar Bakker for i in range (0,15) :
arcpy.CalculateField_management(R1+years+Dam,fields[0]+years,"!TPA1!+ !TPA2!+ !TPA3!","PYTHON")
##########Calculate Acres_Year
for i in range (0,15) :
arcpy.CalculateField_management(R1+years+Dam,fields[1]+years,"!shape.area@acres!","PYTHON_9.3")
##########Calculate NoTrees_Year
for i in range (0,15) :
expression="!"+fields[0]+years+"!"+"*"+"!"+fields[1]+years+"!"
arcpy.CalculateField_management(R1+years+Dam,fields[2]+years,expression,"PYTHON")
#______________________________________________________________________________________________________________#
#Step 7. Union former year to year following starting with 1999. Add new fields. Calcuate fields
# I was unable to figure out how to do this the following step in a loop so the code is being applied to each year.
RangeYear = ['{0}_{1}'.format(years[0], year) for year in years[1:]]
####1999-2000
exp="'!TPA_{0}! + !TPA_{1}!','!shape.area@acres!','!TPA_{2}! * !Acres_{2}!'"
for i in range (0,1):
arcpy.Union_analysis([R1+years+Dam, R1+years[i+1]+Dam],R1+RangeYear+Dam)
exp2= exp.format(years,years[i+1],RangeYears,RangeYears)
expression=exp2.split(',')
for j in range(0,3):
arcpy.AddField_management(R1+RangeYears+Dam,fields +RangeYear,"FLOAT")
print fields +RangeYear
print expression
arcpy.CalculateField_management(R1+RangeYears+Dam,fields +RangeYear,expression ,"PYTHON")
... View more
02-04-2016
01:29 PM
|
0
|
0
|
675
|
POST
|
Xander, Can you please explain what is going on with this line of code: Line 09 range_years = ['{0}_{1}'.format(years[0], year) for year in years[1:]] The results do provide what I need however I do not understand how this code works. If you have time to explain it I would appreciate it. Please do not trouble yourself though. Thanks Howard
... View more
02-04-2016
08:16 AM
|
0
|
4
|
675
|
POST
|
Thank you Xander, I have had to take a break from this to do other work, I certainly appreciate your help. Howard
... View more
02-04-2016
07:25 AM
|
0
|
0
|
675
|
POST
|
Ahhggg! For some reason when I hit post the indentations get all messed up! I have no idea why it is doing this, please see if you can decifer what I am trying to do here. Again I am sorry for this, I will attach the .txt file that has the code looking nice. Thanks
... View more
01-28-2016
09:08 AM
|
0
|
2
|
675
|
POST
|
Thank you to all who responded to this question earlier. I am re-posting a more specific question and hopefully my code will be easier to understand. So, I have number of feature classes in a .gdb that have tree counts per acre in each polygon by year. I am iteratively unioning the features by year adding a field that is the sum of the previous tree count with the new tree count. import arcpy
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput=True
arcpy.env.workspace="C:\LPP\APS.gdb"
R1="R1ADS"
Dam="Damage"
years=['1999','2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013']
fields=['TPA_','Acres_','NoTrees_']
###R1+years(i)+Dam is the name of the feature in the .gdb that will be unioned
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']
### RangeYear is a variable that I created for naming the unioned outfeature
#________________________________________________
###The statement below create first RangeYear feature to base iteration on in following loop. Because only two years are being added it requires a simple Field Calculator Expression to sum the trees per acre per year which I was able to figure out.
arcpy.Union_analysis([R1+years[0]+Dam,R1+years[1]+Dam],R1+years[0]+"_"+years[1]+Dam)
for j in range(0,3):
arcpy.AddField_management(R1+years[0]+"_"+years[1]+Dam,fields +RangeYear[0],"FLOAT","","","")
###I tried to but the statements below in the loop above by having the Field Calculator expressions in a list also, however
###the program would not allow me to iterate through that list so I plugged the expression into the parameters below and ran the tool one at a ###time.
arcpy.CalculateField_management(R1+years[0]+"_"+years[1]+Dam,fields[0]+RangeYear[0],'!TPA_1999!+!TPA_2000!',"PYTHON")
arcpy.CalculateField_management(R1+years[0]+'_'+years[1]+Dam,fields[0]+RangeYear[0],'!Shape_Area! * 0.00024710538',"PYTHON")
arcpy.CalculateField_management(R1+years[0]+'_'+years[1]+Dam,fields[0]+RangeYear[0],'!TPA_1999_2000!*!Acres_1999_2000!',"PYTHON")
###The loop below unions the remaining years the previous years with the next chronological year;
###adds three new field; calculates the new fields values and then move on to the next union.
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)
for j in range(0,3):
arcpy.AddField_management(R1+years[0]+'_'+years[i+2]+Dam,fields +RangeYear[i+1],"FLOAT","","","")
arcpy.CalculateField_management(R1+years[0]+'_'+years[i+2]+Dam,fields , ????Expression????, "PYTHON")
###OK so the major crux here is that for TPA_1999_20XX, I need to add the previously unioned TPA_1999_20XX-1 to TPA_20XX.
###I have no idea how to do this. I suspect that once I figure this out, the next two expressions will be evident.
####################################Thanks for reading, sorry for being such a hack programmer!#################### Field Calculator Expression Arcpy Message was edited by: HOWARD WILLIAMS
... View more
01-28-2016
09:01 AM
|
0
|
10
|
4468
|
POST
|
Thanks, it got weird when I cut and pasted it from my IDE, sorry about that. I will fix it up tomorrow. Cheers Howard
... View more
01-27-2016
06:56 PM
|
0
|
0
|
107
|
POST
|
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.
... View more
01-27-2016
06:54 PM
|
0
|
0
|
595
|
POST
|
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
... View more
01-27-2016
03:56 PM
|
0
|
3
|
595
|
POST
|
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
... View more
01-27-2016
03:25 PM
|
0
|
11
|
4772
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|