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.
featlist
and the way you are doing it is iterating over a nested list which i dont Think you have. Have you triedarcpy.CopyFeatures_management(featlist, out_fc)
? What are you using k_id for?[item for sublist in featlist for item in sublist]
should just befeatlist
.