From 2ff3eabc47ff20c55fbc941ad063f50937691e63 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Wed, 9 May 2018 11:25:50 +0200 Subject: [PATCH] fix: [GeoHelper] fix invalid coordinate In some cases, the coordinate given to redis may not match EPSG:900913 / EPSG:3785 / OSGEO:41001, throwing an error. This fix adds a check and skip the addition if it occurs --- helpers/geo_helper.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/helpers/geo_helper.py b/helpers/geo_helper.py index e1ab86f..f87dd25 100644 --- a/helpers/geo_helper.py +++ b/helpers/geo_helper.py @@ -13,6 +13,9 @@ from phonenumbers import geocoder import util +class InvalidCoordinate(Exception): + pass + class Geo_helper: def __init__(self, serv_redis_db, cfg): self.serv_redis_db = serv_redis_db @@ -98,6 +101,8 @@ class Geo_helper: ordDic['categ'] = categ ordDic['value'] = supposed_ip coord_list = [coord['lat'], coord['lon']] + if self.coordinate_list_valid(coord_list): + raise InvalidCoordinate("Coordinate do not match EPSG:900913 / EPSG:3785 / OSGEO:41001") self.push_to_redis_zset(self.keyCategCoord, json.dumps(ordDic)) self.push_to_redis_zset(self.keyCategCountry, rep['full_rep'].country.iso_code) ordDic = OrderedDict() #keep fields with the same layout in redis @@ -119,6 +124,9 @@ class Geo_helper: self.logger.warning("can't resolve ip") except geoip2.errors.AddressNotFoundError: self.logger.warning("Address not in Database") + except InvalidCoordinate: + self.logger.warning("Coordinate do not follow redis specification") + def getCoordFromPhoneAndPublish(self, phoneNumber, categ): try: @@ -138,6 +146,8 @@ class Geo_helper: ordDic['lat'] = coord_dic['lat'] ordDic['lon'] = coord_dic['lon'] coord_list = [coord['lat'], coord['long']] + if self.coordinate_list_valid(coord_list): + raise InvalidCoordinate("Coordinate do not match EPSG:900913 / EPSG:3785 / OSGEO:41001") self.push_to_redis_zset(self.keyCategCoord, json.dumps(ordDic)) self.push_to_redis_zset(self.keyCategCountry, country_code) ordDic = OrderedDict() #keep fields with the same layout in redis @@ -196,3 +206,14 @@ class Geo_helper: if abs(float(coord1[1]) - float(coord2[1])) <= clusterThres: return True return False + + # adjust latitude and longitude to fit the limit, as specified + # by EPSG:900913 / EPSG:3785 / OSGEO:41001 + # coord_list = [lat, lon] + def coordinate_list_valid(coord_list): + lat = coord_list[0] + lon = coord_list[1] + if (-180 <= lon <= 180) and (-85.05112878 <= lat <= 85.05112878): + return True + else: + return False