在我的資料框中,一個系列包含幾何作為 GeoJson :
{“型別”:“多邊形”,“坐標”:[[[2.459721568 ...
我應該如何從中創建地理資料框?
uj5u.com熱心網友回復:
您可以使用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")
| 型別 | 幾何學 | |
|---|---|---|
| 0 | 特征 | {'type':'點','坐標':[-0.279916688851386,51.50264359676248]} |
| 1 | 特征 | {'type':'點','坐標':[-0.134745288767581,51.565370996797775]} |
| 2 | 特征 | {'type':'點','坐標':[-0.071876588767481,51.51514719676551]} |
| 3 | 特征 | {'type':'點','坐標':[-0.10612998877245,51.53182149677656]} |
| 4 | 特征 | {'type':'點','坐標':[-0.075715588769394,51.51409639676498]} |
| 5 | 特征 | {'type':'點','坐標':[-0.2994547888414,51.54055479678616]} |
| 6 | 特征 | {'type':'點','坐標':[-0.608468688901916,51.674206696875174]} |
| 7 | 特征 | {'type':'點','坐標':[-0.133268788743383,51.61647559682913]} |
| 8 | 特征 | {'type':'點','坐標':[-0.108099325493406,51.55782418669154]} |
| 9 | 特征 | {'type':'點','坐標':[-0.011585088740745,51.52473659676975]} |
轉換為幾何
# convert bits of geometry to actual geometry
gdf = gpd.GeoDataFrame.from_features(
df["geometry"].apply(lambda g: {"geometry": g, "properties": {}})
)
| 幾何學 | |
|---|---|
| 0 | 點 (-0.279916688851386 51.50264359676248) |
| 1 | 點 (-0.134745288767581 51.56537099679777) |
| 2 | 點 (-0.071876588767481 51.51514719676551) |
| 3 | 點 (-0.10612998877245 51.53182149677656) |
| 4 | 點 (-0.07571558876939399 51.51409639676498) |
| 5 | 點 (-0.2994547888414 51.54055479678616) |
| 6 | 點 (-0.608468688901916 51.67420669687517) |
| 7 | 點 (-0.133268788743383 51.61647559682913) |
| 8 | 點 (-0.108099325493406 51.55782418669154) |
| 9 | 點 (-0.011585088740745 51.52473659676975) |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384386.html
上一篇:以檔案名結尾的列python
下一篇:重命名資料框的行和列
