I have this script below. When the three arguments are set as variables and I run the script in the ArcMap Python window to test it it runs perfectly. I created a tool for this script. In the parameters for the tool properties I have the Data Types for the 3 arguments are set as "SQL expressions" and all the data types for the feature layers created are "Feature Layers" with a "Type: Derived" and "Direction: Output".
First problem: when I run the tool with the script as is i get this error 'RuntimeError: LayerObject: Set attribute showLabels does not exist'. I remove the code block that applies the labels then I get this error 'ValueError: Unknown value for Extent argument'. Running the script in the mxd both code blocks work perfectly.
Second Problem: I remove both code blocks from the script and run the tool and nothing happens. No errors. Just doesn't add layers.
# Import modules
import os, arcpy
# Set arguments
pws = arcpy.GetParameterAsText(0)
ass_pws = arcpy.GetParameterAsText(1)
mtr_recpws = arcpy.GetParameterAsText(2)
# Set variables
sources = r"V:\Source Water Protection\SWAP Model\SWAP.code_newrasters\GIS\SWAP.gdb\sources" # Will need to change if used outside of SWPP staff because of directory permissions. "Sources" feature class is in WATER_FACILITY.gdb
assessment = r"V:\Source Water Protection\SWAP Model\SWAP.code_newrasters\GIS\SWAP.gdb\assessment_areas" # Will need to change if used outside of SWPP staff because of directory permissions. "Assessments" feature class not in WATER_FACILITY.gdb..
meter = r"V:\WATER_FACILITY\WATER_FACILITY.gdb\MASTER_METER"
files = ["BACTI", "DBP", "DBP_STAGE2", "OFFICE", "PRV", "PUMP_STATION", "SERVICE_AREA_COMBO", "TANK", "VALVE_MISCELLANEOUS", "WTP"] # list all names used for feature classes ans .lyr files.
layers = ["Bacti", "DBP", "DBP Stage 2", "Office", "PRV", "Pump Station", "Service Area", "Tank","Valve", "Water Treatment Plant"]
src_layer = "Source"
ass_layer = "Assessment Area"
mtr_layer = "Master Meter"
src_symb = r"V:\Source Water Protection\PWS_ID_Tool\SOURCE.lyr"
ass_symb = r"V:\Source Water Protection\PWS_ID_Tool\ASSESSMENT.lyr"
mtr_symb = r"V:\Source Water Protection\PWS_ID_Tool\MASTER_METER.lyr"
root = r"V:\WATER_FACILITY\WATER_FACILITY.gdb"
symRoot = r"V:\Source Water Protection\PWS_ID_Tool"
# Create directory paths, make feature layers, and apply symbologies.
for i, file in enumerate(files):
path = os.path.join(root, files) # Create directory path for featureclasses
symPath = os.path.join(symRoot, files + ".lyr") # Create directory path for .lyr files
arcpy.MakeFeatureLayer_management(paths, layers, pws) # Make all the layers from "files" list
arcpy.ApplySymbologyFromLayer_management(layers, symPaths) # Apply the symbologies from "symNames" list.
arcpy.MakeFeatureLayer_management(assessment, ass_layer, ass_pws)
arcpy.MakeFeatureLayer_management(meter, mtr_layer, mtr_recpws)
arcpy.MakeFeatureLayer_management(sources, src_layer, pws) # Ordered to have the "Source" layer in the 0 position.
# Apply label
mxd = arcpy.mapping.MapDocument("CURRENT") # Reference to current open map document. Could insert path.
layer = arcpy.mapping.ListLayers(mxd, "") # Indexing for first layer in table of contents. "Make feature layers" functions ordered so "Source" is in 0 position.
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
lblclass.expression = '"%s" & [SOURCE_ID] & "%s"' % ("<CLR red='255'><FNT size = '12'>", "</FNT></CLR>")
layer.showLabels = True
#Apply extent from "Source" layer
mxd = arcpy.mapping.MapDocument("CURRENT") # Using current map, can also use a path to an mxd here
df = arcpy.mapping.ListDataFrames(mxd)
lyr = arcpy.mapping.ListLayers(mxd, "", df)
ext = lyr.getExtent()
df.extent = ext
Message was edited by: Brad Jones
I think one of your file paths is getting truncated and just the first letter of the file is being used for a tool input, which isn't a valid dataset. Did you get which line the error occured on? You might want to debug using some print statements to make sure all your tool inputs are the values you think they are.
I would use print statements for every parameters being used in lines 29-32 to make sure they are all correct, since almost all the rest of your values are hard coded into your script.