gpt4 book ai didi

python - 地理 Pandas : sort a sample of points like a cycle graph

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:17 25 4
gpt4 key购买 nike

我正在尝试使用 geopandas 来操作一些点数据。我的最终 GeoDataFrame 在那里表示:

20 little points

为了使用an other Python module它使用 OSM 数据计算两点之间的最短路径,我必须对我的点进行排序 like a tour

如果不是,下一个Python模块将计算最短道路,但不一定是最近点之间的道路。主要问题是旅游的限制。

如果我的点只是在一条线上,那么对每个点的纬度和经度进行基本排序功能就足够了,例如:

df1 = pd.read_csv("file.csv", sep = ",")
df1 = df1.sort_values(['Latitude','Longitude'], ascending = [1,1])
# (I'm starting with pandas df before GeoDataFrame conversion)

如果我们从上一张图片的“上”点开始排序,DataFrame 的第二个点将是最近的点,依此类推...直到第五个点,即图片的右侧(所以不再是最近的)...

所以我的问题是:有人知道如何实现这种特殊的排序,还是我必须手动更改索引?

最佳答案

如果我正确理解你的问题,你想重新排列点的顺序,以创建最短的路径。

我也遇到了同样的问题。这是接受常规数据帧的函数(= 每个坐标都有单独的字段。我相信您将能够修改任一函数以接受地理数据帧或数据帧,以便将几何字段拆分为 x 和 y 字段。

def autoroute_points_df(points_df, x_col="e",y_col="n"):

'''
Function, that converts a list of random points into ordered points, searching for the shortest possible distance between the points.
Author: Marjan Moderc, 2016
'''
points_list = points_df[[x_col,y_col]].values.tolist()

# arrange points in by ascending Y or X
points_we = sorted(points_list, key=lambda x: x[0])
points_sn = sorted(points_list, key=lambda x: x[1])

# Calculate the general direction of points (North-South or West-East) - In order to decide where to start the path!
westmost_point = points_we[0]
eastmost_point = points_we[-1]

deltay = eastmost_point[1] - westmost_point[1]
deltax = eastmost_point[0] - westmost_point[0]
alfa = math.degrees(math.atan2(deltay, deltax))
azimut = (90 - alfa) % 360

# If main directon is towards east (45°-135°), take westmost point as starting line.
if (azimut > 45 and azimut < 135):
points_list = points_we
elif azimut > 180:
raise Exception("Error while computing the azimuth! It cant be bigger then 180 since first point is west and second is east.")
else:
points_list = points_sn

# Create output (ordered df) and populate it with the first one already.
ordered_points_df = pd.DataFrame(columns=points_df.columns)
ordered_points_df = ordered_points_df.append(points_df.ix[(points_df[x_col]==points_list[0][0]) & (points_df[y_col]==points_list[0][1])])

for iteration in range(0, len(points_list) - 1):

already_ordered = ordered_points_df[[x_col,y_col]].values.tolist()

current_point = already_ordered[-1] # current point
possible_candidates = [i for i in points_list if i not in already_ordered] # list of candidates

distance = 10000000000000000000000
best_candidate = None
for candidate in possible_candidates:
current_distance = Point(current_point).distance(Point(candidate))
if current_distance < distance:
best_candidate = candidate
distance = current_distance

ordered_points_df = ordered_points_df.append(points_df.ix[(points_df[x_col]==best_candidate[0]) & (points_df[y_col]==best_candidate[1])])

return ordered_points_df

希望能解决您的问题!

关于python - 地理 Pandas : sort a sample of points like a cycle graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40873279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com