Interpolating every X distance along multiline in shapely

送分小仙女□ 提交于 2019-12-05 06:38:05

As @mgc has pointed out, you must project the data to a Cartesian coordinate reference system that has distance units in metres.

I use a function to redistribute the vertices, which uses the interpolate linear referencing method. The distance argument is only approximate, since the total distance of the linestring may not be a multiple of the preferred distance. This function works on only [Multi]LineString geometry types.

from shapely import wkt
from shapely.geometry import LineString

def redistribute_vertices(geom, distance):
    if geom.geom_type == 'LineString':
        num_vert = int(round(geom.length / distance))
        if num_vert == 0:
            num_vert = 1
        return LineString(
            [geom.interpolate(float(n) / num_vert, normalized=True)
             for n in range(num_vert + 1)])
    elif geom.geom_type == 'MultiLineString':
        parts = [redistribute_vertices(part, distance)
                 for part in geom]
        return type(geom)([p for p in parts if not p.is_empty])
    else:
        raise ValueError('unhandled geometry %s', (geom.geom_type,))

# E.g., every 100 m on a projected MultiLineString
multiline_r = redistribute_vertices(multiline, 100)

In your first snippet of code you are iterating 150m by 150m but your dataset seems to be in longitude-latitude. You should reproject it in meters (in the appropriate CRS for your zone) as shapely take the distance parameter in the unit of the objet. Exemple taken from the shapely documentation :

from functools import partial
import pyproj

project = partial(
    pyproj.transform,
    pyproj.Proj(init=’espg:4326’),
    pyproj.Proj(init=’epsg:26913’)) # Replace by the appropriate CRS
g2 = transform(project, g1) # g1 is a shapely geometry

You can also avoid the transformation by using normalized value in the interpolate function :

# Progress of 10% of the length:
line.interpolate(0.10, normalized=True)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!