Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Determine if valid query returns records

$
0
0
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.
Code:

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

Viewing all articles
Browse latest Browse all 2485

Trending Articles