I've got some points pair coordinates (start and end points) which I have to transform into lines. Until now, I used an append of both the coordinates in a pippo.Point()
, a pippo.CalculateGeometry()
to define the geometry of each piont, and pippo.append(defined geometry)
to identify the pair of points, and then PointsToLine to obtain my line. This is quite time expensive to do for hundreds of lines.
Is there a shorter way to do this?
For example, place starting and ending point of each line in different fields of a single table and import lines directly without passing for points geometry.
Answer
This reads a table (Excel sheet in this case, but could be any table type) that looks like so:
S_X is start X point, E_X end X point, same for Y's. We iterate through the input table, then for each row, set the start/end X/Ys into a point, add that point to an array, then create a polyline from the array of two points. Then insert into the featureclass. Rinse and repeat.
import arcpy
in_rows = arcpy.SearchCursor(r"D:\Temp\Lines.xls\Sheet1$")
point = arcpy.Point()
array = arcpy.Array()
featureList = []
cursor = arcpy.InsertCursor(r"D:\Temp\Lines.shp")
feat = cursor.newRow()
for in_row in in_rows:
# Set X and Y for start and end points
point.X = in_row.S_X
point.Y = in_row.S_Y
array.add(point)
point.X = in_row.E_X
point.Y = in_row.E_Y
array.add(point)
# Create a Polyline object based on the array of points
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
# Append to the list of Polyline objects
featureList.append(polyline)
# Insert the feature
feat.shape = polyline
cursor.insertRow(feat)
del feat
del cursor
And you get your lines:
No comments:
Post a Comment