我正在尝试使用 geopandas 来操作一些点数据。我的最终 GeoDataFrame 在那里表示:
为了使用另一个Python模块(通过OSM数据计算两点之间的最短路径),我必须像旅行一样对我的点进行排序。
如果不是,下一个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
希望能解决您的问题!