ogr2ogr Lat/Long Order – Specifying Lat/Long Order on Conversion to GeoJSON

geojsonlatitude longitudeogr

When using ogr2ogr to convert a polygon from a .shp file to GeoJSON, I noticed that it writes the coordinates in order lat, long. I noticed this because whenever I tried to view the GeoJSON on a map, it would place the polygon in the middle of nowhere; I then read a bit more about GeoJSON and realized that the format requires the (x,y,z) order (long,lat,altitude) [source]. Is there a way for me to specify that ogr2ogr should write the GeoJSON in that order? Or is there any other way for me to get around this problem?

I'm 100% open to the possibility that I'm misunderstanding GeoJSON and that it doesn't actually have to be in long,lat,altitude order.

Best Answer

Looks like ogr2ogr has support for exactly this problem.

The below is copied directly from that page:

How do I flip coordinates when they are not in the expected order

The EPSG has a recommanded order for geographic SRS where the coordinates tuples of a geometry must appear in the (latitude, longitude) order, whereas most GIS will properly display such geometries if they appear in the (longitude, latitude) order. This issue can be often encountered in situation with GML3 files, WFS 1.1 data, etc... that adhere to the (latitude, longitude) order.

When, for some reason, the coordinate order isn't the one that is wished, the following trick can be used to flip them:

ogr2ogr -s_srs "+proj=latlong +datum=WGS84 +axis=neu +wktext"
        -t_srs "+proj=latlong +datum=WGS84 +axis=enu +wktext" dest.shp source.shp

... An alternative way of achieving the same result, providing using GDAL 2.0 compiled with Spatialite support to benefit from SQLite SQL dialect, is:

ogr2ogr -dialect SQLite -sql "SELECT SwapCoordinates(geometry) AS geometry, * FROM source" dest.shp source.shp