edit_features to add FeatureLayer records to hosted table

406
5
07-09-2021 06:50 AM
KyleGallagher16
New Contributor III

I am attempting to query existing hosted FeatureLayer features and push them to an existing hosted table using the edit_features function and adds parameter. The FeatureLayer and table have the same schemas with the exception of geometry not present in the table.

The code executes all the way through but no new records are added to the hosted table. There are only 64 records being added so there is no limitation issue. Is edit_features the correct function to use or should append be used instead?

#pacpInspects is existing hosted FeatureLayer
#histInspects is existing hosted table
#existPipes is a list of PipeIDs
numExistPipes = 64
records_to_add = []
for x in range(0,numExistPipes):
pipe = existPipes[x]
pipeQuery = pacpInspects.query(where="""PipeID = '%s'"""% pipe, out_fields='*')
feature_to_be_added = deepcopy(pipeQuery.features[0])
records_to_add.append(feature_to_be_added)
histInspects.edit_features(adds=records_to_add)

 

0 Kudos
5 Replies
jcarlson
MVP Notable Contributor

Instead of iterating through and building a list, why not make a single query? It will probably work better if the adds parameter is a FeatureSet.

Using your Pipe ID list, construct a single comma-delimited string. If the ID field being queried is a string type, you'll need to add quotation marks, too.

 

# List of ints to string of ints: [1,2,3,4] → '1,2,3,4'
id_list = ','.join([str(i) for i in existPipes])

# List of ints to string of strings: [1,2,3,4] → '"1","2","3","4"'
id_list = ','.join([f'"{str(i)}"' for i in existPipes])

 

If existPipes is a list of strings, not ints, just drop the str( ) and leave the i by itself.

With this string, you can use IN(...) in the where parameter.

 

records_to_add = pacpInspects.query(
    where = f'PipeID IN({id_list})',
    out_fields = '*'
)

 

That should give you the features you need in a single query, which returns a single FeatureSet. Then try the edit_features with that instead.

- Josh Carlson
Kendall County GIS
0 Kudos
KyleGallagher16
New Contributor III

Thanks, Josh. That runs all the way through, I outputted the results of the FeatureSet and it looks good, but the records are not getting added to the table. I'm wondering if it has anything to do with features trying to be added to a table and whether the geometry piece is getting in the way. 

0 Kudos
jcarlson
MVP Notable Contributor

That's interesting. Assign the edit_features line to its own variable so that you can inspect it.

add_feats = histInsects.edit_features(adds=records_to_add)

print(add_feats['addResults'])

There should be some message in there about whether or not it was successful.

- Josh Carlson
Kendall County GIS
0 Kudos
KyleGallagher16
New Contributor III

Here is the result:

[{'objectId': -1, 'uniqueId': -1, 'globalId': None, 'success': False, 'error': {'code': 1000, 'description': 'Object reference not set to an instance of an object.'}}]

I found this post that may or may not be related. Seems like geometry may be a factor here.

https://github.com/Esri/arcgis-python-api/issues/600 

0 Kudos
KyleGallagher16
New Contributor III

I was able to make this work after simply adding the return_geometry=False parameter to the pipeQuery.

records_to_add = pacpInspects.query(where=f'PipeID IN({id_list})',out_fields = '*',return_geometry=False)

 

0 Kudos