出图是项目里常见的任务,有的项目甚至会要上百张图片,因此批量出图工具颇有必要。arcpy.mapping就是ArcGIS里的出图模块,能快速完成一个出图工具。app
arcpy.mapping模块里经常使用的类有MapDocument、DataFrame、Layer、DataDrivenPages和TextElement。函数
MapDocument类是地图文档(.mxd文件)对应的类。初始化参数是一个字符串,通常是.mxd文件的路径:工具
mxd=arcpy.mapping.MapDocument(r"F:\GeoData\ChinaArea\ChinaVector.mxd")spa
DataFrame类用于操做地图内的Data Frame(即下图的Layers),可以控制地图的范围、比例尺等。用arcpy.mapping.ListDataFrames(map_document, {wildcard})函数获取。code
df= arcpy.mapping.ListDataFrames(mxd)[0]blog
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)utf-8
lyr2=arcpy.mapping.ListLayer(mxd,"",df)[0]element
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-8 import 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-8 import arcpy import 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。