Solved! Go to Solution.
import arcpy
fc = "c:/...your featureclass"
use_field = "UseClass"
length_field = "SUM_SHAPE_Length"
# Create an expression with proper delimiters
#
a1expression = arcpy.AddFieldDelimiters(fc, Use_field) + " = A1"
with arcpy.da.SearchCursor(fc, (use_field, length_field),
where_clause=a1expression) as a1cursor:
for row in a1cursor:
a1value = row[1]
d1expression = arcpy.AddFieldDelimiters(fc, Use_field) + " = D1"
with arcpy.da.SearchCursor(fc, (use_field, length_field),
where_clause=d1expression) as d1cursor:
for row in d1cursor:
d1value = row[1]
## you can then use a1value and d1value in an if statement to calculate the percentages in the same way as field calculator
if d1value > (a1value * .2):
print "more than 20%" ## if you are running as a script tool then use arcpy.AddMessage("more than 20%")
elif d1value < (a1value * .2):
print "less than 20%" ## if you are running as a script tool then use arcpy.AddMessage("less than 20%")
elif d1value == (a1value * .2):
print "exactly 20%" ## if you are running as a script tool then use arcpy.AddMessage("exactly 20%")
else:
print "invalid calculation - please check inputs and try again"
del a1value, d1value, a1cursor, d1cursor, a1expression, d1expression, row, use_field, length_field, fcno worries mark - if you are working in arcmap then in the first instance I would just run the code line by line in the python window:
http://resources.arcgis.com/en/help/main/10.1/index.html#//002100000017000000
you can then copy my code statement by statement into this, hit return after each line and see if it runs ok - you'll get red error messages if it doesn't which will help to resolve any issues - there might be a couple.
when you have fixed all the niggles and run it a few times then you can save it as a python script, add to a toolbox and define your input/output parameters:
http://resources.arcgis.com/en/help/main/10.1/index.html#/Adding_a_script_tool/00150000001r000000/
cheers
Tim
Hi,
Thanks you for the code. However the values I want to compare are in the same field.
Thanks
Mark
[ATTACH=CONFIG]27966[/ATTACH]