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
1 Answer 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
lang-py
point
object for start and end points, trying to traverse theend_cursor
multiple times (it is AFAIK possible only once). I'd go phloem's way in this case.