As part of our visualization project (Datamocha), we have worked with many GeoJSON files.
Map files provided by clients are often in different formats and large in size.
Loading a 20MB+ GeoJSON file is not feasible for web applications. At least not for showing overview visuals like:
Why not just use online tools to minify GeoJSON files?
While online tools can be convenient, they often have limitations in terms of file size, privacy, and customization. These tools also don't support other formats like shapefiles, topojson, etc.
Here are two ways that have helped us reduce GeoJSON file size by ~95%:
QGIS
QGIS is an open-source, free-to-use application that handles most map file formats. It's also cross-platform.
It also loads large files that web apps struggle with.
Apart from minifying, we can do many things like editing shapes, merging features to form new regions, etc.
Steps to simplify and export GeoJSON using QGIS:
- Open shape/geojson file in QGIS.
- Select layer in left side panel.
- Open
Vector > Geometry Tools > Simplify - Provide a
Tolerancevalue.-
0.005is a good starting point for zoom level 5 in Leaflet. - (A bigger tolerance value means a smaller file but lower map resolution.)
-
- Click Run.
- Right-click the new layer (named
Simplifiedby default). - Select
Export > Save Features As.... - Reduce
COORDINATE_PRECISIONas needed (4 is good enough for zoom level 5 in Leaflet). - Ensure Encoding = UTF-8 and CRS = EPSG:4326 - WGS 84.
- Provide a file name and folder location.
Using this method, we have reduced the GeoJSON file size from 24MB to 1.2MB.
Still the file contains white spaces and long property keys. We use the following tool to minify it further.
minify-geojson
Use the following CLI tool to further reduce file size:
https://github.com/TNOCS/minify-geojson
Examples:
# Shrink property keys and output to original.min.geojson
npx minify-geojson -k original.geojson
# Remove blacklisted properties
minify-geojson -b "property1, property2" original.geojson
# Keep only whitelisted properties
minify-geojson -w "property1, property2" original.geojson
# Remove superfluous decimals (keep first 5)
minify-geojson -c 5 original.geojson
After using this tool, we were able to reduce the 1.2MB file further to ~1MB. Not as much as the previous step, but every bit helps when it comes to web performance.
~ That's it folks ~
Thanks to QGIS Contributors and TNOCS for these awesome tools!
Cover Image by Yuri from Pixabay
Map By English English is Correct - Own work, CC0, Link

Top comments (0)