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
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,))
  • 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 Oct 31 '16 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/… – Ben Gosack Oct 31 '16 at 15:12
  • Be certain that your geometries share a common spatial reference, or all geometry tests and operators will fail. – Vince Nov 1 '16 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)
  • 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 Oct 31 '16 at 14:35
  • arcpy.Polyline needs an array as input.. – SIslam Oct 31 '16 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 Oct 31 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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