11

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 in ArcGIS Desktop 10.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Mehul
  • 179
  • 2
  • 2
  • 5

3 Answers3

17

For creating points:

ptList =[[20.000,43.000],[25.500, 45.085],[26.574, 46.025], [28.131, 48.124]]
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'>
benchenggis
  • 1,406
  • 2
  • 12
  • 30
urcm
  • 22,533
  • 4
  • 57
  • 109
6

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

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

To add a layer to an mxd file, see Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Casey
  • 2,040
  • 1
  • 16
  • 23
6

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")
artwork21
  • 35,114
  • 8
  • 66
  • 134