Select to view content in your preferred language

Adding feature class to map script

3260
9
11-16-2018 03:43 PM
JonahLay1
Emerging Contributor

Hi,

 I created a python script tool that creates a Thiessen Polygons. The tool runs fine. But how do I create it so that it automatically adds a new layer to the map? Right now, it only creates it in the geodatabase. I would have to manually drag and drop it into the map to get it to appear.

Thank you!

Jonah

9 Replies
DanPatterson_Retired
MVP Emeritus

If the script is attached to a tool in arctoolbox, then the results should be added to the display (I assume that your featureclass is an output parameter).

If you are running the script as a standalone script, it probably won't automatically since there may be a disconnect between the scripting environment and the project... obviously if Pro is closed, it won't add it at all, which is where the arcpy.mp (aka mapping module) comes in and using 'CURRENT' for the aprx.

You can try 

Make Feature Layer—Data Management toolbox | ArcGIS Desktop 

to see if that helps.

But the surefire way is to make your script run from a tool in a toolbox in ArcToolbox

DanPatterson_Retired
MVP Emeritus

This is an oldie but should get your started... just the simple toolbox incarnation, but easy to follow

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 

JonahLay1
Emerging Contributor

Hi Dan,

 Thank you for the response and resources. I believe my script is attached to the toolbox:

I tried "arcpy.MakeFeatureLayer_management". But there is no difference when I run the tool. However, if I run the same script through the python window, two of the same layers get displayed. One from analysis.CreateThiessenPolygons and the other from MakeFeatureLayer_management.

Is there a reason for this? The same script runs in the python window but doesn't display through the tool.

0 Kudos
DanPatterson_Retired
MVP Emeritus

check your parameter list in the tool.  The your result of the process needs to be an 'output'.  maybe a screen grab of the parameters

0 Kudos
JonahLay1
Emerging Contributor

Thank you. This is what I have:

0 Kudos
DanPatterson_Retired
MVP Emeritus

hmmmm 

number, Label/Name Direction Data Type

0 Input_Features   Input   Feature_layer

1 Fields_to_copy   Input    (did you check 'Dependency (input_features)' so you can chose from a list of fields?

2 Output_Feature  Output  I always try Feature Class or Feature Layer when one or the other doesn't work.  I always put output as the last parameter and it is the only output parameter.

JonahLay1
Emerging Contributor

I tried both Feature Class or Feature Layer. It didn't seem to make any difference. I was modelling the parameters after the default Thiessen Polygon tool in Pro.

Do you think it is a parameter setting that is preventing the layer from displaying automatically and not the script?

0 Kudos
DanPatterson_Retired
MVP Emeritus

in Pro results are supposed to be automatically added to the display.  It is the 2 'output' types which I don't think is working

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Hello,

Any success with this? As of Pro 2.8 I'm having the same issue.

Working from a notebook, this successfully adds a feature class to the map.

import arcpy, os
data = r'path\to\BaseLayer_Processed_Elementary_08302021_clip'
arcpy.env.addOutputsToMap = True 

try:

arcpy.management.MakeFeatureLayer(data, data) 
    
except:
    print(arcpy.GetMessages())

 

But, according to the docs the above only creates a temp layer in the map. So, unless you save the map or export the layer to a feature class it won't be saved. So, I tried the below script:

import arcpy, os
data = r'path\to\BaseLayer_Processed_Elementary_08302021_clip'
arcpy.env.addOutputsToMap = True 

try:

    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(data, os.path.basename(data))
    
except:
    print(arcpy.GetMessages())

 

Both scripts work to add a feature class to the map. However, when I convert to a script tool to run within pro it runs successfully, but nothing is added to the map.

Tool version of the script:

import arcpy, os

#input feature class from model. It will be added to the map.
data = arcpy.GetParameterAsText(0)
arcpy.env.addOutputsToMap = True
arcpy.AddMessage(f'adding to map: {data}') 

try:

    #Copies features from the input feature class or layer to a new feature class.
    arcpy.CopyFeatures_management(data, os.path.basename(data))
    
except:
    arcpy.AddMessage(arcpy.GetMessage())

 

Tool interface:

JaredPilbeam2_0-1632166144459.png

 

 

0 Kudos