Skip to content

Commit d3d73e1

Browse files
author
Jim Barry
committed
added elapsed days script
1 parent 730394f commit d3d73e1

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

elapsed_days_python_script.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Here is the code that you can paste into a Jupyter Notebook
2+
3+
#FIRST CELL
4+
5+
# leave these three lines here
6+
# nothing to edit
7+
from arcgis.gis import GIS
8+
from datetime import date
9+
gis = GIS("home")
10+
11+
12+
# SECOND CELL
13+
14+
# replace the portalId of my feature service with yours
15+
inspections_item = gis.content.get('d52f49495f5645f6a031aa8dbc8263f5')
16+
17+
# '0' assumes the feature layer we're working with is either the only layer in the
18+
# feature service or the first layer in the feature service. If the layer you're
19+
# working with has a different layer index, change '0' to that index.
20+
inspections_layer = inspections_item.layers[0]
21+
22+
# these three lines you can leave as-is
23+
inspections_fset = inspections_layer.query()
24+
num_features = len(inspections_fset)
25+
print(num_features)
26+
27+
28+
#THIRD CELL
29+
30+
# we're going to loop through the records of the feature service layer one at a time
31+
for i in range(num_features):
32+
33+
# the rest of the code in this 'for' loop must be indented 4 spaces for it to work
34+
# Python uses indentations to separate code blocks.
35+
36+
# get the feature
37+
feature = inspections_fset.features[i]
38+
39+
# read the value out of the 'date_inspected' fields. If you're field is called
40+
# something different, change it here
41+
last_inspection_datetime = int(feature.attributes['date_inspected']) / 1000
42+
# ( we're dividing by 1000 because the date value is in unix epoch milliseconds.
43+
# and we're converting it to unix epoch seconds)
44+
45+
# we're converting the unix epoch time of the last inspection into
46+
# a python date object
47+
last_inspection_date = date.fromtimestamp(last_inspection_datetime)
48+
49+
# we're getting today's date as a python date object
50+
today = date.today()
51+
52+
# we're subtracting today's date with the date of the last inspection
53+
# to return a DateDiff object, then reading the actual number of days
54+
# from the DateDiff object's 'days' property
55+
days_since_last_inspection = (today - last_inspection_date).days
56+
57+
# then we take the number of days that have elapsed between today and
58+
# the last time the feature was inspected, and write that into the
59+
# 'days_since_inspected' field. If your field is called something
60+
# different, change it here
61+
feature.attributes['days_since_inspected'] = days_since_last_inspection
62+
63+
# then we call the layer's 'edit_features' method, passing the edited
64+
# feature as an array into the updates parameter
65+
update_result = inspections_layer.edit_features(updates=[feature])
66+
67+
# if you're running this in a notebook, it's just handy to print out to the
68+
# next line that the loop, and this entire script, is 'done'
69+
print('done')
70+
71+
END OF SCRIPT

0 commit comments

Comments
 (0)