Select to view content in your preferred language

Determine if valid query returns records

760
2
Jump to solution
01-09-2014 06:08 AM
JohnDye
Deactivated User
What's the most elegant way to determine if a valid SQL Query (Attribute Query or Definition Query) returns no records?

I wrote this little function to just take a layer on disk, apply a definition query and update the name of the layer then pop it into the dataframe, but before I do that I want to validate that the definition query actually returned some records.
def createDerivedLayer(SourceLayer, UpdateName, DefinitionQuery = ""):     """     Creates a new in-memory Layer in the ArcMap Table of Contents derived from an     existing Layer on disk or in the ArcMap Table of Contents as referenced by the 'SourceLayer'     argument, and pastes the DerivedLayer into the Table of Contents.     Optionally applies a Definition Query to the newly Dervied Layer.     """     SourceLayer = arcpy.mapping.Layer(str(SourceLayer))     DerivedLayer = SourceLayer     DerivedLayer.name = str(UpdateName)     if DefinitionQuery:         DerivedLayer.definitionQuery = str(DefinitionQuery)     arcpy.mapping.AddLayer(df, DerivedLayer, "AUTO_ARRANGE")     arcpy.RefreshTOC()     arcpy.RefreshActiveView()


ICan anyone think of a way to see if 'DerivedLayer' has at least 1 records without having to go through the entire dataset? I think I'm just overthinking this...probably a very simply solution I'm overlooking
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Alum
This works on a Feature Layer.  See if it will work for you!


rowcount = int(str(arcpy.GetCount_management("DerivedLayer")))  if rowcount > 0:    'we have rows, do something with them! else:    'no rows in the DerivedLayer, I guess exit?    return 

View solution in original post

0 Kudos
2 Replies
JamesCrandall
MVP Alum
This works on a Feature Layer.  See if it will work for you!


rowcount = int(str(arcpy.GetCount_management("DerivedLayer")))  if rowcount > 0:    'we have rows, do something with them! else:    'no rows in the DerivedLayer, I guess exit?    return 
0 Kudos
JohnDye
Deactivated User
This works on a Feature Layer.  See if it will work for you!


rowcount = int(str(arcpy.GetCount_management("DerivedLayer")))

if rowcount > 0:
   'we have rows, do something with them!
else:
   'no rows in the DerivedLayer, I guess exit?
   return 


Exactly what I needed! Thanks James!!
0 Kudos