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 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 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 expiration datetime, that is all currently active reports, assuming the
current datetime is `2018-10-18T09:06:38.538375`. 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 Models and database definition
""" """
import json
import os import os
import bottle import bottle
@ -62,3 +63,24 @@ class Report(BaseModel):
if k != "id" 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: if page_number and page_size:
query = query.paginate(page_number, page_size) query = query.paginate(page_number, page_size)
return { if (
"data": [ 'format' in bottle.request.query and
r.to_json() bottle.request.query['format'] == 'geojson'
for r in query ):
] 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"]) @bottle.route('/api/v1/reports', ["POST", "OPTIONS"])