From c76bbdc42fd866fe5aaf583358b07f1412858b9c Mon Sep 17 00:00:00 2001 From: David Eder Date: Tue, 25 May 2021 19:38:59 +0200 Subject: [PATCH] Add traffic light range to entity --- .../entity_ident_service/entity_ident.py | 26 ++++++++++++------- .../entitiy_ident/mongo/traffic_lights.json | 11 ++++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/components/entitiy_ident/entity_ident_service/entity_ident.py b/components/entitiy_ident/entity_ident_service/entity_ident.py index fafecaf..60df14d 100644 --- a/components/entitiy_ident/entity_ident_service/entity_ident.py +++ b/components/entitiy_ident/entity_ident_service/entity_ident.py @@ -40,19 +40,27 @@ def get_traffic_lights_geo(): 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] + mongo.db.trafficLights.aggregate([ + {"$geoNear": { + "near": { + "type": "Point", + "coordinates": [lon, lat] }, - '$maxDistance': distance - } - }}) + "spherical": True, + "distanceField": "calculatedRange" + }}, + {"$redact": { + "$cond": { + "if": {"$lte": ["$calculatedRange", "$range"]}, + "then": "$$KEEP", + "else": "$$PRUNE" + } + }} + ] + ) ] return json_util.dumps({'cursor': traffic_lights}) diff --git a/components/entitiy_ident/mongo/traffic_lights.json b/components/entitiy_ident/mongo/traffic_lights.json index d916345..d9f41a3 100644 --- a/components/entitiy_ident/mongo/traffic_lights.json +++ b/components/entitiy_ident/mongo/traffic_lights.json @@ -1,10 +1,17 @@ [ { "id": "1", - "location": [16.315283, 48.190331] + "location": [16.0, 48.0], + "range": 2000 }, { "id": "2", - "location": [16.316331, 48.192382] + "location": [16.0, 48.1], + "range": 800 + }, + { + "id": "3", + "location": [16.0, 48.2], + "range": 1000 } ] \ No newline at end of file