【问题标题】:geodataframe from dataframe with a Geojson serie来自具有 Geojson 系列的数据框的地理数据框
【发布时间】:2022-01-20 09:42:48
【问题描述】:

在我的数据框中,一个系列包含几何作为 GeoJson :

{"type":"Polygon","coordinates":[[[2.459721568...

我应该如何从中创建一个地理数据框?

【问题讨论】:

    标签: pandas geojson geopandas


    【解决方案1】:

    您可以使用https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.from_features.html。需要额外的步骤来格式化 dict,使其成为有效的功能

    样本数据

    import requests
    import geopandas as gpd
    import pandas as pd
    
    # get geometry of london underground stations so it is a dict in a column
    df = pd.DataFrame(
        requests.get(
            "https://raw.githubusercontent.com/oobrien/vis/master/tube/data/tfl_stations.json"
        ).json()["features"]
    ).drop(columns="properties")
    
    
    type geometry
    0 Feature {'type': 'Point', 'coordinates': [-0.279916688851386, 51.50264359676248]}
    1 Feature {'type': 'Point', 'coordinates': [-0.134745288767581, 51.565370996797775]}
    2 Feature {'type': 'Point', 'coordinates': [-0.071876588767481, 51.51514719676551]}
    3 Feature {'type': 'Point', 'coordinates': [-0.10612998877245, 51.53182149677656]}
    4 Feature {'type': 'Point', 'coordinates': [-0.075715588769394, 51.51409639676498]}
    5 Feature {'type': 'Point', 'coordinates': [-0.2994547888414, 51.54055479678616]}
    6 Feature {'type': 'Point', 'coordinates': [-0.608468688901916, 51.674206696875174]}
    7 Feature {'type': 'Point', 'coordinates': [-0.133268788743383, 51.61647559682913]}
    8 Feature {'type': 'Point', 'coordinates': [-0.108099325493406, 51.55782418669154]}
    9 Feature {'type': 'Point', 'coordinates': [-0.011585088740745, 51.52473659676975]}

    转换为几何体

    # convert bits of geometry to actual geometry
    gdf = gpd.GeoDataFrame.from_features(
        df["geometry"].apply(lambda g: {"geometry": g, "properties": {}})
    )
    
    geometry
    0 POINT (-0.279916688851386 51.50264359676248)
    1 POINT (-0.134745288767581 51.56537099679777)
    2 POINT (-0.071876588767481 51.51514719676551)
    3 POINT (-0.10612998877245 51.53182149677656)
    4 POINT (-0.07571558876939399 51.51409639676498)
    5 POINT (-0.2994547888414 51.54055479678616)
    6 POINT (-0.608468688901916 51.67420669687517)
    7 POINT (-0.133268788743383 51.61647559682913)
    8 POINT (-0.108099325493406 51.55782418669154)
    9 POINT (-0.011585088740745 51.52473659676975)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 2018-03-29
      • 2018-07-13
      • 2015-06-23
      • 2016-06-06
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多