Arcpy script : add output with predefined symbology to current project's active map

160
1
3 weeks ago
Labels (1)
Anne-MarieDubois
New Contributor III

Hi,

I'm trying to create a script that will add output with a predefined symbology (.lyrx parameter). Apparently you can control that with tool parameters  (https://community.esri.com/t5/python-questions/cannot-apply-symbology-from-layer-using-python/m-p/16...) but I can't figure how to set that there AND THEN have the data added to the map.

  Here is my current code:

 

    aprx = arcpy.mp.ArcGISProject("CURRENT")
    aprxMap = aprx.activeMap
    aprxMap.addDataFromPath(arcpy.management.ApplySymbologyFromLayer(in_layer=arcpy.MakeFeatureLayer_management(ZONE_DETAILS_FILLED, "Carte d'occupation du territoire"), in_symbology_layer=Symbologie, symbology_fields="VALUE_FIELD TYPE TYPE", update_symbology="MAINTAIN"))

#Where 
#ZONE_FILLED_DETAILS is the created output from previous tool
#Symbology is the .lyrx that is defined in the script parameters

 

 

It  works really well with raw path within the python window of ArcGIS Pro. However, if I try it within the full script I get this error:

Traceback (most recent call last):
  File "<string>", line 61, in <module>
  File "C:\Program Files\XTools\XTools AGP\Python\Tools\FeaturesToPointsTool.py", line 7, in <module>
    import XTools.XToolsAGP.Geoprocessing
ModuleNotFoundError: No module named 'XTools'
Traceback (most recent call last):
  File "C:\Temp\22_0993_Outil_carbone\SCRIPTS\OutilCarboneCERFO_v4.py", line 643, in <module>
    OutilCarboneCouches(*argv[1:])
  File "C:\Temp\22_0993_Outil_carbone\SCRIPTS\OutilCarboneCERFO_v4.py", line 550, in OutilCarboneCouches
    aprxMap.addDataFromPath(arcpy.management.ApplySymbologyFromLayer(in_layer=arcpy.MakeFeatureLayer_management(ZONE_DETAILS_FILLED, "Carte d'occupation du territoire"), in_symbology_layer=Symbologie, symbology_fields="VALUE_FIELD TYPE TYPE", update_symbology="MAINTAIN"))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 3744, in addDataFromPath
    return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path, web_service_type, custom_parameters,), True)))
RuntimeError

 

Does anyone have a cue for me?

Thanks!

0 Kudos
1 Reply
BrendaMikkiBarnes
New Contributor

I am looking to do the same thing in a stand-alone script, so I'd love to know if there's a cure as well!

0 Kudos