摘要
可通过索引或参数名称设置脚本工具指定输出参数的符号系统属性。
可以使用图层文件的路径或制图信息模型 (CIM) 或 Esri Web 地图规范的 JSON 字符串表示来设置符号系统。 矢量和栅格输出图层均支持符号化。
说明
此函数可将符号系统应用于脚本工具的输出。 在 text 参数中指定图层文件的路径时,脚本将取决于现有的图层文件。 如果不使用预设的图层文件,则也可以通过传递以 JSONRENDERER=、JSONCIMDEF= 或 JSONCLASSDEF= 开头的 JSON CIM 或 Web 地图规范对象的字符串,将符号系统保留在脚本中。 要了解用于获取对象的 JSON 字符串的选项,请参阅设置脚本中的输出符号系统。
语法
SetParameterSymbology (index, text)
参数 | 说明 | 数据类型 |
index | The specified output parameter's index position or parameter name. The specified symbology properties are applied to this output of the script tool. | Integer |
text | Set the specified parameter's symbology properties using the path to a layer file (.lyrx) or a JSON string representation of a CIM or Web Map Specification object. A JSON string representation must be preceded by the appropriate keyword as follows:
| String |
代码示例
通过存储在工具分发文件夹中的 .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)
通过存储在工具分发文件夹中的 .lyrx 文件,使用参数名称设置脚本工具栅格输出的符号系统。 该脚本工具具有两个参数、一个输入栅格图层和一个输出栅格图层。
参数名称分别为 in_raster 和 out_raster。
import arcpy
# Obtain script tool input parameter values.
inras = arcpy.GetParameter("in_raster")
outras = arcpy.GetParameterAsText("out_raster")
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("out_raster", outsym)
使用 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}")
使用 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"')
# Run Copy Features
arcpy.management.CopyFeatures(infc, outfc)
arcpy.SetParameterSymbology(1, outsym)
使用 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"
}
}"""
# Run aggregate points and symbolize the output polygon by area using natural breaks.
arcpy.cartography.AggregatePoints(infc, outfc, dist)
arcpy.SetParameterSymbology(1, f"JSONCLASSDEF={outsym}")