ERROR
In ArcGIS Notebooks, when exporting extracted deleted features from a hosted feature layer to a new feature class, the following error message is returned:
Error: ERROR 1: Cannot encode value

In some cases, the following error message is returned:
Error: ERROR 1: Cannot encode Y value

The spatial reference of the FeatureSet is not correctly defined before exporting the deleted features to a new feature class. As a result, the geometry values cannot be encoded during feature class creation.
from arcgis.gis import GIS
gis = GIS("Home")
print("Logged in as:", gis.users.me.username)
from datetime import datetime, timedelta, timezone
import pandas as pd
from arcgis.features import FeatureLayerCollection
from arcgis.features import FeatureSet
print("Starting")
Note: After pasting the code, click Run this cell and advanceto execute the cell before proceeding to the next step.
item_id = "<ITEM_ID>" flc_item = gis.content.get(item_id) flc = FeatureLayerCollection.fromitem(flc_item) utc_now = datetime.now(timezone.utc) start_time_ms = int((utc_now - timedelta(hours=24)).timestamp() * 1000) print(utc_now)
changes = flc.extract_changes(
layers=[0],
servergen=<replicaServerGen_Value>,
return_inserts=False,
return_updates=False,
return_deletes=True
)
print("Processing deleted features")
deletes = changes["edits"][0]["features"].get("deletes", [])
feature_list = []
for feat in deletes:
geom = feat.get("geometry")
# skip only truly invalid geometry
if geom is None:
continue
feature_list.append({
"attributes": feat.get("attributes"),
"geometry": geom
})
if not feature_list:
raise Exception("No valid features.")
feature_set = FeatureSet.from_dict({"features": feature_list})
# FIX part
feature_set.spatial_reference = {"wkid": 3857}
print(feature_set.sdf)
print("sending to csv")
output_csv_path = "/arcgis/home/output_data.csv"
feature_set.sdf.spatial.to_table(location=output_csv_path)
print("sending to feature class (GDB)")
# File geodatabase feature class
output_path = "/arcgis/home/test.gdb/NewTestClass"
feature_set.sdf.spatial.to_featureclass(location=output_path)
print("Done")
)
The code block below shows the full sample script.
from arcgis.gis import GIS
gis = GIS("Home")
print("Logged in as:", gis.users.me.username)
from datetime import datetime, timedelta, timezone
import pandas as pd
from arcgis.features import FeatureLayerCollection
from arcgis.features import FeatureSet
print("Starting")
item_id = "fbc594fdbff1435780ebc36cf09666e6"
flc_item = gis.content.get(item_id)
flc = FeatureLayerCollection.fromitem(flc_item)
utc_now = datetime.now(timezone.utc)
start_time_ms = int((utc_now - timedelta(hours=24)).timestamp() * 1000)
print(utc_now)
changes = flc.extract_changes(
layers=[0],
servergen=11360400, # ReplicaServerGen value
return_inserts=False,
return_updates=False,
return_deletes=True
)
print("Processing deleted features")
deletes = changes["edits"][0]["features"].get("deletes", [])
feature_list = []
for feat in deletes:
geom = feat.get("geometry")
# skip only truly invalid geometry
if geom is None:
continue
feature_list.append({
"attributes": feat.get("attributes"),
"geometry": geom
})
if not feature_list:
raise Exception("No valid features.")
feature_set = FeatureSet.from_dict({"features": feature_list})
feature_set.spatial_reference = {"wkid": 3857}
print(feature_set.sdf)
print("sending to csv")
output_csv_path = "/arcgis/home/output_data.csv"
feature_set.sdf.spatial.to_table(location=output_csv_path)
print("sending to feature class (GDB)")
# File geodatabase feature class
output_path = "/arcgis/home/test.gdb/NewTestClass"
feature_set.sdf.spatial.to_featureclass(location=output_path)
print("Done")
The output message is displayed in the cell.

Article ID: 000042526
Get help from ArcGIS experts
Start chatting now