Below are detailed technical instructions for conversion of geojson files that do not have WGS84 as map projection.


Before you start


Examine the geojson files for the coordinate reference system (crs) used. If there is a line with "crs" not using WGS84 (same as EPSG:4326, and defined as "urn:ogc:def:crs:OGC:1.3:CRS84" in geojson files if a crs is used.


When geojson files have no line with "crs", coordinates should be in WGS84 anyway. In fact to produce valid geojson it should always contain WGS84 coordinates according to the standard (see https://geojson.org/ and https://datatracker.ietf.org/doc/html/rfc7946).


If geojson uses another crs then according to the standard it should be considered invalid (it is still valid json though). In real life there are multiple good reasons to 'extend' geojson with a crs of choice and/or other properties.

If there is a line with "crs" not using WGS84, then conversion is necessary to use the geojson in in any other web mapping app like Focus or display in geojson.io as these apps always expect geojson to use WGS84.


Example with WGS84

{
"type": "FeatureCollection",
`"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },`
...
}

Example not WGS84 ...

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32738" } },
...
}

NOTE: If you have any geojson files with coordinates not in WGS84, it might be easiest to check with supplier if exporting geojson in WGS84 is possible too to get the right data directly from the source. If so, you can stop reading here :-)

Requirements

ogr2ogr Infamous tool from the GDAL/OGR library to convert simple features data between file formats.

See https://gdal.org/download.html about how to install.

Use Conda cross OS

conda install -c conda-forge gdal

Or use something like Homebrew and brew install gdal on macOS.

As an alternative or for quick&dirty testing https://geojson.io can be used to convert geojson to another csr.

Steps

For every file run the command below. To make working on multiple files easier some code is added to loop through all files in a folder assuming they have the same crs. The folder structure must be created upfront and the right crs must be set (replace 12345 with the actual value).


ogr2ogr -f GeoJSON -lco "COORDINATE_PRECISION=5" <result.geojson> <source.geojson> -a_srs EPSG:<crs> -t_srs EPSG:4326

Bash (on Linux and macOS, on Windows you can use Git bash)

dir="$(pwd)"
crs=12345
rm -f Results/WGS84/*.geojson
clear
cd Source/$crs
for f in *.geojson; do ogr2ogr -f GeoJSON -lco "COORDINATE_PRECISION=5" ../../Results/WGS84/$f $f -a_srs EPSG:$crs -t_srs EPSG:4326; done
cd $dir

Windows equivalent to run on cmd command prompt if no bash available

SET dir="%CD%"
SET crs=12345
DEL /F/Q/S Results/WGS84/*.geojson
CLS
CD Source/%crs
FOR %f in *.geojson DO ogr2ogr -f GeoJSON -lco "COORDINATE_PRECISION=5" ../../Results/WGS84/%f %f -a_srs EPSG:%crs -t_srs EPSG:4326
CD %dir

NOTE: Use double precentage signs when running the code for Windows cmd with a batch file.

Results

Test the resulting files using https://geojson.io, or your favourite GIS program like QGIS or Esri tooling.

Tips

  • On accuracy: in most cases 5 digits is enough, see http://wiki.gis.com/wiki/index.php/Decimal_degrees and https://wiki.openstreetmap.org/wiki/Precision_of_coordinates More digits might seem to provide higher accuracy but this is usually not reliable because the (GPS) data was not sourced with such high accuracy. Disadvantages of using more unnecessary digits in coordinate data are larger data sizes, more data traffic, bigger indexes and slower spatial operations.
  • Don't use the resulting data in WGS84 to convert back to the original or another crs.
  • You can use gdal libraries and integrate in Python, R, Node etc to write code that does the same. Do stick to one tool for coordinate conversion, gdal is a very good choice. Don't rely on online tooling (including geojson.io) where you cannot see/verify the parameters used.
  • If you want a tool with GUI, then QGIS is an option. Uses GDAL/OGR under the hood you can even copy working Python code from QGIS).
  • Another useful tool to examine geospatial data is ogrinfo.

Links

https://gdal.org/programs/ogr2ogr.html

https://gdal.org/programs/ogrinfo.html

https://www.bostongis.com/PrinterFriendly.aspx?content_name=ogr_cheatsheet