|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const axios = require('axios') |
| 4 | +const turf = require('@turf/turf') |
4 | 5 | const OpenWeather = require('../../../config/openweather') |
5 | 6 | const TensorFlow = require('../../../config/tensorflow') |
6 | 7 | const fs = require('fs') |
7 | 8 | const FormData = require('form-data'); |
8 | 9 | const { parseMultipartData, sanitizeEntity } = require('strapi-utils'); |
| 10 | +const filterGeoreferenceParams = ['lat', 'long', 'distance'] |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers) |
@@ -61,5 +63,38 @@ module.exports = { |
61 | 63 | entity = await strapi.services.observation.create(ctx.request.body); |
62 | 64 | } |
63 | 65 | return sanitizeEntity(entity, { model: strapi.models.observation }); |
64 | | - } |
| 66 | + }, |
| 67 | + |
| 68 | + async find(ctx) { |
| 69 | + const georeferenceParams = Object.assign({}, ...filterGeoreferenceParams.map(param => { |
| 70 | + const obj = Object.assign({}, { [param]: ctx.query[param] }) |
| 71 | + delete ctx.query[param] |
| 72 | + return obj |
| 73 | + })) |
| 74 | + |
| 75 | + let entities; |
| 76 | + |
| 77 | + if (ctx.query._q) { |
| 78 | + entities = await strapi.services.observation.search(ctx.query) |
| 79 | + } else { |
| 80 | + entities = await strapi.services.observation.find(ctx.query) |
| 81 | + } |
| 82 | + |
| 83 | + if(georeferenceParams.lat && georeferenceParams.long && georeferenceParams.distance) { |
| 84 | + let distance |
| 85 | + entities = entities.filter(entity => { |
| 86 | + try { |
| 87 | + let from = turf.point(entity.geojson.features[0].geometry.coordinates) |
| 88 | + let to = turf.point([georeferenceParams.long, georeferenceParams.lat]) |
| 89 | + |
| 90 | + distance = turf.distance(from, to) |
| 91 | + return distance <= georeferenceParams.distance |
| 92 | + } catch (error) { |
| 93 | + return false |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.observation })) |
| 99 | + }, |
65 | 100 | }; |
0 commit comments