def substring_3d(linestring, from_, to_):
"the linestring a shapely geometry, from_ and to_ are in length units"
tot_len = 0
sq = lambda x: x*x
def interpolate(a, b, ratio):
return (a[0] + ratio*(b[0] - a[0]), a[1] + ratio*(b[1] - a[1]), a[2] + ratio*(b[2] - a[2]))
res = []
for s, e in zip(linestring.coords[0:-1], linestring.coords[1:]):
length = sqrt(sq(s[0]-e[0]) + sq(s[1]-e[1]) + sq(s[2]-e[2]))
tot_len += length
if tot_len > from_:
if not len(res):
#interpolate first
res.append(interpolate(e, s, (tot_len - from_)/length))
if tot_len >= to_:
#interpolate last
res.append(interpolate(e, s, (tot_len - to_)/length))
break
res.append(e)
return LineString(res)
评论列表
文章目录