Sunday 23 April 2017

arcpy - Create line from two points, two different feature class


I'm attempting to automate this in ModelBuilder in ArcMap. I have one feature class with one point (Start) and one feature class with multiple points (Destinations). I want to iterate through the Destinations so I create multiple lines from the Start point to each Destination point.


I was looking at possibly extracting the XY coordinates and then using "XY To Line", but that wants the xy to be in one table (or feature), not two. Is there a simpler way of connecting two points?



Answer



I don't have Arc on this machine but this should work. It would actually work with multiple start points, but will work on a single start point as well.


import arcpy

start_feature = arcpy.GetParameterAsText(0)
end_feature = arcpy.GetParameterAsText(1)
out_feature = arcpy.GetParameterAsText(2)


start_cursor = arcpy.SearchCursor(start_feature)
end_cursor = arcpy.SearchCursor(end_feature)

desc = arcpy.Describe(start_feature)
shapefieldname = desc.ShapeFieldName

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


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

for start in start_cursor:
startFeature = start.getValue(shapefieldname)
pnt1 = startFeature.getPart()
for end in end_cursor:
endFeature = end.getValue(shapefieldname)
pnt2 = endFeature.getPart()

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

You may actually want to change the inputs so it creates the output file during the script instead of having to point at one that's already created, but that depends on what you want to do.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...