[GIS] How to insert polyline features with the list of vertices in another polyline

arcpycursorgeometry

I want to insert polyline features by another polyline vertices using arcpy.
I try to list X and Y coordinates of the source layer then create array from the XY and insert the array to the new feature class. the new feature class has records and length but not show any poyline.I have three segments in the source layer but the result is more than 3 records. I don't know what's the part of my code is wrong ?

# Imports
import arcpy
# Spatial reference of input feature class
SR = arcpy.SpatialReference(4326)
InFc=r"D:\_GIS\VerticesToLine.gdb\Line"
output = r"D:\_GIS\VerticesToLine.gdb\test"
for row in arcpy.da.SearchCursor(InFc, ["OID@", "SHAPE@"]):
    print("Feature {}:".format(row[0]))
    partnum = 0
    vertexArray = arcpy.Array()
    for part in row[1]:
        for pnt in part:
            if pnt:
                long = pnt.X
                lat = pnt.Y
                point = arcpy.Point(long,lat)
                print point
                vertexArray.add(point)
                with arcpy.da.InsertCursor(output, ("SHAPE@",)) as cursor:
                    polyline = arcpy.Polyline(vertexArray, SR  )
                    cursor.insertRow((polyline,))
        partnum += 1

Best Answer

If you don't need to perform an operation on the individual vertices, you can simply insert the geometry object into the new feature class.

# Imports
import arcpy
# Spatial reference of input feature class
SR = arcpy.SpatialReference(4326)
InFc=r"D:\_GIS\VerticesToLine.gdb\Line"
output = r"D:\_GIS\VerticesToLine.gdb\test"
for row in arcpy.da.SearchCursor(InFc, ["OID@", "SHAPE@"]):
    print("Feature {}:".format(row[0]))
    with arcpy.da.InsertCursor(output, ("SHAPE@",)) as cursor:
        cursor.insertRow((row[1],))

Or, you could use list comprehension and avoid creating so many individual cursors:

geoms = [row[0] for row in arcpy.da.SearchCursor(InFc, ["SHAPE@"])]
with arcpy.da.InsertCursor(output, ["SHAPE@"]) as cur:
    for geo in geom:
        cur.insertRow((geo,))
Related Question