python使用arcpy.mapping模組批量出圖方法

來源:互聯網
上載者:User
出圖是項目裡常見的任務,有的項目甚至會要上百張圖片,所以批量出土工具很有必要。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'

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援topic.alibabacloud.com。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.