我有一个geoJSON
geo = {'type': 'Polygon',
'coordinates': [[[23.08437310100004, 53.15448536100007],
[23.08459767900007, 53.15448536100007],
[23.08594514600003, 53.153587050000056],
(...)
[23.08437310100004, 53.15448536100007]]]}
我想用这些坐标作为shapely.geometry.Polygon
的输入。问题是多边形只接受tuple
值,这意味着我必须将这个geojson转换成一个多边形。当我尝试将这种类型的数据输入多边形时,会出现一个错误ValueError: A LinearRing must have at least 3 coordinate tuples
我试过这个:
[tuple(l) for l in geo['coordinates']]
但是这个不太好用,因为它只返回
[([23.08437310100004, 53.15448536100007],
[23.08459767900007, 53.15448536100007],
(...)
[23.08437310100004, 53.15448536100007])]
我需要的是这个(我想这是个元组)
([(23.08437310100004, 53.15448536100007),
(23.08459767900007, 53.15448536100007),
(...)
(23.08437310100004, 53.15448536100007)])
有这个功能吗?
发布于 2021-12-02 12:53:30
一个通用的解决方案是使用shape
函数。这适用于所有的几何图形,而不仅仅是多边形。
from shapely.geometry import shape
from shapely.geometry.polygon import Polygon
geo: dict = {'type': 'Polygon',
'coordinates': [[[23.08437310100004, 53.15448536100007],
[23.08459767900007, 53.15448536100007],
[23.08594514600003, 53.153587050000056],
[23.08437310100004, 53.15448536100007]]]}
polygon: Polygon = shape(geo)
发布于 2021-08-17 15:40:10
尝尝这个,
from itertools import chain
geom = {...}
polygon = Polygon(list(chain(*geom['coordinates']))
发布于 2021-08-17 15:41:57
from shapely.geometry import Polygon
geo = {'type': 'Polygon',
'coordinates': [[[23.08437310100004, 53.15448536100007],
[23.08459767900007, 53.15448536100007],
[23.08594514600003, 53.153587050000056],
[23.08437310100004, 53.15448536100007]]]}
Polygon([tuple(l) for l in geo['coordinates'][0]])
https://stackoverflow.com/questions/68820085
复制相似问题