From b5a2c6069780d924e1f007c6917ab00681d3560a Mon Sep 17 00:00:00 2001 From: Aidan Lister Date: Tue, 1 Jun 2021 09:04:10 +1000 Subject: [PATCH] Properly serialize decimals to ensure lat/lng accuracy (Fixes #185) --- mapboxgl/viz.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mapboxgl/viz.py b/mapboxgl/viz.py index 213ac4c..a1c90aa 100644 --- a/mapboxgl/viz.py +++ b/mapboxgl/viz.py @@ -1,4 +1,5 @@ import codecs +import decimal import json import os @@ -15,6 +16,13 @@ GL_JS_VERSION = 'v1.5.0' +class FullJSONEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, decimal.Decimal): + return str(o) + return super().default(o) + + class VectorMixin(object): def generate_vector_color_map(self): @@ -278,7 +286,7 @@ def create_html(self, filename=None): style=style, center=list(self.center), zoom=self.zoom, - geojson_data=json.dumps(self.data, ensure_ascii=False), + geojson_data=json.dumps(self.data, ensure_ascii=False, cls=FullJSONEncoder), belowLayer=self.below_layer, opacity=self.opacity, minzoom=self.min_zoom,