AnsweredAssumed Answered

needing some help  with python code lyr not defined

Question asked by Devarsi.Majumder on Apr 30, 2019
Latest reply on May 17, 2019 by Dan_Patterson

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 == "City Points (2010)":
        city_lyr = lyr
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageRow = mxd.dataDrivenPages.pageRow
    pageName =
    DDP_PageName = pageRow.getValue(pageName)
    for lyr in arcpy.mapping.ListLayers(mxd, "Elk*", df):
        if == "ElkUnitsSDE - match":
            unit_lyr = lyr
            arcpy.SelectLayerByAttribute_management(unit_lyr, 'NEW_SELECTION', '1=1')
        if == "ElkUnitsBoundaryRds":
             unit_boundary_lyr = lyr
             unit_boundary_lyr.definitionQuery = "Units Like " + "'%" + DDP_PageName + "%'"


    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')
        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