SetParameterSymbology

Synthèse

Sets the symbology properties of the specified output parameter of a script tool by index or parameter name.

Symbology can be set using either the path to a layer file or a JSON string representation of a Cartographic Information Model (CIM) or Esri Web Map Specification. Both vector and raster output layers can be symbolized.

Discussion

This function is used to apply symbology to the output of a script tool. When specifying a path to a layer file in the text parameter, the script depends on the existing layer file. Instead of relying on preset layer files, the symbology can alternatively be persisted in the script by passing the string of a JSON CIM or Web Map Specification object preceded with JSONRENDERER=, JSONCIMDEF=, or JSONCLASSDEF=. To learn about the options for obtaining the JSON string of an object, see Setting output symbology in scripts.

Syntaxe

SetParameterSymbology (index, text)
ParamètreExplicationType de données
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:

  • JSONRENDERER=—A JSON renderer object formatted using either the CIM or Esri Web Map schema.
  • JSONCIMDEF=—A JSON CIM definition object for a layer.
  • JSONCLASSDEF=—A JSON class definition from the Esri Web Map schema.
String

Exemple de code

SetParameterSymbology example 1

Use the parameter's positional index to set the symbology for a script tool's raster output with a .lyrx file stored in the tool's distribution folder. The script tool has two parameters, an input and an output raster layer.

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 example 2

Use the parameter's name to set the symbology for a script tool's raster output with a .lyrx file stored in the tool's distribution folder. The script tool has two parameters, an input and an output raster layer.

The parameter names are in_raster and out_raster, respectively.

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)
SetParameterSymbology example 3

Set the symbology for a script tool's output using a CIM renderer object. The script tool has two parameters, an input and an output feature layer.

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 example 4

Set the symbology based on the output shape type of the script tool using a Web Map JSON renderer object. The script tool has two parameters, an input and an output feature layer.

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)
SetParameterSymbology example 5

Set the symbology for a script tool's output dynamically with natural breaks using the JSON classification definition. The script tool has three parameters, an input and output feature class and an aggregation distance integer.

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}")

Rubriques connexes