in my dataframe a serie contains gemetry as GeoJson :
{"type":"Polygon","coordinates":[[[2.459721568...
How should i create a geodataframe from it ?
CodePudding user response:
You can use https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.from_features.html. An additional step is required to format dict so it is a valid feature
sample data
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 to geometry
# 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) |