1

I'm running into an issue applying a symbology layer (.lyrx) to a feature layer from an ArcGIS Toolbox. I am able to apply the symbology manually as well as through the ArcGIS terminal window without any issues. I have seen a number of posts with others having similar issues but not great resolution.

Any suggestions on how to add the "raster_to_point_out_path" file to the ArcGIS project, then apply the symbology through an ArcGIS toolbox?

  # Add the point feature class to the map
  aprx = arcpy.mp.ArcGISProject("CURRENT")
  active_map = aprx.activeMap
  # Apply symbology from .lyrx file
  symbology_lyrx = r"Modeling.lyrx"

 # Add the point feature class to the map
 added_layer = active_map.addDataFromPath(raster_to_point_out_path)
 arcpy.AddMessage(f"Layer added to map for Dir_Arrow{value}")

 # Apply symbology from .lyrx file
 symbology_layer = arcpy.mp.LayerFile(symbology_lyrx)
 arcpy.ApplySymbologyFromLayer_management(added_layer, symbology_layer)
 arcpy.AddMessage("Symbology applied from the .lyrx file")
    
4
  • Is the layer added but the symbology not applied or is the layer not added? I notice that the symbology_lyrx doesn't have a fully qualified path, is this because of obfuscation or is the path as stated? Commented Jun 11 at 23:51
  • 2
    I can confirm. This tool is not working from script. So, add lyrx and replace it's datasource or connection
    – FelixIP
    Commented Jun 12 at 2:46
  • 1
    gis.stackexchange.com/questions/465755/…
    – FelixIP
    Commented Jun 12 at 2:50
  • The "raster_to_point_out_path" is an output from earlier in the script. The script I have above will add the layer to the map, though I'm having issues when I try to apply the symbology to that layer which was just added. For the lyrx file, I just removed the beginning portion of the location for privacy reasons.
    – Geno
    Commented Jun 12 at 5:30

1 Answer 1

1

This seems to have resolved the issue. Thanks @FelixIP, your suggestion helped me resolve this!

try:
            symbology_lyrx = arcpy.GetParameterAsText(8)
            # Update this with the prefix for the output
            DA = "Directional_Arrow"  

            # Add data from the given path and assign it to variable L
            L = active_map.addDataFromPath(raster_to_point_out_path)

            # Get the first layer from the list of layers that match the criteria 'Directional_Arrow'
            layers = [layer for layer in active_map.listLayers() if DA in layer.name]
            if not layers:
                raise Exception(f"No layers found matching the criteria '{DA}'")
            lyr = layers[0]

            # Get the symbology (styling and visual representation) of the newly added layer
            symbology_layer = arcpy.mp.LayerFile(symbology_lyrx)

            # Apply the symbology from the symbology_layer to the lyr
            lyr.symbology = symbology_layer.listLayers()[0].symbology

        except Exception as e:
            arcpy.AddMessage(f"Symbology Failed: {e}")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.