I have what I hope is a simple question. I simply need to use the Python API to update attribute values in a feature layer based on values in another feature layer, using a common ID. Here's the scenario:
Thanks in advance, your help is much appreciated!
Solved! Go to Solution.
I figured it out and thought I'd post my code here in case it helps anyone (and in case someone wants to pick it apart - suggestions for improvement are welcome!).
from arcgis.gis import GIS
import pandas as pd
from arcgis.features import SpatialDataFrame
gis = GIS(<portal_url>, <username>, <password>")
workOrderMaster = GIS().content.get(<itemID>)
workOrderFlayer = workOrderMaster.layers[0]
workOrderSdf = SpatialDataFrame.from_layer(workOrderFlayer)
approvals = GIS().content.get(<itemID>)
approvalFlayer = approvals.layers[0]
approvalsSdf = SpatialDataFrame.from_layer(approvalFlayer)
df=approvalsSdf.sort_values('CreationDate', ascending=True)
df=df.drop_duplicates(subset="workOrderId")
workOrder_fset = workOrderFlayer.query()
approval_fset = approvalFlayer.query()
overlap_rows = pd.merge(left = workOrderSdf, right = df, how='inner', on = 'workOrderId')
all_features = workOrder_fset.features
approval_updates = approval_fset.features
for workOrderId in overlap_rows['workOrderId']:
original_feature = [f for f in all_features if f.attributes['workOrderId'] == workOrderId][0]
update_feature = [f for f in approval_updates if f.attributes['workOrderId'] == workOrderId][0]
workOrder_edit = original_feature
approval_edit = update_feature
workOrder_edit.attributes['apprName'] = approval_edit.attributes['approved_by']
workOrder_edit.attributes['isAppr'] = approval_edit.attributes['approvedecline']
workOrder_edit.attributes['apprDate'] = approval_edit.attributes['approved_on']
workOrder_edit.attributes['apprNote'] = approval_edit.attributes['approvalNotes']
update_result = workOrderFlayer.edit_features(updates=[workOrder_edit])
I figured it out and thought I'd post my code here in case it helps anyone (and in case someone wants to pick it apart - suggestions for improvement are welcome!).
from arcgis.gis import GIS
import pandas as pd
from arcgis.features import SpatialDataFrame
gis = GIS(<portal_url>, <username>, <password>")
workOrderMaster = GIS().content.get(<itemID>)
workOrderFlayer = workOrderMaster.layers[0]
workOrderSdf = SpatialDataFrame.from_layer(workOrderFlayer)
approvals = GIS().content.get(<itemID>)
approvalFlayer = approvals.layers[0]
approvalsSdf = SpatialDataFrame.from_layer(approvalFlayer)
df=approvalsSdf.sort_values('CreationDate', ascending=True)
df=df.drop_duplicates(subset="workOrderId")
workOrder_fset = workOrderFlayer.query()
approval_fset = approvalFlayer.query()
overlap_rows = pd.merge(left = workOrderSdf, right = df, how='inner', on = 'workOrderId')
all_features = workOrder_fset.features
approval_updates = approval_fset.features
for workOrderId in overlap_rows['workOrderId']:
original_feature = [f for f in all_features if f.attributes['workOrderId'] == workOrderId][0]
update_feature = [f for f in approval_updates if f.attributes['workOrderId'] == workOrderId][0]
workOrder_edit = original_feature
approval_edit = update_feature
workOrder_edit.attributes['apprName'] = approval_edit.attributes['approved_by']
workOrder_edit.attributes['isAppr'] = approval_edit.attributes['approvedecline']
workOrder_edit.attributes['apprDate'] = approval_edit.attributes['approved_on']
workOrder_edit.attributes['apprNote'] = approval_edit.attributes['approvalNotes']
update_result = workOrderFlayer.edit_features(updates=[workOrder_edit])
Hi Joseph,
what happened if you'd like to update your attributes based on the geometry changes, more concretely polygons? what I mean if you have change in some records and the change is done with the geometry, how do you do it?
Ashraf,