1

Building on Standalone Python script to split a polyline with a point layer, I don't think I am manipulating geometry objects correctly. I am splitting an original line, preserving all the attribution, and inserting new lines with the original attribution but updated geometry.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
layers = ["Primary OH Conductor", "Transformer Bank"]
line_lyr = layers[0]
point_lyr = layers[1]
points = [row[0] for row in arcpy.da.SearchCursor(point_lyr, "SHAPE@")]

line_fields = []
line_fields.append("SHAPE@")

try: arcpy.Delete_management("lines")
except: pass
arcpy.MakeFeatureLayer_management(line_lyr,"lines")
for rec in "lines":
    with arcpy.da.SearchCursor(in_table = "lines",
                               field_names = line_fields) as o_search_cur:
        feature_values = []
        for row in o_search_cur:
            feature_values.append(row)
            line = row[-1]
    for point in points:
        if line.distanceTo(point) < 0.001:  
            snappoint = line.snapToLine(point).firstPoint 
            if not (snappoint.equals(line.lastPoint) and snappoint.equals(line.firstPoint)): 
                cutline1, cutline2 = line.cut(arcpy.Polyline(arcpy.Array([arcpy.Point(snappoint.X+10.0, snappoint.Y+10.0),
                                                                          arcpy.Point(snappoint.X-10.0, snappoint.Y-10.0)]),
                                                             arcpy.SpatialReference(2276)))
                a = feature_values[0][:-1] + tuple(cutline1) 
                row_values = [a]
                with arcpy.da.InsertCursor(in_table = line_lyr,
                                           field_names = line_fields) as line_in_cur:
                    for row in row_values:
                        print("inserting into column " + str(line_fields))
                        print("values: " + str(row))
                        print("original feature values (for reference): " + str(feature_values[0]))
                        line_in_cur.insertRow(row)

And my results:

inserting into column ['SHAPE@']
values: (<Array [<Point (2336683.24, 7228439.51939, #, #)>, <Point (2336751.76429, 7228385.5766, #, #)>]>,)
original feature values (for reference): (<Polyline object at 0x5b409c90[0x8a000660L]>,)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 42, in <module>
TypeError: cannot read geometry sequence. expected list of floats
>>> 
mfrancis
  • 125
  • 1
  • 1
  • 10
  • What values does line_fields have in your code? Also, try line_in_cur.insertRow(row). No need to pass a tuple – Marcelo Villa Sep 17 '19 at 13:39
  • Thanks @marcello-villa... I updated the code to show how I constructed my field list. I want to keep everything that's not automatically maintained/generated. If I modify insertRow(row), I get SystemError: error return without exception set. – mfrancis Sep 17 '19 at 14:26
  • If you want to insert a new geometry, shouldn't you include the SHAPE field? – Berend Sep 17 '19 at 14:44
  • 1
    It seems to me that the error is caused because you are passing a different number of values than the fields you are specifying in field_names, so I'd start by checking if the values you are passing have the same length as the list of fields. Also, it seems that you are passing a tuple with another tuple of values inside to insertRow()... that doesn't seem quite right. Regarding the geometry, the first way you are passing the geometry should work. – Marcelo Villa Sep 17 '19 at 15:03
  • @Berend I think I am doing that--it is the last value appended in line_fields[] and it shows up as the last item to be inserted for each row In the OP, I included a sample from the original feature class (Polyline object) and a sample constructed for row, the one that looks like an array. – mfrancis Sep 17 '19 at 15:05
  • @MarceloVilla, thanks... In removing all fields except SHAPE@, I now am getting TypeError: cannot read geometry sequence. expected list of floats Maybe I am getting closer? – mfrancis Sep 17 '19 at 15:58
  • You haven't provided the code where the geometry is created, so it's difficult to tell what might be wrong. – Vince Sep 17 '19 at 16:00
  • @Vince because it's ugly, fraught with comments... and broken. I've cleaned some and posted the first part of the function, until the place where it breaks. Again, hats off to johnktran – mfrancis Sep 17 '19 at 16:31
  • @mfrancis See this: https://stackoverflow.com/help/minimal-reproducible-example – Paul H Sep 17 '19 at 17:34
  • 1
    From your example of the new row you're inserting, you look to be inserting an arcpy.Array() object, which isn't a valid geometry. Wrap that in an arcpy.Polyline() – mikewatt Sep 17 '19 at 17:39
  • 1
    Yes, construct the Polyline but be sure to use the SpatialReference of the target feature class in the constructor, lest the coordinates be truncated to four places. – Vince Sep 17 '19 at 18:00
  • @mikewatt, when I attempt a_geo = arcpy.Polyline(cutline1) that is when I get the cannot create geometry from inputs error. – mfrancis Sep 17 '19 at 20:58
  • for rec in "lines": iterates through the string "lines". This merely repeats your code 5 times and should be removed. – Emil Brundage Sep 18 '19 at 22:56
  • @EmilBrundage this is refactored per Paul H suggestion--that's where I intend to add in more fields, but geometry by itself first. At this point, I have verified that I am passing legitimate geometries into the cursor, but the cursor is crapping out. – mfrancis Sep 19 '19 at 13:31

1 Answers1

0

It's an ESRI bug with the da.insertCursor operating on features in a geometric network. NIM102778

mfrancis
  • 125
  • 1
  • 1
  • 10