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.
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.
– jsbueno