Я также планирую включить третий фрейм данных geopandas (
Код: Выделить всё
linesМожно ли импортировать сеть связанных линий (
Код: Выделить всё
roadsКод: Выделить всё
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import LineString
point1 = pd.DataFrame({
'Cat': ['t1', 't2'],
'LAT': [-20, -30],
'LON': [140, 145],
})
point2 = pd.DataFrame({
'Cat': ['a', 'b'],
'LAT': [-30, -20],
'LON': [140, 145],
})
lines = pd.DataFrame({
'Cat': ['1', '1','2','2','3','3'],
'LAT': [-10, -35, -30, -30, -20, -20],
'LON': [140, 140, 130, 148, 145, 145],
})
P1_gpd = gpd.GeoDataFrame(point1, geometry = gpd.points_from_xy(point1.LON, point1.LAT, crs = 4326))
P2_gpd = gpd.GeoDataFrame(point2, geometry = gpd.points_from_xy(point2.LON, point2.LAT, crs = 4326))
lines_gpd = gpd.GeoDataFrame(lines, geometry = gpd.points_from_xy(lines.LON, lines.LAT, crs = 4326))
P1_gpd = P1_gpd.to_crs("epsg:4326")
P2_gpd = P2_gpd.to_crs("epsg:4326")
lines_gpd = lines_gpd.to_crs("epsg:4326")
roads_gpd = lines_gpd.groupby(['Cat'])['geometry'].apply(lambda x: LineString(x.tolist()))
roads_gpd = gpd.GeoDataFrame(roads_gpd, geometry='geometry')
nearest_points = gpd.sjoin_nearest(P1_gpd, P2_gpd,
distance_col="nearest_distance", lsuffix="left", rsuffix="right")
print(nearest_points)
fig, ax = plt.subplots()
P1_gpd.plot(ax = ax, markersize = 10, color = 'blue', zorder = 2)
P2_gpd.plot(ax = ax, markersize = 10, color = 'red', zorder = 2)
roads_gpd.plot(ax = ax, color = 'black')
plt.show()
Я хочу преобразовать расстояние в км. Я сделал приблизительные расчеты. Расстояние первой точки составляет 1107 км, а второй — 482 км.
Предполагаемый результат:
Код: Выделить всё
Cat_left LAT_left LON_left geometry index_right Cat_right LAT_right LON_right nearest_distance
0 t1 -20 140 POINT (140.00000 -20.00000) 0 a -30 140 1112
1 t2 -30 145 POINT (145.00000 -30.00000) 1 a -30 140 481

Подробнее здесь: https://stackoverflow.com/questions/776 ... ork-python