diff --git a/doc/20.api.md b/doc/20.api.md index e3bec43..1ca5e9f 100644 --- a/doc/20.api.md +++ b/doc/20.api.md @@ -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 +``` diff --git a/server/models.py b/server/models.py index 6d6b10c..576cb19 100644 --- a/server/models.py +++ b/server/models.py @@ -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, + } diff --git a/server/routes.py b/server/routes.py index 901f13c..b84b70d 100644 --- a/server/routes.py +++ b/server/routes.py @@ -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"])