diff --git a/Dockerfile b/Dockerfile index 91fb8f5..20e027d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ WORKDIR /mnt/datasette # for csv import - https://datasette.io/tools/csvs-to-sqlite # for datasette db maniupluations and tools - https://datasette.io/tools/sqlite-utils # for geojson api responses - https://pypi.org/project/geojson/ -RUN pip install csvs-to-sqlite sqlite-utils geojson +RUN pip install csvs-to-sqlite sqlite-utils geojson plpygis # Add the csv data files COPY data/ . diff --git a/plugins/geo_json_renderer.py b/plugins/geo_json_renderer.py index 8d3a7cc..1c80c51 100644 --- a/plugins/geo_json_renderer.py +++ b/plugins/geo_json_renderer.py @@ -1,6 +1,7 @@ from datasette import hookimpl from datasette.utils.asgi import Response import geojson +from plpygis import Geometry # https://docs.datasette.io/en/stable/plugin_hooks.html#register-output-renderer-datasette @@ -23,12 +24,23 @@ async def render_geo_json( feature_list = list() for row in result: - # https://geojson.org/geojson-spec.html#id2 - point = geojson.Point((row['longitude'], row['latitude'])) - # create a geojson feature - feature = geojson.Feature(geometry=point, properties=dict(row)) - # append the current feature to the list of all features - feature_list.append(feature) + # Is this a blatant Point object? + if 'longitude' in row.keys() and 'latitude' in row.keys(): + # https://geojson.org/geojson-spec.html#id2 + point = geojson.Point((row['longitude'], row['latitude'])) + # create a geojson feature + feature = geojson.Feature(geometry=point, properties=dict(row)) + # append the current feature to the list of all features + feature_list.append(feature) + + # Otherwise, does this have a "the_geom" object, which was used in the old Carto database, which encodes geographical data as a string in the "well-known binary" format? + elif 'the_geom' in row.keys(): + feature = Geometry(row['the_geom']) + feature_list.append(feature) + + else: + # TODO: do we need to have error handling here? + pass feature_collection = geojson.FeatureCollection(feature_list) body = geojson.dumps(feature_collection)