HOW TO
Instructions provided demonstrate one method for creating a line feature between origin and destination point XY coordinates from a table using the Python scripting environment.
Code:
import sys, string, os, arcgisscripting
gp = arcgisscripting.create()
gp.Overwriteoutput = 1
#Specify the location for the new shapefile.
gp.CreateFeatureclass("C:/temp/", "test_line.shp", "POLYLINE")
#Define Coordinate System
gp.workspace = "C:/temp/"
gp.toolbox = "management"
coordsys = "Coordinate Systems/Geographic Coordinate Systems/North America/North American Datum 1983.prj"
gp.defineprojection("test_line.shp", coordsys)
#Open a cursor to insert rows into the shapefile.
cur = gp.InsertCursor("C:/temp/test_line.shp")
#Create an Array and Point object.
lineArray = gp.CreateObject("Array")
pnt = gp.CreateObject("Point")
#Open a cursor on the table of XY coordinates to read from.
rows = gp.SearchCursor("c:/pythonTest.dbf")
#Reset the cursor to the top.
row = rows.Next()
#Loop through each record in the XY table..
while row:
#Set the X and Y coordinates for origin vertex.
pnt.x = row.GetValue("Origin_x")
pnt.y = row.GetValue("Origin_y")
#Insert it into the line array
lineArray.add(pnt)
#Set the X and Y coordinates for destination vertex
pnt.x = row.GetValue("dest_x")
pnt.y = row.GetValue("dest_y")
#Insert it into the line array
lineArray.add(pnt)
#Go to next row in table.
row = rows.Next()
#Insert the new poly into the feature class.
feat = cur.NewRow()
feat.shape = lineArray
cur.InsertRow(feat)
lineArray.RemoveAll()
del cur, row, rows
Code:
import sys, string, os, win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.Overwriteoutput = 1
#Specify the location for the new shapefile.
gp.CreateFeatureclass("C:/temp/", "test_line.shp", "POLYLINE")
#Define Coordinate System
gp.workspace = "C:/temp/"
gp.toolbox = "management"
coordsys = "Coordinate Systems/Geographic Coordinate Systems/North America/North American Datum 1983.prj"
gp.defineprojection("test_line.shp", coordsys)
#Open a cursor to insert rows into the shapefile.
cur = gp.InsertCursor("C:/temp/test_line.shp")
#Create an Array and Point object.
lineArray = gp.CreateObject("Array")
pnt = gp.CreateObject("Point")
#Open a cursor on the table of XY coordinates to read from.
rows = gp.SearchCursor("c:/pythonTest.dbf")
#Reset the cursor to the top.
row = rows.Next()
#Loop through each record in the XY table..
while row:
#Set the X and Y coordinates for origin vertex.
pnt.x = row.GetValue("Origin_x")
pnt.y = row.GetValue("Origin_y")
#Insert it into the line array
lineArray.add(pnt)
#Set the X and Y coordinates for destination vertex
pnt.x = row.GetValue("dest_x")
pnt.y = row.GetValue("dest_y")
#Insert it into the line array
lineArray.add(pnt)
#Go to next row in table.
row = rows.Next()
#Insert the new poly into the feature class.
feat = cur.NewRow()
feat.shape = lineArray
cur.InsertRow(feat)
lineArray.RemoveAll()
del cur, row, rows
Get help from ArcGIS experts
Download the Esri Support App