3

I Cannot find what this error means or what could be causing it? Script worked until I added the lines under "Set map label to containing quad name". My environment is ArcGIS 10.1 SP1, Windows 7. Here is my code:

import arcpy

#Define I/O and workspaces
arcpy.env.workspace = "FolderPath"
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("MXDPath.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
outpath = arcpy.GetParameterAsText(2)

def getAuthCode(lon):
    if lon >= -126.0 and lon < -120.0:
        authCode = 26910 #UTM Zone 10N
    elif lon >= -120.0 and lon < -114.0:
        authCode = 26911 #UTM Zone 11N
    elif lon >= -114.0 and lon < -108.0:
        authCode = 26912 #UTM Zone 12N
    elif lon >= -108.0 and lon < -102.0:
        authCode = 26913 #UTM Zone 13N
    elif lon >= -102.0 and lon < -96.0:
        authCode = 26914 #UTM Zone 14N
    elif lon >= -96.0 and lon < -90.0:
        authCode = 26915 #UTM Zone 15N
    elif lon >= -90.0 and lon < -84.0:
        authCode = 26916 #UTM Zone 16N
    elif lon >= -84.0 and lon < -78.0:
        authCode = 26917 #UTM Zone 17N
    elif lon >= -78.0 and lon < -72.0:
        authCode = 26918 #UTM Zone 18N
    elif lon >= -72.0 and lon < -66.0:
        authCode = 26919 #UTM Zone 19N
    else:
        authCode = 4326 # WGS84
    return authCode

#Create extent/spatial referenece objects
lat = arcpy.GetParameterAsText(0)
lon = arcpy.GetParameterAsText(1)
WGS84 = arcpy.SpatialReference(4326) #Creates spatial reference variable to switch back to later
UTM = arcpy.SpatialReference(getAuthCode(lon))
df.spatialReference = WGS84 #Set spatial reference to WGS84
resetPT = arcpy.Extent(-97.747957, 30.280553, -97.747957, 30.280553)

#Set map label to containing quad name
point = arcpy.Point(lon, lat)
ptGeo = arcpy.PointGeometry(point, WGS84)
quadPath = "LayerPath.lyr"
quadLyr = arcpy.mapping.Layer(quadPath) #arcpy.mapping.ListLayers(mxd)[0]
selectedLyr = arcpy.SelectLayerByLocation_management(quadLyr, "INTERSECT", ptGeo)
cursor = arcpy.da.SearchCursor(selectedLyr, "QUAD_NAME")
quadName = ""
for row in cursor:
    quadName = str(row)
label = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Label")[0]
label.text = "NWI Map" + "\n" + "Quad: " + quadName[3:-3] + ", TX"
label.elementPositionX = 0.3
label.elementPositionY = 0.3

#Pans to centerpoint and sets scale to 1:12000
extent = arcpy.Extent(lon, lat, lon, lat)
df.panToExtent(extent)
df.spatialReference = UTM #Temporarily set spatial reference to correct UTM zone
df.scale = 12000
arcpy.RefreshActiveView()

#Export map to ES# folder
arcpy.mapping.ExportToPDF(mxd, outpath, resolution=300) #exportToPDF

#Reset extent/spatial reference
df.spatialReference = WGS84 
df.panToExtent(resetPT)
arcpy.RefreshActiveView()

mxd.save()
  • 1
    Are you running this in the Python Window inside ArcMap or as a Script tool? – MrBubbles May 9 '16 at 23:49
  • I'm running it as a script tool – cevondanady May 9 '16 at 23:51
  • Is there an error message? That would help a lot. Is FolderPath a substitue for a real path that has been changed to protect your anonymity? Is your layer file "LayerPath.lyr" a full path? if not does it exist in your workspace? – Michael Stimson May 9 '16 at 23:52
  • It's in the title "Runtime error: cannot open 'GPL0'". Yes, I took out the full paths – cevondanady May 9 '16 at 23:54
  • 1
    If the answer provided by @MrBubbles does not resolve this for you, and in future questions, I think you should try to cut your code down to a code snippet that shows the same error before presenting it. That way where to look for the issue becomes more obvious to you and us. – PolyGeo May 10 '16 at 0:34
3

Your error is essentially saying that it can't open the layer file due to the manner in which you provided it. Esri loves cryptic errors like that.

If you're running this as a Script Tool, I suspect the issue is with the manner in which you reference the quadPath variable. That's not a normal way to reference a layer file saved on disk. The way you're doing it is what I typically see when people are working with the Python Window, because the Python Window has a built-in list of all the items in the Table of Contents and there it references items in that built-in list according to their namespace.

Try referencing that Layer file using its absolute path instead.

1

Figured it out. The SelectLayerByLocation method changes the state of the quadLyr object, so instead of assigning the method to selectedLyr I ran the method and then used quadLyr as input for the cursor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.