如何使用 GeoPandas 将点的 GeoSeries 转换为元组列表(经纬度)
How to convert a GeoSeries of points to a list of tuples (lat, lon) using GeoPandas
我有一个名为 barrios
的 GeoPandas 数据框,我使用 barrios.geometry.centroid
并将其分配给 center
。
所以 center
是 geopandas.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]
但它 returns 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)
这个 return 是一个 GeoDataFrame
具有 x 和 y 坐标列。现在将坐标提取到列表中。
coordinate_list = [(x,y) for x,y in zip(gdf.x , gdf.y)]
这个return坐标元组列表
[(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。
我有一个名为 barrios
的 GeoPandas 数据框,我使用 barrios.geometry.centroid
并将其分配给 center
。
所以 center
是 geopandas.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]
但它 returns 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)
这个 return 是一个 GeoDataFrame
具有 x 和 y 坐标列。现在将坐标提取到列表中。
coordinate_list = [(x,y) for x,y in zip(gdf.x , gdf.y)]
这个return坐标元组列表
[(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。