Beispiel #1
0
def geolocate(address, try_all=True):
    try:
        # see https://astral.readthedocs.io/en/latest/#cities
        a = lookup(address, database())
        return a.latitude, a.longitude
    except:
        pass  # use online geocoder

    location_data = geocoder.geonames(address, method='details', key='jarbas')
    if not location_data.ok:
        location_data = geocoder.geocodefarm(address)
    if not location_data.ok:
        location_data = geocoder.osm(address)
    if try_all:
        # more are just making it slow
        if not location_data.ok:
            location_data = geocoder.google(address)
        if not location_data.ok:
            location_data = geocoder.arcgis(address)
        if not location_data.ok:
            location_data = geocoder.bing(address)
        if not location_data.ok:
            location_data = geocoder.canadapost(address)
        if not location_data.ok:
            location_data = geocoder.yandex(address)
        if not location_data.ok:
            location_data = geocoder.tgos(address)

    if location_data.ok:
        location_data = location_data.json
        lat = location_data.get("lat")
        lon = location_data.get("lng")

        return lat, lon
    raise ValueError
def geonames_response(request):
    url = 'http://api.geonames.org/searchJSON?q=Ottawa%2C+Ontario&fuzzy=0.8&username=mock&maxRows=1'
    data_file = 'tests/results/geonames.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        result = geocoder.geonames(location, key='mock')
    return result
Beispiel #3
0
def geolocate_locations(loc, countryBias=['AU']):
    """Given a data frame with a column 'placename' run
    a geolocation service over each placename. 
    Add new columns to the data frame for 
    'address', 'country', 'lat', 'long'
    returns the new dataframe
    """

    GEONAMES_KEY = secret('geonames')
    geo = []
    for place in loc['entity']:
        g = geocoder.geonames(place, key=GEONAMES_KEY, countryBias=countryBias)
        if g:
            result = {
                'lat': g.lat,
                'lng': g.lng,
                'address': g.address,
                'country': g.country
            }
        else:
            result = {'lat': 0, 'lng': 0, 'address': '', 'country': ''}
        geo.append(result)

    geo = pd.DataFrame(geo)
    return pd.concat([loc, geo], axis=1)
Beispiel #4
0
def test_details():
    url = 'http://api.geonames.org/getJSON?geonameId=6094817&username=mock&style=full'
    data_file = 'tests/results/geonames_details.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='details', key='mock')

        assert g.lat == "45.41117"
        assert g.lng == "-75.69812"
        assert g.geonames_id == 6094817
        assert g.address == "Ottawa"
        assert g.feature_class == "P"
        assert g.class_description == "city, village,..."
        assert g.code == "PPLC"
        assert g.description == "capital of a political entity"
        assert g.continent == "NA"
        assert g.country_geonames_id == "6251999"
        assert g.country_code == "CA"
        assert g.country == "Canada"
        assert g.state == "Ontario"
        assert g.state_code == "08"
        assert g.state_geonames_id == "6093943"
        assert g.admin2 == ""
        assert g.admin3 == ""
        assert g.admin4 == ""
        assert g.admin5 == ""
        assert g.population == 812129
        assert g.srtm3 == 71
        assert g.wikipedia == "en.wikipedia.org/wiki/Ottawa"
        assert g.timeZoneId == "America/Toronto"
        assert g.timeZoneName == "America/Toronto"
        assert g.rawOffset == -5
        assert g.dstOffset == -4
def find_city(words):
	for word in words:
		if len(word) > 3 and pymorphy2.MorphAnalyzer().parse(word)[0].tag.POS in {'NOUN'}:
			geocode = geocoder.geonames(word, key='schooler')
			pop = geocode.population
			if (pop is not None) and pop > 10000 and (word not in not_cities):
				return geocode.address
Beispiel #6
0
def geonames_response(request):
    url = 'http://api.geonames.org/searchJSON?q=Ottawa%2C+Ontario&fuzzy=0.8&username=mock&maxRows=1'
    data_file = 'tests/results/geonames.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        result = geocoder.geonames(location, key='mock')
    return result
Beispiel #7
0
def get_country_code(country_name):
    try:
        g = geocoder.geonames(country_name,
                              key=os.environ.get('GEO_TOKEN', 'ilov3'))
        return g.geojson['features'][0]['properties']['country_code']
    except Exception as e:
        logger.error(
            f'Can not find code for country: {country_name}. Error: {e}')
def get_geocoding(query_str, geonames_user):
    q = geocoder.geonames(query_str,
                          key=geonames_user,
                          featureClass='P',
                          country='IE',
                          fuzzy=0.6,
                          orderby='relevance')
    return q
Beispiel #9
0
    def __init__(self, geokey, geoauth, loc, code, searchtype='R', mset=EH_STANDARD, \
      nset=EN_STANDARD, fuzzy=FUZ_STANDARD, max_return=None):
        self.location = loc

        if type(code) != str:
            raise TypeError(typerr_msg)
        if searchtype not in SEARCH_TYPES:
            raise GeoError(searchtype)

        extra_args = {'fuzzy': fuzzy} if searchtype == 'RF' else {}
        rows = max_return if max_return is not None else 1

        if searchtype in ['E', 'EH', 'EN', 'EH EN', 'EN EH']:
            extra_args['name_equals'] = loc
            if max_return is None:
                rows = 10

        search_res = geonames(loc,
                              key=geokey,
                              auth=geoauth,
                              country=code,
                              maxRows=rows,
                              **extra_args)

        if search_res.status_code == 200:
            if search_res:
                if searchtype in ['R', 'RF', 'E']:
                    self.result = search_res[0]
                    return

                if searchtype in ['EH EN', 'EH', 'RIH']:
                    fcl = mset
                else:
                    fcl = nset

                if searchtype in ['RIH', 'RIN']:
                    if search_res[0].feature_class in fcl and loc in search_res[
                            0].address:
                        self.result = search_res[0]
                    else:
                        self.result = None
                    return

                for res in search_res:
                    if res.feature_class in fcl:
                        self.result = res
                        return

                if searchtype in ['EH EN', 'EN EH']:
                    self.result = search_res[0]
                else:
                    self.result = None
            else:
                self.result = None
        else:
            raise HTTPError(search_res.url, search_res.status_code,
                            'GeoNames request failed', search_res.headers,
                            None)
Beispiel #10
0
def getcoordinates(loc):
    try:
        g = geocoder.geonames(loc, key="KEY")
        #replace KEY with your geonames api key. Register at https://www.geonames.org/
        longitude = g.lng
        latitude = g.lat
        return (float(longitude), float(latitude))
    except:
        return 'NA', 'NA'
    def hydrate_from_geonames(self):
        if not self.geonames_id:
            return

        g = geocoder.geonames(
            self.geonames_id, key=settings.GEONAMES_KEY, method="details"
        )

        self._hydrate(g)
Beispiel #12
0
def geoLocate(countryName):
    global countryLocCache


    if not countryName in countryLocCache:
        try:
            #first pass
            res = geocoder.geonames(countryName, key=geoNamesKey)
            #second pass to get more details!
            res = geocoder.geonames(res.geojson['features'][0]['properties']['geonames_id'], method='details', key=geoNamesKey)
            props = res.geojson['features'][0]['properties']

            countryLocCache[countryName] = {'lonlat': [float(props['lng']), float(props['lat'])], 
                                            'cont': props['continent']}
        except Exception as E:
            print('Failed to get details for %s (error %s)'%(countryName, str(E)))
            countryLocCache[countryName] = {'lonlat': [0,0], 'cont': 'UN'}

    return countryLocCache[countryName] 
Beispiel #13
0
def getLocation(textLoc):
    #parameters maxRows=5 will return a list of the five first cities
    location = geonames(textLoc, key=config['geoname']['login_key'])
    if len(location) == 1:
        # print(location.address)
        # print(location.point.format_unicode())
        return [float(location.lng), float(location.lat)]
    else:
        print("GEONAME :: Location non trouvée : " + textLoc)
        raise NameError("LocationNotFound")
 def noun_resolver(string):
     geonames = geocoder.geonames(string,
                                  maxRows=5,
                                  key='e_hamzei',
                                  fuzzy=1)
     logging.info(geonames)
     print([(r.address, r.country, r.latlng) for r in geonames])
     if len(geonames) > 0:
         return True
     return False
Beispiel #15
0
def city_to_coord(city_string):
    '''
    Geocodes a city name into latitude, longitude coordinates

    @param city_string: the name of the city

    @return: A tuple with (latitude, longitude)
    '''
    print("restaurant coord\n\n")
    g = geocoder.geonames(city_string, key=GEONAMES_USERNAME)
    return (g.lat, g.lng)
Beispiel #16
0
def get_city_coord(city_name, country_code):
    try:
        g = geocoder.geonames(city_name,
                              country=[country_code],
                              key=os.environ.get('GEO_TOKEN', 'ilov3'))
        logger.debug(f'Found coords: {g.latlng} for {city_name}')
        return g.latlng
    except Exception as e:
        logger.error(
            f'Can not find coordinate for city: {city_name}. Error: {e}')
        return None, None
Beispiel #17
0
    def test__hydrate(self):
        p = Place(geonames_id=self.geonames_id)
        p._hydrate(None)
        self.assertIsNone(p.address)

        g = geocoder.geonames(p.geonames_id,
                              key=settings.GEONAMES_KEY,
                              method='details')

        p._hydrate(g)
        self.assertEquals(self.geonames_address, p.address)
Beispiel #18
0
def geoid(GEO_LIST):
    A = []
    B = []
    for i in GEO_LIST:
        g = geocoder.geonames(i, key='kevinshi')
        A.append(i)
        B.append(g.geonames_id)
        print(i)
        print(g.geonames_id)

    GEO_NAME = pd.DataFrame({'City': A, 'Geo_id': B})

    GEO_NAME.to_excel('./list_name.xlsx', index=False)
Beispiel #19
0
def test_hierarchy():
    url = 'http://api.geonames.org/hierarchyJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_hierarchy.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
    g = geocoder.geonames(6094817, method='hierarchy', key='mock')
    assert g.ok
    assert repr(g) == '<[OK] Geonames - Hierarchy #5 results>'
    assert len(g) == 5
    assert g.status_code == 200

    expected_names = ["Earth", "North America", "Canada", "Ontario", "Ottawa"]
    assert expected_names == [res.address for res in g]
Beispiel #20
0
def geonames_query(location):
    time.sleep(2.5)  # Slow down requests to avoid timeout (< 1/sec)

    # Enhance query with all named parts of location, if present
    query = location["base_name"]
    if (location["state_name"]):
        query = query + " " + location["state_name"]
    if (location["type"] == "city"):
        query = query + " " + location["country_name"]

    # Limit results to city and country feature types
    feature_classes = ['A', 'P']

    return geocoder.geonames(query, key=geonames_username, featureClass=feature_classes, maxRows=1).json["raw"]
def test_hierarchy():
    url = 'http://api.geonames.org/hierarchyJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_hierarchy.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='hierarchy', key='mock')
        assert g.ok
        assert repr(g) == '<[OK] Geonames - Hierarchy #5 results>'
        assert len(g) == 5
        assert g.status_code == 200

        expected_names = ["Earth", "North America",
                          "Canada", "Ontario", "Ottawa"]
        assert expected_names == [res.address for res in g]
def get_all_fa_places(fa_path):
    """Gets the English names from the GeoNames database for the Persian place names from Wikipedia
    that don't yet have an English counterpart in place_names_en_da.csv."""
    # List of Persian place names from Wikipedia
    fa_csv_list = csv_as_list(fa_path)
    # For each Persian name
    for fa_name in [r[0] for r in fa_csv_list]:
        # Try to get a list of the (English) names found for the search term from the GeoNames database
        en_names_list = list(
            geocoder.geonames(fa_name, key='USERNAME', maxRows=10))
        # If the list is not empty
        if en_names_list:
            # print the Persian name and the English names for review.
            print(f'{fa_name}\n{en_names_list}')
def test_children():
    url = 'http://api.geonames.org/childrenJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_children.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='children', key='mock')
        assert g.ok
        assert repr(g) == '<[OK] Geonames - Children #2 results>'
        assert len(g) == 2
        assert g.status_code == 200

        expected_names = ["Birch Manor", "Templeton-Est"]
        expected_geonames_id = [5901584, 6162703]
        assert expected_names == [res.address for res in g]
        assert expected_geonames_id == [res.geonames_id for res in g]
Beispiel #24
0
def city_to_coord(city_string):
    """
    Geocodes a city name into latitude, longitude coordinates

    @param city_string: the name of the city

    @return: A tuple with (latitude, longitude). If the city name
        is not valid, then (None, None)
    """

    g = geocoder.geonames(city_string, key=GEONAMES_USERNAME)

    if g.code == None:
        return (None, None)
    return (g.lat, g.lng)
Beispiel #25
0
 def get_lat_lon(self, city, country):
     """
     Get a city's lat and lon using Geonames
     """
     try:
         geodata = geocoder.geonames(city,
                                     country=country,
                                     key=self.geonames_user)
     except requests.exceptions.RequestException as error:
         print "Could not connect to geocoding API - exiting"
         raise SystemExit(error)
     if geodata.ok:
         return geodata.lat, geodata.lng
     print "No Geocode results found for %s %s" % (city, country)
     return False, False
    def _query(self, q, **kwargs):
        """Query GeoNames, change API key if hourly limit reached for current key, """
        g = None

        while (not hasattr(g, 'status')) or \
                ('timeout' in g.status) or ('Read timed out' in g.status) or ('hourly limit' in g.status):
            g = geocoder.geonames(q, **kwargs, key=self.get_apikey())
            self.log.info('Got GeoNames reply %s' % g)
            if 'hourly limit' in g.status:
                self.change_apikey()
                if self.apikey_index == 0:
                    self.log.info('Hourly limit reached for all keys, waiting for 10 minutes')
                    sleep(60 * 10)

        return g
Beispiel #27
0
def test_children():
    url = 'http://api.geonames.org/childrenJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_children.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='children', key='mock')
        assert g.ok
        assert repr(g) == '<[OK] Geonames - Children #2 results>'
        assert len(g) == 2
        assert g.status_code == 200

        expected_names = ["Birch Manor", "Templeton-Est"]
        expected_geonames_id = [5901584, 6162703]
        assert expected_names == [res.address for res in g]
        assert expected_geonames_id == [res.geonames_id for res in g]
def test_children_delegation():
    url = 'http://api.geonames.org/childrenJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_children.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='children', key='mock')
        assert g.ok
        assert repr(g) == '<[OK] Geonames - Children #2 results>'

        # next calls are delegated to result
        assert g.address == "Birch Manor"
        assert g.geonames_id == 5901584

        g.set_default_result(1)
        assert g.address == "Templeton-Est"
        assert g.geonames_id == 6162703
Beispiel #29
0
def test_children_delegation():
    url = 'http://api.geonames.org/childrenJSON?geonameId=6094817&username=mock'
    data_file = 'tests/results/geonames_children.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='children', key='mock')
        assert g.ok
        assert repr(g) == '<[OK] Geonames - Children #2 results>'

        # next calls are delegated to result
        assert g.address == "Birch Manor"
        assert g.geonames_id == 5901584

        g.set_default_result(1)
        assert g.address == "Templeton-Est"
        assert g.geonames_id == 6162703
def test_extra():
    url = 'http://api.geonames.org/searchJSON?q=Ottawa%2C+Ontario&fuzzy=0.8&username=mock&maxRows=1&featureClass=A'
    data_file = 'tests/results/geonames_extra.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(location, key='mock', featureClass='A')
        assert g.ok
        assert g.geonames_id == 8581623
        assert g.lat == '45.41858'
        assert g.lng == '-75.69717'
        assert g.address == 'Ottawa'
        assert g.country == 'Canada'
        assert g.country_code == 'CA'
        assert g.description == 'second-order administrative division'
        assert g.class_description == 'country, state, region,...'
        assert g.feature_class == 'A'
        assert g.code == 'ADM2'
Beispiel #31
0
def test_extra():
    url = 'http://api.geonames.org/searchJSON?q=Ottawa%2C+Ontario&fuzzy=0.8&username=mock&maxRows=1&featureClass=A'
    data_file = 'tests/results/geonames_extra.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(location, key='mock', featureClass='A')
        assert g.ok
        assert g.geonames_id == 8581623
        assert g.lat == '45.41858'
        assert g.lng == '-75.69717'
        assert g.address == 'Ottawa'
        assert g.country == 'Canada'
        assert g.country_code == 'CA'
        assert g.description == 'second-order administrative division'
        assert g.class_description == 'country, state, region,...'
        assert g.feature_class == 'A'
        assert g.code == 'ADM2'
Beispiel #32
0
    def tweets_by_countries_on_day(self):
        '''
        Returns number of tweets on a certain day from different countries

        :return: amount of tweets on mondays(eg)
        '''
        num_of_tweets = self._tweets_for_week_day()
        with sqlite3.connect('Twitter_data.db') as connection:
            cursor = connection.cursor()

            user_day = 'Sunday'  #only as example
            locations = []
            countries = {}
            for u_date in num_of_tweets[user_day]['dates']:
                selection = "SELECT * FROM TWITTER WHERE created_at LIKE ?"
                cr_at = f'%{u_date}%'
                cursor.execute(selection, [(cr_at)])

                posts = cursor.fetchall()
                for p in posts:
                    if 'No location' not in p[3]:
                        locations.append(p[3].replace(': ', '').strip())

                num_of_tweets[user_day]['posts'][u_date] = posts

            with sqlite3.connect('Cities_data.db') as connect:
                for loc in locations:
                    if ',' in loc: loc = loc[:loc.index(',')]
                    cursor_1 = connect.cursor()

                    select = "SELECT country_name FROM CITIES WHERE city=?"
                    cursor_1.execute(select, [(loc)])
                    print(loc)
                    res = cursor.fetchall()
                    # print(res)

                    try:
                        geo = geocoder.geonames(loc, key='sofiiatatosh')
                        country = geo.country
                    except:
                        country = res[0][0]

                    countries[country] = countries.get(country, 0)
                    countries[country] += 1

        return countries
    def get_geo(self, request, geo_query):
        """geocoder api endpoint"""
        try:
            resp = geocoder.geonames(geo_query,
                                     country=['BG'],
                                     key='timadevelop',
                                     maxRows=10,
                                     lang='bg',
                                     # adm and cities / village
                                     featureClass=['P', 'A'],
                                     fuzzy=1.0)
            # isNameRequired=True)
            # name_startsWith=[geo_query],
        except Location.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        result = [r.json for r in resp]
        return Response(result)
def test_geocoding_with_proximity():
    # query google first to get a bbox
    urls = [
        # when testing locally
        'https://maps.googleapis.com/maps/api/geocode/json?language=&address=Ottawa,%20Ontario&bounds=&components=&region=&key=mock',
        # when building in Travis (secured connection implies ordered parameters)
        'https://maps.googleapis.com/maps/api/geocode/json?client=[secure]&latlng=45.4215296%2C+-75.697193&sensor=false&signature=iXbq6odmrYN0XgcfB5EPcgEvR-I%3D'
    ]
    data_file = 'tests/results/google.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        for url in urls:
            mocker.get(url, text=input.read())
        google = geocoder.google(location, client=None, key='mock')
    # query geonames with bbox
    url = 'http://api.geonames.org/searchJSON?q=Ottawa%2C+Ontario&fuzzy=0.8&username=mock&maxRows=1&east=-75.2465979&west=-76.3539158&north=45.5375801&south=44.962733'
    data_file = 'tests/results/geonames_proximity.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(location, key='mock', proximity=google.bbox)
        assert g.ok
Beispiel #35
0
 def smallest_child(towns):
     import geocoder
     import random
     import workbench
     global geonames_username
     if towns:
         municipality = random.choice(towns)
         print("municipality:   " + str(municipality))
         villages = geocoder.geonames(municipality.geonames_id,
                                      key=geonames_username,
                                      method='children')
         if villages:
             #print(list(villages))
             village = random.choice(villages)
             #print("village:   "+str(village))
             return village
         else:
             return municipality
     else:
         return towns
Beispiel #36
0
    def tweets_amount_geonames(self):
        '''
        Method to get amount of tweets in certain countries using geonames

        Unfortunately, works with a limited amount of requests, so be careful
        using it in your code.
        It has approximately 1000 requests available in one hour

        :return: dictionary of a look {'country': num_of_tweets}
        '''
        cleared_locations = self.clean_locations()

        for loc in cleared_locations:
            g = geocoder.geonames(loc, key='sofiiatatosh')
            if g.country in self.tweets_amount_by_countries:
                self.tweets_amount_by_countries[g.country] += 1
            else:
                self.tweets_amount_by_countries[g.country] = 1

        return self.tweets_amount_by_countries
def test_details():
    url = 'http://api.geonames.org/getJSON?geonameId=6094817&username=mock&style=full'
    data_file = 'tests/results/geonames_details.json'
    with requests_mock.Mocker() as mocker, open(data_file, 'r') as input:
        mocker.get(url, text=input.read())
        g = geocoder.geonames(6094817, method='details', key='mock')

        assert g.lat == "45.41117"
        assert g.lng == "-75.69812"
        assert g.geonames_id == 6094817
        assert g.address == "Ottawa"
        assert g.feature_class == "P"
        assert g.class_description == "city, village,..."
        assert g.code == "PPLC"
        assert g.description == "capital of a political entity"
        assert g.continent == "NA"
        assert g.country_geonames_id == "6251999"
        assert g.country_code == "CA"
        assert g.country == "Canada"
        assert g.state == "Ontario"
        assert g.state_code == "08"
        assert g.state_geonames_id == "6093943"
        assert g.admin2 == ""
        assert g.admin3 == ""
        assert g.admin4 == ""
        assert g.admin5 == ""
        assert g.population == 812129
        assert g.srtm3 == 71
        assert g.wikipedia == "en.wikipedia.org/wiki/Ottawa"
        assert g.timeZoneId == "America/Toronto"
        assert g.timeZoneName == "America/Toronto"
        assert g.rawOffset == -5
        assert g.dstOffset == -4
        assert g.bbox == {
            'northeast': [45.58753415000007, -75.07957784899992],
            'southwest': [44.962202955000066, -76.35400795899994]}
Beispiel #38
0
def test_geonames():
    g = geocoder.geonames(location)
    assert g.ok
Beispiel #39
0
def test_geonames():
    g = geocoder.geonames(city)
    assert g.ok