def test_key_data_for_mapbox(self): ''' Test basic use of mapbox key ''' previous = get_mapbox_key() set_mapbox_key(None) assert(None == get_mapbox_key()) set_mapbox_key(previous)
def test_key_data_for_google(self): ''' Test basic use of google key ''' previous = get_mapbox_key() set_mapbox_key(None) assert (None, get_mapbox_key()) set_mapbox_key(previous)
def get_formatted_location(location_string): ''' This function will provide a standard location based on google maps service online ''' #Set up first the output output = {} output["raw"] = location_string mapbox_results = [] #New code moving from google maps to MapBox. if (get_mapbox_key() is None) or (location_string == ""): #Data is not found, let's try removing some logging.warning(NO_VALID_KEY) return output else: try: mapbox_results = Geocoder( access_token=get_mapbox_key()).forward(location_string).json() except: #In case we have a problem with the key from mapbox or internet is down return output wrong_message = ("message" in mapbox_results.keys()) if ((not wrong_message) and (len(mapbox_results) > 0) and (len(mapbox_results["features"]) > 0) and ("context" in mapbox_results["features"][0])): #Received data is ok, we can proceed output["latitude"] = mapbox_results["features"][0]["center"][1] output["longitude"] = mapbox_results["features"][0]["center"][0] for location_data in mapbox_results["features"][0]["context"]: if "locality" in location_data["id"]: output["city"] = location_data["text"] elif "place" in location_data["id"]: output["place_name"] = location_data["text"] elif "region" in location_data["id"]: output["county"] = location_data["text"] elif "country" in location_data["id"]: output["country"] = location_data["text"] #In the MapBox API apparently the more accurate location is at the beginning, to make sure overwrites any other place_type = mapbox_results["features"][0]["place_type"] place_text = mapbox_results["features"][0]["text"] if (place_type[0] in MAPBOX_TRANS_GENITOOLS.keys()): output[MAPBOX_TRANS_GENITOOLS[place_type[0]]] = place_text return output else: #Data is not found, let's try removing some logging.debug(NO_VALID_LOCATION) logging.debug(location_string) if (len(location_string.split(",")) > 3): output2 = get_formatted_location(",".join( location_string.split(",")[1:])) output2["raw"] = output["raw"] return output2 else: return output
def get_formatted_location(location_string): ''' This function will provide a standard location based on google maps service online ''' #Set up first the output output = {} output["raw"] = location_string mapbox_results = [] #New code moving from google maps to MapBox. if (get_mapbox_key() == None) or (location_string == ""): #Data is not found, let's try removing some logging.warning(NO_VALID_KEY) return output else: url = MAPBOX_ADDRESS + location_string.replace( "?", "") + ".json?access_token=" + get_mapbox_key() r = requests.get(url) mapbox_results = r.json() if ((len(mapbox_results) > 0) and ("context" in mapbox_results["features"][0])): #Received data is ok, we can proceed output["latitude"] = mapbox_results["features"][0]["center"][1] output["longitude"] = mapbox_results["features"][0]["center"][0] for location_data in mapbox_results["features"][0]["context"]: if "locality" in location_data["id"]: output["city"] = location_data["text"] elif "place" in location_data["id"]: output["place_name"] = location_data["text"] elif "region" in location_data["id"]: output["county"] = location_data["text"] elif "country" in location_data["id"]: output["country"] = location_data["text"] #In the MapBox API apparently the more accurate location is at the beginning, to make sure overwrites any other place_type = mapbox_results["features"][0]["place_type"] place_text = mapbox_results["features"][0]["text"] if (place_type[0] in MAPBOX_TRANS_GENITOOLS.keys()): output[MAPBOX_TRANS_GENITOOLS[place_type[0]]] = place_text return output else: #Data is not found, let's try removing some logging.warning(NO_VALID_LOCATION) logging.warning(location_string) if (len(location_string.split(",")) > 3): output2 = get_formatted_location(",".join( location_string.split(",")[1:])) output2["raw"] = output["raw"] return output2 else: return output