1

I have geojson file as follows:

{
  "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
          ]
        ]
      }
    }
  ]
}

I want to replace LineString in "type": "LineString" with Polygon, and also, replace coordinates last point of each linestring by coordinates of first point to make it close if it has more than 3 points.

How can I do it in Python with geopandas or pandas? Thanks.

Here is expected output:

{
  "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
          ]
        ]
      }
    }
  ]
} 

Script to get type and coordinates of first 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']
3
  • This can be done using json module in python. please refer this Commented Mar 15, 2019 at 8:13
  • what have you tried so far? Take a look at how to read and parse jsons into python dictionaries (using json module), and work with those. Commented Mar 15, 2019 at 8:23
  • Thanks for your comment. I'll try and update my question. Commented Mar 15, 2019 at 8:37

1 Answer 1

4

You can achieve that with the json module:

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.