NASA-IMPACT / covid-api

MIT License
14 stars 3 forks source link

Feature: abstract timelapse endpoint #113

Closed leothomas closed 3 years ago

leothomas commented 3 years ago

Closes #110

What I did:

I made the /timelapse endpoint work for any global, raster dataset (as opposed to just no2-mean and no2-diff).

How I did it:

I added a field for datasetId in the query parameters. The API validate that the dataset indeed a raster dataset.

I added a field for spotlightId in the query parameters in order to make the /timelapse endpoint also work with non-global datasets. This functionality is not currently supported by the frontend, since the timelapse requests are generated by drawing a bounding box on the map - which has no knowledge of which "spotlight" it might be over. A potential workaround is for the API to calculate if the bounding box supplied fits within a spotlight's tile, returning an error if it does not. For the time being, the endpoint will only support global datasets.

**Note: the field month is now date in order to support both monthly datasets and daily datasets

How you can test it:

Executing the following query:

curl 'http://localhost:8000/v1/timelapse' --data-raw '{"datasetId":"no2","date":"202004","geojson":{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[77.695,17.978],[90.351,17.978],[90.351,28.304],[77.695,28.304],[77.695,17.978]]]}}}'

returns:

{"mean":2271165821222912.0,"median":2252534253092864.0}%

Example of a spotlight-specific dataset query:

curl 'http://localhost:8000/v1/timelapse' --data-raw '{"datasetId":"nightlights-viirs","spotlightId":"la", "date":"2020_04_01","geojson":{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[
          [
            [
              -118.74847412109375,
              33.55512901742288
            ],
            [
              -117.35595703124999,
              33.55512901742288
            ],
            [
              -117.35595703124999,
              34.420504880133834
            ],
            [
              -118.74847412109375,
              34.420504880133834
            ],
            [
              -118.74847412109375,
              33.55512901742288
            ]
          ]
        ]}}}'

returns:

{"mean":20.382328097428335,"median":2.8000000000000003}%

Note the field spotlightId in the request.

Examples of handled failures:

Invalid date format:

curl 'http://localhost:8000/v1/timelapse' --data-raw '{"datasetId":"no2","date":"2020","geojson":{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[
          [
            [
              -118.74847412109375,
              33.55512901742288
            ],
            [
              -117.35595703124999,
              33.55512901742288
            ],
            [
              -117.35595703124999,
              34.420504880133834
            ],
            [
              -118.74847412109375,
              34.420504880133834
            ],
            [
              -118.74847412109375,
              33.55512901742288
            ]
          ]
        ]}}}'

returns:

{"detail":"Invalid date format. 2020 should be either YYYY_MM_DD or YYYYMM"}%

Other handled failures include: