Create a line with attributes based on origin and destination points with Python

488
4
09-26-2011 12:15 PM
RenatoSilveira_Borges
New Contributor
I need to create a line with attributes based on origin and destination points with Python.

The source table is like a:

ORIGIN_X, ORIGIN_Y, DEST_X, DEST_Y, ATTRIBUTE_1, ATTRIBUTE_2, ATTRIBUTE_3

I have used the code in this article [http://support.esri.com/en/knowledgebase/techarticles/detail/31879] to create the lines, but i can't put the attributes in the shapefile.

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
Tags (2)
0 Kudos
4 Replies
RenatoSilveira_Borges
New Contributor
No one? Help, please!!
0 Kudos
DuncanHornby
MVP Notable Contributor
It looks like you are almost there. All you need to do is create the attribute fields you wish to populate in your output dataset. You do this by using the AddField tool and do that after you have called the createfeatureclass tool. You already have the code to extract data from your input file so you simply extract your attributes and write these to your new output row which you are calling feat.

If you can't work out how to do that then upload a sample of your input file so people can see what fields you are wanting to pass to your output dataset.

Duncan
0 Kudos
KimOllivier
Occasional Contributor III
At 10.0 there is a tool to turn a table into points and another to turn points into lines.
To save some coding.
0 Kudos
OlivierOlivier
New Contributor III
I have a script that works using

      row.SetValue("y",pnt.y)
      rows.UpdateRow(row)

y field must exists before like Hornbydd said

Olivier
0 Kudos