Customer Service | Training | Contact Us
Welcome!
Login
Search Options   products areas display

Which products should be considered?

All Products

ArcCAD
ArcEditor
ArcExplorer
ArcGIS Engine
ArcGIS Explorer
ArcGIS Image Server
ArcGIS Mobile
ArcGIS Server
ArcIMS
ArcInfo Desktop
ArcInfo Workstation
ArcLogistics Route
ArcPad
ArcPad Application Builder
ArcReader
ArcSDE
ArcView
ArcView 3.x
ArcWeb Services APIs
ArcWeb Toolbar for ArcGIS
Atlas GIS
BusinessMap
BusinessMap Pro
GIS Portal Toolkit
Job Tracking for ArcGIS
MapIt
Maplex
MapObjects -- Java
MapObjects -- Windows
MapObjects IMS
MapObjects LT
MapStudio
Military Overlay Editor
NetEngine
PC ARC/INFO & DAK
PLTS
RouteMap
RouteMap IMS
SDE
Tracking Server

    Remember these settings for each visit More info
You are here:

Technical Article   HowTo:  Create a line based on origin and destination points with Python

Article ID: 31879
Software:  ArcGIS - ArcEditor 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms: N/A

Summary

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.

Procedure

This article contains a sample table and script that demonstrates one method for creating a line feature between an origin and destination point XY coordinates from a table.

  • The script creates a feature class, defines the coordinate system and then uses the information provided in the table to populate the feature class with the line geometries in ArcGIS 9.2 or higher release:

    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

  • The formatting of the source table should include X and Y data for both the origin and destination points. -show me-

    [O-Image] Points
  • The script can be expanded to process the output feature class in various ways. A few examples are adding fields and calculate values, appending the new values to an existing feature class, or adding features to an existing topology.
  • The sample code is slightly different for versions of ArcGIS prior to version 9.2:

    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


Created: 9/27/2006
Last Modified: 11/25/2009

This website's graphical display is now viewable only with W3C standards-compliant browsers, but the content is accessible to all browsers and Internet devices. View our supported browser matrix for more information on our website display.