Working with Hosted feature layers in Python

409
5
03-30-2023 12:41 PM
SanchezNuñez
Occasional Contributor

Hello everyone,

Anyone can recommend samples of Python source code updating hosted feature layers?

Thanks

Jose

0 Kudos
5 Replies
Jacob_Helfman
New Contributor III

There's plenty of documentation, like here: https://developers.arcgis.com/python/guide/appending-features/

0 Kudos
JoseSanchez
Occasional Contributor III

Thank you Jacob!

0 Kudos
avonmoos
Occasional Contributor

import arcgis
from arcgis.gis import GIS

# Connect to your ArcGIS Online organization
gis = GIS("https://www.arcgis.com", "username", "password")

# Get the feature layer you want to update
layer = gis.content.get("feature_layer_item_id").layers[0]

# Create a feature set object containing the features you want to update
feature_set = arcgis.features.FeatureSet.from_csv("path_to_csv_file")

# Use the feature set to update the feature layer
layer.edit_features(updates=feature_set)

print("Feature layer updated successfully!")

0 Kudos
JoseSanchez
Occasional Contributor III

Thanks @avonmoos 

In my case I am getting a feature class with new records that have to be append to the hosted feature class.

Does this line append the hosted feature with records?

# Use the feature set to update the feature layer

layer.edit_features(updates=feature_set)

 

Thanks



0 Kudos
avonmoos
Occasional Contributor

If you're looking to append then give this a try...

 

import arcgis

# Set the URL of your ArcGIS Online organization
portal_url = "https://www.arcgis.com/"

# Set the credentials for your ArcGIS Online account
username = "<username>"
password = "<password>"

# Set the ID of the feature service you want to update
service_id = "<service_id>"

# Set the name of the feature class in your geodatabase that you want to update the service with
fc_name = "<feature_class_name>"

# Connect to your ArcGIS Online organization
gis = arcgis.gis.GIS(portal_url, username, password)

# Get the feature service
feature_service = arcgis.features.FeatureLayerCollection.fromitem(gis.content.get(service_id))

# Get the feature layer from the feature service
feature_layer = feature_service.layers[0]

# Get the feature class from the geodatabase
feature_class = arcgis.features.FeatureSet.from_featureclass(fc_name)

# Update the feature service with the feature set
feature_layer.edit_features(updates=feature_class)

# Disconnect from the ArcGIS Online organization
gis = None

0 Kudos