Appending rows to an empty hosted Feature Service using python

1926
4
Jump to solution
03-02-2020 12:05 PM
ErnestoCarreras2
New Contributor III

Is it possible to append data from an excel table to a hosted Table (stand-alone table – not a layer) using python? I am attempting to replicate the process that is available in ArcGIS Online > Data > Append Data to Layer using an Excel table as the input file. The table may have more than one record.

Jake Skinner 

Jake, will the tool referenced in the link below work? Can it be modified so th einput is a table instead of a feature class? 

Update Hosted Feature Service with Feature Class 

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

You can use the Add Features REST API command.  You first will want to get the attribute data by iterating through the Excel file.  I would recommend first importing the excel file to a File Geodatabase using the Excel to Table function, then do the iteration on the geodatabase table.

You can then pass the attributes from the geodatabase table to the feature service.  Below is an example on how to update a feature service:

import requests, json, arcpy

# Disable warnings
requests.packages.urllib3.disable_warnings()

# Variables
gdbTable = r"C:\temp\python\test.gdb\xy"
username = "agol"
password = "gis1234"
addfsURL = 'https://services.arcgis.com/dlFJXQQtlWFB4qUk/ArcGIS/rest/services/Recycling/FeatureServer/2/addFeatures'

# Generate Token
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']

# Add Features
with arcpy.da.SearchCursor(gdbTable, ["Latitude", "Longitude"]) as cursor:
  for row in cursor:
    latitude = row[0]
    longitude = row[1]
    attr = [{"attributes":{"Latitude":"{0}".format(latitude),"Longitude":"{0}".format(longitude)}}]
    params = {"features": json.dumps(attr), 'token': token, 'f': 'json'}
    r = requests.post(addfsURL, data = params, verify=False)
del cursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I have not tested the above, so there may be some minor errors.

View solution in original post

4 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Ernesto,

Unfortunately, the tool I developed will not work as it uses the Features to JSON tool.  This tool requires a feature class for the input.

ErnestoCarreras2
New Contributor III

Any suggestions? Can you point me to the right direction? Any assistance will be appreciated/ 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

You can use the Add Features REST API command.  You first will want to get the attribute data by iterating through the Excel file.  I would recommend first importing the excel file to a File Geodatabase using the Excel to Table function, then do the iteration on the geodatabase table.

You can then pass the attributes from the geodatabase table to the feature service.  Below is an example on how to update a feature service:

import requests, json, arcpy

# Disable warnings
requests.packages.urllib3.disable_warnings()

# Variables
gdbTable = r"C:\temp\python\test.gdb\xy"
username = "agol"
password = "gis1234"
addfsURL = 'https://services.arcgis.com/dlFJXQQtlWFB4qUk/ArcGIS/rest/services/Recycling/FeatureServer/2/addFeatures'

# Generate Token
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']

# Add Features
with arcpy.da.SearchCursor(gdbTable, ["Latitude", "Longitude"]) as cursor:
  for row in cursor:
    latitude = row[0]
    longitude = row[1]
    attr = [{"attributes":{"Latitude":"{0}".format(latitude),"Longitude":"{0}".format(longitude)}}]
    params = {"features": json.dumps(attr), 'token': token, 'f': 'json'}
    r = requests.post(addfsURL, data = params, verify=False)
del cursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I have not tested the above, so there may be some minor errors.

ErnestoCarreras2
New Contributor III

Exactly what I needed... I just tried it and worked as expected!

0 Kudos