I am trying to use the SaveT To PDF Example by Ismael. I successfully set up the template for my use. My issue, is the scale at which the PDF prints. In the MXD, the Layout is set to the County but the PDF prints at the State scale (see attached). I am thinking the script is using the extent of all layers to determine the scale, and since I am using a Definition Query on a layer, the extent is that of the entire layer.
Below is the code. Anybody have any suggestions as to how to force the script to maintain the scale set in the Layout Window? Thanks.
import arcpy
import os
arcpy.env.overwriteOutput = True
def main():
arcpy.AddMessage(" Collecting input parameters")
# Get Initial Coordinates
xMin = arcpy.GetParameterAsText(0)
if xMin == '#' or not xMin:
arcpy.AddError("Cannot proceed if xMin is not passed")
yMin = arcpy.GetParameterAsText(1)
if yMin == '#' or not yMin:
arcpy.AddError("Cannot proceed if yMin is not passed")
xMax = arcpy.GetParameterAsText(2)
if xMax == '#' or not xMax:
arcpy.AddError("Cannot proceed if xMax is not passed")
yMax = arcpy.GetParameterAsText(3)
if yMax == '#' or not yMax:
arcpy.AddError("Cannot proceed if yMax is not passed")
xMin = float(xMin)
yMin = float(yMin)
xMax = float(xMax)
yMax = float(yMax)
# Get the input spatial reference
srValIn = arcpy.GetParameterAsText(4)
if srValIn == '#' or not srValIn:
arcpy.AddError("Cannot proceed if the Spatial Reference of your extent is not passed")
srIn = arcpy.CreateObject('SpatialReference')
srIn.loadFromString(srValIn)
# Get Layout Folder
LayoutsFolderPath = arcpy.GetParameterAsText(5)
if LayoutsFolderPath == '#' or not LayoutsFolderPath:
arcpy.AddError("Cannot proceed if the Layout Folder is not passed")
return
# Get template document
Layout = arcpy.GetParameterAsText(6)
if Layout == '#' or not Layout:
arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)
return
mxdName = os.path.join(LayoutsFolderPath, Layout)
if not (os.path.exists(mxdName)):
arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)
return
mapDoc = arcpy.mapping.MapDocument(mxdName)
# Get the requested scale
mapScale = arcpy.GetParameterAsText(7)
if mapScale == '#' or not mapScale:
mapScale = 0 # provide a default value if unspecified
mapScale = float(mapScale)
# layers only or layers and attributes
attributesFlag = arcpy.GetParameterAsText(8)
if attributesFlag.lower() == 'true':
layers_string = "LAYERS_AND_ATTRIBUTES"
else:
layers_string = "LAYERS_ONLY"
# Get the requested Map Title
title = arcpy.GetParameterAsText(9)
if title == '#' or not title:
title = " " # provide a default value if unspecified
# Get pdf file path to create
outputPDFPath = arcpy.GetParameterAsText(10)
if outputPDFPath == '#' or not outputPDFPath:
arcpy.AddError("Cannot proceed without specifying an output path for the pdf file")
return
# Build the extent in the Layout document
dataFrame = arcpy.mapping.ListDataFrames(mapDoc)[0]
arcpy.AddMessage(" Processing Map Extent")
dataFrame.extent = calculateExtent_Original(xMin,xMax,yMin,yMax,dataFrame,srIn)
# Set the scale if necessary
if mapScale > 0:
arcpy.AddMessage(" Setting scale to: " + str(mapScale))
dataFrame.scale = mapScale
# Set the Map title
if title != " ":
for elm in arcpy.mapping.ListLayoutElements(mapDoc, "TEXT_ELEMENT"):
if elm.text == "Map Title":
elm.text = title
# Export
arcpy.AddMessage(" Saving as PDF")
arcpy.mapping.ExportToPDF(mapDoc,outputPDFPath, layers_attributes=layers_string)
def calculateExtent_Original(xMin,xMax,yMin,yMax,dataFrame,srIn):
# Make input extent coordinates in the same sr as the data frame
pntInLL = arcpy.Point()
pntInLL.X = xMin
pntInLL.Y = yMin
pntInUR = arcpy.Point()
pntInUR.X = xMax
pntInUR.Y = yMax
pntGeomInLL = arcpy.PointGeometry(pntInLL,srIn)
pntGeomInUR = arcpy.PointGeometry(pntInUR,srIn)
pntGeomList = []
pntGeomList.append(pntGeomInLL)
pntGeomList.append(pntGeomInUR)
arcpy.CopyFeatures_management(pntGeomList, "in_memory/extentPnts")
rows = arcpy.SearchCursor("in_memory/extentPnts", "", dataFrame.spatialReference)
rows.reset()
row = rows.next()
newPntLL = row.shape.getPart(0)
row = rows.next()
newPntUR = row.shape.getPart(0)
myExtent = arcpy.CreateObject('Extent', newPntLL.X, newPntLL.Y, newPntUR.X, newPntUR.Y)
#arcpy.AddMessage("New extent: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
return myExtent
if __name__ == "__main__":
main()