Refer this code instead:
import arcpy
import math
import os
import numpy as np
from numpy import trapz
arcpy.env.overwriteOutput = True
fc = r'D:/PTWork/GIS_RIVER/23_elevation.shp'
fld_memo = 'TxtMemo'
fc_river = r'D:/PTWork/GIS_RIVER/RB_23.shp'
fc_out = r'D:/PTWork/GIS_RIVER/RiverBank1.shp'
fld_id = 'LineID'
id_format = 'CS-{}'
tolerance = 350
dist = 100 # distance, in units of projection, to extend the line on each end
flds = ('OID@', 'SHAPE@', fld_memo)
lst_data = [[r[0], r[1], float(r[2])] for r in arcpy.da.SearchCursor(fc, flds)]
# create output featureclass
sr = arcpy.Describe(fc).spatialReference
ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(ws, fc_name, "POLYLINE", None, None, "ENABLED", sr)
arcpy.AddField_management(fc_out, fld_id, "TEXT", None, None, 25)
# insert cursor
flds = ('SHAPE@', fld_id)
cnt_id = 0
with arcpy.da.InsertCursor(fc_out, flds) as curs:
lst_lines = []
lst_pnts = []
for i in range(1, len(lst_data)):
print("i:{}".format(i))
lst_cur = lst_data
lst_prev = lst_data[i-1]
pntg_cur = lst_cur[1]
pntg_prev = lst_prev[1]
dist = pntg_prev.distanceTo(pntg_cur)
if dist <= tolerance:
# add to existing list
pnt = pntg_cur.firstPoint
z = lst_cur[2]
pnt.Z = z
lst_pnts.append(pnt)
else:
print(" - create and store line and add point to new list...")
# create and store line and add point to new list
if len(lst_pnts) > 1:
polyline = arcpy.Polyline(arcpy.Array(lst_pnts), sr, True, False)
lst_lines.append(polyline)
cnt_id += 1
line_id = id_format.format(cnt_id)
fc_line = arcpy.MakeFeatureLayer_management(polyline, "fc_line")
fc_poly = arcpy.MakeFeatureLayer_management(fc_river, "fc_poly")
with arcpy.da.SearchCursor(fc_poly, "SHAPE@") as scur:
for poly, in scur:
boundary = poly.boundary()
arcpy.SelectLayerByLocation_management(fc_line, "WITHIN", poly)
with arcpy.da.SearchCursor(fc_line, "SHAPE@") as ucur:
for line, in ucur:
arr, = line.getPart()
SR = line.spatialReference
p1, p2 = arr[:2]
angle = math.atan2(p2.Y - p1.Y, p2.X - p1.X)
p = arcpy.Point(p1.X - dist * math.cos(angle),
p1.Y - dist * math.sin(angle))
arr.insert(0, p)
pn1, pn = arr[-2:]
angle = math.atan2(pn.Y - pn1.Y, pn.X - pn1.X)
p = arcpy.Point(pn.X + dist * math.cos(angle),
pn.Y + dist * math.sin(angle))
arr.append(p)
line = arcpy.Polyline(arr, SR)
line = line.cut(boundary)[1]
curs.insertRow((line, line_id))
else:
# not enough points to create line
pass
pnt = pntg_cur.firstPoint
z = lst_cur[2]
pnt.Z = z
lst_pnts = [pnt]