Geographic Information Systems Asked by Nate Wold on December 11, 2020
I am receive a Runtime Error when using a Search Cursor on a hosted Table in ArcGIS Online and I cannot figure out why. I am using an AGOL Notebook. The message is that it cannot access the Table. Full editing is allowed on the table and my user is the owner.
import pandas, arcgis, arcpy
from arcgis.gis import GIS
from statistics import mean
import numpy as np
import datetime
from getpass import getpass
# Sign in
gis = GIS("home")
tableURL = "https://services.arcgis.com/qnjIrwR8z5Izc0ij/arcgis/rest/services/MLIACollectionsMonthly/FeatureServer/0"
### Create Trend Line table entry ###
def best_fit_slope_and_intercept(xs,ys):
m = (((mean(xs)*mean(ys)) - mean(xs*ys)) / ((mean(xs)*mean(xs)) -
mean(xs*xs)))
b = mean(ys) - m*mean(xs)
return m, b
def calculate_trend(table, value_field, date_field, trend_field, num_days,
category_field, trend_daily_change_field):
table = tableURL
value_field = "month_collect_total"
date_field = "date"
trend_field = "collect_trend"
num_days = 360
category_field = "geography"
trend_daily_change_field = "collect_change"
search_fields = [value_field, date_field]
if len(category_field) > 0:
search_fields.append(category_field)
category_values = {}
with arcpy.da.SearchCursor(table, search_fields) as cursor:
for row in cursor:
category = "all"
if len(category_field) > 0:
category = row[2]
if category is None:
continue
if category not in category_values:
category_values[category] = {'max_date' : None, 'values' : []}
values = category_values[category]
v = row[0]
d = row[1]
if d is None or v is None:
continue
if values['max_date'] == None or values['max_date'] < d:
values['max_date'] = d
values['values'].append([d, v])
for k, v in category_values.items():
if v['max_date'] is None:
continue
min_date = v['max_date'] - datetime.timedelta(days=num_days)
x_list = []
y_list = []
for value in v['values']:
if value[0] > min_date:
x_list.append(int(value[0].timestamp()))
y_list.append(value[1])
if len(y_list) < 2:
continue
xs = np.array(x_list, dtype=np.float64)
ys = np.array(y_list, dtype=np.float64)
m, b = best_fit_slope_and_intercept(xs,ys)
where_clause = "1=1"
if len(category_field) > 0:
where_clause = "{0} = '{1}'".format(category_field, k)
daily_change = ((m*(int(v['max_date'].timestamp())))+b) - ((m*
(int((v['max_date'] - datetime.timedelta(days=1)).timestamp())))+b)
update_fields = [date_field, trend_field]
if len(trend_daily_change_field) > 0:
update_fields.append(trend_daily_change_field)
with arcpy.da.UpdateCursor(table, update_fields, where_clause) as cursor:
for row in cursor:
if row[0] > min_date:
row[1] = (m*(int(row[0].timestamp())))+b
if len(trend_daily_change_field) > 0:
row[2] = daily_change
else:
row[1] = None
if len(trend_daily_change_field) > 0:
row[2] = None
cursor.updateRow(row)
del cursor
print("Complete")
RuntimeError Traceback (most recent call last)
<ipython-input-21-df64dc63df0d> in <module>
1 # Calculate trend for collections
----> 2 calculate_trend(tableURL, "month_collect_total", "date", "collect_trend", 360, "county",
"collect_change")
<ipython-input-20-2d85229df245> in calculate_trend(table, value_field, date_field, trend_field,
num_days, category_field, trend_daily_change_field)
20
21 category_values = {}
---> 22 with arcpy.da.SearchCursor(table, search_fields) as cursor:
23 for row in cursor:
24 category = "all"
RuntimeError: cannot open 'https://services.arcgis.com/qnjIrwR8z5Izc0ij/arcgis/rest/services/MLIACollectionsMonthly/FeatureServer/0'
Your process works for me using ArcGIS Online Advanced Notebooks. You've only provided a snippet of your code. My only suggestion is to check the table variable you're passing into the SearchCursor and make sure it's correct. Per the error, it seems like it's just the URL as I've used it.... so its hard to say. Perhaps just the timing of your attempt that it couldn't access the service?
import arcpy
url = "https://services.arcgis.com/qnjIrwR8z5Izc0ij/arcgis/rest/services/MLIACollectionsMonthly/FeatureServer/0"
with arcpy.da.SearchCursor(url, "*") as cursor:
for r in cursor:
print(r)
(1, 'Beaverhead', datetime.datetime(2005, 8, 1, 12, 0), 734.25, 734.25, None, None) (2, 'Beaverhead', datetime.datetime(2005, 9, 1, 12, 0), 621.75, 1356.0, None, None) (3, 'Beaverhead', datetime.datetime(2005, 10, 1, 12, 0), 637.5, 1993.5, None, None)
Answered by KHibma on December 11, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP