i am having some issues running a simple python script alongside my data driven pages map. I am trying to clip out some roads and selected names from the outside of my layer scope i have written the code down below could anyone help me with this the lines in question is highlighted in bold. still new to python if someone can help that would be great. thanks
import arcpy
# Set overwrite option
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument(r"W:\Information Technology\GIS\Projects\Wildlife\BigGame\PermitMaps\BigGameUnitsDDP_ElkLandscape.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "City*", df):
if lyr.name == "City Points (2010)":
city_lyr = lyr
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
pageRow = mxd.dataDrivenPages.pageRow
pageName = mxd.dataDrivenPages.pageNameField.name
DDP_PageName = pageRow.getValue(pageName)
for lyr in arcpy.mapping.ListLayers(mxd, "Elk*", df):
if lyr.name == "ElkUnitsSDE - match":
unit_lyr = lyr
arcpy.SelectLayerByAttribute_management(unit_lyr, 'NEW_SELECTION', '1=1')
if lyr.name == "ElkUnitsBoundaryRds":
unit_boundary_lyr = lyr
unit_boundary_lyr.definitionQuery = "Units Like " + "'%" + DDP_PageName + "%'"
print city_lyr.name
print unit_lyr.name
arcpy.SelectLayerByLocation_management(city_lyr, 'INTERSECT', unit_lyr, '1 Mile', 'NEW_SELECTION', 'NOT_INVERT')
selcount = int(arcpy.GetCount_management(city_lyr)[0])
print selcount
if selcount == 0:
print ('No city points selected')
else:
arcpy.CopyFeatures_management(city_lyr, r"W:\Information Technology\GIS\Projects\Wildlife\BigGame\PermitMaps\Python\BigGameUnits.gdb\Cities_Sel")
city_lyr.replaceDataSource(r'W:\Information Technology\GIS\Projects\Wildlife\BigGame\PermitMaps\Python\BigGameUnits.gdb', 'FILEGDB_WORKSPACE', 'Cities_Sel')
for lyr in arcpy.mapping.ListLayers(mxd, '*', df):
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
arcpy.mapping.ExportToPDF(mxd, r"W:\Information Technology\GIS\Projects\Wildlife\BigGame\PermitMaps\2019 Permit Maps\Elk" + "_" + str(DDP_PageName) + ".pdf", resolution=150, georef_info="false", jpeg_compression_quality=75, image_quality="NORMAL")
city_lyr.replaceDataSource(r'Database Connections\GIS_WEB.sde', 'SDE_WORKSPACE', 'GIS_WEB.SDE.CityPoints')
del mxd, df, city_lyr, unit_lyr, unit_boundary_lyr
when it says "City_Lyr is not defined", what this means is, you have not yet "defined" your city layer
This is because in this line of code:
for lyr in arcpy.mapping.ListLayers(mxd, "City*", df):
if lyr.name == "City Points (2010)":
city_lyr = lyr
it never actually gets to run the line of code:
city_lyr = lyr
Which is where you try to define it. The reason for that, I am not so sure, but the "If" statement is somehow not letting any of your layers get defined as the city lyr.
Same issue regarding your other layer.