I am having an issue with a recent script/toolbox I have developed. My aim is to perform some tasks (copy features, then add them to the map then symbolise them). The majority of the tasks complete successfully, however, only the last feature layer adds to the map. I think the error is being generated as the "arcpy.SetParameter" function is not a global variable, therefore it only works on the last feature layer.
import arcpy
import os
from arcpy.mp import *
from arcpy import env
arcpy.env.addOutputsToMap = True
# User input feature
GDB_Store = arcpy.GetParameterAsText(0)
# Define the name of the project (long and clunky process -so could be improved)
aprox = arcpy.mp.ArcGISProject("CURRENT")
username = os.getlogin()
# Field Points that will be used
ISOs = r'C:\Users\{}\xxx\xxx\Collector Template.gdb\Artefact_Data\ISOs'.format(username)
New_Site_Boundary = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Heritage_Site_Boundary\Site_Boundary".format(username)
NEW_Feature_Polygon = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Features\NEW_Feature_Polygon".format(username)
NEW_Feature_Point = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Features\NEW_Feature_Point".format(username)
NEW_Feature_Line = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Features\NEW_Feature_Line".format(username)
Sample_Salvage_Squares = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Artefact_Data\Sample_Salvage_Squares".format(username)
New_Artefact_Point = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Artefact_Data\Artefact_Point".format(username)
New_Site_Boundary_Point = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Collector Template.gdb\Heritage_Site_Boundary\Site_Boundary_Points".format(username)
#set symbology paths
ISOs_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\ISOs.lyrx".format(username)
New_Site_Boundary_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\New Site Boundary.lyrx".format(username)
NEW_Feature_Polygon_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\NEW_Feature_Polygon.lyrx".format(username)
NEW_Feature_Point_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\New Feature Points.lyrx".format(username)
NEW_Feature_Line_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\NEW_Feature_Line.lyrx".format(username)
Sample_Salvage_Squares_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\Sample - Salvage Squares.lyrx".format(username)
New_Artefact_Point_Sym = r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\Artefact Point.lyrx".format(username)
New_Site_Boundary_Point_Sym =r"C:\Users\{}\xxx\xxx\Collector Template.gdb\Symbology\New Site Boundary Points.lyrx".format(username)
# Create static layer lists
static_layers = [ISOs, New_Site_Boundary, NEW_Feature_Polygon, NEW_Feature_Point, NEW_Feature_Line, Sample_Salvage_Squares, New_Artefact_Point, New_Site_Boundary_Point]
static_layers_sym = [ISOs_Sym, New_Site_Boundary_Sym, NEW_Feature_Polygon_Sym, NEW_Feature_Point_Sym, NEW_Feature_Line_Sym, Sample_Salvage_Squares_Sym, New_Artefact_Point_Sym, New_Site_Boundary_Point_Sym]
static_layer_name = ["ISOs", "New Site Boundary", "NEW Feature Polygon", "NEW Feature Point", "NEW Feature Line", "Sample Salvage Squares", "New Artefact Point", "New Site Boundary Point"]
static_layer_name_store = ["ISOs", "New_Site_Boundary", "NEW_Feature_Polygon", "NEW_Feature_Point", "NEW_Feature_Line", "Sample_Salvage_Squares", "New_Artefact_Point", "New_Site_Boundary_Point"]
# define model for static layers
def Model1(static_layers, static_layer_name, static_layer_name_store, static_layers_sym): # Model1
# Define location and name of feature layers
feature_path = GDB_Store + "\\" + static_layer_name_store
# Copy the feature layers
arcpy.management.CopyFeatures(in_features=static_layers, out_feature_class=feature_path, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)
# Process: Make Feature Layer (Make Feature Layer) (management)
layer_e = static_layer_name
arcpy.management.MakeFeatureLayer(in_features=feature_path, out_layer=layer_e, where_clause="", workspace="", field_info="OBJECTID OBJECTID VISIBLE NONE;SHAPE SHAPE VISIBLE NONE;Art_ID Art_ID VISIBLE NONE;Type Type VISIBLE NONE;Notes Notes VISIBLE NONE")
# Add layers to map
arcpy.SetParameter(1, static_layer_name)
# Process: Apply Symbology From Layer (Apply Symbology From Layer) (management)
arcpy.management.ApplySymbologyFromLayer(in_layer=layer_e, in_symbology_layer=static_layers_sym, symbology_fields=[], update_symbology="DEFAULT")[0]
# Run feature layers
if __name__ == '__main__':
with arcpy.EnvManager(scratchWorkspace=GDB_Store, workspace=GDB_Store):
for r, x, y, z in zip(static_layers, static_layer_name, static_layer_name_store, static_layers_sym):
Model1(r, x, y, z)
Solved! Go to Solution.
Hi Guys,
It seems that the issue was with only using one index as the SetParameter variable. I used a list of numbers so that each time the for loop iterates through the function, there is a new number assigned to the SetParameter variable, for example:
The first iteration is
SetParameter(1, xyz)
The second is
SetParameter(2, xyz)
etc
I also swapped ApplySymbologyFromLayer to SetParameterSymbology.
Please see the example code below, and hopefully it can be useful to someone!
feature_count = [14, 15, 16, 17, 18, 19, 20, 21]
# define model for feature layers
def feature_layer_tool(feature_layers, feature_layer_names,feature_layer_names_store, feature_layers_sym,feature_count): # Model
# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = True
# Process: Select Layer By Location (Select Layer By Location) (management)
layerA = arcpy.management.SelectLayerByLocation([feature_layers], overlap_type="WITHIN_A_DISTANCE", select_features=Survey_Area_shp, search_distance=Distance, selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT",)
# Process: Copy Features (Copy Features) (management)
layerB = GDB_Store + "\\" + feature_layer_names_store
arcpy.management.CopyFeatures(in_features=layerA, out_feature_class=layerB, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)
# Process: Make Feature Layer (Make Feature Layer) (management)
layerC = feature_layer_names
arcpy.management.MakeFeatureLayer(in_features=layerB, out_layer=layerC, where_clause="")
#add the layer to the map using set paramter
arcpy.SetParameter(feature_count, feature_layer_names)
# Process: Apply Symbology From Layer (Apply Symbology From Layer) (management)
arcpy.SetParameterSymbology(feature_count,feature_layers_sym)
What is the exact error it's raising? And it's not being raised on every iteration of the model?
Hi @MitchellAspinall ,
Try adding the layer using the addLayer method. Below is an example:
import arcpy
# Reference Project and Map
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("*")[0]
# Create Feature Layer
arcpy.AddMessage("Create feature layer")
featureLayer = arcpy.MakeFeatureLayer_management(r"C:\TEMP\Python\Test.gdb\Earthquakes", "EarthquakesLyr")
# Add Layer to Map
arcpy.AddMessage("Add layer to map")
layer = featureLayer.getOutput(0)
m.addLayer(layer)
Hi Guys,
It seems that the issue was with only using one index as the SetParameter variable. I used a list of numbers so that each time the for loop iterates through the function, there is a new number assigned to the SetParameter variable, for example:
The first iteration is
SetParameter(1, xyz)
The second is
SetParameter(2, xyz)
etc
I also swapped ApplySymbologyFromLayer to SetParameterSymbology.
Please see the example code below, and hopefully it can be useful to someone!
feature_count = [14, 15, 16, 17, 18, 19, 20, 21]
# define model for feature layers
def feature_layer_tool(feature_layers, feature_layer_names,feature_layer_names_store, feature_layers_sym,feature_count): # Model
# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = True
# Process: Select Layer By Location (Select Layer By Location) (management)
layerA = arcpy.management.SelectLayerByLocation([feature_layers], overlap_type="WITHIN_A_DISTANCE", select_features=Survey_Area_shp, search_distance=Distance, selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT",)
# Process: Copy Features (Copy Features) (management)
layerB = GDB_Store + "\\" + feature_layer_names_store
arcpy.management.CopyFeatures(in_features=layerA, out_feature_class=layerB, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)
# Process: Make Feature Layer (Make Feature Layer) (management)
layerC = feature_layer_names
arcpy.management.MakeFeatureLayer(in_features=layerB, out_layer=layerC, where_clause="")
#add the layer to the map using set paramter
arcpy.SetParameter(feature_count, feature_layer_names)
# Process: Apply Symbology From Layer (Apply Symbology From Layer) (management)
arcpy.SetParameterSymbology(feature_count,feature_layers_sym)