22

I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shapefile to the current map. (And then do stuff to it, but that's another story).

The best I can do so far is to add a layer file to the current map, using the following ("addLayer" is a layer file object):

def AddLayerFromLayerFile(addLayer):
 import arcpy
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
 arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
 arcpy.RefreshActiveView()
 arcpy.RefreshTOC()
 del mxd, df, addLayer

However, my raw data is always going be shapefiles, so I need to be able to open them. (Equivalently: convert a shapefile to a layer file without opening it, but I'd prefer not to do that).

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Tom W
  • 429
  • 1
  • 4
  • 12

2 Answers2

34

Here's what I found worked:

import arcpy
from arcpy import env

# get the map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

# create a new layer
newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)

# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

The dataframe (variable df) that this code will put the new layer into is the first dataframe in the map document. Also note that this code adds the data as a new layer at the bottom of the TOC. You can also use the other arrangement options, which are "AUTO_ARRANGE" and "TOP".

jbalk
  • 7,505
  • 1
  • 17
  • 39
Kenton W
  • 1,085
  • 1
  • 11
  • 16
  • 3
    Another option for specifying the dataframe is to use the active dataframe: df = mxd.activeDataFrame instead of df = arcpy.mapping.ListDataFrames(mxd)[0] -- also, you don't need the "*" in the listdataframe call. – jbalk Oct 20 '17 at 07:02
10

Make Feature Layer (Data Management) http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000.htm

I just tried it in the Python window of ArcMap and it adds directly to my map ( I didn't have to get my dataframe and call AddLayer).

arcpy.MakeFeatureLayer_management('r:/temp/a.shp','test') alt text

Jay Cummins
  • 14,642
  • 7
  • 66
  • 141
  • That works great in the immediate window... but when I try the same code in a script file and run it, nothing happens! (The code runs without error messages, but nothing appears in the ToC)

    Also, if I remove the layer from the ToC, then try and run the code in the immediate window again, I get a "file already exists" type error. Where is the "test" layer file saved?

    – Tom W Jan 06 '11 at 20:11
  • Did you install SP1? – Jason Scheirer Jan 06 '11 at 21:22
  • 1
    @Tom W: I'm pretty much a noob with python and arcpy. But I don't think a layer file is physically created. If you want a layer file, you have to make it and pass the layer name as a parameter: arcpy.SaveToLayerFile_management('test', 'r:/temp/evilmonkey.lyr', 'ABSOLUTE') If you don't want to save the layer and you just want it gone, arcpy.Delete_management('test'). – Jay Cummins Jan 06 '11 at 21:55
  • @Tom W: just re-read your comment. Are you trying to add a layer to ArcMap from a separate python shell(not the immediate window)? I didn't think you could do that (but I'm no authority on this...maybe you can). – Jay Cummins Jan 06 '11 at 22:02
  • @Jay It seems like the file doesn't exist on yoru hard drive, yet exists somewhere, otherwise Arc wouldn't complain it already exists! I'm trying to add a shape from a script run from within ArcMap. (Eventually, the script will do various things with the shape file after its added - it's not just there to open the file!). – Tom W Jan 07 '11 at 14:07
  • @Jason Scheirer: Yes. There was a pre-SP1 bug in Arc10 that meant you could add a layer file via script, but it would vanish once the script finished! – Tom W Jan 07 '11 at 14:09
  • 1
    @Tom W: I know about that layer bug, which is why I was making sure that SP1 was installed. To add the layer to the TOC as you're describing, you need your script to have a derived output feature layer and set the value to the name of the layer you made. GP tools in ArcMap try to protect the TOC from spurious layers (temp FCs etc) in GP tools so you need to define in the script tool's params that your new feature layer is going to stay in the TOC when done. Where the layer is living on disk is likely the data source of the FC, so a Feature Class named the same as the layer on your workspace. – Jason Scheirer Jan 10 '11 at 04:43
  • the help says: The layer that is created by the tool is temporary and will not persist after the session ends unless the layer is saved to disk or the map document is saved. – A.Adel Saleh Oct 15 '16 at 16:29