2

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. (Equivantly: convert a shapefile to a layer file without opening it, but I'd prefer not to do that).

migrated from superuser.com May 5 '11 at 18:48

This question came from our site for computer enthusiasts and power users.

4

Assuming there is no addshapefile function, one solution would be:

Make Feature Layer (Data Management) - Syntax:
MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info})

Then add the layer...

2

I don't have ArcPy to hand but can you not use the MakeFeatureLayer tool and then add the result to your map?

arcpy.MakeFeatureLayer(r"C:\myData.shp","myData")
arcpy.mapping.AddLayer(df, "myData", "AUTO_ARRANGE")  
-3

This library reads and writes ESRI Shapefiles in pure Python. You can read and write shp, shx, and dbf files with all types of geometry. Everything in the public ESRI shapefile specification is implemented.

http://code.google.com/p/pyshp/

  • 1
    I'm curious as to what's wrong with this solution? Thought it was an exact match for the question asked. – Jacques Tardie May 9 '11 at 14:15
  • This can be used to read/write shapefiles but the question is how to create a layer in ArcMap from a shapefile. – PolyGeo Dec 20 '14 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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