python 从具有坐标的列创建一个包含多边形的列
python create a column with polygons from column with coordinates
我需要向我的 df (df['poly']) 添加一列,它采用 df['coords'] 中的坐标(纬度和经度)并将它们转换为不同大小的多边形.有的key_id只有两分,有的有将近20分
我试过 shapely 但我不知道如何进行。我能做什么?
我的 df 有类似的结构:
df = pd.DataFrame ({'key_id': ['151,'244','245'],
'coords': [[[-8.37793, 37.42726],
[-8.29726, 37.42958],
[-8.1975, 37.35019],
[-8.07657, 37.3241],
[-7.98004, 37.40123],
[-7.73969, 37.49087],
[-7.51269, 37.52626]],
[[28.44261, 38.10466],
[28.61609, 38.08959]],
[[-5.7376, 40.29416],
[-5.78421, 40.292],
[-5.81097, 40.34586],
[-5.92499, 40.28829],
[-5.97971, 40.29835],
[-6.22156, 40.48251],
[-6.53513, 40.34072],
[-6.59286, 40.27262],
[-6.77174, 40.24218],
[-6.86514, 40.27069]]]
})
- 已下载:https://gisco-services.ec.europa.eu/distribution/v2/nuts/download/ref-nuts-2021-60m.geojson.zip
- 简单使用geopandas,无需弄清楚如何构建多边形
import json
import geopandas as gpd
from pathlib import Path
# NUTS_BN_60M_2021_3035.geojson
p = Path.home().joinpath("Downloads/ref-nuts-2021-60m").joinpath("NUTS_BN_60M_2021_3035.geojson")
with open(p) as f: js = json.load(f)
gpd.GeoDataFrame.from_features(js)
示例输出
geometry
EU_FLAG
EFTA_FLAG
CC_FLAG
LEVL_CODE
NUTS_BN_ID
COAS_FLAG
OTHR_FLAG
FID
0
LINESTRING (4710072.1544 1558882.7113, 4682530.865 1568851.5189)
T
F
F
0
155
T
F
155
1
LINESTRING (4750146.4639 1575613.1831, 4719326.4198 1565032.5462)
T
F
F
3
156
F
F
156
2
LINESTRING (4750146.4639 1575613.1831, 4771337.8016 1529624.4864)
T
F
F
3
157
F
F
157
3
LINESTRING (5708788.4877 1703805.8981, 5716323.6218 1677819.7608, 5699205.566099999 1671425.5691, 5689132.9338 1687843.7667, 5674013.6588 1672224.311, 5660024.3965 1679252.6566, 5666955.0392 1699891.5505, 5708788.4877 1703805.8981)
T
F
F
0
158
T
F
158
4
LINESTRING (2862160.935 1681571.7459, 2817387.2651 1732249.228499999, 2777974.267 1743031.7994)
T
F
F
0
159
T
F
159
我需要向我的 df (df['poly']) 添加一列,它采用 df['coords'] 中的坐标(纬度和经度)并将它们转换为不同大小的多边形.有的key_id只有两分,有的有将近20分
我试过 shapely 但我不知道如何进行。我能做什么?
我的 df 有类似的结构:
df = pd.DataFrame ({'key_id': ['151,'244','245'],
'coords': [[[-8.37793, 37.42726],
[-8.29726, 37.42958],
[-8.1975, 37.35019],
[-8.07657, 37.3241],
[-7.98004, 37.40123],
[-7.73969, 37.49087],
[-7.51269, 37.52626]],
[[28.44261, 38.10466],
[28.61609, 38.08959]],
[[-5.7376, 40.29416],
[-5.78421, 40.292],
[-5.81097, 40.34586],
[-5.92499, 40.28829],
[-5.97971, 40.29835],
[-6.22156, 40.48251],
[-6.53513, 40.34072],
[-6.59286, 40.27262],
[-6.77174, 40.24218],
[-6.86514, 40.27069]]]
})
- 已下载:https://gisco-services.ec.europa.eu/distribution/v2/nuts/download/ref-nuts-2021-60m.geojson.zip
- 简单使用geopandas,无需弄清楚如何构建多边形
import json
import geopandas as gpd
from pathlib import Path
# NUTS_BN_60M_2021_3035.geojson
p = Path.home().joinpath("Downloads/ref-nuts-2021-60m").joinpath("NUTS_BN_60M_2021_3035.geojson")
with open(p) as f: js = json.load(f)
gpd.GeoDataFrame.from_features(js)
示例输出
geometry | EU_FLAG | EFTA_FLAG | CC_FLAG | LEVL_CODE | NUTS_BN_ID | COAS_FLAG | OTHR_FLAG | FID | |
---|---|---|---|---|---|---|---|---|---|
0 | LINESTRING (4710072.1544 1558882.7113, 4682530.865 1568851.5189) | T | F | F | 0 | 155 | T | F | 155 |
1 | LINESTRING (4750146.4639 1575613.1831, 4719326.4198 1565032.5462) | T | F | F | 3 | 156 | F | F | 156 |
2 | LINESTRING (4750146.4639 1575613.1831, 4771337.8016 1529624.4864) | T | F | F | 3 | 157 | F | F | 157 |
3 | LINESTRING (5708788.4877 1703805.8981, 5716323.6218 1677819.7608, 5699205.566099999 1671425.5691, 5689132.9338 1687843.7667, 5674013.6588 1672224.311, 5660024.3965 1679252.6566, 5666955.0392 1699891.5505, 5708788.4877 1703805.8981) | T | F | F | 0 | 158 | T | F | 158 |
4 | LINESTRING (2862160.935 1681571.7459, 2817387.2651 1732249.228499999, 2777974.267 1743031.7994) | T | F | F | 0 | 159 | T | F | 159 |