Конвертация LineString в Polygon

Имеется файл json формата следующего вида:

{'type': 'FeatureCollection',
 'features': [{'id': '195',
   'type': 'Feature',
   'properties': {},
   'geometry': {'type': 'LineString',
    'coordinates': [[37.5185027, 55.7525846],
     [37.5202286, 55.7500222],
     [37.5203603, 55.7498517],
     [37.5220496, 55.7475834],
     [37.5224216, 55.7472846]]},
   'bbox': [37.5185027, 55.7472846, 37.5224216, 55.7525846]},
  {'id': '200',
   'type': 'Feature',
   'properties': {},
   'geometry': {'type': 'LineString',
    'coordinates': [[37.5592321, 55.7102802],
     [37.5609954, 55.7103378],
     [37.5654006, 55.7107574],
     [37.5696101, 55.7116173],
     [37.574928, 55.7131177],
     [37.5776083, 55.7142058]]]]},
   'bbox': [37.5592321, 55.7102802, 37.5776083, 55.7142058]}],
 'bbox': [36.8031012, 55.1421745, 37.9674277, 56.0212238]}

Необходимо его привести к следующему формату (изменить 'type': 'LineString' на 'type': 'Polygon', смэппив все координаты):

{'type': 'FeatureCollection',
 'features': [{'id': '1899',
   'type': 'Feature',
   'properties': {},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[37.5185027, 55.7525846],
     [37.5202286, 55.7500222],
     [37.5203603, 55.7498517],
     [37.5220496, 55.7475834],
     [37.5224216, 55.7472846],
     [37.5592321, 55.7102802],
     [37.5609954, 55.7103378],
     [37.5654006, 55.7107574],
     [37.5696101, 55.7116173],
     [37.574928, 55.7131177],
     [37.5776083, 55.7142058]]]},
   'bbox': [37.5592321, 55.7102802, 37.5776083, 55.7142058]}],
 'bbox': [36.8031012, 55.1421745, 37.9674277, 56.0212238]}

Нашлось подобное решение:

import geopandas as gpd
from shapely.geometry import Polygon, mapping

def linestring_to_polygon(fili_shps):
    gdf = gpd.read_file(fili_shps) #LINESTRING
    gdf['geometry'] = [Polygon(mapping(x)['coordinates']) for x in gdf.geometry]
    return gdf

Для обращения к координатам я использую:

geoJson['features'][0]['geometry']['coordinates']

Не совсем понимаю как получить все элементы 'geometry' и соединить их.

Как можно привести к формату 'type': 'Polygon' для передачи в качестве переменной fili_shps в функцию linestring_to_polygon?


Ответы (0 шт):