如何将 geojson 转换为形状多边形?

how to convert geojson to shapely polygon?

我有一个 geoJSON

geo = {'type': 'Polygon',
 'coordinates': [[[23.08437310100004, 53.15448536100007],
   [23.08459767900007, 53.15448536100007],
   [23.08594514600003, 53.153587050000056],
   (...)
   [23.08437310100004, 53.15448536100007]]]}

我想使用这些坐标作为 shapely.geometry.Polygon 的输入。问题是 Polygon 只接受 tuple 值,这意味着我必须将此 geojson 转换为多边形。当我尝试将这种类型的数据输入多边形时出现错误 ValueError: A LinearRing must have at least 3 coordinate tuples

我试过这个:

[tuple(l) for l in geo['coordinates']]

但这不太有效,因为它只有 returns 这个

[([23.08437310100004, 53.15448536100007],
  [23.08459767900007, 53.15448536100007],
  (...)
  [23.08437310100004, 53.15448536100007])]

我需要的是这个(我认为它是一个元组)

([(23.08437310100004, 53.15448536100007),
  (23.08459767900007, 53.15448536100007),
  (...)
  (23.08437310100004, 53.15448536100007)])

有这个功能吗?

试试这个,

from itertools import chain


geom = {...}
polygon = Polygon(list(chain(*geom['coordinates']))
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]])

通用解决方案是使用 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)