之前做了配合地图系列批量导图的脚本工具
ArcGIS Pro脚本工具(9)——配合地图系列批量导图_学学GIS的博客-CSDN博客_arcgispro批量导出地图pngPro的地图系列是批量制图的利器,但是有个不便的地方,就是设置完成地图系列后,只能批量导出为PDF,而不能批量导出为JPG、PNG这些通用的图片格式。以上脚本即可实现将地图系列批量导出为PNG图像文件,但是使用起来还不够简单。另外有一些功能还需要加入。在示例脚本的基础上,增加实现上述功能的代码,可制作脚本工具的完整Python代码如下。不过帮助文档还是很贴心的为我们准备了解决方案。.........https://blog.csdn.net/baidu_28157641/article/details/125841844?spm=1001.2014.3001.5502经过网友提醒发现,有一种情况这个工具是满足不了的,那就是按字段属性值分类导图。
比如,有江西省所有的县, 现在需要按市级(下图中用颜色区分)各导出一张图,之前的配合地图系列批量导图工具是做不到的,因为地图系列是索引图层中的每个要素,而不是一类要素。
因此,需要开发一个新的工具,它可以根据索引图层的某个字段,将属性值相同的要素导出在一张图上。下面江西省所有县为例,按市级分类导图。
工具演示
工具脚本
import arcpy
import re
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyt = aprx.activeView
mf = lyt.listElements("MAPFRAME_ELEMENT")[0]
map = aprx.activeMap
lyrName = arcpy.GetParameterAsText(0)
lyr = map.listLayers(lyrName)[0]
field = arcpy.GetParameterAsText(1)
cursor = arcpy.da.SearchCursor(lyr, field)
uniqueList = []
for row in cursor:
v = row[0]
if v not in uniqueList:
uniqueList.append(v)
del cursor
map.clearSelection()
dpi = arcpy.GetParameter(2)
viewRules = arcpy.GetParameterAsText(3)
margin = arcpy.GetParameter(4)
dirpath = arcpy.GetParameterAsText(5)
def clean_invalid(filename: str):
invalid_chars = '[?,*,:,<,>,\,/,|]'
replace_char = '_'
return re.sub(invalid_chars, replace_char, filename)
if viewRules == "无":
for uniqueValue in uniqueList:
lyr.definitionQuery = field + " = '" + uniqueValue + "'"
extent = mf.getLayerExtent(lyr)
mf.camera.setExtent(extent)
mf.camera.scale = mf.camera.scale/(1-margin)
pngName = clean_invalid(uniqueValue)
lyt.exportToJPEG(dirpath + "\\" + pngName,
resolution=dpi)
elif viewRules == "固定比例":
for uniqueValue in uniqueList:
lyr.definitionQuery = field + " = '" + uniqueValue + "'"
extent = mf.getLayerExtent(lyr)
mf.panToExtent(extent)
pngName = clean_invalid(uniqueValue)
lyt.exportToJPEG(dirpath + "\\" + pngName,
resolution=dpi)
else:
for uniqueValue in uniqueList:
lyr.definitionQuery = field + " = '" + uniqueValue + "'"
pngName = clean_invalid(uniqueValue)
lyt.exportToJPEG(dirpath + "\\" + pngName,
resolution=dpi)
lyr.updateDefinitionQueries([])
工具参数
参数验证
class ToolValidator:
# Class to add custom behavior and properties to the tool and tool parameters.
def __init__(self):
# set self.params for use in other function
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
# Customize parameter properties.
# This gets called when the tool is opened.
return
def updateParameters(self):
# Modify parameter values and properties.
# This gets called each time a parameter is modified, before
# standard validation.
if self.params[3].value=="无":
self.params[4].enabled = True
else:
self.params[4].enabled = False
return
def updateMessages(self):
# Customize messages for the parameters.
# This gets called after standard validation.
return
# def isLicensed(self):
# # set tool isLicensed.
# return True
# def postExecute(self):
# # This method takes place after outputs are processed and
# # added to the display.
# return
工具说明
- 需要先制作布局,布局只放置一个地图框
- 工具运行前请切换至布局视图
工具获取
代码可自取,需要完整工具请私信联系