How can I add a feature class (shapefile) to the current map doc using arcpy.mp (Python 3)?

6728
6
Jump to solution
08-15-2017 01:12 PM
DeanMirabito
New Contributor II

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.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor
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)

View solution in original post

6 Replies
JohannesBierer
Occasional Contributor III
0 Kudos
DeanMirabito
New Contributor II

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.

0 Kudos
JohannesBierer
Occasional Contributor III

Sorry Dean, I don't use Pro ... overread that ...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor
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)
DeanMirabito
New Contributor II

Joshua, thank you so much. This is exactly what I needed. The .addDataFromPath method is a lifesaver.

deleted-user-25j2k-XonNEg
New Contributor III

And this works for adding feature classes, as well.  Thank you.

0 Kudos