2

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

2 Answers 2

3

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,))
3
  • How can i create one line feature from multiple features using this method? I don't want to merge the records. I want to create onle line with the vertices. Thanks
    – BBG_GIS
    Commented Oct 31, 2016 at 15:08
  • You would try the geometry method union - geom1.union(geom2) but it depends on exactly what you want to do: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/…
    – bebego
    Commented Oct 31, 2016 at 15:12
  • Be certain that your geometries share a common spatial reference, or all geometry tests and operators will fail.
    – Vince
    Commented Nov 1, 2016 at 2:03
1

I assumed you need geometric operation on vertices.So you just mis-positioned the insert cursor block:

# Imports
import arcpy
# Spatial reference of input feature class
SR = arcpy.SpatialReference(32646)
InFc=r'C:\Users\USER\Documents\ArcGIS\Default.gdb\ddd\eee'
output = r'C:\Users\USER\Documents\ArcGIS\Default.gdb\ddd\eee1'
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:
                longt = pnt.X
                lat = pnt.Y
                point = arcpy.Point(longt,lat)
                print point
                vertexArray.add(point)
        with arcpy.da.InsertCursor(output, ("SHAPE@",)) as cursor:
            polyline = arcpy.Polyline(vertexArray, SR  )
            cursor.insertRow((polyline,))
        partnum += 1

Rather than creating feature for each part, create feature for each row :)


But if you do not need vetexwise geometric operation then just use copy feature function.

import arcpy
InFc=r'C:\Users\USER\Documents\ArcGIS\Default.gdb\ddd\eee'
output = r'C:\Users\USER\Documents\ArcGIS\Default.gdb\ddd\eee1'
features = []
for row in arcpy.da.SearchCursor(InFc, ["OID@", "SHAPE@"]):
    print("Feature {}:".format(row[0]))
    features.append(row[1])
arcpy.CopyFeatures_management(features, output)
3
  • do you even need to the arcpy.Array() or would it be possible to just insert the point = arcpy.Point(longt,lat) straight into the arcpy.Polyline??
    – ziggy
    Commented Oct 31, 2016 at 14:35
  • arcpy.Polyline needs an array as input..
    – Learner
    Commented Oct 31, 2016 at 14:38
  • @SIslam Thanks. I also need to create one feature from multiple features using arcpy. I don't want to merge the records.
    – BBG_GIS
    Commented Oct 31, 2016 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.