Select to view content in your preferred language

Unexpected Behavior with arcpy.addDataFromPath

3177
5
Jump to solution
11-09-2020 03:05 PM
Arne_Gelfert
Frequent Contributor

Working on a work-around for another conundrum and tried the following:

 

import arcpy

x = some X
y = some Y

point = arcpy.Point(x,y)

ptGeometry = arcpy.PointGeometry(point)

feature_class = arcpy.CreateFeatureclass_management("in_memory", "tempfc", "POINT")[0]

with arcpy.da.InsertCursor(feature_class, ["SHAPE@XY"]) as cursor:
    cursor.insertRow(ptGeometry)

aprx = arcpy.mp.ArcGISProject("CURRENT")
symbologyLayer = r"<some path>...\someSymbology.lyrx"

arcpy.management.ApplySymbologyFromLayer(feature_class,symbologyLayer)

# Interestingly, the following works too
#arcpy.ApplySymbologyFromLayer_management(feature_class,symbologyLayer)

map = aprx.listMaps()[0]
map.addDataFromPath(feature_class)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

So this works great in that I'm getting a symbol plotted where I needed. But it does that three (3)  times giving me the following output.

 

Now, I realize I could try and control the place of insertion by using addLayer() and using the LYRX file directly after updating its data source. But the above output is not what I would expect based on what I'm doing. What's going on there?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Regarding these lines:

arcpy.management.ApplySymbologyFromLayer(feature_class,symbologyLayer)

# Interestingly, the following works too
#arcpy.ApplySymbologyFromLayer_management(feature_class,symbologyLayer)

Line #01 and Line #04 call the exact same geoprocessing tool, so you should expect them to both work.  Line #01 represents the new syntax that Esri is pushing with Pro while Line #04 represents the original/legacy syntax.

The default for arcpy.env.addOutputsToMap is True, and I am guessing you are using the defaults, so lines #10, #18, and #24 in your code snippet will all add layers to the TOC, which is what you are seeing.

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

Regarding these lines:

arcpy.management.ApplySymbologyFromLayer(feature_class,symbologyLayer)

# Interestingly, the following works too
#arcpy.ApplySymbologyFromLayer_management(feature_class,symbologyLayer)

Line #01 and Line #04 call the exact same geoprocessing tool, so you should expect them to both work.  Line #01 represents the new syntax that Esri is pushing with Pro while Line #04 represents the original/legacy syntax.

The default for arcpy.env.addOutputsToMap is True, and I am guessing you are using the defaults, so lines #10, #18, and #24 in your code snippet will all add layers to the TOC, which is what you are seeing.

DanPatterson
MVP Esteemed Contributor

C:\....Your_Install_path...\Resources\ArcPy\arcpy\management.py

arcpy.management.ApplySymbologyFromLayer.__esri_toolname__
'ApplySymbologyFromLayer_management'

arcpy.management.ApplySymbologyFromLayer.__module__
'arcpy.management'

# ---- same
arcpy.ApplySymbologyFromLayer_management.__esri_toolname__
'ApplySymbologyFromLayer_management'

arcpy.ApplySymbologyFromLayer_management.__module__
'arcpy.management'

# ---- and finally

arcpy.management.ApplySymbologyFromLayer.__code__

<code object ApplySymbologyFromLayer at 0x00000158E26CDA50, file

"C:\arc_pro\Resources\ArcPy\arcpy\management.py", line 8520>

‍‍‍‍‍‍‍‍‍‍‍‍‍

Lots to explore in those folders, especially the new toolbox structure

C:\....Your_Install_path...\Resources\ArcToolBox\toolboxes\Data Management Tools.tbx\ApplySymbologyFromLayer.tool


... sort of retired...
Arne_Gelfert
Frequent Contributor

Good stuff, Dan. This wasn't the first time I got close to sifting through those folders. But I'll be back to the JSAPI tomorrow, so I'm doomed to a life of limited understahding!

0 Kudos
Arne_Gelfert
Frequent Contributor

I was beginning to wonder something like this... whether some "actions" are implied. Glad you bring  

arcpy.env.addOutputsToMap

to my attention. Will take a look. Thanks!

0 Kudos
Arne_Gelfert
Frequent Contributor

On second though, this might explain why the behavior is even different as part of a geoprocessing service... maybe server Python/arcpy has different environmental settings by defaul? I will have to look at that, too. 

0 Kudos