I am working on a script that iterates through a grid feature class, zooms/pans to the next square of the grid and outputs a PDF, compiling them all into one PDF to be printed into an atlas booklet. This is an internal script that has been rewritten many times to accommodate different uses for different departments and agencies. For this one, we want to show text on the pages that don't show the relevant feature class: "This page does not contain any [feature class] features". I found a way to do this by grabbing the extent of the map frame camera, creating a polygon of it, selecting the features that intersect and then adding the text to the pages that do not have a selection.
I'm thinking there's at least two ways this process could be shortened:
Currently, the {Layer}.visible() Boolean seems to only check if the layer is toggled visible in the table of contents, so I'm not sure what it might be called. {Layer}.isVisible() might be too confusing.
Here's what I ended up coming up with. It's not the prettiest, but it gets the job done.
# Add NO ARTERIAL TEXT for pages without arterial classifications...
mainMF = mf.map
layer_name = "ArterialClass"
arterialLyr = next((lyr for lyr in mainMF.listLayers() if lyr.name == layer_name), None)
# GET MAPFRAME EXTENT AND CREATE TEMPORARY POLYGON FEATURE FROM IT
mfExtent = mf.camera.getExtent()
extentPoly = mfExtent.polygon
arcpy.env.workspace = 'memory'
arcpy.env.overwriteOutput = True
if arcpy.Exists(r'memory\featLyr'):
print('featLyr exists... deleting...')
arcpy.management.DeleteFeatures(r'memory\featLyr')
# CREATE TEMPORARY ARTERIAL CLASS FEATURE LAYER IN MEMORY
print('creating featLyr')
featLyr = arcpy.MakeFeatureLayer_management(arterialLyr, r'memory\featLyr')
# SELECT ANY ARTERIAL FEATURES THAT INTERSECT WITH THE MAPFRAME POLYGON
arcpy.SelectLayerByLocation_management(featLyr, "INTERSECT", select_features=extentPoly, selection_type="NEW_SELECTION")
# CHECK IF ANY FEATURES ARE SELECTED
# IF ANY ARE SELECTED, TEXT ELEMENT IS MODIFIED TO SHOW THAT THE PAGE SHOWS NO ARTERIAL CLASS FEATURES
selected_count = int(arcpy.management.GetCount(featLyr)[0])
if selected_count > 0:
print(f"{selected_count} feature(s) visible in the camera's current extent.")
else:
print(f"{selected_count} features visible in the camera's current extent.")
print(f'ArterialClass is not visible within map frame extent')
print("Page " + str(page_number) + " is a no arterial classification page.")
for elm in lyt.listElements("TEXT_ELEMENT", "*"):
if elm.name == "NoArtText":
elm.text = "THERE ARE NO \r ARTERIAL CLASSIFICATIONS \r ON THIS PAGE"
print('clearing selection')
# CLEAR SELECTION AND DELETE TEMPORARY FEATURES
arcpy.SelectLayerByAttribute_management(featLyr, selection_type="CLEAR_SELECTION")
if arcpy.Exists(r'memory\featLyr'):
print('temp exists... deleting...')
arcpy.management.DeleteFeatures(r'memory\featLyr')
if arcpy.Exists(featLyr):
print('featLyr exists... deleting...')
arcpy.management.DeleteFeatures(featLyr)
arcpy.ClearWorkspaceCache_management('memory')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.