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.

How can I create a shapefile using Python in ArcGIS 10?

I have lat & long. From this I need Python code which will create a shapefile and display it in ArcGIS 10.

Thanks.

share|improve this question
1  
Are you trying to create a point shapefiles, or do you need to turn your lat/long points into lines or polygons? –  RyanDalton Oct 24 '11 at 14:41
    
Hi I have lat and long and need an array to add all the lat and long in the same shape file –  Mehul Oct 24 '11 at 17:12
add comment

3 Answers 3

up vote 11 down vote accepted

For creating points:

ptList =[[20.000,43.000],[25.500, 45.085],[26.574, 46.025]]
pt = arcpy.Point()
ptGeoms = []
for p in ptList:
    pt.x = p[0]
    pt.Y = p[1]
    ptGeoms.append(arcpy.PointGeometry(pt))

arcpy.CopyFeatures_management(ptGeoms, r"C:\Temp\test.shp")

It will return a message like this:

<Result 'C:\\Temp\\test.shp'>
share|improve this answer
    
Also how can i add the same shape file through python in ArcGIS 10 –  Mehul Oct 24 '11 at 17:15
    
u will run python interpreter from Geoprocessing > Python then first of all write import arcpy and finally add code line by line... i hope you have asked this... :) –  Aragon Oct 25 '11 at 9:14
add comment

You can create a shapefile in Python using the Create Feature Class tool. There is an example at the bottom of the page.

To populate the shapefile with your lat & long data, you'll need to use an Insert Cursor.

Perhaps you can load your lat & long data as a list into Python, then iterate through the array populating the rows of your new shapefile with the insert cursor.

EDIT, response to comment

A python list of coordinates can be constructed like this:

latLonList = [[40.000,-75.000],[39.998,-75.432],[39.981,-75.343]]

Then to iterate through coordinates in the list (and print them, for example), do this:

for coord in latLonList:
    print "lat: " + str(coord[0])
    print "lon: " + str(coord[1])

If I'm interpreting your second question correctly, to add a layer to an mxd file, see this post on GIS.SE.

share|improve this answer
    
Hi Can you please give me a sample code which has list and array in it ? –  Mehul Oct 24 '11 at 17:13
    
Also how can i add the same shape file through python in ArcGIS 10 –  Mehul Oct 24 '11 at 17:14
    
I did look at the post but i am not able to implement it. Can you suggest me any other method or may be a sample code with explanation –  Mehul Oct 25 '11 at 9:09
add comment

Another option would be to just use the existing arcpy geoprocessing tools, see code below.

   # Import arcpy module
import arcpy


# Local variables:
table_dbf = "C:\\temp\\table.dbf"
table_Layer2 = "table_Layer2"
point3_shp = "C:\\temp\\point3.shp"

# Process: Make XY Event Layer
arcpy.MakeXYEventLayer_management(table_dbf, "x_coord", "y_coord", table_Layer2, "", "")

# Process: Copy Features
arcpy.CopyFeatures_management(table_Layer2, point3_shp, "", "0", "0", "0")

mxd = arcpy.mapping.MapDocument(r"C:\temp\Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
addLayer = arcpy.mapping.Layer(point3_shp)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.saveACopy(r"C:\temp\Untitled1.mxd")
share|improve this answer
    
Also how can i add the same shape file through python in ArcGIS 10 –  Mehul Oct 24 '11 at 17:15
1  
@Mehul, See my updated code. –  artwork21 Oct 24 '11 at 19:01
    
Can you please explain me from where will i get .dbf file and also some how i am unable to implement this code. –  Mehul Oct 25 '11 at 9:08
    
what is the meaning of x_coord and y_coord in arcpy.MakeXYEventLayer_management(table_dbf, "x_coord", "y_coord", table_Layer2, "", "") and what is it mean? –  Mehul Oct 27 '11 at 7:24
    
@Mehul, DBF is just the tabular file that makes up a shapefile (e.g. .shp, .shx, & .dbf). The x_coord and y_coord are just the two fields in the .dbf file that contain the long/lat coordinates. These fields are most likely different for you database. –  artwork21 Nov 17 '11 at 19:28
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.