I am writing a script tool to be used in ArcGIS Pro. The script tool performs some geoprocessing and saves its results to a folder. What I would like to do is add the results from disk back to the current map document. Basically, I am looking for the equivalent to the below for arcpy.mp in Python 3:
import arcpyfrom arcpy import env # get the map documentmxd = arcpy.mapping.MapDocument("CURRENT")# get the data framedf = arcpy.mapping.ListDataFrames(mxd,"*")[0]# create a new layernewlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)# add the layer to the map at the bottom of the TOC in data frame 0arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")
I need to add a shapefile, not a .lyrx file. Is there a way to do this with arcpy.mp? I have been looking everywhere.
Solved! Go to Solution.
shp_path = # path to shape file
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0] # assumes data to be added to first map listed
map.addDataFromPath(shp_path)
Maybe this could help you?
Hi Johannes,
Thanks for the link! It is helpful, but it does not address how to perform the operation with the arcpy.mp module. I have used the arcpy.mapping module in the past, but now I am on ArcGIS Pro. It seems like some of the changes from mapping ---> mp have made certain operations very difficult! It is mind-boggling to me that it should be so hard to add a shapefile from disk to the TOC.
Sorry Dean, I don't use Pro ... overread that ...
shp_path = # path to shape file
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0] # assumes data to be added to first map listed
map.addDataFromPath(shp_path)
Joshua, thank you so much. This is exactly what I needed. The .addDataFromPath method is a lifesaver.
And this works for adding feature classes, as well. Thank you.