【问题标题】:How to convert a GeoSeries of points to a list of tuples (lat, lon) using GeoPandas如何使用 GeoPandas 将点的 GeoSeries 转换为元组列表(纬度、经度)
【发布时间】:2020-08-28 22:02:37
【问题描述】:

我有一个名为 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)]

我怎样才能得到想要的结果?

【问题讨论】:

    标签: python geopandas shapely


    【解决方案1】:

    您可以使用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。

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2021-05-23
      • 2018-10-02
      • 2021-04-15
      • 1970-01-01
      • 2020-01-26
      • 2013-09-09
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多