0

I'm trying to use geometries to cut down a feature class of polylines to just the first 400m of each polyline. I modified an answer from Split a shapefile of lines into even spaced segments and am receiving an error. I believe the error is related to CopyFeatures not being able to accept multiple inputs, but writing out the geometries to individual feature classes is taking a while. Does anyone have a recommendation for ways to write all 2000+ polylines into one feature class? (update cursor? Does merge work with geometries?)

This is using ArcPy with ArcGIS Pro 2.1.2

featlist = []
out_fc = "outfcname"
with arcpy.da.SearchCursor("inlyr", ["myid", "SHAPE@"]) as cursor:
    for row in cursor:
        k_id = row[0]
        feat = row[1].segmentAlongLine(0,400)
        featlist.append(feat)

arcpy.CopyFeatures_management([item for sublist in featlist for item in sublist],out_fc)

Error below:

Traceback (most recent call last): File "", line 1, in File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 2101, in CopyFeatures raise e File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 2098, in CopyFeatures retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True))) File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing_base.py", line 506, in return lambda *args: val(*gp_fixargs(args, True)) RuntimeError: Object: Error in executing tool


EDIT: Just adding some clarification for others who may land on this question.

In testing, featlist starts out as a list of polyline objects e.g. [<Polyline object at 0x2172f6eb588[0x2172f6e9530]>, <Polyline object at 0x2172f6eb5c0[0x2172f6e9580]>, <Polyline object at 0x2172f6eb5f8[0x2172f6e95d0]>]. The list comprehension turns it into a list of point arrays. (It's unclear whether CopyFeatures can handle list list of point arrays, and it would not be my desired output anyway.)

I believe the question I referenced for this code was turning each line into multiple features and just appending the new list of polylines into featlist so this makes sense (a better way may be to extend the overall list in that answer rather than creating nested lists, which would not require later expansion of the list.)

CopyFeatures DOES work with multiple input geometries of polylines, so using arcpy.CopyFeatures(featlist, out_fc) tested OK (note: loses attributes)

UpdateCursor does work with geometries so for my purposes (trim the line, retaining attributes) it was the best solution. As this modifies in place, copy the feature class before proceeding.

In terms of speed, the SearchCursor/CopyFeatures method was on par with the UpdateCursor method, with UpdateCursor edging out the former due to need to perform spatial join to get back the attributes.

  • 1
    There should be no need to iterate over featlist and the way you are doing it is iterating over a nested list which i dont Think you have. Have you tried arcpy.CopyFeatures_management(featlist, out_fc)? What are you using k_id for? – BERA May 14 at 17:13
  • k_id is currently being used in my workaround of one-at-a-time copy. If I can get to a point where it's able to be canceled, I'll try again using featlist instead of the list comprehension from the other question. – smiller May 14 at 17:21
  • I'm surprised not to see an InsertCursor used here. – PolyGeo May 14 at 18:53
  • 1
    Yes - an InsertCursor needs something to insert into. I usually CreateFeatureClass or CreateTable and do a few AddFields before using one. – PolyGeo May 14 at 19:03
  • 1
    I strongly suspect that [item for sublist in featlist for item in sublist] should just be featlist. – Emil Brundage May 14 at 19:19
1

I would say either the error is because out_fc is not a full path name or that list comprehension is flawed? I'll be honest that style of coding makes no sense to me so when I spoof up an example as such:

sublist = [1,2,3,4,5,6]
featlist = ["a","b","c","d","e","f"]
x= [item for sublist in featlist for item in sublist]
print x

it just returns

['a', 'b', 'c', 'd', 'e', 'f']

So my gut feeling is that is where the problem lies? I'm not sure what the intended return of such syntax is meant to be?

  • Not sure, as that's the main line that is coming from the old SE answer. (I'm confident in the out_fc.) – smiller May 14 at 15:51
  • 2
    Can't it just be: arcpy.CopyFeatures_management(featlist,out_fc) – Hornbydd May 14 at 15:52
  • 2
    featlist is a list of tuples, so it's more like: featlist = [("a","a2"),("b","b2"),("c","c2"),("d","d2"),("e","e2"),("f","f2")] The list comprehension is breaking apart the tuples with that sublist declaration. Either way, that's not how I expect CopyFeatures_management to work. – Richard Morgan May 14 at 15:55
  • 1
    I've definitely seen lists used as input to copy feature, have a look at example 11 here otherwise official documentation on writing geometries is here. – Hornbydd May 14 at 16:13
  • 1
    Included additional testing details in the question body. Summary: list comprehension was unnecessary because my intermediate output was not list of lists. CopyFeatures does work with list input (although not noted in docs). UpdateCursor was better solution for my use case. Thanks! – smiller May 15 at 14:04

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.