python USES the arcpy.mapping module to batch out pictures
- 2020-05-26 09:31:33
- OfStack
Drawing is a common task in projects. Some projects even require hundreds of pictures, so it is necessary to excavate tools in batches. arcpy.mapping is the drawing module in ArcGIS, which can quickly complete a drawing tool.
The commonly used classes in the arcpy.mapping module are MapDocument, DataFrame, Layer, DataDrivenPages, and TextElement.
The MapDocument class is the counterpart of the map document (.mxd file). The initialization parameter is 1 string, 1 is usually the path of the mxd file:
mxd=arcpy.mapping.MapDocument(r"F:\GeoData\ChinaArea\ChinaVector.mxd")
The DataFrame class is used to manipulate Data Frame in the map (Layers in the figure below) and can control the scope, scale, and so on of the map. Get it with the arcpy.mapping.ListDataFrames (map_document, {wildcard}) function.
df= arcpy.mapping.ListDataFrames(mxd)[0]
The Layer class is used to manipulate specific layers. Ability to control pattern pattern, visibility, etc. This can be initialized with the path to the.lyr file, or by 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 be used in conjunction with the Data Driven Pages tool in ArcMap. Used for all or part of a graph in a vector file.
The TextElement class is used to manipulate text on a map, such as map names and pages. arcpy.mapping.ListLayoutElements (map_document, {element_type}, {wildcard}) function.
txtElm=arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")[0]
There are two common drawing modes: one for each drawing in a vector file, and one for each drawing in a folder.
1 picture is produced for each picture:
The Data Driven Pages tool works best in this situation. Open Customize- for ArcMap > Toolbars- > Data Driven Pages, set the layer, name field, sort field, display range and scale, and save the map.
# 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'
1 picture of each vector file in 1 folder:
# 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'