How do I add a shapefile in ArcGIS via python scripting?

Posted by Tom W on Super User See other posts from Super User or by Tom W
Published on 2010-10-27T21:25:26Z Indexed on 2011/01/10 13:55 UTC
Read the original article Hit count: 266

Filed under:
|

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 wiothout opening it, but I'd prefer not to do that).

© Super User or respective owner

Related posts about python

Related posts about gis