Skip to main content
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Source Link

Move Extent Locator Display python error

I am trying to use ESRIs generated python code to move around my Extent Data Frame based on location of my Main Line shapefile, as to not have the Data Frame over top of the main line when developing a data driven page set up.

When attempting I receive this error:

<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000725: Output Feature Class: Dataset in_memory\tempFC already exists.
Failed to execute (CopyFeatures).

Failed to execute (MoveLocatorMap).

ESRI's Code below:

import arcpy, os, sys

#Python function
def findLocation(corner, mainDF, locatorDF, polyLayer):

#The following funtion creates a polygon feature for each of the 4 corners of the
#main data frame.  The X,Y coordinates are calculated using the main data frame's
#extent as well as the proportional distances along the main data frame's extent. For
#example, in the figure below, the X value is simply the data frame's XMax value.  But
#the Y value is the YMax value minus the proportional distance of the locator data
#frames height compared to the main data frame's height. We know the total Y distance
#(YMax - YMin) and we know the height of each data frame (elementHeight) so we can
#multiply the Y delta by the percent of the locator data frame to the main dataframe
#to get:

# Y = mainDF.extent.YMax - ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))

#   +----+----------+----+
#   |    |          |    |
#   +----+          +----+  <-- X, Y value example
#   |                    |
#   |                    |
#   |                    |
#   |                    |
#   |                    |
#   +----+          +----+
#   |    |          |    |
#   +----+----------+----+

if corner =="SW":
    neX = mainDF.extent.XMin + ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    neY = mainDF.extent.YMin + ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    nwX = mainDF.extent.XMin
    nwY = mainDF.extent.YMin + ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    swX = mainDF.extent.XMin
    swY = mainDF.extent.YMin
    seX = mainDF.extent.XMin + ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    seY = mainDF.extent.YMin
    
if corner == "SE":
    neX = mainDF.extent.XMax
    neY = mainDF.extent.YMin + ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    nwX = mainDF.extent.XMax - ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth)) 
    nwY = mainDF.extent.YMin + ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    swX = mainDF.extent.XMax - ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    swY = mainDF.extent.YMin
    seX = mainDF.extent.XMax
    seY = mainDF.extent.YMin
    
if corner =="NW":
    neX = mainDF.extent.XMin + ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    neY = mainDF.extent.YMax
    nwX = mainDF.extent.XMin
    nwY = mainDF.extent.YMax
    swX = mainDF.extent.XMin
    swY = mainDF.extent.YMax - ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    seX = mainDF.extent.XMin + ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    seY = mainDF.extent.YMax - ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    
if corner == "NE":
    neX = mainDF.extent.XMax
    neY = mainDF.extent.YMax
    nwX = mainDF.extent.XMax - ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    nwY = mainDF.extent.YMax
    swX = mainDF.extent.XMax - ((mainDF.extent.XMax - mainDF.extent.XMin) * (locatorDF.elementWidth / mainDF.elementWidth))
    swY = mainDF.extent.YMax - ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))
    seX = mainDF.extent.XMax
    seY = mainDF.extent.YMax - ((mainDF.extent.YMax - mainDF.extent.YMin) * (locatorDF.elementHeight / mainDF.elementHeight))

#After the coordinates are calculated, they are used to create a polygon feature and saved to a temporary shapefile.  The
#code below is nearly identical to the Polygon Class code sample found in help.     
coordList = [[[neX,neY],[nwX,nwY],[swX,swY],[seX,seY]]]
point = arcpy.Point()
array = arcpy.Array()
featureList = []
for feature in coordList:
    for coordPair in feature:
        point.X = coordPair[0]
        point.Y = coordPair[1]
        array.add(point)
    array.add(array.getObject(0))
    polygon = arcpy.Polygon(array)
    array.removeAll()
    featureList.append(polygon)

arcpy.CreateFeatureclass_management(r"in_memory", "tempFC", "POLYGON")
arcpy.CopyFeatures_management(featureList, r"in_memory\tempFC")

#The temporary shapefile is used to perform a select by location to see if it intersects with features in the index layer.
arcpy.SelectLayerByLocation_management(polyLayer, "INTERSECT", r"in_memory\tempFC", "", "NEW_SELECTION")

#Using a cursor, it checks for selected features.  If 0, then there is no intersection and the locator map can be placed there
#without conflict. If >= 1, then there is a conflict and the next location will be tested.
polyCursor = arcpy.SearchCursor(polyLayer)
polyRow = polyCursor.next()
count = 0
while polyRow:
    count = count + 1
    polyRow = polyCursor.next()

#Clear the selection
arcpy.SelectLayerByAttribute_management(polyLayer, "CLEAR_SELECTION")    

#Delete the temporary shapefile.
arcpy.Delete_management(r"in_memory\tempFC")

#Return the count value to main part of script to determine placement of locator map.
return count


#*******************************
#MAIN SCRIPT STARTS HERE
#*******************************

#read input parameter
mxdPath = os.path.join(sys.path[0], "LigSWIndex.mxd")

#Reference map document
mxd = arcpy.mapping.MapDocument(mxdPath)

#reference data frames
mainDF = arcpy.mapping.ListDataFrames(mxd, "MainDF")[0]
locatorDF = arcpy.mapping.ListDataFrames(mxd, "LocatorDF")[0]

#create PDF and check to see if it already exists
finalPDF = os.path.join(sys.path[0], "ligonier_stormwater.pdf")
if os.path.exists(finalPDF):
    os.remove(finalPDF)

outPDF = arcpy.mapping.PDFDocumentCreate(finalPDF)


#Loop through each Data Driven Page
#for pageNum in range(1, 10): # Loop through just 10 records for faster testing
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    print "Page " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount)
    arcpy.AddMessage("Page " + str(pageNum) + " of " + str   (mxd.dataDrivenPages.pageCount))


#Reference the index layer that uses DDP query definition
for layer in arcpy.mapping.ListLayers(mxd, "", mainDF):
    if layer.name == "Main Line":
        polyLayer = layer

#Place locator data frame - priority location is: SW, SE, NW, NE. If all conflict,
#the default is the SW corner.
if findLocation("SW", mainDF, locatorDF, polyLayer) == 0:
    locatorDF.elementPositionX = 1
    locatorDF.elementPositionY = 1
    txtSW.text = "SW = True"
elif findLocation("SE", mainDF, locatorDF, polyLayer) == 0:
    locatorDF.elementPositionX = 5.5
    locatorDF.elementPositionY = 1
    txtSE.text = "SE = True"
elif findLocation("NW", mainDF, locatorDF, polyLayer) == 0:
    locatorDF.elementPositionX = 1
    locatorDF.elementPositionY = 6
    txtNW.text = "NW = True"
elif findLocation("NE", mainDF, locatorDF, polyLayer) == 0:
    locatorDF.elementPositionX = 5.5
    locatorDF.elementPositionY = 6
    txtNE.text = "NE = TRUE"
else:
    locatorDF.elementPositionX = 1
    locatorDF.elementPositionY = 1
    txtSW.text = "NW - DEFAULT"

tempPDF = os.path.join(sys.path[0], "temp.pdf")
arcpy.mapping.ExportToPDF(mxd, tempPDF)
outPDF.appendPages(tempPDF)
os.remove(tempPDF)

outPDF.saveAndClose()
os.startfile(finalPDF)

del mxd