[GIS] How to create lines from points from different feature class ArcPy

arcpypython

I want to create lines based on two points:
1st line: point number 1 from pointFc, point number 1 from centFc
2nd line: point number 2 from pointFc, point number 2 from centFc
and so on

Where did I make mistake?

start_cursor = arcpy.SearchCursor(pointFc)
end_cursor = arcpy.SearchCursor(centFc)

desc = arcpy.Describe(pointFc)
desc2 = arcpy.Describe(centFc)
shapefieldname = desc.ShapeFieldName
OID_start = desc.OIDFieldName
OID_end = desc2.OIDFieldName

point = arcpy.Point()
array = arcpy.Array()
featureList = []

cursor = arcpy.InsertCursor(lineFc)
feat = cursor.newRow()

for start in start_cursor:
startFeature = start.getValue(shapefieldname)
start_id = start.getValue(OID_start)
pnt1 = startFeature.getPart()
for end in end_cursor:
    end_id = end.getValue(OID_end)
    endFeature = end.getValue(shapefieldname)
    pnt2 = endFeature.getPart()
    if start_id == end_id:
        point.X = pnt1.X
        point.Y = pnt1.Y
        array.add(point)
        point.X = pnt2.X
        point.Y = pnt2.Y
        array.add(point)
        polyline = arcpy.Polyline(array)
        array.removeAll()
        featureList.append(polyline)
        feat.shape = polyline
        cursor.insertRow(feat)

del feat, cursor, end_cursor, start_cursor

Best Answer

Here's how to do it using arcpy.da cursors (ArcGIS 10.1+):

>>> lines = []
>>> sr = arcpy.Describe("points1").spatialReference
>>> with arcpy.da.SearchCursor("points1",["OID@","SHAPE@"],'#',sr) as cursor1:
...     for row1 in cursor1:
...         with arcpy.da.SearchCursor("points2",["OID@","SHAPE@"],'#',sr) as cursor2:
...             for row2 in cursor2:
...                 if row1[0] == row2[0]:
...                     points = arcpy.Array([row1[1].centroid,row2[1].centroid])
...                     line = arcpy.Polyline(points,sr)
...                     lines.append(line)
... arcpy.CopyFeatures_management(lines,'in_memory\lines')

OR using existing tools (example below has OID values moved into "ID" field. OIDs will be recalculated when merged):

>>> arcpy.Merge_management(["points1","points2"],'in_memory\merged')
>>> arcpy.PointsToLine_management('in_memory\merged','in_memory\lines',"Id")