Search code examples
javascriptawksedgeojsontext-processing

How top reorganize geo JSON file


I have a geoJSON file which is in the format

{
    "type": "Feature",
    "id":"01",
    "properties": {
        "name": "AREA"

    },
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
[103.83994102478027, 1.5533451402176797], [103.84079933166502, 1.5600374631000955], [103.84440422058104, 1.5627830253088353], [103.85178565979004, 1.5632978178235577], [103.85693550109862, 1.5566055053011625], [103.8552188873291, 1.548368783815046], [103.84852409362793, 1.546995993784303], [103.84320259094238, 1.5480255863906685], [103.8413143157959, 1.550427967193869],[103.83994102478027, 1.5533451402176797]
                ]
            ]
        ]
    }
},

{
        "type": "Feature",
        "id":"02"

.... .... .... and so on.

But when I validate this it shows me the geoJSON is not following the right hand rule.But when I plot it in the map it works fine.But I wanted mine to be a valid geoJSON So How can I change this to

{
        "type": "Feature",
        "id":"01",
        "properties": {
            "name": "AREA"

        },
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
    [103.83994102478027, 1.5533451402176797],[103.8413143157959, 1.550427967193869],[103.84320259094238, 1.5480255863906685] ,[103.84320259094238, 1.5480255863906685],[103.84852409362793, 1.546995993784303], [103.8552188873291, 1.548368783815046],[103.85693550109862, 1.5566055053011625],[103.85178565979004, 1.5632978178235577], [103.84440422058104, 1.5627830253088353],[103.84079933166502, 1.5600374631000955],[103.83994102478027, 1.5533451402176797]
                    ]
                ]
            ]
        }
    },



{
            "type": "Feature",
            "id":"02"

....
....
.... and so on.

Any help is appreciated.


Solution

  • Here is a JS function that will make sure the points of a polygon are ordered counterclockwise (as they should for the outside of a right-hand rule polygon).

    var testPoly = [[103.83994102478027, 1.5533451402176797], [103.84079933166502, 1.5600374631000955], [103.84440422058104, 1.5627830253088353], [103.85178565979004, 1.5632978178235577], [103.85693550109862, 1.5566055053011625], [103.8552188873291, 1.548368783815046], [103.84852409362793, 1.546995993784303], [103.84320259094238, 1.5480255863906685], [103.8413143157959, 1.550427967193869],[103.83994102478027, 1.5533451402176797]];
    
    function makePolyCCW(points) {
      var sum = 0;
      for (var i = 0; i < (points.length - 1); i++) {
        sum += (points[i+1][0] - points[i][0])*(points[i+1][1] + points[i][1]);
      }
      return sum > 0 ? points.slice().reverse() : points;
    }
    
    console.log(makePolyCCW(testPoly));
    

    It looks like you provided only a fragment of the full JSON file. Depending on what geometry types you need to fix (Polygon, MultiPolygon, etc.) the code may be more or less involved. If you need help applying the above function to a full file, maybe provide more details and maybe I can assist.