Hi all,
This feels like a problem that should have a really straightforward solution but for some reason I can't wrap my head around it. I have a simple arcade script that was given to me that was used in the Calculate Field window. I'm trying to take that expression and incorporate it into a python script in jupyter notebook.
This is the arcade script:
var LINED = $feature.LINED
var startDate = IIf(LINED == "Yes", Date($feature.LINEDYEAR), Date($feature.INSTALLDATE));
var endDate = Now();
var age = round(DateDiff(endDate, startDate, 'years'),2);
return age;
It's being used to populate a field called AGE_YEARS
Basically I need a python script that says:
If (LINED == 'Yes'):
# Calculate AGE_YEARS to be the difference between LINEDYEAR and the current date
else:
# Calculate AGE_YEARS to be the difference between INSTALLYEAR and the current date
# Below is what I have so far
from datetime import datetime
edit_layer = "AIRDRIE_EDIT.GIS.sGravityMain"
LinedDate = !LINED_YEAR!
InstallDate = !INSTALL_DATE!
CurrentDate = datetime.now()
diff1 = CurrentDate - LinedDate
diff2 = CurrentDate - InstallDate
AgeYears = "getClass(!LINED!)"
# Criteria for how to calculate field
codeblock = """
def getClass(LINED):
if (LINED == 'Yes'):
return diff1
else:
return diff2"""
# calculate age in years
arcpy.management.CalculateField(edit_layer, 'AGE_YEARS', AgeYears, "PYTHON3", codeblock)
The above block shows what I have so far. Any suggestions are appreciated!