簡體   English   中英

如何使用 GeoPandas 將點的 GeoSeries 轉換為元組(經緯度)列表

[英]How to convert a GeoSeries of points to a list of tuples (lat, lon) using GeoPandas

我有一個名為barrios的 GeoPandas 數據框,我使用barrios.geometry.centroid並將其分配給center

所以, centergeopandas.geoseries.GeoSeries與索引和價值觀- POINT (-58.42266 -34.57393)

我需要獲取這些坐標並將它們保存為列表

[(point_1_lat, point_1_lon), (point_2_lat, point_2_lon), ...]

我試過:

[center.values.y , center.values.x]

但它返回一個包含 2 個數組的列表 - [array(lat), array(lng)]

我怎樣才能得到想要的結果?

您可以使用zip來循環訪問多個變量。 這應該將坐標提取到列表中。

coord_list = [(x,y) for x,y in zip(gdf['geometry'].x , gdf['geometry'].y)]

或者,您可以使用 x 和 y 坐標創建一個GeoDataFrame 首先,提取 x 和 y 坐標並將它們放在新列中。

import geopandas as gpd
url = r"link\to\file"
gdf = gpd.read_file(url)

gdf['x'] = None
gdf['y'] = None

gdf['x'] = gdf.geometry.apply(lambda x: x.x)
gdf['y'] = gdf.geometry.apply(lambda x: x.y)

這將返回一個帶有 x 和 y 坐標列的GeoDataFrame 現在將坐標提取到列表中。

coordinate_list = [(x,y) for x,y in zip(gdf.x , gdf.y)]

這將返回坐標元組列表

[(105.27, -5.391),
 (107.615, -6.945264),
 (107.629, -6.941126700000001),
 (107.391, -6.9168726),
 (107.6569, -6.9087003),
 (107.638, -6.9999),
 (107.67, -6.553),
 (107.656, -6.8),
 ...

您將有一個列表和一個帶有 x 和 y 列的 GeoDataFrame。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM