0

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
Chad Cooper
12.8k4 gold badges48 silver badges87 bronze badges
asked Feb 7, 2015 at 17:04
2
  • 1
    The old style cursors are notorious for not nesting properly. If using ArcGIS 10.1+, try arcpy.da cursors, or even easier Merge your points into one file and use Points to Line tool. Commented Feb 7, 2015 at 17:48
  • What error message or type of bad result does the script produce? I can see multiple possible issues - using the same point object for start and end points, trying to traverse the end_cursor multiple times (it is AFAIK possible only once). I'd go phloem's way in this case. Commented Feb 8, 2015 at 15:22

1 Answer 1

1

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")
answered Feb 8, 2015 at 19:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.