Skip to content

Commit 4d0a2bc

Browse files
committed
Agregada libreria turf.js para geoespacializacion, agregado parametros para filtro por coordenadas y distancia en observaciones
1 parent 3331eb9 commit 4d0a2bc

File tree

3 files changed

+1503
-1
lines changed

3 files changed

+1503
-1
lines changed

api/observation/controllers/observation.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

33
const axios = require('axios')
4+
const turf = require('@turf/turf')
45
const OpenWeather = require('../../../config/openweather')
56
const TensorFlow = require('../../../config/tensorflow')
67
const fs = require('fs')
78
const FormData = require('form-data');
89
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
10+
const filterGeoreferenceParams = ['lat', 'long', 'distance']
911

1012
/**
1113
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
@@ -61,5 +63,38 @@ module.exports = {
6163
entity = await strapi.services.observation.create(ctx.request.body);
6264
}
6365
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+
},
65100
};

0 commit comments

Comments
 (0)