Drawing is a common task in the project, and some items even have hundreds of pictures, so the batch excavation tool is necessary. Arcpy.mapping is a diagram module in ArcGIS that can quickly complete a drawing tool.
The classes commonly used in the Arcpy.mapping module are mapdocument, DataFrame, Layer, Datadrivenpages, and TextElement.
The Mapdocument class is the class that corresponds to the map document (. mxd file). The initialization parameter is a string, typically the path to an. mxd file:
Mxd=arcpy.mapping.mapdocument (r "F:\GeoData\ChinaArea\ChinaVector.mxd")
The Dataframe class is used to manipulate the data frame within the map (that is, the layers) to control the map's extent, scale, and so on. Obtained using the Arcpy.mapping.ListDataFrames (Map_document, {wildcard}) function.
df= arcpy.mapping.ListDataFrames (MXD) [0]
The layer class is used to manipulate specific layers. Can control the pattern, visibility and so on. Can be initialized with the path to the. lyr file, or it can be obtained through the arcpy.mapping.ListLayers (Map_document_or_layer, {wildcard}, {Data_frame}) function.
Lyr1=arcpy.mapping.layer (r "F:\GeoData\ChinaArea\Province.lyr")
Df.addlayer (LYR1)
Lyr2=arcpy.mapping.listlayer (Mxd, "", DF) [0]
The Datadrivenpages class needs to work with the Data Driven pages tool in ArcMap. Used for all or part of a vector file in a picture of each plot.
The TextElement class is used to manipulate the text on the map, compared to the name and number of pages. Obtained by arcpy.mapping.ListLayoutElements (Map_document, {element_type}, {wildcard}) function.
Txtelm=arcpy.mapping.listlayoutelements (Mxd, "text_element") [0]
There are two common plot patterns: one for each map in a vector file, and one for each vector file in a folder.
Each map has a picture:
This is the case with the Data Driven pages tool that works best. Open the ArcMap customize->toolbars->data driven Pages and save the map by setting the layer, Name field, sort field, display range, and scale bar.
# 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 '
A picture of each vector file in a folder:
# 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\ChinaAr Ea\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], "SHA Pefile_workspace ", Paths[1]) Arcpy.mapping.ExportToPNG (mxd,r" F:\GeoData\ChinaArea\Province\\ "+paths[1]+". png ") print ' OK '