137 words
1 minute
Outputting JSON with reduced floating point precision
Anubhav Gain
2024-05-17

Outputting JSON with reduced floating point precision#

datasette-leaflet-geojson outputs GeoJSON geometries in HTML pages in a way that can be picked up by JavaScript and used to plot a Leaflet map.

These geometries often look something like this:

{
"type":"MultiPolygon",
"coordinates":[[[[-122.4457678900319,37.77292891669105],[-122.441075063058,37.77352490695095]...

Decimal_degrees: Precision on Wikipedia says that 0.00001 should be accurate to within around a meter.

Shortening these floating point representations can shave 100KB+ off an HTML page with a lot of GeoJSON shapes on it!

There’s a lengthy Stack Overflow about this but it’s difficult to follow because ways of doing this changed between Python 2 and Python 3. Here’s what worked for me:

def round_floats(o):
if isinstance(o, float):
return round(o, 5)
if isinstance(o, dict):
return {k: round_floats(v) for k, v in o.items()}
if isinstance(o, (list, tuple)):
return [round_floats(x) for x in o]
return o
data = json.dumps(round_floats(data))

See issue 11 for details.

Outputting JSON with reduced floating point precision
https://mranv.pages.dev/posts/outputting-json-with-reduced-floating-point-precision/
Author
Anubhav Gain
Published at
2024-05-17
License
CC BY-NC-SA 4.0