aud*_*tec 2 python shapely geopandas
我有一个名为 GeoPandas 数据框barrios,我使用barrios.geometry.centroid它并将其分配给center.
索引和值center也是如此-geopandas.geoseries.GeoSeriesPOINT (-58.42266 -34.57393)
我需要获取这些坐标并将它们保存为列表:
[(point_1_lat, point_1_lon), (point_2_lat, point_2_lon), ...]
Run Code Online (Sandbox Code Playgroud)
我试过:
[center.values.y , center.values.x]
Run Code Online (Sandbox Code Playgroud)
但它返回一个包含 2 个数组的列表 - [array(lat), array(lng)]。
我怎样才能得到想要的结果?
您可以使用zip来循环多个变量。这应该将坐标提取到列表中。
coord_list = [(x,y) for x,y in zip(gdf['geometry'].x , gdf['geometry'].y)]
Run Code Online (Sandbox Code Playgroud)
GeoDataFrame或者,您可以使用 x 和 y 坐标创建。首先,提取 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)
Run Code Online (Sandbox Code Playgroud)
这将返回GeoDataFrame带有 x 和 y 坐标列的 a。现在将坐标提取到列表中。
coordinate_list = [(x,y) for x,y in zip(gdf.x , gdf.y)]
Run Code Online (Sandbox Code Playgroud)
这返回坐标元组列表
[(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),
...
Run Code Online (Sandbox Code Playgroud)
您将有一个列表和一个包含 x 和 y 列的 GeoDataFrame。
| 归档时间: |
|
| 查看次数: |
8676 次 |
| 最近记录: |