SetParameterSymbology

描述

用于设置脚本工具的指定输出参数的符号系统属性。

可以使用图层文件的路径或制图信息模型 (CIM)Esri Web 地图规范的 JSON 字符串表示来设置符号系统。矢量和栅格输出图层均支持符号化。

讨论

此函数可将符号系统应用于脚本工具的输出。在 text 参数中指定图层文件的路径时,脚本将取决于现有的图层文件。如果不使用预设的图层文件,则也可以通过传递以 JSONRENDERER=JSONCIMDEF=JSONCLASSDEF= 开头的 JSON CIM 或 Web 地图规范对象的字符串,将符号系统保留在脚本中。要了解用于获取对象的 JSON 字符串的选项,请参阅设置脚本中的输出符号系统

语法

SetParameterSymbology (index, text)
参数说明数据类型
index

参数列表中指定输出参数的索引位置。指定的符号系统属性将应用于此脚本工具的输出。

Integer
text

使用图层文件 (.lyrx) 的路径或 CIM 或 Web 地图规范对象的 JSON 字符串表示来设置指定参数的符号系统属性。

JSON 字符串表示必须以适当的关键字开头,如下所示:

  • JSONRENDERER= - 使用 CIM 或 Esri Web 地图方案进行格式化的 JSON renderer 对象。
  • JSONCIMDEF= - 图层的 JSON CIM 定义对象。
  • JSONCLASSDEF= - 来自 Esri Web 地图方案的 JSON 类定义。
String

代码示例

SetParameterSymbology 示例 1

使用存储在工具分发文件夹中的 .lyrx 文件,为脚本工具的栅格输出设置符号系统。该脚本工具具有两个参数、一个输入栅格图层和一个输出栅格图层。

import arcpy
# Obtain script tool input parameter values.
inras = arcpy.GetParameter(0)
outras = arcpy.GetParameterAsText(1)
outsym = r"C:\Path\To\OutRasterSymbology.lyrx"
# Reclassify raster and apply custom symbology from layer
reclass = arcpy.sa.Reclassify(
  inras,
  "Value",
  arcpy.sa.RemapRange(
    [[0, 100, 0],
    [101, 200, 1],
    [201, 300, 3]]),
  "NODATA")
reclass.save(outras)
arcpy.SetParameterSymbology(1, outsym)
SetParameterSymbology 示例 2

使用 CIM 渲染器对象设置脚本工具输出的符号系统。该脚本工具具有两个参数、一个输入要素图层和一个输出要素图层。

import arcpy
# Obtain script tool input parameter values.
infc = arcpy.GetParameter(0)
outfc = arcpy.GetParameter(1)
# Define symbology
outsym = """{
  "type" : "CIMSimpleRenderer",
  "patch" : "Default",
  "symbol" : {
    "type" : "CIMSymbolReference",
    "symbol" : {
      "type" : "CIMLineSymbol",
      "symbolLayers" : [
        {
          "type" : "CIMSolidStroke",
          "effects" : [
            {
              "type" : "CIMGeometricEffectDashes",
              "dashTemplate" : [ 5, 5],
              "lineDashEnding" : "NoConstraint",
              "controlPointEnding" : "NoConstraint"
            }
          ],
          "enable" : true,
          "capStyle" : "Round",
          "joinStyle" : "Round",
          "lineStyle3D" : "Strip",
          "miterLimit" : 10,
          "width" : 1,
          "color" : {
            "type" : "CIMRGBColor",
            "values" : [
              0,
              0,
              0,
              100
            ]
          }
        }
      ]
    }
  }
}"""
# Convert input polygon to line and apply a dashed line symbology
arcpy.management.FeatureToLine(infc, outfc)
arcpy.SetParameterSymbology(1, f"JSONRENDERER={outsym}")
SetParameterSymbology 示例 3

使用 Web 地图 JSON 渲染器对象根据脚本工具的输出形状类型设置符号系统。该脚本工具具有两个参数、一个输入要素图层和一个输出要素图层。

import arcpy
# Obtain script tool input parameter values.
infc = arcpy.GetParameter(0)
outfc = arcpy.GetParameter(1)
desc = arcpy.da.Describe(infc)
# Define symbology
sym_poly = """{
  "type": "simple",
  "symbol": {
    "type": "esriSFS",
    "style": "esriSFSSolid",
    "color": [
      10,
      120,
      230,
      255
    ],
    "outline": {
      "type": "esriSLS",
      "style": "esriSLSSolid",
      "color": [
        255,
        255,
        255,
        255
      ],
      "width": 2
    }
  },
  "label": "",
  "description": "",
  "rotationType": "geographic",
  "rotationExpression": ""
}"""
sym_line = """{
  "type": "simple",
  "symbol": {
    "type": "esriSLS",
    "style": "esriSLSSolid",
    "color": [
      10,
      120,
      230,
      255
    ],
    "width": 2
  },
  "label": "",
  "description": "",
  "rotationType": "geographic",
  "rotationExpression": ""
}"""
sym_point = """{
  "type": "simple",
  "symbol": {
    "type": "esriSMS",
    "style": "esriSMSCircle",
    "color": [
      10,
      120,
      230,
      255
    ],
    "size": 8,
    "angle": 0,
    "xoffset": 0,
    "yoffset": 0,
    "outline": {
      "color": [
        255,
        255,
        255,
        255
      ],
      "width": 2
    }
  },
  "label": "",
  "description": "",
  "rotationType": "geographic",
  "rotationExpression": ""
}"""
# Set symbology properties for the output based on shape type of the input
if desc['shapeType'] == 'Polygon':
    # JSON renderer specifying a blue polygon fill with a white outline of width 2
    outsym = f"JSONRENDERER={sym_poly}"
elif desc['shapeType'] == 'Polyline':
    # JSON renderer specifying a blue line of width 2
    outsym = f"JSONRENDERER={sym_line}"
elif desc['shapeType'] == 'Point':
    # JSON renderer specifying a blue point of size 8 with a white outline of width 2
    outsym = f"JSONRENDERER={sym_point}"
else:
    arcpy.AddError('Invalid shape type, must be either "Polygon", "Polyline", or "Point"')
# Execute Copy Features
arcpy.management.CopyFeatures(infc, outfc)
arcpy.SetParameterSymbology(1, outsym)
SetParameterSymbology 示例 4

使用 JSON 分类定义并通过自然间断点来动态设置脚本工具输出的符号系统。脚本工具具有三个参数、一个输入和输出要素类以及一个聚合距离整数。

import arcpy
# Obtain script tool parameter values.
infc = arcpy.GetParameterAsText(0)
outfc = arcpy.GetParameterAsText(1)
dist = arcpy.GetParameter(2)
#Define symbology
outsym = """{
  "type": "classBreaksDef",
  "classificationField": "Shape_Area",
  "classificationMethod": "esriClassifyNaturalBreaks",
  "breakCount": 5,
  "baseSymbol": {
      "type": "esriSFS",
      "style": "esriSFSSolid",
      "color": [
          0,
          0,
          0,
          200
      ],
      "outline": {
          "type": "esriSLS",
          "style": "esriSLSSolid",
          "color": [
              110,
              110,
              110,
              255
          ],
          "width": 0.4
      }
  },
  "colorRamp": {
      "type": "algorithmic",
      "fromColor": [
          245,
          245,
          0,
          200
      ],
      "toColor": [
          245,
          0,
          0,
          200
      ],
      "algorithm": "esriHSVAlgorithm"
  }
}"""
# Execute aggregate points and symbolize the output polygon by area using natural breaks.
arcpy.cartography.AggregatePoints(infc, outfc, dist)
arcpy.SetParameterSymbology(1, f"JSONCLASSDEF={outsym}")

相关主题