3

This question has been asked before, but I don't understand how to use the answer given by cengel. I want to make a GeoJSON file with lat long coords, not the default of long lat coords. I have a current (wrong order) GeoJSON file, I have the original shp file it was created from and I can't for the life of me find a .gml that is referred to in the answer the first time this question was asked. Please Help! :(

Here is the old question: Exporting GeoJSON file with correct order of coordinates?

7
  • Why do you want a GeoJSON with coordinates in latitude, longitude order? According to standards, the coordinates should be in longitude, latitude order. Any sensible software will expect coordinates in this particular order. Feb 4, 2015 at 13:10
  • Clearly the software I am using isn't sensible then. It is to put in to some lua code and it requires Lat Long
    – Louise
    Feb 4, 2015 at 13:11
  • 1
    The GeoJSON spec says: "The order of elements must follow x, y, z order... longitude, latitude, altitude for coordinates in a geographic coordinate reference system..." If you are working with lua code, why can't you change it in that code? Feb 4, 2015 at 13:15
  • Cos I'm not that good at coding. There seemed to be an option for swapping the co-ords in the post I referenced, I am just unsure how to implement the instructions in qgis or anything else for that matter
    – Louise
    Feb 4, 2015 at 13:18
  • Can you post the lua-code. This should be easy to fix.
    – johanvdw
    Feb 4, 2015 at 13:29

2 Answers 2

4

There are couple of things which you might not know, which is causing all this issue.

Firstly the GeoJSON is supposed to have coordinates in longitude, latitude order. This can be seen in the specifications, which says:

The order of elements must follow x, y, z order (easting, northing, altitude for coordinates in a projected coordinate reference system, or longitude, latitude, altitude for coordinates in a geographic coordinate reference system).

Secondly, the Question which you have linked is actually about a different issue. That question deals with reading GML correctly, when it contains coordinates in latitude-longitude format. Hence the answers on it won't really help you.

Now coming to your issue. You need your data in a particular format. Looking at the code that you have posted, the geometry is expected in the following format:

-- polygon format: 
-- { 
--   ["name"] = polypoints = { { { lat, long }, { lat, long }, ... }, { { lat, long }, { lat, long }, ... } },
--   ["name"] = polypoints = { { { lat, long }, { lat, long }, ... }, { { lat, long }, { lat, long }, ... } },
-- }

If you look at this format, you'll see that even though this data looks like JSON, neither is it valid JSON, nor does it come even close to any formal GeoJSON.

Hence it really won't help you, even if we manage to produce GeoJSON with coordinates in latitude, longitude order.

So how do we solve your problem? Unfortunately, there isn't one easy solution. Some custom code will be required, or manual editing, none of which is really scales well.

You might best be served by getting the code re-written.

2
  • I can't get the code re-written. I realise the code I am using isn't formal GeoJSON, but apart from the order of the co-ords, it's very easy for me to re-format to work in this code. If there is a way to implement the solution outlined in the other topic, I'd love to find out more on how to do that. Or if someone know a couple of lines of code I could put into the code I have, that would also be great.
    – Louise
    Feb 5, 2015 at 5:56
  • @Louise: One possible way would be to export your data to GML, and then use the solution given in the linked question to export the GML to GeoJSON with the Axis Inverted. Feb 5, 2015 at 8:03
0

This is simpler let polyGonFubar = []; if(this.props.coords.geometry){ this.props.coords.geometry.coordinates[0].map((fubar, i) => (polyGonFubar.push([this.props.coords.geometry.coordinates[0][i][1],this.props.coords.geometry.coordinates[0][i][0]]))) }

Okay folks, here's some code for a loop to flip the coordinates. Just pay attention to the number of brackets surrounding the coordinates. I noticed differences between ArcMap and Qgis. Add or remove the appropriate amount of brackets to make the code work. I included a couple console.logs for this purpose. This uses shape as a variable but rename it to whatever you wish and paste in your geojson. Good luck! This helped me out a lot for leaflet where coordinates need to be flipped sometimes. The collective result will appear in the demo3 element when you launch the code.

<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>

<script>
  var shape= {
    "type": "FeatureCollection",
    "name": "shape",
    "crs": {
      "type": "name",
      "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
      }
    },
    "features": [{
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [-122.609028095726003, 47.338238713328302],
                [-122.609025973776994, 47.3377970233905],
                [-122.609021550275003, 47.337205925864403],
                [-122.610343369934995, 47.337171414964402],
                [-122.610345939832001, 47.338338659425197],
                [-122.609220494053005, 47.338361467095197],
                [-122.609089253435997, 47.338364053954997],
                [-122.609029056291007, 47.338365253124003],
                [-122.609028095726003, 47.338238713328302]
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [-122.615707340569003, 47.340769368725901],
                [-122.615066667625001, 47.340770615121698],
                [-122.615085309576997, 47.338979615811397],
                [-122.615720865713996, 47.338980247640798],
                [-122.616370283872996, 47.338985380060002],
                [-122.616364130753993, 47.340774017453199],
                [-122.615707340569003, 47.340769368725901]
              ]
            ]
          ]
        }
      }  
    ]
  }

  collectiveString = "";
  for (j = 0; j < shape.features.length; j++){
    var coordinates = shape.features[j].geometry.coordinates;
    var stringCoordinates = JSON.stringify(coordinates);

    var sliceCoordinates1 = stringCoordinates.slice(3);
    var sliceCoordinates2 = sliceCoordinates1.slice(0, -3);
    var array = JSON.parse("[" + sliceCoordinates2 + "]");
    console.log(array);


    var a = array

    var b = []
    for (var i = 0; i < a.length; i++) {
      b.push([a[i][1], a[i][0]]);
    }

    console.log(b);

    var stringArray = JSON.stringify(b);

    document.getElementById("demo1").innerHTML = sliceCoordinates2;
    document.getElementById("demo2").innerHTML = stringArray;

    collectiveString = collectiveString + stringArray + "<br>" + "<br>";
    }

    document.getElementById("demo3").innerHTML = collectiveString;


</script>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.