Filtering a layer using another layers attributes with python?

432
4
07-19-2022 10:16 AM
Labels (2)
Davec43
Occasional Contributor

I've been trying to use Python to automate the geoprocessing I do for a product I have to create weekly, I'm on the last step. One point layer is the "start position" and the other point layer is tracks. The start position and tracks aren't spatially connected but they do share an "event" attribute. Usually I filter down the point layer (tracks) and then pull the events column that I need and do a data query to parse the point layer "start position" down to what I need. How would I do this with Python? Or is there a Geoprocessing tool or tools that I could use?

0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor

Seems to me a good approach is to get the event ID from your selected set into a variable using a cursor, use that to build a SQL expression, and run Select Layer By Attributes to select the points in the other feature layer. If you have to do this many times, be sure and index the datasets by your event attribute so the selections will be faster.

Davec43
Occasional Contributor

with arcpy.da.SearchCursor(layer name, ["Event"]) as cur:
for row in cursor:

I keep getting NameError: name 'layer name' is not defined?

0 Kudos
curtvprice
MVP Esteemed Contributor

"layer name" needs to be either a string literal or a string variable containing the name of an existing layer or the path to a dataset.

It's pretty easy to grab the first one this way

 

lyr = "name_of_your_layer"
event_id = arcpy.da.SearchCursor(lyr, ['Event']).next()[0]

 

Davec43
Occasional Contributor

Thanks for the help! I've altered it and now it prints all the events

 

lyr = "Heatmap_Filtered"
events = arcpy.da.SearchCursor(lyr, ['Event'])
unique_events = list(set([row[0] for row in events]))
unique_events
print(unique_events)

 

Where I'm having problem with now is creating the SQL using AddField Delmiters. Where is FC located?

 

where_clause = """ {0} = '{1}' """.format(arcpy.AddFieldDelimiters(fc, 'Heatmap_Filtered'), 'Unique_events')

 

Then I'd assume I'd just drop the sql expression in select by attribute?

arcpy.management.SelectLayerByAttribute("RTL", "NEW_SELECTION", "Event” “sql_exp”)

 

0 Kudos