Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate FeatureCollection GeoJSON from queryset in the database #111

Open
bahoo opened this issue Oct 17, 2020 · 0 comments
Open

Generate FeatureCollection GeoJSON from queryset in the database #111

bahoo opened this issue Oct 17, 2020 · 0 comments

Comments

@bahoo
Copy link

bahoo commented Oct 17, 2020

I've long wanted to generate GeoJSON directly in the database (i.e. Postgres/PostGIS) since it has JSON and GeoJSON capabilities, and probably has some performance benefits over serialization / pulling everything into memory in Python. This weekend I finally assembled it:

https://gist.github.com/bahoo/fca19de157fde5bb34b30dea8f1352d8

>>> from expressions import GeoJSON
>>> geojson = Geo.objects.filter(**kwargs).aggregate(GeoJSON(geom_field='geom', fields=['name', 'code']))['geojson']
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'MultiPolygon', 'coordinates': [ ... ]

It uses many functions that aren't in core Django yet, so it might be too Postgres-specific to merge into the library yet, but if it helps others trying to do something similar, here you go! Or if anyone thinks it's worth incorporating, I'm happy to put a PR together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant