Skip to content

Commit

Permalink
Merge pull request #2 from zooniverse/add-generic-geometric-feature-h…
Browse files Browse the repository at this point in the history
…andling

Allow GeoJson renderer to handle Carto's encrypted 'the_geom' data
  • Loading branch information
shaunanoordin authored Mar 18, 2021
2 parents 8f88074 + d2e4202 commit 4c4eab8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/ .
Expand Down
24 changes: 18 additions & 6 deletions plugins/geo_json_renderer.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 4c4eab8

Please sign in to comment.