Select to view content in your preferred language

Python API: How to update feature layer attributes from a second feature layer using common ID?

3453
2
Jump to solution
09-07-2019 10:08 AM
JosephRhodes3
Emerging Contributor

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:

  • I have a feature layer of work orders
  • I have another feature layer with approvals for those work orders
  • Both layers have a common work order ID
  • I need a script to check the approvals layer and write "Approved" in a relevant field of the work orders layer for any matching records
  • I need to avoid ArcPy (because the script will run on PythonAnywhere so there's no way to set up the licensing necessary for ArcPy).

Thanks in advance, your help is much appreciated!

0 Kudos
1 Solution

Accepted Solutions
JosephRhodes3
Emerging Contributor

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])

View solution in original post

2 Replies
JosephRhodes3
Emerging Contributor

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])

afana1972
Occasional Contributor

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,