Use the arcpy. mapping module to batch plot data. The arcpy. mapping module
Plotting is a common task in projects. In some projects, even hundreds of images are required. Arcpy. mapping is a plotting module in ArcGIS, which can quickly complete a plotting tool.
Common classes in the arcpy. mapping module include MapDocument, DataFrame, Layer, DataDrivenPages, and TextElement.
The MapDocument class is the class corresponding to the map document (. mxd file. The initialization parameter is a string, generally the path of the. mxd file:
Mxd = arcpy. mapping. MapDocument (r "F: \ GeoData \ chinw.ea \ ChinaVector. mxd ")
The DataFrame class is used to operate the Data Frame (Layers) in a map and control the MAP range and scale. Use the arcpy. mapping. ListDataFrames (map_document, {wildcard}) function.
Df = arcpy. mapping. ListDataFrames (mxd) [0]
The Layer class is used to operate a specific Layer. Controls the style and visibility of image spots. You can use the path of the. lyr file or the arcpy. mapping. ListLayers (map_document_or_layer, {wildcard}, {data_frame}) function.
Lyr1 = arcpy. mapping. Layer (r "F: \ GeoData \ chinw.ea \ Province. lyr ")
Df. addLayer (lyr1)
Lyr2 = arcpy. mapping. ListLayer (mxd, "", df) [0]
The DataDrivenPages class must be used with the Data Driven Pages tool In ArcMap. This parameter is used when all or part of the images in a Vector file have one image.
The TextElement class is used to operate texts on a map, such as names and pages. Use the arcpy. mapping. ListLayoutElements (map_document, {element_type}, {wildcard}) function.
TxtElm = arcpy. mapping. ListLayoutElements (mxd, "TEXT_ELEMENT") [0]
There are two common plotting modes: one image for each image in a Vector file and one image for each Vector file in a folder.
Each image is shown as follows:
In this case, the Data Driven Pages tool works best. Open Customize-> Toolbars-> Data Driven Pages of ArcMap, set layers, name fields, sorting fields, display ranges, and scales, and save the map.
# 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'
Each Vector file in a folder is shown in the following figure:
# 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'
For more functions, see the ArcMap help document Geoprocessing-> ArcPy-> Mapping Module.