Create a jointed feature view using Python API

338
1
02-09-2024 11:59 AM
Labels (1)
PhilippeVaillantA
New Contributor

I am using the join_features function to created a joined feature layer, but I cannot manage to make the output a view rather than a new hosted feature layer. Is there a parameter I need to add to create a feature view ? I am able to do so with the GUI but not with the API.

Below my code for the jointed feature layer :

results = join_features(
target_layer=targetLayer,
join_layer=joinLayer,
join_operation="joinOneToOne",
attribute_relationship=[
{
"targetField": "unique_id",
"operator": "equal",
"joinField": "unique_id",
}
],
# Outputs results as a hosted feature layer.
output_name="New name",
)
 
Thanks.
0 Kudos
1 Reply
KimOllivier
Occasional Contributor III

No, cannot be done. Not supported. Anyway it only works for a demo, not large datasets.

I find I have to do my own 'join' using dictionaries. This turns out to be faster, more reliable and more scaleable. One benefit is that you do not have to have the data in the same database because you are using Python structures as an intermediary. You do get into the weeds of cursors, dictionaries and trapping errors a bit. Actually with Pandas now a default package installed you may be able to streamline a join. Must look into that.

My legacy workflow is like this:

1. Describe the two fc or tables and get the schema for the table you wish to join, plus the keys. Or use arcpy.ListFields()

2. Choose the foreign key and user-fields you wish to join. (Make a list of names)

3. Add the new fields to the target featureclass, or perhaps a copy that you want to create.

4. Use a SearchCursor to create a dictionary of the table data, with the foreign key as the dictionary key and the attributes as a tuple. You will not be needing the shape field, OBJECTID, or dynamic fields for area, perimeter.

5.Open an UpdateCursor on your target featureclass with a list of the field in the tuple. Iterate through the table and update the new empty fields using the dictionary and key. Best to use dict.get(key, None) to avoid missing keys.

It all sounds a bit of work, but once you have the pattern it's easy to adapt to the next project. It is really really FAST. If you have billions of records you can always do some partitioning, but you should be OK with millions of records.

There is an example of a table join in my Python Tips talk

 

0 Kudos