I've got a method for making cross-section points along a shapely LineString
object that I'm not totally happy with.
My strategy is:
- Create regular points every 50cm along a LineString
- Create an offset point 1cm away from the 50.1cm point
- Use these two points to find a slope
# Get 50cm spaced points
points = [linestr.interpolate(dist) for dist in np.arange(0, linestr.length, 0.5)]
# Get points at 50.1cm
_offsetpts = [linestr.interpolate(currDist+0.001) for currDist in np.arange(0, linestr.length, 0.5)]
# Use these two points to do rise/run and get a slope
slopes = [((points[idx].coords[0][1] - _offsetpts[idx].coords[0][1]) /
(points[idx].coords[0][0] - _offsetpts[idx].coords[0][0])) for idx, pt in enumerate(_offsetpts)]
It works but it's not very elegant and I worry it will break for edge cases (like when 1 extra cm runs over the edge of the line)
Does anyone know a better way to get the slope of a line a known distance along a LineString
object using python and shapely?
Best Answer
You don't need a second list of points to find the slopes
If you want the mean slope of the slopes
You can also use NumPy or SciPy
If the LineString is straight all the values of slopes = slope
If you want the slope between two point