예제 #1
0
def place_name(lat, lon):

    # Try to get cached location first
    db = Db()
    # 3km distace radious for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    if(cached_place_name is not None):
        return cached_place_name

    lookup_place_name = None
    geolocation_info = reverse_lookup(lat, lon)
    if(geolocation_info is not None):
        if('address' in geolocation_info):
            address = geolocation_info['address']
            if('city' in address):
                lookup_place_name = address['city']
            elif('state' in address):
                lookup_place_name = address['state']
            elif('country' in address):
                lookup_place_name = address['country']

    if(lookup_place_name is not None):
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()
    return lookup_place_name
예제 #2
0
def place_name(lat, lon):

    # Try to get cached location first
    db = Db()
    # 3km distace radious for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    if (cached_place_name is not None):
        return cached_place_name

    lookup_place_name = None
    geolocation_info = reverse_lookup(lat, lon)
    if (geolocation_info is not None):
        if ('address' in geolocation_info):
            address = geolocation_info['address']
            if ('city' in address):
                lookup_place_name = address['city']
            elif ('state' in address):
                lookup_place_name = address['state']
            elif ('country' in address):
                lookup_place_name = address['country']

    if (lookup_place_name is not None):
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()
    return lookup_place_name
예제 #3
0
def place_name(lat, lon):

    # Try to get cached location first
    db = Db()
    # 3km distace radious for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    if cached_place_name is not None:
        return cached_place_name

    lookup_place_name = None
    geolocation_info = reverse_lookup(lat, lon)
    if geolocation_info is not None:
        if "address" in geolocation_info:
            address = geolocation_info["address"]
            if "city" in address:
                lookup_place_name = address["city"]
            elif "state" in address:
                lookup_place_name = address["state"]
            elif "country" in address:
                lookup_place_name = address["country"]

    if lookup_place_name is not None:
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()
    return lookup_place_name
예제 #4
0
def test_add_location():
    db = Db()

    latitude, longitude, name = helper.get_test_location()

    db.add_location(latitude, longitude, name)
    retrieved_name = db.get_location_name(latitude, longitude, 5)

    assert name == retrieved_name
예제 #5
0
def test_get_location_name_outside_threshold():
    db = Db()

    latitude, longitude, name = helper.get_test_location()
    db.add_location(latitude, longitude, name)

    new_latitude = helper.random_coordinate(latitude, 1)
    new_longitude = helper.random_coordinate(longitude, 1)

    # 800 meters
    retrieved_name = db.get_location_name(new_latitude, new_longitude, 800)

    assert retrieved_name is None
예제 #6
0
def test_get_location_name_within_threshold():
    db = Db()

    latitude, longitude, name = helper.get_test_location()
    db.add_location(latitude, longitude, name)

    print(latitude)
    new_latitude = helper.random_coordinate(latitude, 4)
    new_longitude = helper.random_coordinate(longitude, 4)
    print(new_latitude)

    # 10 miles
    retrieved_name = db.get_location_name(new_latitude, new_longitude, 1600*10)

    assert name == retrieved_name, 'Name (%r) did not match retrieved name (%r)' % (name, retrieved_name)
예제 #7
0
def place_name(lat, lon):
    lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
    if (lat is None or lon is None):
        return lookup_place_name_default

    # Convert lat/lon to floats
    if (not isinstance(lat, float)):
        lat = float(lat)
    if (not isinstance(lon, float)):
        lon = float(lon)

    # Try to get cached location first
    db = Db()
    # 3km distace radius for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    # We check that it's a dict to coerce an upgrade of the location
    #  db from a string location to a dictionary. See gh-160.
    if (isinstance(cached_place_name, dict)):
        return cached_place_name

    lookup_place_name = {}
    geolocation_info = lookup(lat=lat, lon=lon)
    if (geolocation_info is not None and 'address' in geolocation_info):
        address = geolocation_info['address']
        log.info('Location: "%s"' % geolocation_info['display_name'])
        # gh-386 adds support for town
        # taking precedence after city for backwards compatibility
        for loc in ['hamlet', 'village', 'city', 'town', 'state', 'country']:
            if (loc in address):
                lookup_place_name[loc] = address[loc]
                # In many cases the desired key is not available so we
                #  set the most specific as the default.
                if ('default' not in lookup_place_name):
                    lookup_place_name['default'] = address[loc]

    if (lookup_place_name):
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()

    if ('default' not in lookup_place_name):
        lookup_place_name = lookup_place_name_default

    return lookup_place_name
예제 #8
0
def place_name(lat, lon):
    # Convert lat/lon to floats
    if not isinstance(lat, float):
        lat = float(lat)
    if not isinstance(lon, float):
        lon = float(lon)

    # Try to get cached location first
    db = Db()
    # 3km distace radious for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    # We check that it's a dict to coerce an upgrade of the location
    #  db from a string location to a dictionary. See gh-160.
    if (isinstance(cached_place_name, dict)):
        return cached_place_name

    lookup_place_name = {}
    geolocation_info = lookup(lat=lat, lon=lon)
    if (geolocation_info is not None):
        if ('address' in geolocation_info):
            address = geolocation_info['address']
            for loc in ['city', 'state', 'country']:
                if (loc in address):
                    lookup_place_name[loc] = address[loc]
                    # In many cases the desired key is not available so we
                    #  set the most specific as the default.
                    if ('default' not in lookup_place_name):
                        lookup_place_name['default'] = address[loc]

    if ('default' not in lookup_place_name):
        lookup_place_name = 'Unknown Location'

    if (lookup_place_name is not {}):
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()
    return lookup_place_name
예제 #9
0
def place_name(lat, lon):
    lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
    if (lat is None or lon is None):
        return lookup_place_name_default

    # Convert lat/lon to floats
    if (not isinstance(lat, float)):
        lat = float(lat)
    if (not isinstance(lon, float)):
        lon = float(lon)

    localdb_response = closestgeoname.query_closest_city(
        closestgeoname.DBFILENAME, lat, lon)

    if localdb_response is not None:
        return {
            "city": localdb_response[0],
            "default": localdb_response[0],
            "state": localdb_response[1],
            "country": localdb_response[2]
        }

    else:
        # This should only occur if the script is searching for a point
        # outside the maximum distance on earth. So this would mean
        # there is likely an error with the GNSS coordinate. But we can
        # continue to see if other services (MapQuest) has a way to handle it.
        print(
            "lat/long likely to be incorrect... continuing with other service")
        print("lat: {}, lon: {}".format(lat, lon))
        pass

    # Try to get cached location first
    db = Db()
    # 3km distace radious for a match
    cached_place_name = db.get_location_name(lat, lon, 3000)
    # We check that it's a dict to coerce an upgrade of the location
    #  db from a string location to a dictionary. See gh-160.
    if (isinstance(cached_place_name, dict)):
        return cached_place_name

    lookup_place_name = {}
    geolocation_info = lookup(lat=lat, lon=lon)
    if (geolocation_info is not None and 'address' in geolocation_info):
        address = geolocation_info['address']
        for loc in ['city', 'state', 'country']:
            if (loc in address):
                lookup_place_name[loc] = address[loc]
                # In many cases the desired key is not available so we
                #  set the most specific as the default.
                if ('default' not in lookup_place_name):
                    lookup_place_name['default'] = address[loc]

    if (lookup_place_name):
        db.add_location(lat, lon, lookup_place_name)
        # TODO: Maybe this should only be done on exit and not for every write.
        db.update_location_db()

    if ('default' not in lookup_place_name):
        lookup_place_name = lookup_place_name_default

    return lookup_place_name