Python Shapely – Find which plot is greater than the other around intersection point

Question:

I have two LineStrings which are generated from random-like data. They share the same x-coordinate values. After finding their intersection point(s), I want to know which plot is greater than the other slightly before the intersection point.

Here an example with few points. The data is first stored as a Dataframe. Let df_x be the x-coordinate values.

import pandas as pd

list_x = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
df_x = pd.Dataframe(list_x, columns['x'])

And let df_y1 and df_y2 be the y-coordinate values from which we create the LineStrings.

list_y1 = [0.4313868408813774384521364663, 0.400220040342625963735454449, 0.3520666640644268160651811697, 
0.345984352372794268166537295, 0.398155510437967529583466821, 0.437966674094413503746160949, 
0.451106397514297879332095752, 0.4360869572516150333820663687, 0.4043518894585338864279971317, 
0.3678952585693463955971846937]

list_y2 = [0.1961768083439320855600354392, 0.09464658811259959427092021877, 0.1066391154978616373164403874, 
0.2220666455871398431511835168, 0.4231647801532083001261756947, 0.626686716228464299638171260, 
0.7952862856139716356959392553, 0.907304564678484330893194896, 0.967834848336290608597376067,
0.9937582769463446912776820287]

df_y1 = pd.Dataframe(list_y1, columns['y1'])
df_y2 = pd.Dataframe(list_y2, columns['y2'])

Then,

from shapely.geometry import LineString

line1 = LineString(np.column_stack((df_x, df_y1)))
line2 = LineString(np.column_stack((df_x, df_y2)))

I thereafter search for potential intersection points between these two LineString.

inter_point = line1.intersection(line2)

print(inter_point.wkt)

>>> POINT (0.3832070251455818 0.3893944209828242)

Here a plot for visual representation (green plot is line1, blue plot is line2):
Two plots that intersects

In the example, it should return that line1 is (locally) greater than line2 before the intersection point. How can I find which plot is greater than the other slightly before this intersection point with Shapely?

With my actual data, it may happen that there are multiple intersection points between the two plots. I want to know how they intersect with one and another without looking at the figure.

Asked By: Aster

||

Answers:

I found a way to answer my issue which is based on Georgy’s answer in How to find the 2nd nearest point of a LineString in Shapely

However, in order to check how the LineStrings intersect with each other, we have to sometimes look before their intersection point, and sometimes after it. It is because we search for the second nearest point on each LineString.

We had:

inter_point = line1.intersection(line2)

Then, we do:

from shapely.ops import nearest_points

line1_nearest_point = nearest_points(line1, inter_point)[0]
line2_nearest_point = nearest_points(line1, inter_point)[0]

line1_except_nearest = MultiPoint([point for point in line1.coords 
                                   if inter_point != (line1_nearest_point.x, line1_nearest_point.y)])
line2_except_nearest = MultiPoint([point for point in line2.coords 
                                   if inter_point != (line2_nearest_point.x, line2_nearest_point.y)])
                
line1_second_nearest = nearest_points(line1_except_nearest, inter_point)[0]
line2_second_nearest = nearest_points(line2_except_nearest, inter_point)[0]

Now, we have the second nearest point on each LineString to the intersection point (they share the same abscissa value).

We can thereafter check which LineString is greater than the other, depending if we are in the interval [0, inter_point.x[ or ]inter_point.x, 1]. (That is because the values on the x-axis are between 0 and 1, and we assume that we have only one intersection point as presented in the example of the question).

# Before intersection point on x-axis
if (line1_second_nearest.x < inter_point.x) and (line2_second_nearest.x < inter_point.x): 
    if line1_second_nearest.y > line2_second_nearest.y:
        print('Line1 is greater than Line2 in the interval [0, inter_point.x[')
    else:
        print('Line2 is greater than Line1 in the interval [0, inter_point.x[')

# After intersection point on x-axis
else:
    if line1_second_nearest.y > line2_second_nearest.y: 
        print('Line1 is greater than Line2 in the interval [inter_point.x, 1[')
    else:
        print('Line2 is greater than Line1 in the interval [inter_point.x, 1[')
Answered By: Aster
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.