Append Data to Hosted Feature Layer

572
4
02-09-2021 07:04 AM
LisaTunnell
Occasional Contributor II

I would like to do something seemingly simple:  upload multiple features to a hosted feature service in Portal.  Weird thing is that I can do a search and I see that there is an "Append Data" option in AGOL. I do not see it in Portal, though??  I also see this tool: https://enterprise.arcgis.com/en/portal/latest/use/geoanalytics-append-data.htm , which looks perfect...but it is a geoanalytics tool.  I just want to upload multiple records to a feature layer.

I can see needing/wanting to use this in many cases, but in this particular case I have created a Hosted Feature Layer out of a CSV.  The user has continued to update his Excel spreadsheet, so I would like to append the new data to the Feature Layer.  In this example, we are just wanting to import more lat/lng values. The schema is the same.

0 Kudos
4 Replies
jcarlson
Regular Contributor II

They state it clearly in the docs that Append is only an option in AGOL. They don't state why this is the case, however, and that has always bothered me. If you're going to hold back one of the more useful tools for dealing with hosted feature layers, I'd like to have a reason. And if it's a legit reason, I can live with it, but that's preferrable to not knowing.

Having dealt with this in the past, the only "easy" way of dealing with this situation is just to overwrite the hosted layer with the updated spreadsheet. As long as the user hasn't removed anything from the spreadsheet, or you haven't made significant changes to the data since adding it, you should be good.

If there have been changes or deletions since publishing the layer, you'll need some way of comparing the two datasets separately before submitting new features to the service. You can do this programmatically via python (we use pandas' compare function to do just this sort of thing), or you can bring in the CSV to Pro and copy/paste features, but that requires more manual looking, filtering, possibly spatial selections, and can be arduous if there are lots of records, and is prone to error.

- Josh Carlson
Kendall County GIS
Jayanta_Poddar
Honored Contributor

Do you have access to ArcMap or ArcGIS Pro (connected to Portal for ArcGIS)?

If yes, check if the following workaround works for you.

Click on the Add item from your computer in ArcGIS Online
Browse for the CSV file
Make sure the box for "Publish as hosted feature service" is checked
Fill in all the required fields
Click on Publish
Add the both published hosted feature layers into ArcMap
Use Append (Data Management) tool
Use the hosted feature layer created from the CSV file as the input feature layer and the other hosted feature layer as the target layer
Hit on Run.

Reference: Appending CSV data to AGOL from Pro not showing geometry

0 Kudos
HenryLindemann
Esri Contributor

Hi @LisaTunnell,

I don't know how comfortable you are with coding but it is quite simple to achieve with the ArcGIS Api for Python and it is all free.

index | ArcGIS for Developers

This is how you do it 

Upload your CSV the first time to create the service 

HenryLindemann_0-1612938781621.png

 

in the new service get the url displayed at the bottom of the page  

HenryLindemann_1-1612938854661.png

 

add /0 if there is only one layer in the service

Here is the code that you need if you run this in ArcGIS Pro not setup is required

from arcgis.gis import GIS
from arcgis.features import Feature
from arcgis.geometry import Point
from arcgis.features import FeatureLayer

"""
# Read the CSV and build the rows to append
"""
rows_to_append = []
# Read the csv and build the rows to append
with open('upload_data.csv', 'r') as read_file: # path should be full if the csv is not in the same folder c:/mypath/upload_data.csv
lines = read_file.readlines()
heading = True
for line in lines:
line = line.strip('\n')
if heading is True:
print(line) #heading
heading = False
continue
print(line) # Records
record = line.split(';') # splits the rows into parts
# build record to append
row_to_append = {"name": record[0], "surname": record[1]} # Create Attributes
geometry_point = Point({"x": record[2], "y": record[3], "spatialReference" : {"wkid" : 4326}}) # Create Geometry
rows_to_append.append(Feature(attributes=row_to_append, geometry=geometry_point))

"""
# log into AGOL and append new rows
"""
con = GIS('https://arcgis.com', 'username', 'password') # log in to AGOL
# connect to the layer to append
layer_to_append = FeatureLayer('https://services.arcgis.com/nZAAAAAAn0yqFPN/arcgis/rest/services/upload_data/FeatureServer/0', con)
# view Current Rows
view_rows = layer_to_append.query(where='1=1')
for row in view_rows:
print(row)

# append new rows
result = layer_to_append.edit_features(adds=rows_to_append)
print(result)

# view all rows
view_rows = layer_to_append.query(where='1=1')
for row in view_rows:
print(row)

 

Hope it helps

Henry

 

 

 

0 Kudos
LisaTunnell
Occasional Contributor II

Thanks for this Henry.  Unfortunately this is not going to work for us...unless I am missing something.  I need a way for my ArcGIS Portal users to be able to update.  These users are not GIS Analysts and do not use ArcGIS Pro.  This particular user just wanted a mapping interface to show his lat/lng fields that he had in an Excel spreadsheet.  He has been using Tableau.  I have created a Hosted Feature Service for him. He now has quite a few more rows of Excel data, in the same format, that he would like to upload.

It seems that using a Hosted Feature service for small updates is ideal, but when there are multiple features to append there are more challenges. I am just a bit confused as to why the tools are available in AGOL and not Portal. Has anyone used the Data Aggregation widget? https://doc.arcgis.com/en/web-appbuilder/create-apps/widget-data-aggregation.htm#ESRI_SECTION1_20B1B...

I will have to muddle through trying to figure out how to apply a global geocoder for this.  The layer we are working with shows locations world-wide. I'm not even sure if this will work for us, as the csv has lat/lng (web mercator) values and it seems to need the geocoder for address locations.  Anyway, this tool may be the closest thing I have found (if it works).

Any other ideas?  Could I publish the "Append" function as a geoprocessing service and use it from within Portal?

0 Kudos