needing some help with python code lyr not defined

1494
10
04-30-2019 12:57 PM
by Anonymous User
Not applicable

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

Tags (1)
0 Kudos
10 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Devarsi Majumder‌,

Two questions:

  1. You indicate that you are having 'some issues' with the Python script above, and you ask for help. Can you please indicate what the issues are? What error messages do you get? We might be able to help if you indicate where things go wrong...
  2. Can you please modify the question to mark your script as Python code (there is an option to do so under the three dots), as this will provide syntax highlighting and render the script more readable.

Thanks in advance,

Egge-Jan

0 Kudos
by Anonymous User
Not applicable

where do i change  under the three dots  not sure where you are saying to change it also  the   error message that i get is  saying that  my  city.lyr is not defined  when i have  it clearly defined above  thanks

0 Kudos
Egge-Jan_Pollé
MVP Regular Contributor

About marking the syntax highlighting:

  • edit your answer and select the python code
  • use the option below to mark the syntax - sorry, my UI is in Dutch. in English it will read something like 'Mark syntax', I suppose.

0 Kudos
by Anonymous User
Not applicable

i marked the  lines that are in question in bold. it says im missing parentheses syntax is wrong.

0 Kudos
DanPatterson_Retired
MVP Emeritus

In your script editor, what line is causing the error?  

Syntax highlighting won't help if there is an error in the code.  Post the error message

0 Kudos
DanPatterson_Retired
MVP Emeritus

Is this in ArcGIS Pro and for python 3?

If so, print statements now go in brackets

print "hello"  # old 2.7

print("hello") # new python 3.*

0 Kudos
by Anonymous User
Not applicable

the issues that  i am having is that it i ssaying that my  city.lyr is not defined  when  we have  defined  from waht  feature class to use also 

0 Kudos
DanPatterson_Retired
MVP Emeritus

Code formatting will also provide line numbers 

/blogs/dan_patterson/2016/08/14/script-formatting 

by Anonymous User
Not applicable

i see   where your saying  for  code formatting but  it say s that my print syntax is wrong due to missing parthenses.  is there a way you can help with this?

0 Kudos