Add GeoJSON output capability
This commit is contained in:
parent
de32f47c4c
commit
d079df4feb
@ -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
|
||||||
|
```
|
||||||
|
@ -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,
|
||||||
|
}
|
||||||
|
@ -107,6 +107,18 @@ 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)
|
||||||
|
|
||||||
|
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 {
|
return {
|
||||||
"data": [
|
"data": [
|
||||||
r.to_json()
|
r.to_json()
|
||||||
|
Loading…
Reference in New Issue
Block a user