ArcGIS Pro脚本工具(9)——配合地图系列批量导图

发布于:2023-02-01 ⋅ 阅读:(1276) ⋅ 点赞:(0)

Pro的地图系列是批量制图的利器,但是有个不便的地方,就是设置完成地图系列后,只能批量导出为PDF,而不能批量导出为JPG、PNG这些通用的图片格式。

不过帮助文档还是很贴心的为我们准备了解决方案。

以下脚本将地图系列的每一页导出到单个 PNG 文件中,并将索引要素的页面名称作为输出文件名的一部分使用:

import arcpy, os, sys
relpath = os.path.dirname(sys.argv[0])

p = arcpy.mp.ArcGISProject(relpath + "\\MapSeries\\US_States.aprx")
l = p.listLayouts()[0]
if not l.mapSeries is None:
  ms = l.mapSeries
  if ms.enabled:
    for pageNum in range(1, ms.pageCount + 1):
      ms.currentPageNumber = pageNum
      print("Exporting {0}".format(ms.pageRow.STATE_NAME))
      pageName = ms.pageRow.STATE_NAME
      l.exportToPNG(os.path.join(relpath, "Output", f"Ex2_{ms.pageRow.STATE_NAME}.png"))

以上脚本即可实现将地图系列批量导出为PNG图像文件,但是使用起来还不够简单。另外有一些功能还需要加入。

  1. 判断文件夹中是否已存在同名文件
  2. 自由选择字段为PNG图像文件命名

在示例脚本的基础上,增加实现上述功能的代码,制作脚本工具。

 效果演示

Python脚本

import arcpy
import os


aprx = arcpy.mp.ArcGISProject("CURRENT")
lyt = aprx.activeView
ms = lyt.mapSeries


def files_name(dirpath):
    for root, dirs, files in os.walk(dirpath):
        return files


file_type = arcpy.GetParameterAsText(0)


def exportTo(out_path, resolution):
    if file_type == "PNG":
        return lyt.exportToPNG(out_path, resolution)
    elif file_type == "JPEG":
        return lyt.exportToJPEG(out_path, resolution)


dirpath = arcpy.GetParameterAsText(1)
namelist = files_name(dirpath)

fid = arcpy.GetParameterAsText(2)
dpi = arcpy.GetParameter(3)

pageRange = arcpy.GetParameterAsText(4)
startNum = arcpy.GetParameter(5)
endNum = arcpy.GetParameter(6)

if not ms is None:
    if ms.enabled:
        if pageRange == "所有页面":
            startNum = 1
            endNum = ms.pageCount
        if pageRange == "当前页面":
            startNum = ms.currentPageNumber
            endNum = ms.currentPageNumber

        for pageNum in range(startNum, endNum + 1):
            ms.currentPageNumber = pageNum
            pageName = getattr(ms.pageRow, fid)
            if pageName not in namelist:
                exportTo(dirpath + "\\" + pageName, resolution=dpi)
                arcpy.AddMessage("Exporting {0}.{1}".format(pageName, file_type.lower()))

参数设置

参数验证

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.
        
        import arcpy
        
        aprx = arcpy.mp.ArcGISProject("CURRENT")
        lyt = aprx.activeView
        ms = lyt.mapSeries
        indexlyr=ms.indexLayer
        fields = arcpy.ListFields(indexlyr)
        fieldvalues=[]
        for field in fields:
            fieldvalues.append(field.name)
        self.params[2].filter.list=fieldvalues
        
        self.params[5].enabled = False
        self.params[6].enabled = False
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.
        if self.params[4].value=="自定义页面范围":
            self.params[5].enabled = True
            self.params[6].enabled = True
        else:
            self.params[5].enabled = False
            self.params[6].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

工具说明

  1. 只适用于ArcGIS Pro
  2. 布局视图中需要预先设置好地图系列
  3. 运行工具前,请在Pro中切换到布局视图
  4. 命名字段应该选择具有唯一值的字段

工具下载

请私信联系


更新

  1. 增加页面范围选项,有所有页面、当前页面、自定义页面范围3种类型可选
  2. 取消多余的参数,初始化后即可填充命名字段
  3. 增加输出图片格式选项,可以选择png或jpeg
  4. 现在可以对索引图层的字段进行组合,为输出图片命名。


网站公告

今日签到

点亮在社区的每一天
去签到