Hello. I have created an ArcGIS tool that runs through a join, selection by attributes, and various field calculations. The tool works, assuming the input for the tool is the actual feature class name, however, many of the feature classes that this tool will be used for have aliases. As such, when the layers are selected from ArcMap, the alias is passed as the parameter rather than the actual FC name, which breaks the tool.
I think my biggest problem here is that I don't know what I am looking for to address this. Essentially, I want the tool to ignore the alias and use the actual FC name. Is there a way to do this? I thought maybe something with the GetParameter portion of the tool that defines the first 2 variables would work, but I don't really know where to start looking. Any ideas?
Here is my code:
import arcpy, sys, string, os, subprocess
gdfc = arcpy.GetParameterAsText(0)
vwfc = arcpy.GetParameterAsText(1)
gca = vca = "Conservation_Area_Name"
gcn = vcn = "Conservation_Area_Number"
gn = vn = "Network"
gfi = "Field_ID"
gai = "Asset_ID"
gt1n = "Tier_1_System_Name"
gt1t = "Tier_1_System_Type"
gt2n = "Tier_2_Major_Subsystem_Name"
gt2t = "Tier_2_Major_Subsystem_Type"
gt3n = "Tier_3_Minor_Subsystem_Name"
gt3t = "Tier_3_Minor_Subsystem_Type"
gt4n = "Asset_Name"
vfi = "FieldID"
vai = "AssetID"
vt1n = "Tier_1_Name"
vt1t = "Tier_1_Type"
vt2n = "Tier_2_Name"
vt2t = "Tier_2_Type"
vt3n = "Tier_3_Name"
vt3t = "Tier_3_Type"
vt4n = "Tier_4_Name"
slct = gdfc+"."+gai+" IS NULL OR ("+gdfc+"."+gai+" <> "+vwfc+"."+vai+")"
try:
arcpy.AddJoin_management(gdfc, gfi, vwfc, vfi)
arcpy.SelectLayerByAttribute_management(gdfc, "NEW_SELECTION", slct)
arcpy.CalculateField_management(gdfc, gdfc+"."+gai, "["+vwfc+"."+vai+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gca, "["+vwfc+"."+vca+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gcn, "["+vwfc+"."+vcn+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gn, "["+vwfc+"."+vn+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt1n, "["+vwfc+"."+vt1n+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt1t, "["+vwfc+"."+vt1t+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt2n, "["+vwfc+"."+vt2n+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt2t, "["+vwfc+"."+vt2t+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt3n, "["+vwfc+"."+vt3n+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt3t, "["+vwfc+"."+vt3t+"]", "VB", "")
arcpy.CalculateField_management(gdfc, gdfc+"."+gt4n, "["+vwfc+"."+vt4n+"]", "VB", "")
except:
print arcpy.GetMessages()
arcpy.RemoveJoin_management(gdfc)
arcpy.SelectLayerByAttribute_management(gdfc, "CLEAR_SELECTION", "")
Solved! Go to Solution.
if the path is separated by \
path.split('\\')[-1]
will give the featureclassname
Are you using a 'featurelayer' as an input parameter to a tool? or are you trying to run this as just a script ?
If you are running it as a conventional tool with defined parameters and you select a layer in the dataframe/map, then you will have to examine the layer properties to get the actual source and its name
http://pro.arcgis.com/en/pro-app/arcpy/functions/layer-properties.htm
Gave this a try, and I am not sure what property I need to use for this. I tried using the layer name (nameString), but I got the same errors as before. This appears to just pull the name from the General tab in the layer properties, which is the same as the name being pulled as a parameter...rather than using the actual name as it appears in catalog.
For now, I am using this as an ArcGIS Tool, but will eventually need to automate the script to work against all layers in a database.
I will try the Describe function and see if I can get it work.
Thanks!
Perhaps describe catalogPath:
import arcpy
InputFeatureClass = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(InputFeatureClass)
arcpy.AddMessage("{}".format(desc.catalogPath))
# or
arcpy.AddMessage("{}".format(desc.dataElement.catalogPath))
A little closer, but not quite there. catalogPath appears to be pulling the entire path rather than just the feature class name.
Thanks!
if the path is separated by \
path.split('\\')[-1]
will give the featureclassname
In addition to what Dan is mentioning, you can also use the os module for this purpose:
import os
cat_path = r'C:\Esri\Telefonica\MUB_CHIA.gdb\MUB_CHIA\LIMITE_CHIA'
ws, fc_name = os.path.split(cat_path)
print(ws)
print(fc_name)
which will yield:
C:\Esri\Telefonica\MUB_CHIA.gdb\MUB_CHIA
LIMITE_CHIA
Awesome. Thanks a lot for the help!
The path.split seems to be getting me what I need out of the feature class to address the naming issue.