第四章 制图
打印地图
import arcpy.mapping as mapping
mxd = mapping.MapDocument("CURRENT")
for df in mapping.ListDataFrames(mxd):
if df.name == "Test_Performance":
mapping.PrintMap(mxd,"",df)
导出为pdf文件
import arcpy.mapping as mapping mxd = mapping.MapDocument("CURRENT") mapping.ExportToPDF(mxd,r"c:\ArcpyBook\Ch4\Map_PageLayout.pdf")
import arcpy.mapping as mapping mxd = mapping.MapDocument("CURRENT") for df in mapping.ListDataFrames(mxd): if df.name == "Crime": df.referenceScale = df.scale mapping.ExportToPDF(mxd,r"c:\ArcpyBook\Ch4\DataFrameCrime.pdf",df)
导出地图为图像文件
import arcpy.mapping as mapping mxd = mapping.MapDocument("CURRENT") for df in mapping.ListDataFrames(mxd): if df.name == "Crime": mapping.ExportToJPEG(mxd,r"c:\ArcpyBook\Ch4\DataFrameCrime.jpg",df)
导出报表
import arcpy import os path = os.getcwd() #Create PDF and remove if it already exists pdfPath = path + r"\CrimeReport.pdf" if os.path.exists(pdfPath): os.remove(pdfPath) pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath) districtList = ["Harlandale", "East Central", "Edgewood", "Alamo Heights", "South San Antonio", "Southside", "Ft Sam Houston","North East", "Northside", "Lackland", "Southwest", "Judson", "San Antonio"] mxd = arcpy.mapping.MapDocument(path + r"\Crime_Ch4.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.ListLayers(mxd, "Crime Density by School District")[0] pageCount = 1 for district in districtList: #Generate image for each district whereClause = "\"NAME\" = '" + district + " ISD'" lyr.definitionQuery = whereClause arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause) df.extent = lyr.getSelectedExtent() arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") arcpy.mapping.ExportToBMP(mxd, path + "\DistrictPicture.bmp", df) #single file #Generate report print("Generating report for: " + district + " ISD") arcpy.mapping.ExportReport(report_source=lyr,report_layout_file=path + r"\CrimeLayout.rlf",output_file=path + r"\temp" + str(pageCount) + ".pdf", starting_page_number=pageCount) #Append pages into final output print("Appending page: " + str(pageCount)) pdfDoc.appendPages(path + r"\temp" + str(pageCount) + ".pdf") os.remove(path + r"\temp" + str(pageCount) + ".pdf") pageCount = pageCount + 1 pdfDoc.saveAndClose() del mxd
导出为地图册
import arcpy import os # Create an output directory variable outDir = r"C:\ArcpyBook\Ch4" # Create a new, empty pdf document in the specified output directory finalpdf_filename = outDir + r"\MapBook.pdf" if os.path.exists(finalpdf_filename): os.remove(finalpdf_filename) finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename) # Add the title page to the pdf print("Adding the title page \n") finalPdf.appendPages(outDir + r"\TitlePage.pdf") # Add the index map to the pdf print "Adding the index page \n" finalPdf.appendPages(outDir + r"\MapIndex.pdf") # Export the Data Driven Pages to a temporary pdf and then add it to the # final pdf. Alternately, if your Data Driven Pages have already been # exported, simply append that document to the final pdf. mxdPath = outDir + r"\Topographic.mxd" mxd = arcpy.mapping.MapDocument(mxdPath) print("Creating the data driven pages \n") ddp = mxd.dataDrivenPages temp_filename = outDir + r"\tempDDP.pdf" if os.path.exists(temp_filename): os.remove(temp_filename) ddp.exportToPDF(temp_filename, "ALL") print("Appending the map series \n") finalPdf.appendPages(temp_filename) # Update the properties of the final pdf finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", pdf_layout="SINGLE_PAGE") # Save your result finalPdf.saveAndClose() # remove the temporary data driven pages file if os.path.exists(temp_filename): print("Removing the temporary map series file") os.remove(temp_filename) # Delete variables #del finalPdf, mxd, ddp