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

share|improve this question

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

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

add comment

3 Answers 3

I have the same problem. This is such a basic thing; there should be a simple solution.

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

share|improve this answer
add comment

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")  
share|improve this answer
add comment

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/

share|improve this answer
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
add comment

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.