Обновить значения в файле geojson в Python - PullRequest
1 голос
/ 15 марта 2019

У меня есть файл geojson следующим образом:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            57.45849609375,
            57.36801461845934
          ],
          [
            57.10693359375,
            56.31044317134597
          ],
          [
            59.205322265625,
            56.20059291588374
          ],
          [
            59.4140625,
            57.29091812634045
          ],
          [
            57.55737304687501,
            57.36801461845934
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            59.40307617187499,
            57.29685437021898
          ],
          [
            60.8203125,
            57.314657355733274
          ],
          [
            60.74340820312499,
            56.26776108757582
          ],
          [
            59.227294921875,
            56.21281407174654
          ],
          [
            59.447021484375,
            57.29091812634045
          ]
        ]
      }
    }
  ]
}

Я хочу заменить LineString в "type": "LineString" на Polygon, а также заменить координаты последней точки каждой linestring координатами первой точки, чтобы закрыть ее, если она имеет более 3 точек.

Как я могу сделать это в Python с геопандами или пандами? Благодаря.

Здесь ожидается вывод:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            57.45849609375,
            57.36801461845934
          ],
          [
            57.10693359375,
            56.31044317134597
          ],
          [
            59.205322265625,
            56.20059291588374
          ],
          [
            59.4140625,
            57.29091812634045
          ],
          [
            57.45849609375,
            57.36801461845934
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            59.40307617187499,
            57.29685437021898
          ],
          [
            60.8203125,
            57.314657355733274
          ],
          [
            60.74340820312499,
            56.26776108757582
          ],
          [
            59.227294921875,
            56.21281407174654
          ],
          [
            59.40307617187499,
            57.29685437021898
          ]
        ]
      }
    }
  ]
} 

Скрипт для получения type и coordinates первого LineString:

import json
from pprint import pprint

with open('data.geojson') as f:
    data = json.load(f)

pprint(data)

data["features"][0]["geometry"]['type']
data["features"][0]["geometry"]['coordinates']

1 Ответ

3 голосов
/ 15 марта 2019

Этого можно добиться с помощью модуля json:

file_line = 'file.json'
file_poly = 'file_poly.json'

import json
with open(file_line, 'r') as f:
    data = json.load(f)

for feature in data['features']:
    if (feature['geometry']['type'] == 'LineString') & (len(feature['geometry']['coordinates']) >= 3):
        feature['geometry']['type'] = 'Polygon'
        feature['geometry']['coordinates'].append(feature['geometry']['coordinates'][0])

with open(file_poly, 'w+') as f:
    json.dump(data, f, indent=2)

...