Select to view content in your preferred language

How to display feature layer from Python Toolbox?

1608
2
Jump to solution
01-19-2023 10:41 AM
Labels (3)
Davec43
Regular Contributor

I have a Search Cursor that operates at the end of the model. It works as intended but does not add the layer it ends up creating to the Map. I've tried connecting it to the "Collect Values" tool with Paramaters and Display selected, and it still won't work.

Paramaters.png

Here's the script:

 

 

import os
import sys
import arcpy
Filter_Layer = arcpy.GetParameterAsText(0) # Feature Layer
Search_Layer = arcpy.GetParameterAsText(1) # Feature Layer
Workspace = arcpy.GetParameterAsText(2) # Workspace

FP1 = os.path.abspath(Workspace)
events = arcpy.da.SearchCursor(Filter_Layer, ['Event'])
unique_events = list(set([row[0] for row in events]))
sql = "Event IN(" + ','.join(map(str, unique_events)) + ")"
arcpy.management.SelectLayerByAttribute(Search_Layer, "NEW_SELECTION", sql, None)
arcpy.management.CopyFeatures(Search_Layer, fr"{FP1}\RTL_Filtered", '', None, None, None)

 

 

 

0 Kudos
1 Solution

Accepted Solutions
DavidSolari
MVP Regular Contributor

Try adding a derived output parameter to your tool, then using the "SetParameter" or "SetParameterAsText" function in your script to put the output features into that parameter. Tools don't output data unless they have an output parameter configured.

Also for the future, what you're writing is a script tool in a normal toolbox, a Python toolbox is a different beast entirely.

View solution in original post

2 Replies
DavidSolari
MVP Regular Contributor

Try adding a derived output parameter to your tool, then using the "SetParameter" or "SetParameterAsText" function in your script to put the output features into that parameter. Tools don't output data unless they have an output parameter configured.

Also for the future, what you're writing is a script tool in a normal toolbox, a Python toolbox is a different beast entirely.

Davec43
Regular Contributor

It worked thank you!

Added a new paramater (output) and then, 

arcpy.SetParameterAsText(3, outputFile)

 

 

mport os
import sys
import arcpy
Filter_Layer = arcpy.GetParameterAsText(0) # Feature Layer
Search_Layer = arcpy.GetParameterAsText(1) # Feature Layer
Workspace = arcpy.GetParameterAsText(2) # Workspace
FP1 = os.path.abspath(Workspace)
outputFile =  fr"{FP1}\RTL_Filtered"

events = arcpy.da.SearchCursor(Filter_Layer, ['Event'])
unique_events = list(set([row[0] for row in events]))
sql = "Event IN(" + ','.join(map(str, unique_events)) + ")"
arcpy.management.SelectLayerByAttribute(Search_Layer, "NEW_SELECTION", sql, None)
arcpy.management.CopyFeatures(Search_Layer, fr"{FP1}\RTL_Filtered", '', None, None, None)

arcpy.SetParameterAsText(3, outputFile)

 

0 Kudos