Point of Intersection between two lines using Geoplades

Asked

Viewed 666 times

1

Good morning to you all. I’m making a code and I came across a problem.

I am using Geopandas and I need to find the intersection between two lines (Linestring Z) and the respective elevation values (Z).

what has happened is that when executing the intersection function this returns me correcting X and Y, but not Z for each line.

Example: Intersection in line_Laranja = (2,1,3) Intersection in line_Verde = (2,1,4)

Thus, X and Y would be equal, but the elevations would not (Z1=3 and Z2=4)

Code used and result

line_Laranja.intersection.(line_Verde). z

Result = 3

line_Verde.intersection.(line_Laranja). z

Result = 3 (should be 4)

Below illustrated the problem.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Since the dots on my lines are not equal, this intersection XYZ must come from an interpolation.

Below follows the code I’m using:

import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString, Point


def load_xyz(file_path, file_name, spatial_reference, sep=';', header=0):
    df = pd.read_csv(file_path+'\\'+file_name, header=header, sep=sep,
                     names=['Line','X','Y','Z'])

    gdf = gpd.GeoDataFrame(df.values,
                           crs=spatial_reference,
                           geometry=[Point(xyz) for xyz in zip(df['X'], df['Y'], df['Z'])],
                           columns=['Line','X','Y','Z'])

    gdf = gdf.groupby(['Line'])['geometry'].apply(lambda x: LineString(x.tolist()))

    return gdf, df



spatial_reference = {'init': 'epsg:32724'}

file_path = r'C:\Users\VN\Desktop'
file_name = r'xyz.csv'
df_lines, df_lines_raw = load_xyz(file_path, file_name, spatial_reference)

file_path = r'C:\Users\VN\Desktop'
file_name = r'xyz_cross.csv'
df_cross, df_cross_raw = load_xyz(file_path, file_name, spatial_reference)


cross_validation = gpd.GeoDataFrame([],
                                    columns=['Line','Cross_Line','X','Y','Z1','Z2'],
                                    crs=spatial_reference)


lines_names = df_lines.index.tolist()
cross_names  = df_cross.index.tolist()
idx = 0


for i in range(len(df_lines)):
    for j in range(len(df_cross)):
        if(df_lines[i].crosses(df_cross[j])):
            cross_validation.loc[idx, 'Line'] = lines_names[i]
            cross_validation.loc[idx, 'Cross_Line'] = cross_names[j]
            cross_validation.loc[idx, 'X'] = df_lines[i].intersection(df_cross[j]).x
            cross_validation.loc[idx, 'Y'] = df_lines[i].intersection(df_cross[j]).y
            cross_validation.loc[idx, 'Z1'] = df_lines[i].intersection(df_cross[j]).z
            cross_validation.loc[idx, 'Z2'] = df_cross[j].intersection(df_lines[i]).z
            idx = idx + 1


print(cross_validation)

Thank you!

  • You put all the code, but it depends on the data that is in the CSV file. without knowing how this data is, it’s much harder for someone to reproduce their problem and be able to help you. If you have all the elements for people to be able to reproduce exactly what is happening is the only way to answer the question, with the exception of the very small number of people who are already familiar with Geopandals - these are the only ones who could hit the eye and say what’s wrong without being able to reproduce the problem locally.

1 answer

1

Try this function:

   def exists_intersection(line1, line2):

    def ccw(A,B,C):
        return (C[1]-A[1])*(B[0]-A[0]) > (B[1]-A[1])*(C[0]-A[0])

    def intersect(line1, line2):
        A,B = line1
        C,D = line2
        bol1 = ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
        bol2 = (A == C or A == D) or (B == C or B == D)
        return bol1 or bol2

    resultado = []

    x1, y1 = line1[0]
    x2, y2 = line1[1]

    x3, y3 = line2[0]
    x4, y4 = line2[1]

    px_a, px_b = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)), ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
    py_a, py_b = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)), ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))

    if ([px_a, px_b] != [0, 0] and [px_b, py_b] != [0, 0]):
        if intersect(line1, line2):
            px, py = px_a/px_b , py_a/py_b
            resultado = [px, py]

    return resultado

I used it for straight lines, I’m not sure of the result in curves, but it might help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.