arcpy.Exists & arcpy.ListFeatureClasses and wildcards

4590
8
09-09-2015 09:04 AM
BradJones
New Contributor III

I have a gdb with around 200 feature classes (waterlines).  I working on a tool that will add the featureclass desired base on an ID number. the featureclasses are named "WATERLINE_<ID NUMBER>_<NAME>".  Example: WATERLINE_123_SPRINGFIELD. Since the only parameter is entered as a 3 digit ID number (string)  I use a wildcard query with arcpy.ListFeatureClasses then use arcpy.Exists to add it to the map.  Some systems don't have waterline data.   This approach isn't working.  If I hardcode the featureclass name into arcpy.Exists it works.  Any ideas are appreciated.

# Import modules
import arcpy

# Set overwrite option
from arcpy import env
env.overwriteOutput = True

# Variables
pws = arcpy.GetParameterAsText(0)

waterlines = "WATERLINE_" + pws + "_*"
wl_layer = "Waterlines " + pws
wl_symb = r"G:\Source Water Protection\PWS_ID_Tool\WATERLINES.lyr"


# Add waterlines
env.workspace = r"G:\WATER_FACILITY\WATERLINES.gdb"
wlFclass = arcpy.ListFeatureClasses(waterlines)
arcpy.AddMessage(wlFclass)  # Print/message statement for results dialog box. Confirms wlFclass is there.
if arcpy.Exists(wlFclass):  # This where it fails.
    arcpy.AddMessage("Waterline data exist for this PWS.")
    arcpy.MakeFeatureLayer_management(waterlines, wl_layer)
    arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb)
    newLayer = arcpy.mapping.Layer(wl_layer)
    arcpy.mapping.AddLayer(df, newLayer, "TOP")
else:
    arcpy.AddWarning("No waterline data exist for this PWS.")

Tool results dialog box:

Executing: waterlinestest 465

Start Time: Wed Sep 09 10:36:53 2015

Running script waterlinestest...

[u'WATERLINE_123_SPRINGFIELD']

No waterline data exist for this PWS.

Completed script waterlinestest...

Succeeded at Wed Sep 09 10:37:27 2015 (Elapsed Time: 33.95 seconds)

5 (Elapsed Time: 33.95 seconds)

0 Kudos
8 Replies
WesMiller
Regular Contributor III

You are passing a list where you need a path to "Exists"

import arcpy, os 
workspace =  r"G:\WATER_FACILITY\WATERLINES.gdb"  
wlFclass = arcpy.ListFeatureClasses(waterlines)
if arcpy.Exists(os.path.join(workspace,wlFclass[0])):
  print 'yes'
BradJones
New Contributor III

this is the code block that works. I needed the for loop to convert the unicode list of one featureclass to a 'regular' string.  thanks for pointing out my stupid mistake.

# Import modules
import arcpy
import os


# Set overwrite option
from arcpy import env
env.overwriteOutput = True


pws = arcpy.GetParameterAsText(0)


waterlines = "WATERLINE_" + pws + "_*"
wl_layer = "Waterlines " + pws
wl_symb = r"G:\Source Water Protection\PWS_ID_Tool\WATERLINES.lyr"


# Set current mxd and dataframe.
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]


# Add waterlines
env.workspace = r"G:\WATER_FACILITY\WATERLINES.gdb"
wlFclass = arcpy.ListFeatureClasses(waterlines)
arcpy.AddMessage(wlFclass)
for wl in wlFclass:
  wlPath = os.path.join(env.workspace,wl)
  if arcpy.Exists(wlPath):
    arcpy.AddMessage("Waterline data exist for this PWS.")
    arcpy.MakeFeatureLayer_management(wlPath, wl_layer)
    arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb)
    newLayer = arcpy.mapping.Layer(wl_layer)
    arcpy.mapping.AddLayer(df, newLayer, "TOP")
  else:
    arcpy.AddWarning("No waterline data exist for this PWS.")
0 Kudos
WesMiller
Regular Contributor III

Glad to hear you got it working

0 Kudos
FreddieGibson
Occasional Contributor III

If you're getting the names of the feature classes via arcpy.ListFeatureClasses is there a reason why you'd need to check if they exist? The ListFeatureClasses method would only list the feature classes if they existed.

BradJones
New Contributor III

I need ListFeaturecClasses because i'm working from a wildcard querry.  I can't use a wildcard with Exists.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I agree you don't need Exists. You could check the length of the returned list of feature classes to see if any match.

len(wlFclass)

MichaelSouffront
New Contributor III

What about replacing the exists function in line 20 with an "if statement". The listfeatures function returns a list and then you could do something like "if len(list) == 1:" or similar

BradJones
New Contributor III

Yes, you all are correct.  I didn't think about that!

0 Kudos