2013-05-14 77 views
0

該代碼的目的是製作PDF地圖冊,顯示北美所有大型湖泊。我試圖運行這個代碼來製作地圖冊,但它給了我一個空白的PDF。我怎樣才能解決這個問題?ArcGIS Python地圖冊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!" 
+0

您是否能夠簡化代碼以打開您的地圖並將其成功導出爲PDF?你知道http://gis.stackexchange.com嗎? – PolyGeo 2013-05-14 12:10:46

回答

0

首先,您應該刪除代碼的最後一行,刪除mxd。再次運行代碼並檢查MXD。數據圖層是否正確繪製?我建議讓代碼在執行文件清理之前完全工作,以便識別潛在的錯誤。