I've got this working to print the description of a feature service, but how do I get a search cursor on a feature service out of AGOL?! I'm new at this arcrest bit!
import arcrest
un = r'username'
pw = r'password'
sh =arcrest.AGOLTokenSecurityHandler(org_url='myOrgURL', username=un, password=pw)
admin = arcrest.manageorg.Administration(securityHandler=sh)
content = admin.content
currentUser = content.users.user()
fsurl = r'theFeatureServiceURL'
fs = arcrest.agol.FeatureService(fsurl,sh)
print fs.description
Solved! Go to Solution.
You would use the REST API query. Here's some sample code which you will need to modify.
import arcpy, urllib, urllib2, json, sys, time, datetime, collections
from datetime import datetime, timedelta
# how far back do you want to go? Remember AGO uses GMT / UTC.
H = 220
date_H_hours_ago = datetime.now() - timedelta(hours=H)
# Credentials and feature service information
# URL, referrer and tokenurl may vary based on ArcGIS Online or your server setup
# Credentials and feature service information
username = 'Username' # use your username here
password = 'Password' # use your password here
# This needs to be the path to your featue, edit as needed, note /query at end
URL = "https://services2.arcgis.com/somestring/arcgis/rest/services/FeatureName/FeatureServer/0/query"
referer = "http://www.arcgis.com/"
tokenurl = "https://www.arcgis.com/sharing/rest/generateToken"
# obtain a token
query_dict = { 'username': username, 'password': password, 'referer': referer }
query_string = urllib.urlencode(query_dict)
token = json.loads(urllib.urlopen(tokenurl + "?f=json", query_string).read())
if "token" not in token:
print(token['error'])
sys.exit(1)
# Your fields go in comma delimited list (outFields)
query_dict = {
"where" : "EditDate >= DATE '2016-06-01 00:00:00'",
"outFields" : "OBJECTID, EditDate",
"orderByFields" : "EditDate",
"returnGeometry" : "true",
"f": "json", "token": token['token'] }
# to select all fields use: "outFields" : "*",
# to select individual fields use comma delimited list: "outFields" : "OBJECTID, EditDate",
# a date certain: "where" : "EditDate >= DATE '2016-05-29 18:30:00'",
# this morning at 2 am: "where" : "EditDate >= DATE '"+(datetime.now()).strftime("%Y-%m-%d 02:00:00")+"'",
# some time ago: "where" : "EditDate > DATE '"+date_H_hours_ago.strftime("%Y-%m-%d %H:%M:%S")+"'",
# if you do not want geometry: "returnGeometry" : "false",
# results in json format
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
features = json.loads(jsonResponse.read(),
object_pairs_hook=collections.OrderedDict)[u'features']
# print json.dumps(features, indent=4, sort_keys=False) # formatted json
print '\n\nTitle if needed:'
print 'ObjectID\tEditDate\tX-lon\tY-lat' #Field list you want to use
for feature in features:
# AGO normally uses GMT/UTC, you may wish to convert to local time
editTime = time.strftime('%c', time.localtime(feature['attributes']['EditDate']/1000))
# print your fields here, using something like this:
print str(feature['attributes']['OBJECTID']) + '\t' + editTime,
# if you wanted geometry; AGO returns web mercator, reproject if necessary
# print "x: " + str(feature['geometry']['x'])+ "\ty: " + str(feature['geometry']['y'])
# WGS 1984 : (4326) Lat/Lon
# WGS 1984 Web Mercator (auxiliary sphere) : (102100) or (3857)
ptGeometry = arcpy.PointGeometry(arcpy.Point(feature['geometry']['x'],feature['geometry']['y']),
arcpy.SpatialReference(3857)).projectAs(arcpy.SpatialReference(4326))
print "\t" + str(ptGeometry.firstPoint.X) +"\t" + str(ptGeometry.firstPoint.Y)
Some of the places you need to change are lines: 12, 13, 16, 32, 53, 54, 61. There's also some general notes and debugging code, which you can cut out.
What do you plan on doing with this feature service data once you access it? It may be working with cursors isn't the best path forward.
I need to get at the attributes of features, and then generate a derived service from the attributes.
How do I get at the attributes for each row of a feature service?
I need to get at the attributes of features, and then generate a derived service from the attributes.
I need a scheduled task to read the feature service attributes outside of AGOL/etc, for automated processing.
You would use the REST API query. Here's some sample code which you will need to modify.
import arcpy, urllib, urllib2, json, sys, time, datetime, collections
from datetime import datetime, timedelta
# how far back do you want to go? Remember AGO uses GMT / UTC.
H = 220
date_H_hours_ago = datetime.now() - timedelta(hours=H)
# Credentials and feature service information
# URL, referrer and tokenurl may vary based on ArcGIS Online or your server setup
# Credentials and feature service information
username = 'Username' # use your username here
password = 'Password' # use your password here
# This needs to be the path to your featue, edit as needed, note /query at end
URL = "https://services2.arcgis.com/somestring/arcgis/rest/services/FeatureName/FeatureServer/0/query"
referer = "http://www.arcgis.com/"
tokenurl = "https://www.arcgis.com/sharing/rest/generateToken"
# obtain a token
query_dict = { 'username': username, 'password': password, 'referer': referer }
query_string = urllib.urlencode(query_dict)
token = json.loads(urllib.urlopen(tokenurl + "?f=json", query_string).read())
if "token" not in token:
print(token['error'])
sys.exit(1)
# Your fields go in comma delimited list (outFields)
query_dict = {
"where" : "EditDate >= DATE '2016-06-01 00:00:00'",
"outFields" : "OBJECTID, EditDate",
"orderByFields" : "EditDate",
"returnGeometry" : "true",
"f": "json", "token": token['token'] }
# to select all fields use: "outFields" : "*",
# to select individual fields use comma delimited list: "outFields" : "OBJECTID, EditDate",
# a date certain: "where" : "EditDate >= DATE '2016-05-29 18:30:00'",
# this morning at 2 am: "where" : "EditDate >= DATE '"+(datetime.now()).strftime("%Y-%m-%d 02:00:00")+"'",
# some time ago: "where" : "EditDate > DATE '"+date_H_hours_ago.strftime("%Y-%m-%d %H:%M:%S")+"'",
# if you do not want geometry: "returnGeometry" : "false",
# results in json format
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
features = json.loads(jsonResponse.read(),
object_pairs_hook=collections.OrderedDict)[u'features']
# print json.dumps(features, indent=4, sort_keys=False) # formatted json
print '\n\nTitle if needed:'
print 'ObjectID\tEditDate\tX-lon\tY-lat' #Field list you want to use
for feature in features:
# AGO normally uses GMT/UTC, you may wish to convert to local time
editTime = time.strftime('%c', time.localtime(feature['attributes']['EditDate']/1000))
# print your fields here, using something like this:
print str(feature['attributes']['OBJECTID']) + '\t' + editTime,
# if you wanted geometry; AGO returns web mercator, reproject if necessary
# print "x: " + str(feature['geometry']['x'])+ "\ty: " + str(feature['geometry']['y'])
# WGS 1984 : (4326) Lat/Lon
# WGS 1984 Web Mercator (auxiliary sphere) : (102100) or (3857)
ptGeometry = arcpy.PointGeometry(arcpy.Point(feature['geometry']['x'],feature['geometry']['y']),
arcpy.SpatialReference(3857)).projectAs(arcpy.SpatialReference(4326))
print "\t" + str(ptGeometry.firstPoint.X) +"\t" + str(ptGeometry.firstPoint.Y)
Some of the places you need to change are lines: 12, 13, 16, 32, 53, 54, 61. There's also some general notes and debugging code, which you can cut out.