Select to view content in your preferred language

Updating polygon features from a feature layer in arcgis API Python

1968
3
04-03-2021 01:25 AM
afana1972
Occasional Contributor

Hi all,

I'm trying to replicate the code published by ESRI in https://developers.arcgis.com/python/sample-notebooks/updating-features-in-a-feature-layer/ .  In the link example, they use point geometry, but in my case I’m trying to update polygon geometry. I have updated the code to work with polygon geometry but I’m receiving the following error "None of [Index([('SHAPE',)], dtype='object')] are in the [columns]", which is related to the input geometry line in my code. 

my code is 

# complete the updates
from arcgis import geometry #use geometry module to project the records
from copy import deepcopy

for cr_id in overlap_rows['cr_id']:
    # get the feature to be updated
    original_feature = [f for f in all_features if f.attributes['cr_id'] == cr_id][0]
    feature_to_be_updated = deepcopy(original_feature)
    
    print(str(original_feature))
    
    # get the matching row from the update layer ()
    matching_row = CRID_New_ly_FS2.where(CRID_New_ly_FS2.cr_id == cr_id).dropna()
    
    #get geometries in the destination coordinate system
    input_geometry = {'rings': float(matching_row[[['SHAPE']]])}
        
    output_geometry = geometry.project(geometries = [input_geometry],
                                       in_sr = CRID_Old_FS.spatial_reference['wkid'],
                                       out_sr = CRID_Old_FS.spatial_reference['latestWkid'],
                                       gis = gis1)
    
    # assign the updated values
    feature_to_be_updated.geometry = output_geometry[0]
    feature_to_be_updated.attributes['cr_id'] = int(matching_row['cr_id'])
    feature_to_be_updated.attributes['C31013'] = matching_row['C31013'].values[0]
    feature_to_be_updated.attributes['big_shape'] = matching_row['big_shape'].values[0] 
    feature_to_be_updated.attributes['site_name'] = matching_row['site_name'].values[0]
    feature_to_be_updated.attributes['awh_stage'] = int(matching_row['awh_stage'])
    feature_to_be_updated.attributes['awh_package'] = int(matching_row['awh_package'])
    
   # #add this to the list of features to be updated
    features_for_update.append(feature_to_be_updated)
   # 
    print(str(feature_to_be_updated))
    print("========================================================================")

 

Any suggestions? 

many thanks

Ash

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

did you check the nesting depth in

input_geometry = {'rings': float(matching_row[[['SHAPE']]])}

throw a print statement in to see if you are getting the appropriate content


... sort of retired...
0 Kudos
afana1972
Occasional Contributor

Hi Dan,

Thanks for your reply. 

I think your comment triggered me to print every step in my code and I think the problem reside in the dictionary format resulted from the "input_geometry" line code. 

The matching_row is a record, which contains the SHAPE attribute. So the code should be  

input_geometry = {'rings': matching_row['SHAPE']}. However, printing one record, will produce the ploygon geometry {"rings": [[[-149459.83432124555, 6834762.7625... }Name: SHAPE, dtype: geometry. The problem is how to access the dictionary values of [[[]]]?

Ashraf, 

0 Kudos
MehdiPira1
Esri Contributor

@afana1972 ,

The following code snippet should work.

You just need to change the layer names and variables accordingly.

 

from arcgis import geometry #use geometry module to project the records
from copy import deepcopy

original_item = gis.content.get("item_id")
original_layers = original_item.layers
fLyr = original_item.layers[0]
fset = fLyr.query()

new_item = gis.content.get("item_id")
new_fLyr = new_item.layers[0]
new_fset = new_fLyr.query()
new_fset_features = new_fLyr.query().features
new_flayer_rows = new_fset.sdf

features_to_be_added = []
template_hostedFeature = deepcopy(fset.features[0])
for index, row in new_flayer_rows.iterrows():
    feature_to_be_updated = deepcopy(template_hostedFeature)
    input_geometry = new_fset_features[index].geometry
    
    output_geometry = geometry.project(geometries = [input_geometry],
                                       in_sr = fset.spatial_reference['wkid'], 
                                       out_sr = fset.spatial_reference['latestWkid'], 
                                       gis = gis1)
    
    # assign the updated values
    feature_to_be_updated.geometry = output_geometry[0]
    feature_to_be_updated.attributes['cr_id'] = int(row['cr_id'])
    feature_to_be_updated.attributes['C31013'] = row['C31013'].values[0]
    feature_to_be_updated.attributes['big_shape'] = row['big_shape'].values[0] 
    feature_to_be_updated.attributes['site_name'] = row['site_name'].values[0]
    feature_to_be_updated.attributes['awh_stage'] = int(row['awh_stage'])
    feature_to_be_updated.attributes['awh_package'] = int(row['awh_package'])
    
   # #add this to the list of features to be updated
   features_to_be_added.append(feature_to_be_updated)

original_layers[0].edit_features(adds = features_to_be_added) 

 

I hope that's helpful.

0 Kudos