DEV Community

Cover image for Minify GeoJSON Files: A Practical Guide
Saravanapriyan
Saravanapriyan

Posted on

Minify GeoJSON Files: A Practical Guide

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:

World Population Density Map

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:

  1. Open shape/geojson file in QGIS.
  2. Select layer in left side panel.
  3. Open Vector > Geometry Tools > Simplify
  4. Provide a Tolerance value.
    • 0.005 is a good starting point for zoom level 5 in Leaflet.
    • (A bigger tolerance value means a smaller file but lower map resolution.)
  5. Click Run.
  6. Right-click the new layer (named Simplified by default).
  7. Select Export > Save Features As....
  8. Reduce COORDINATE_PRECISION as needed (4 is good enough for zoom level 5 in Leaflet).
  9. Ensure Encoding = UTF-8 and CRS = EPSG:4326 - WGS 84.
  10. 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
Enter fullscreen mode Exit fullscreen mode

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)