Based on another post, I wrote a Python script that reads coordinates, creates points, and then connects them. Here is the code:
import arcpy
in_rows = arcpy.SearchCursor(r"C:\Users\cchen\Documents\temp\test.gdb\linetest")
point = arcpy.Point()
array = arcpy.Array()
featureList = []
cursor = arcpy.InsertCursor(r"C:\Users\cchen\Documents\temp\test.gdb\newlines")
feat = cursor.newRow()
for in_row in in_rows:
# Set X and Y for start and end points
point.X = in_row.sx
point.Y = in_row.sy
array.add(point)
point.X = in_row.ex
point.Y = in_row.ey
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
It gives me this error message: Runtime error Traceback (most recent call last): File "", line 14, in File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects_base.py", line 89, in _set return setattr(self._arc_object, attr_name, cval(val)) RuntimeError: Point: Input value is not numeric
Since the field is numeric(double), I am confused on why this does not work.
-
You need to create more than one Point (inside the loop), since this code just moves one point around and attempts to create a line with only one (repeating) vertex. You should use a DA cursor for an order of magnitude performance benefit, and the Polyline constructor shoild include a SpatialReference.Vince– Vince2017年05月17日 15:00:31 +00:00Commented May 17, 2017 at 15:00
-
1try converting "in_row.sx", "in_row.sy","in_row.ex","in_row.ey" to float. i.e. float(in_row.sx). That error is probably becasue the ex,ey,sx, and sy values are strings.PMK– PMK2017年05月17日 15:13:18 +00:00Commented May 17, 2017 at 15:13
-
You might be able to use "XY to Line" to create your 2 point lines.klewis– klewis2017年05月17日 17:23:21 +00:00Commented May 17, 2017 at 17:23
1 Answer 1
search cursor is declaring all fields by default so in_row is a list of field values. You would need to access the shape by index so I would expect an error, but that the attribute .sx exist is a little confusing, but can modify.
in_rows = arcpy.SearchCursor(r"C:\Users\cchen\Documents\temp\test.gdb\linetest",
["SHAPE@XY"])
for in_row in in_rows:
x, y = in_row[0]
print("{}, {}".format(x, y))
Then you would need to get at least two unique points into the array to successfully create a polyline.