Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shape file 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 shape files, so I need to be able to open them. (Equivantly: convert a shape file to a layer file without opening it, but I'd prefer not to do that).

share|improve this question

2 Answers 2

up vote 14 down vote accepted

So, I wrestled with this exact same problem for quite a while. Here's what I found to work:

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 a number of other arrangement options such as "AUTO_ARRANGE".

share|improve this answer

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

share|improve this answer
    
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 6 '11 at 20:11
    
Did you install SP1? –  Jason Scheirer Jan 6 '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 6 '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 6 '11 at 22:02
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 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.