使用arcpy.mapping模組批量出圖,arcpy.mapping模組
出圖是項目裡常見的任務,有的項目甚至會要上百張圖片,所以批量出土工具很有必要。arcpy.mapping就是ArcGIS裡的出圖模組,能快速完成一個出圖工具。
arcpy.mapping模組裡常用的類有MapDocument、DataFrame、Layer、DataDrivenPages和TextElement。
MapDocument類是地圖文檔(.mxd檔案)對應的類。初始化參數是一個字串,一般是.mxd檔案的路徑:
mxd=arcpy.mapping.MapDocument(r"F:\GeoData\ChinaArea\ChinaVector.mxd")
DataFrame類用於操作地圖內的Data Frame(即的Layers),能夠控制地圖的範圍、比例尺等。用arcpy.mapping.ListDataFrames(map_document, {wildcard})函數擷取。
df= arcpy.mapping.ListDataFrames(mxd)[0]
Layer類用於操作具體的圖層。能夠控製圖斑的樣式、可見度等。可以用.lyr檔案的路徑初始化,也可以通過arcpy.mapping.ListLayers(map_document_or_layer, {wildcard}, {data_frame})函數擷取。
lyr1=arcpy.mapping.Layer(r" F:\GeoData\ChinaArea\Province.lyr")
df.addLayer(lyr1)
lyr2=arcpy.mapping.ListLayer(mxd,"",df)[0]
DataDrivenPages類需要配合ArcMap中的Data Driven Pages工具使用。用於一個向量檔案內的全部或部分圖斑每個出一張圖的情況。
TextElement類用於操作地圖上的文字,比名、頁數。通過arcpy.mapping.ListLayoutElements (map_document, {element_type}, {wildcard})函數擷取。
txtElm=arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")[0]
常見的出圖模式有兩種:一個向量檔案裡每個圖斑出一張圖,一個檔案夾下每個向量檔案出一張圖。
每個圖斑出一張圖:
這種情況有Data Driven Pages工具配合最好。開啟ArcMap的Customize->Toolbars->Data Driven Pages,設定好圖層、名稱欄位、排序欄位、顯示範圍和比例尺,儲存地圖。
# coding:utf-8import arcpy mxd=arcpy.mapping.MapDocument(r"F:\GeoData\ChinaArea\ChinaVector.mxd")for pageNum in range(1,mxd.dataDrivenPages.pageCount): mxd.dataDrivenPages.currentPageID=pageNum mapName=mxd.dataDrivenPages.pageRow.getValue(mxd.dataDrivenPages.pageNameField.name) print mapName arcpy.mapping.ExportToPNG(mxd,r"F:\GeoData\ChinaArea\Province\\"+mapName+".png")print 'ok'
一個檔案夾下的每個向量檔案出一張圖:
# coding:utf-8import arcpyimport os def GetShpfiles(shpdir): shpfiles=[] allfiles=os.listdir(shpdir) for file in allfiles: if os.path.isfile(file): if file.endswith('.shp'): shpfiles.append(file) else: shpfiles.extend(GetShpfiles(file)) return shpfiles allshps=GetShpfiles(r"F:\GeoData\ChinaArea\Province")mxd=arcpy.mapping.MapDocument(r"F:\GeoData\ChinaArea\ChinaVector.mxd")lyr=arcpy.mapping.ListLayer(mxd)[0]for shp in allshps: paths=os.path.split(shp) print paths[1] lyr.replaceDataSource(paths[0],"SHAPEFILE_WORKSPACE",paths[1]) arcpy.mapping.ExportToPNG(mxd,r"F:\GeoData\ChinaArea\Province\\"+paths[1]+".png")print 'ok'
更多功能見ArcMap協助文檔Geoprocessing->ArcPy->Mapping Module。