ArcGIS Python Map Book PDF无效空白PDF

时间:2013-05-14 00:01:39

标签: python pdf arcgis

该代码的目的是制作一本显示北美所有大型湖泊的PDF地图册。我正在尝试运行此代码来制作地图册,但它给了我一个空白的PDF。我怎样才能解决这个问题?

## Import arcpy module
import arcpy
import math
import os
from arcpy import env
arcpy.env.overwriteOutput = True

# Define inputs and outputs - Script arguments

arcpy.env.workspace = r"F:\Geog173\Lab7\Lab7_Data"

Lakes = "NA_Big_Lakes.shp"
Cities = "NA_Cities.shp"
NA = "North_America.shp"


##Python arguments
## Arguments = NA_Big_Lakes.shp NA_Cities.shp New_Lakes.shp  Center_Lakes.shp 
Lakes= 'NA_Big_Lakes.shp'
NA = 'North_America.shp'
Cities = 'NA_Cities.shp'
##New_Lakes = 'New_Lakes.shp'
##Center_Lakes = 'Center_Lakes.shp'

# Identify the geometry field
desc = arcpy.Describe(Lakes)
shapeName = desc.ShapeFieldName

# Identify the geometry field in Cities shapefile
##desc = arcpy.Describe(Cities)
##shapefieldnameCity = desc.ShapeFieldName

#Get lake cursor
inrows = arcpy.SearchCursor(Lakes)

# Set up variables for output path and PDF file name
outDir = r"F:\Geog173\Lab7\Lab7_Data"
finalMapPDF_filename = outDir + r"\NA_Big_Lake_Mapbook.pdf"


# Check whether the mapbook PDF exists. If it does, delete it.
if os.path.exists(finalMapPDF_filename):
    os.remove(finalMapPDF_filename)

# Create map book PDF
finalMapPDF = arcpy.mapping.PDFDocumentCreate(finalMapPDF_filename)

# Create MapDocument object pointing to specified mxd
mxd = arcpy.mapping.MapDocument(outDir + r"\OriginalMap.mxd")

# Get dataframe
df = arcpy.mapping.ListDataFrames(mxd)[0]

# ----------------------------------------------------------------------------#
# Start appending pages. Title page first.
# ----------------------------------------------------------------------------#
# Find text element with value "test", and replace it with other value
mapText = "A Map Book for North American Large Lakes " + '\n\r' + "Kishore, A., Geog173, Geography, UCLA" +  '\n\r' + " Lake number: 18" +  '\n\r' + " Total area: 362117 km2" +  '\n\r' + " Mean area: 20118 km2"
print mapText
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.text == "test":
        elm.text = mapText

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

#df.extent = feature.extent
arcpy.mapping.ExportToPDF(mxd, outDir + r"\TempMapPages.pdf")

# Append multi-page PDF to finalMapPDF
finalMapPDF.appendPages(outDir + r"\TempMapPages.pdf")

#initialize text value, so it can be reused in next iteration
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.text == mapText:
        elm.text = "test"


# ----------------------------------------------------------------------------#
# Loop through each lake
# ----------------------------------------------------------------------------#

# Loop through each row/feature
lakecount = 0
for row in inrows:
    lakecount = lakecount + 1
    CITY_NAME = ""
    CNTRY_NAME = ""
    ADMIN_NAME = ""
    POP_CLASS = ""
    DISTANCE = 0
    XY = ""
    #print "shapeName" , shapeName
    # Create the geometry object
    feature = row.getValue(shapeName)
    mapText = "Lake FID: " + str(row.FID) +  ", Area (km2): " + str(row.Area_km2)
    print mapText

    # Find text element with value "test", and replace it with other value
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == "test":
            elm.text = mapText


    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()

    df.extent = feature.extent
    arcpy.mapping.ExportToPDF(mxd, outDir + r"\TempMapPages.pdf")

    # Append multi-page PDF to finalMapPDF
    finalMapPDF.appendPages(outDir + r"\TempMapPages.pdf")


# Set up properties for Adobe Reader and save PDF.
finalMapPDF.updateDocProperties(pdf_open_view = "USE_THUMBS",
                             pdf_layout = "SINGLE_PAGE")       

finalMapPDF.saveAndClose()


# Done. Clean up and let user know the process has finished.
del row, inrows
del mxd, finalMapPDF
print "Map book for lakes in North America is complete!"

1 个答案:

答案 0 :(得分:0)

首先,您应删除删除mxd的代码的最后几行。再次运行代码并检查MXD。数据层是否正确绘制?我建议在执行文件清理之前使代码完全正常工作,以便识别潜在的错误。