5

In an active ArcMap session - I want to change the symbology of a layer which is repeated multiple times in the mxd. Can I do this?

The basic code I have for getting the layers is below. This is running within ArcMap (not as a .py script)

myMXD = arcpy.mapping.MapDocument("Current")
lstDataFrames=arcpy.mapping.ListDataFrames(myMXD)
lstLayers=arcpy.mapping.ListLayers(myMXD)

hazardLayer=arcpy.mapping.ListLayers(myMXD,'*Haz*')

allFrames=arcpy.mapping.ListDataFrames(myMXD)

for dataFrame in allFrames:
    myMXD.activeView=dataFrame
    for mapLayer in hazardLayer:
        mapLayer.visible=False

This just changes the visibility to off...can I change the color for example to some RGB value?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
GeorgeC
  • 8,228
  • 7
  • 52
  • 136

3 Answers3

9

You could use the Apply Symbology From Layer tool.

This tool applies the symbology from a layer to the Input Layer. It can be applied to feature, raster, network analysis, TIN, and geostatistical layer files or layers in the ArcMap table of contents. This tool is primarily for use in scripts or ModelBuilder.

artwork21
  • 35,114
  • 8
  • 66
  • 134
7

First create a default layer file to use as a template then:

myMXD = arcpy.mapping.MapDocument("Current")
lstDataFrames=arcpy.mapping.ListDataFrames(myMXD)
lstLayers=arcpy.mapping.ListLayers(myMXD)

hazardLayer=arcpy.mapping.ListLayers(myMXD,'*Haz*')

allFrames=arcpy.mapping.ListDataFrames(myMXD)

for dataFrame in allFrames:
     myMXD.activeView=dataFrame
     for mapLayer in hazardLayer:
         arcpy.ApplySymbologyFromLayer_management(mapLayer, r"C:\TemplateLayers\HazardTemplate.lyr")
Rob Clark
  • 1,273
  • 10
  • 14
  • Thanks...that's great. Is this also possible with the symbology of .shp files and not only .lyr files? – GeorgeC May 17 '11 at 06:04
1

My answer to similar question Changing color of symbol in UniqueValuesSymbology object using ArcPy? :

Current answer for ArcMAP 10.8 (may be some previous versions too) is "yes, it's possible". You can use a Layer method .updateLayerFromJSON

Core part of solution is to create JSON description of layer symbology.

Code sample:

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "name*")[0]
lyr.updateLayerFromJSON(json_dictionary)

There is a valid example for polygon layer with single symbol symbology.

{"name": "pointLayer",
"geometryType": "esriGeometryPolygon",
"drawingInfo": {
       "renderer":{
       "type": "simple",
       "symbol": {
       "type": "esriSFS",
       "style": "esriSFSSolid",
       "color": [           76,           115,           0,           255          >],
       "outline": {
       "color": [            255,            0,            0,            255           >],
       "width": 1          }}}}}

Above code is a value of tag layerDefinition from this specification: >https://enterprise.arcgis.com/ru/server/10.8/create-web-apps/windows/exportwebmap->specification.htm#ESRI_SECTION1_FC212A2B579B425A9FE4358ACC89482A

Details about specification can be found here: https://developers.arcgis.com/web->map-specification/objects/renderer/

gpinigin
  • 21
  • 3