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).

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 27, 2010 at 21:25

3 Answers 3

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...

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Nov 6, 2010 at 3:30
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") 
answered May 9, 2011 at 10:52
-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/

answered May 8, 2011 at 16:55
2
  • 1
    I'm curious as to what's wrong with this solution? Thought it was an exact match for the question asked. Commented May 9, 2011 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. Commented Dec 20, 2014 at 7:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.