来自带有 Geojson 系列的数据框的地理数据框
geodataframe from dataframe with a Geojson serie
在我的数据框中,一个系列包含几何图形作为 GeoJson:
{“类型”:“多边形”,“坐标”:[[[2.459721568...
我应该如何从中创建地理数据框?
您可以使用 https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.from_features.html。需要一个额外的步骤来格式化 dict 所以它是一个有效的 feature
示例数据
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)
在我的数据框中,一个系列包含几何图形作为 GeoJson:
{“类型”:“多边形”,“坐标”:[[[2.459721568...
我应该如何从中创建地理数据框?
您可以使用 https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.from_features.html。需要一个额外的步骤来格式化 dict 所以它是一个有效的 feature
示例数据
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) |