Select to view content in your preferred language

SearchCursor within Modelbuilder not filtering

1141
8
04-03-2023 07:32 AM
Davec43
Regular Contributor

I have a working model that filters data down to a few layer files. I also have a working python script that will take those filtered layers and further filter them. As of now I have to run the model and then run the script, I'm trying to merge the processes for efficiency. When I drag the script into Modelbuilder or try to create another model to do a model within a model, the process will run except the searchcursor won't do any "searching."

Heres the Script

import os
import sys
import arcpy
Workspace = arcpy.GetParameterAsText(0) # Workspace
Points_Filtered = arcpy.GetParameterAsText(1) # Feature Layer
Event_id = arcpy.GetParameterAsText(2) # Event_ID
RTL_Unfiltered = arcpy.GetParameterAsText(3) # Feature Layer
FP1 = os.path.abspath(Workspace)
outputFile =  fr"{FP1}\RTL_Filtered"
events = arcpy.da.SearchCursor(Points_Filtered, Event_id)
unique_events = list(set([row[0] for row in events]))
sql = "event_id IN(" + ','.join(map(str, unique_events)) + ")"
arcpy.management.SelectLayerByAttribute(RTL_Unfiltered, "NEW_SELECTION", sql, None)
arcpy.management.CopyFeatures(RTL_Unfiltered, outputFile, '', None, None, None)
arcpy.SetParameterAsText(4, outputFile)
0 Kudos
8 Replies
DuncanHornby
MVP Notable Contributor

Are you saying in the image you have uploaded that "SearchCursor" is a model? Also the inputs to "SearchCursor" are all grey, they have not been set, so not surprising that "SearchCursor" is not running.

 

There is little point setting them as Parameters if you intend to run the model within modelbuilder.

0 Kudos
Davec43
Regular Contributor

Its just a picture to show that portion of the process since my model is a little more complex. The model stays gray since it requires user input when ran even when all the parameter's are defined/connected.

The process runs just the searchcursor portion of the script doesn't work when inserted into the model.

0 Kudos
DuncanHornby
MVP Notable Contributor

OK, without seeing what "SearchCursor" is no one can answer this question. I suspect the inputs are the wrong data type that they are feeding into. Share that portion of the model with some sample data?

0 Kudos
Davec43
Regular Contributor
import os
import sys
import arcpy
Workspace = arcpy.GetParameterAsText(0) # Workspace
Points_Filtered = arcpy.GetParameterAsText(1) # Feature Layer
Event_id = arcpy.GetParameterAsText(2) # Event_ID
RTL_Unfiltered = arcpy.GetParameterAsText(3) # Feature Layer
FP1 = os.path.abspath(Workspace)
outputFile =  fr"{FP1}\RTL_Filtered"
events = arcpy.da.SearchCursor(Points_Filtered, Event_id)
unique_events = list(set([row[0] for row in events]))
sql = "event_id IN(" + ','.join(map(str, unique_events)) + ")"
arcpy.management.SelectLayerByAttribute(RTL_Unfiltered, "NEW_SELECTION", sql, None)
arcpy.management.CopyFeatures(RTL_Unfiltered, outputFile, '', None, None, None)
arcpy.SetParameterAsText(4, outputFile)
0 Kudos
DuncanHornby
MVP Notable Contributor

You Script tool (so not model) code takes 4 parameters, in your image you are not connecting in Event_id, the 3rd of 4.  

Event_id = arcpy.GetParameterAsText(2) # Event_ID

 

Which suggests your current parameters are potentially linking to the wrong input parameter as well as not providing all four required parameters?

0 Kudos
Davec43
Regular Contributor

It's set in the paramaters. Do I need to change the Script?

0 Kudos
DuncanHornby
MVP Notable Contributor

You have set the parameters to the model variables but you have not set the model variables, I know this as they are grey. Right click on them, open them in your model and set them to the actual dataset.

Also looking at your code you have hard wired the SQL where clause to event_id, so why offer it up as a parameter?

0 Kudos
RhettZufelt
MVP Notable Contributor

Are you actually entering the the field name for Event_id on line 6?  That is what you are telling the search cursor on line 10. 

First input is dataset, second is field list.

R_

0 Kudos