2

I need to get features in a File GeoDatabase feature class whose "fieldName" (string field) value matches the value of "strValue" (string variable). While I know there are features satisfying this condition, Make Feature Layer seems to return an empty layer (the "Describe" object of it has an empty "FIDSet" list). On the other hand search cursor with the same SQL expression successfully returns those features. I'm scratching my head what I am missing.

This is the where clause:

wc = arcpy.AddFieldDelimiters(FGDB_FC, fieldName) + ' = ' + '\'' + strValue + '\''

This worked as expected:

rows = arcpy.SearchCursor(FGDB_FC, wc)

But this returned a seemingly empty feature layer (dd.FID has no element):

arcpy.MakeFeatureLayer_management(FGDB_FC, "fl", wc)
dd = arcpy.Describe("fl")
print dd.FIDSet.split(';')
  • 1
    arcpy.SearchCursor has been superceded by arcpy.da.SearchCursor. String math has been superceded by using string.format(). You should always inlcude the ouput of a diagnotic print statement showing the final expression. It's difficult to determine what is wrong with fragmentary code samples. – Vince Feb 21 '18 at 14:43
2

I just experimented with this, and yes indeed it returns and empty FID set. But that doesn't mean that there is nothing in your layer. If you do this in commandline, you can see the results of the MakeFeatureLayer does use the where clause. If you want to see how many are returned you can use the GetCount tool.

wc = arcpy.AddFieldDelimiters(FGDB_FC, fieldName) + " = '%s'"%strValue
print wc #confirm the clause
lyr = arcpy.MakeFeatureLayer_management(FGDB_FC, "fl", wc)[0]
print arcpy.GetCount_management(lyr)[0]

You could also use your layer in a searchcursor.

with arcpy.da.SearchCursor(lyr,["*"]) as sc:
    for row in sc:
        print row[0] #prints the fid since I used * for the fields
  • Thanks for confirming this and looking deeper. Yes I confirmed with your cursor code and my data that the layer is NOT empty. I wonder where the disconnect is that the FIDSet of the layer's Describe object is empty... – xyk Feb 23 '18 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.