datasette-leaflet-geojson/datasette_leaflet_geojson/__init__.py
from datasette import hookimplimport jsonGEOJSON_TYPES = {"Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon","GeometryCollection","Feature","FeatureCollection",}@hookimpl(tryfirst=True)def render_cell(value):# If value is JSON that looks like geojson, return it so no other# plugin interferes with it# https://github.com/simonw/datasette-leaflet-geojson/issues/3try:data = json.loads(value)except (ValueError, TypeError):return Noneif not isinstance(data, dict):return Noneif "type" not in data:return Noneif data["type"] in GEOJSON_TYPES:# Reduce floating point accuracy to something sensiblereturn json.dumps(round_floats(data))return Nonedef 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@hookimpldef extra_js_urls(columns, datasette):if not columns:return Nonereturn [{"url": datasette.urls.static_plugins("datasette-leaflet-geojson", "datasette-leaflet-geojson.js"),"module": True,}]@hookimpldef extra_body_script(datasette, database, table):config = (datasette.plugin_config("datasette-leaflet-geojson", database=database, table=table)or {})return "window.DATASETTE_LEAFLET_GEOJSON_DEFAULT_MAPS_TO_LOAD = {};".format(json.dumps(config.get("default_maps_to_load") or 10))