Add GeoJSON output capability

This commit is contained in:
Lucas Verney 2019-05-08 19:37:52 +02:00
parent de32f47c4c
commit d079df4feb
3 changed files with 54 additions and 7 deletions

View File

@ -84,9 +84,22 @@ All operators can have a trailing `?` to make them consider fields without
value as matching the condition as well. This means you can use something like
```
> /api/v1/reports?filter[expiration_datetime][gt?]=2018-10-18T09:06:38.538375
> GET /api/v1/reports?filter[expiration_datetime][gt?]=2018-10-18T09:06:38.538375
```
to get the all the reports with an expiration datetime in the future or no
expiration datetime, that is all currently active reports, assuming the
current datetime is `2018-10-18T09:06:38.538375`.
### Output format
The default output format is a JSON dump of the reports, in a format specific
to Cygnal.
It is possible to get a more standard GeoJSON output using the `format` query
parameter, typically:
```
> GET /api/v1/reports?format=geojson
```

View File

@ -3,6 +3,7 @@
"""
Models and database definition
"""
import json
import os
import bottle
@ -62,3 +63,24 @@ class Report(BaseModel):
if k != "id"
}
}
def to_geojson_feature(self):
properties = {
"type": "reports",
}
for k, v in model_to_dict(self).items():
properties[k] = v
geometry = None
if self.shape_geojson:
geometry = json.loads(self.shape_geojson)
else:
geometry = {
"type": "Point",
"coordinates": [self.lng, self.lat]
}
return {
"type": "Feature",
"properties": properties,
"geometry": geometry,
}

View File

@ -107,12 +107,24 @@ def get_all_reports():
if page_number and page_size:
query = query.paginate(page_number, page_size)
return {
"data": [
r.to_json()
for r in query
]
}
if (
'format' in bottle.request.query and
bottle.request.query['format'] == 'geojson'
):
return {
"type": "FeatureCollection",
"features": [
r.to_geojson_feature()
for r in query
]
}
else:
return {
"data": [
r.to_json()
for r in query
]
}
@bottle.route('/api/v1/reports', ["POST", "OPTIONS"])