Shapely has now the split function (see Split lines at points using Shapely)
points = MultiPoint([(4,5),(9,18),(6,5)])
line = LineString([(1,2),(8,7),(4,5),(2,4),(4,7),(8,5),(9,18),(1,2),(12,7),(4,5),(6,5),(4,9)])
from shapely.ops import split
splitted = split(line, points)
for lin in splitted:
print lin
LINESTRING (1 2, 8 7, 4 5)
LINESTRING (4 5, 2 4, 4 7, 8 5, 9 18)
LINESTRING (9 18, 1 2, 12 7, 4 5, 6 5)
LINESTRING (6 5, 4 9)
You don't need a second list of points to find the slopes
import itertools
# iterate by pairs of points
slopes = [(second.y-first.y)/(second.x-first.x)for first, second in itertools.izip(points, points[1:])]
If you want the mean slope of the slopes
import numpy as np
def mean_slope(xs,ys):
m = (((np.mean(xs)*np.mean(ys)) - np.mean(xs*ys)) /((np.mean(xs)**2) - np.mean(xs*xs)))
return m
print mean_slope(slopes)
-2.2934712193638029
You can also use NumPy or SciPy
x, y = zip(*[(pt.x, pt.y) for pt in points])
slope, intercept = np.polyfit(x,y,1)
print slope
-2.2934712193638029
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
print slope
-2.2934712193638029
If the LineString is straight all the values of slopes = slope
If you want the slope between two point
first = points[5]
second = points[6]
print (second.y-first.y)/(second.x-first.x)
-2.2934712193638029 #straight line in my case
print np.polyfit([second.x,first.x],[second.y,first.y],1)[0]
-2.2934712193638029
print stats.linregress([second.x,first.x],[second.y,first.y])[0]
-2.2934712193638029
Best Answer
I don't understand your question:
You want the two lines which lie either side of the point ?
Upgrade 1: with a line with multiple vertices
You need to iterate through the segments of the LineString to find the one that contains the point
The LineString must be iterate as pair to divide the line in segments.
And you can use the previous answer: a rapid solution, for example ( this can be done better):
Result: