From dfd53a6623dd88cc8a53c4ebae8afb47bbb99371 Mon Sep 17 00:00:00 2001 From: David Eder Date: Tue, 18 May 2021 19:17:59 +0200 Subject: [PATCH] Add filter functionalities by geo data --- .../entity_ident_service/entity_ident.py | 27 ++++++++++++++++++- components/entitiy_ident/mongo/import.sh | 2 ++ .../entitiy_ident/mongo/traffic_lights.json | 10 ++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/components/entitiy_ident/entity_ident_service/entity_ident.py b/components/entitiy_ident/entity_ident_service/entity_ident.py index 462210e..fafecaf 100644 --- a/components/entitiy_ident/entity_ident_service/entity_ident.py +++ b/components/entitiy_ident/entity_ident_service/entity_ident.py @@ -26,12 +26,37 @@ def get_traffic_lights(): tl_id = query_parameters.get('id') if tl_id is not None: - traffic_lights = [traffic_light for traffic_light in mongo.db.trafficLights.find({'id': tl_id})] + traffic_lights = [traffic_light for traffic_light in + mongo.db.trafficLights.find({'id': tl_id})] else: traffic_lights = [traffic_light for traffic_light in mongo.db.trafficLights.find({})] return json_util.dumps({'cursor': traffic_lights}) +@app.route('/api/v1/resources/traffic_lights_geo', methods=['GET']) +def get_traffic_lights_geo(): + query_parameters = request.args + + lat = float(query_parameters.get('lat')) + lon = float(query_parameters.get('lon')) + distance = float(query_parameters.get('distance')) + + traffic_lights = [ + traffic_light for traffic_light in + mongo.db.trafficLights.find({'location': { + '$near': { + '$geometry': { + 'type': "Point", + 'coordinates': [lon, lat] + }, + '$maxDistance': distance + } + }}) + ] + + return json_util.dumps({'cursor': traffic_lights}) + + if __name__ == '__main__': app.run(host='0.0.0.0') diff --git a/components/entitiy_ident/mongo/import.sh b/components/entitiy_ident/mongo/import.sh index ec22493..2595813 100644 --- a/components/entitiy_ident/mongo/import.sh +++ b/components/entitiy_ident/mongo/import.sh @@ -2,3 +2,5 @@ mongoimport --db entities --collection cars --type json --file cars.json --jsonArray mongoimport --db entities --collection trafficLights --type json --file traffic_lights.json --jsonArray + +mongo --eval 'db.trafficLights.ensureIndex({location:"2dsphere"})' entities \ No newline at end of file diff --git a/components/entitiy_ident/mongo/traffic_lights.json b/components/entitiy_ident/mongo/traffic_lights.json index e464166..d916345 100644 --- a/components/entitiy_ident/mongo/traffic_lights.json +++ b/components/entitiy_ident/mongo/traffic_lights.json @@ -1,16 +1,10 @@ [ { "id": "1", - "location": { - "type": "Point", - "coordinates": [-73.856077, 40.848447] - } + "location": [16.315283, 48.190331] }, { "id": "2", - "location": { - "type": "Point", - "coordinates": [-73.856077, 40.848447] - } + "location": [16.316331, 48.192382] } ] \ No newline at end of file