Esempio n. 1
0
def create_location_model(data):
    task_list = data['task_list']
    geopy.geocoders.options.default_timeout = 15
    location = []
    #not_found = 0
    for index, task in enumerate(task_list):
        t = (0, 0)
        try:
            task["lat"]
            # lat not given, try reversing address
        except KeyError:
            geolocator = Here("Pe8yMxfGgxyn9yWsRDU9", "OrUmfFZvhbc2KNVLpvffrw")
            address = clean_address(task["address"])
            glocation = geolocator.geocode(address)
            #check if lat lon is available, else move it to the antartic zone :)
            try:
                glocation.address
            except AttributeError:
                t = (0, 0)
            else:
                t = (glocation.latitude, glocation.longitude)
        else:
            t = (task["lat"], task["lon"])
            task["address"] = index
        finally:
            print(task["address"])
            print('{"lat":"', t[0], '","lon":"', t[1], '"},')
            location.append(t)
    return location
Esempio n. 2
0
def geocoding():
    result = request.get_json(force=True)
    data = {}

    try:
        data['address'] = result['address']
    except KeyError:
        response = {}
        response["status"] = False
        response["message"] = "address is required"
        response["request"] = result
        return json.dumps(response)

    t = "0,0"
    geolocator = Here(app_id="brUMhDedQJcTEBv3WRm2",
                      app_code="gEuqige5xqyAJ3Gi5x1Qaw")
    #geolocator = Here("Pe8yMxfGgxyn9yWsRDU9","OrUmfFZvhbc2KNVLpvffrw") #old auth
    address = clean_address(data["address"])
    glocation = geolocator.geocode(address)
    #check if lat lon is available, else move it to the antartic zone :)
    try:
        glocation.address
    except KeyError:
        t = "0,0"
    else:
        t = str(glocation.latitude) + "," + str(glocation.longitude)

    data["coordinate"] = t
    return json.dumps(data)
Esempio n. 3
0
def create_location_model(data):
    task_list = data['task_list']
    location = []
    not_found = 0
    for task in task_list:
        t = (0, 0)
        try:
            task["lat"]
            # lat not given, try reversing address
        except KeyError:
            #georeverse address
            geolocator = Here("brUMhDedQJcTEBv3WRm2", "gEuqige5xqyAJ3Gi5x1Qaw")
            #geodata =  {
            #"street": clean_address(task["address"]),
            #"country": "IDN"
            #}
            #print (geodata["street"])
            address = clean_address(task["address"])
            #print(address)
            glocation = geolocator.geocode(address)
            #check if lat lon is available, else move it to the antartic zone :)
            try:
                glocation.address
            except AttributeError:
                t = (0, 0)
                #not_found = not_found +1
                #print (not_found)
            else:
                t = (glocation.latitude, glocation.longitude)
        else:
            t = (task["lat"], task["lon"])
        finally:
            print('{"lat":"', t[0], '","lon":"', t[1], '"},')
            location.append(t)
    return location
Esempio n. 4
0
    def get_geo_coordinates_from_address(self, address):
        if not address:
            return {'error': True, 'message': 'Address is required'}

        geocoder = Here(self.app_id, self.app_code)
        response = geocoder.geocode(address)

        return response
Esempio n. 5
0
    def here(self, addr, local, country, saveraw):
        output = self.initOutput()

        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)

        # init service if not init yet
        if not self.geolocator_here:
            self.geolocator_here = Here(
                app_id=self.SERVICES[self.CURRENT_SERVICE]['app_id'],
                app_code=self.SERVICES[self.CURRENT_SERVICE]['app_code'])

        # geocode address
        location = self.geolocator_here.geocode(address,
                                                exactly_one=False,
                                                language="pt-PT")
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["number_of_results"] = len(location)

            output["input_string"] = address

            output["accuracy"] = answer.get('Relevance')

            if answer.get("Location"):
                output["formatted_address"] = answer.get("Location").get(
                    'Address').get('Label')
                output["place_id"] = answer.get("Location").get("LocationId")

            if answer.get("Location"):
                if answer.get("Location").get("Address"):
                    output["postcode"] = answer.get("Location").get(
                        "Address").get("PostalCode")
                    # all 4 are not tghrustworthy
                    output["freguesia"] = answer.get("Location").get(
                        "Address").get("District")
                    output["distrito"] = answer.get("Location").get(
                        "Address").get("County")
                    output["concelho"] = answer.get("Location").get(
                        "Address").get("City")
                    output["localidade"] = answer.get("Location").get(
                        "Address").get("City")

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output
Esempio n. 6
0
	def coordonnees_adresse(self):
		''' Définition des latitude et longitude de l'Adresse.'''
		geoloc = Here(apikey=env('HERE_APIKEY'))
		try:
			loc = geoloc.geocode(self.voirie+', '+self.numero+', '+\
		self.localite).raw['Location']['NavigationPosition'][0]
			return (loc['Latitude'], loc['Longitude'])
		except:
			return (0,0)
Esempio n. 7
0
def get_lat_lon(address):
    global APP_ID_HERE, APP_CODE_HERE

    geocoder = Here(APP_ID_HERE, APP_CODE_HERE)
    result = geocoder.geocode(address)

    point = result.point

    return point
def get_location_info(filename):
    exif = get_exif(filename)
    if exif:
        geotags = get_geotagging(exif)
        if geotags:
            coords = get_coordinates(geotags)
            if coords:
                geocoder = Here(apikey=HERE_APIKEY)
                return geocoder.reverse("%s,%s" % coords)
Esempio n. 9
0
 def test_warning_with_legacy_auth(self):
     with warnings.catch_warnings(record=True) as w:
         Here(
             app_id='DUMMYID1234',
             app_code='DUMMYCODE1234',
         )
     assert len(w) == 1
Esempio n. 10
0
 def make_geocoder(cls, **kwargs):
     with warnings.catch_warnings(record=True) as w:
         geocoder = Here(app_id=env['HERE_APP_ID'],
                         app_code=env['HERE_APP_CODE'],
                         timeout=10,
                         **kwargs)
     assert len(w) == 1
     return geocoder
Esempio n. 11
0
def get_geocoder(apikey, map_service='google'):
    if map_service == 'google':
        return GoogleV3(api_key=apikey)
    elif map_service == 'osm':
        _osm = Nominatim(user_agent="test_app")
        return RateLimitedGeocoder(_osm)
    elif map_service == 'here':
        return Here(apikey=apikey)

    raise Exception('Map service not supported')
Esempio n. 12
0
def geocoding(ctx, query, apikey, forward, raw, display):
    """
    HERE's geocoding service.
    \f

    :param ctx: A context dictionary.
    :param query: A string to represent address query for geocoding.
    :param apikey: An API key for authentication.
    :param forward: A boolean flag for forward/reverse geocoding.
    :param raw: A boolean flag to show api response as it is.
    :param display: A boolean flag to show result in web browser.
    :return: None.
    """
    apikey = apikey or os.environ.get("HERE_APIKEY")
    if apikey is None:
        raise ApiKeyNotFoundError(
            "Please pass HERE API KEY as --apikey or set it as environment "
            "variable in HERE_APIKEY "
        )
    ctx.obj["apikey"] = apikey
    geolocator = Here(apikey=ctx.obj["apikey"])
    if forward:
        location = geolocator.geocode(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        elif display:
            feature = get_feature_from_lat_lon(location.latitude, location.longitude)
            geo_display(feature)
        else:
            result = {"lat": location.latitude, "lon": location.longitude}
            click.secho(json.dumps(result, indent=2), fg="green")
    else:
        location = geolocator.reverse(query)
        if raw:
            click.secho(json.dumps(location.raw, indent=2), fg="green")
        else:
            click.secho(location.address, fg="green")
Esempio n. 13
0
def georeverse():
    result = request.get_json(force=True)
    data = {}

    try:
        data['coordinate'] = result['coordinate']
    except AttributeError:
        response = {}
        response["status"] = False
        response["message"] = "coordinate is required"
        response["request"] = result
        return json.dumps(response)

    if (not validate_lat_lon(data['coordinate'])):
        response = {}
        response["status"] = False
        response["message"] = "coordinate format is invalid"
        response["request"] = result
        return json.dumps(response)

    t = ""
    geolocator = Here(app_id="brUMhDedQJcTEBv3WRm2",
                      app_code="gEuqige5xqyAJ3Gi5x1Qaw")
    #geolocator = Here("Pe8yMxfGgxyn9yWsRDU9","OrUmfFZvhbc2KNVLpvffrw") #old auth
    glocation = geolocator.reverse(query=data["coordinate"])
    #check if lat lon is available, else move it to the antartic zone :)

    try:
        glocation.address
    except AttributeError:
        t = ""
    else:
        t = glocation.address

    data["address"] = t
    return json.dumps(data)
Esempio n. 14
0
def test_geolocators():
    """Test if locations from various geolocators are more than 1km apart.
    """
    geocoders = [Nominatim(user_agent="pyteen")]
    try:
        apikey = environ["HEREMAPS_API_KEY"]
        geocoders.append(Here(apikey=apikey))
    except KeyError:
        pass
    query = "Berlin, Germany"
    locations = list(multi_geocode(query, geocoders=geocoders))
    for (p, q) in combinations(locations, 2):
        dist = great_circle((p["lat"], p["lon"]), (q["lat"], q["lon"]))
        msg = f"Geocoders {p['name']} and {q['name']} return locations {dist} km apart."
        assert dist <= 1, msg
def collectGeocoders():
    config = configparser.ConfigParser()
    conf = r'..\conf\config.ini'
    config.read(conf)
    keys = {
        'Here_app_id': config['DEFAULT']['Here_app_id'],
        'Here_app_code': config['DEFAULT']['Here_app_code'],
        'TomTom': config['DEFAULT']['TomTom_api_key'],
        'OpenMapQuest': config['DEFAULT']['OpenMapQuest_api_key'],
        'GoogleV3': config['DEFAULT']['GoogleV3_api_key']
    }
    locators = [{
        'locator': Nominatim(user_agent="afan"),
        'name': 'Nominatim',
        'type': 'Geopy'
    }, {
        'locator': GeoNames(username="******"),
        'name': 'GeoNames',
        'type': 'Geopy'
    }, {
        'locator':
        Here(app_id=keys['Here_app_id'], app_code=keys['Here_app_code']),
        'name':
        'Here',
        'type':
        'Geopy'
    }, {
        'locator': TomTom(api_key=keys['TomTom']),
        'name': 'TomTom',
        'type': 'Geopy'
    }, {
        'locator': OpenMapQuest(api_key=keys['OpenMapQuest']),
        'name': 'OpenMapQuest',
        'type': 'Geopy'
    }, {
        'locator': Photon(),
        'name': 'Photon',
        'type': 'Geopy'
    }]
    #locators.append({'locator':GoogleV3(api_key=keys['GoogleV3']),'name':'GoogleV3','type':'Geopy'})
    locators.append({
        'locator': revGeocodingbyIQ,
        'name': 'revGeocodingbyIQ',
        'type': 'Custom'
    })

    return locators
Esempio n. 16
0
# %%
import pandas as pd
from geopy.geocoders.base import GeocoderTimedOut
from geopy.geocoders import Here

geolocator = Here('kO1cowFWMjjOgIKv6cFd', '_llArvZJD-XXKWs4q41hkQ')

# %%
states = pd.read_csv('data/state_fips.csv',
                     dtype=object).set_index('State Code')['Name'].to_dict()
counties = pd.read_csv('data/county_fips.csv', dtype=object)
counties.loc[counties['State Code'] == '72', 'Name'] = ''


# %%
def lookup(county):
    query = {'county': county['Name'], 'state': states[county['State Code']]}
    lat, lon = 0, 0
    try:
        obj = geolocator.geocode(query)
        lat, lon = obj.latitude, obj.longitude
    except AttributeError:
        print(
            f"-------------------------\n{county['Name']}, {states[county['State Code']]} not found."
        )
    except GeocoderTimedOut:
        print(
            f"-------------------------\nHERE timed out. on {county['Name']}, {states[county['State Code']]}"
        )
    finally:
        return lat, lon
Esempio n. 17
0
 def test_user_agent_custom(self):
     geocoder = Here(app_id='DUMMYID1234',
                     app_code='DUMMYCODE1234',
                     user_agent='my_user_agent/1.0')
     self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
Esempio n. 18
0
 def setUpClass(cls):
     cls.geocoder = Here(
         app_id=env['HERE_APP_ID'],
         app_code=env['HERE_APP_CODE'],
         timeout=10,
     )
Esempio n. 19
0
def get_geolocation(street: str, postal_code: str, city: str) -> dict:
    geolocator = Here(settings.HERE_APP_ID, settings.HERE_APP_CODE)
    location = geolocator.geocode(" ".join([street, postal_code, city]))
    if location:
        return {"lat": location.latitude, "lng": location.longitude}
    return {"lat": None, "lng": None}
Esempio n. 20
0
APP_ID = config[0]
APP_CODE = config[1]

label_size = 16
mpl.rcParams['xtick.labelsize'] = label_size 
mpl.rcParams['ytick.labelsize'] = label_size 
mpl.rcParams['axes.labelsize'] = label_size
mpl.rcParams['axes.titlesize'] = label_size + 4


DF = "sept2018_pc.csv"
HEADER = "Date,City,State,Event Type,TO,Best of,Event Locator Link,Notes,Total Players,Lat,Lon"

# create north polar stereographic basemap
m = Basemap(width=6000000,height=4500000,resolution='c',projection='aea',lat_1=35.,lat_2=45,lon_0=-100,lat_0=40)
geolocator = Here(APP_ID, APP_CODE)

# number of points, bins to plot.
bins = 12

# Read the data
data = np.genfromtxt(DF, dtype=str, delimiter=",", skip_header=1)

city =  data[:,1]
state = data[:,2]
nplayers = []
for ii in data[:,8]:
    if ii == '':
        nplayers.append(0)
    else:
        nplayers.append(int(ii))
Esempio n. 21
0
from itertools import tee

import requests
# import contextily as ctx
from geographiclib.geodesic import Geodesic
from geopy.geocoders import Here
from geopy.distance import geodesic
from ipyleaflet import Marker, CircleMarker, Polyline
from ipywidgets import HTML
# from pyproj import Proj, transform

from credentials import APP_ID, APP_CODE

app_id = APP_ID
app_code = APP_CODE
geocoder = Here(app_id=app_id, app_code=app_code)

# Conversion between lat/lon in degrees (and zoom) to x/y/zoom as used in tile sets,
# from http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Python

from math import radians, degrees, log, cos, tan, pi, atan, sinh


def deg2tile(lat_deg, lon_deg, zoom):
    lat_rad = radians(lat_deg)
    n = 2.0**zoom
    xtile = int((lon_deg + 180.0) / 360.0 * n)
    ytile = int((1.0 - log(tan(lat_rad) + (1 / cos(lat_rad))) / pi) / 2.0 * n)
    return (xtile, ytile)

Esempio n. 22
0
 def test_no_warning_with_apikey(self):
     with warnings.catch_warnings(record=True) as w:
         Here(
             apikey='DUMMYKEY1234',
         )
     assert len(w) == 0
Esempio n. 23
0
class Geocode():

    #SERVICES = []

    #IGNORE = []

    CURRENT_SERVICE = 0

    geolocator_google = None
    geolocator_here = None
    geolocator_bing = None
    geolocator_tomtom = None
    geolocator_azure = None
    geolocator_nominatum = None

    #SHOW_ERRORS = True

    def __init__(self, services=None, ignore=None):
        self.SERVICES = services
        self.IGNORE = ignore

    ############ SERVICES ############

    def initOutput(self):
        output = {}
        output["formatted_address"] = None
        output["latitude"] = None
        output["longitude"] = None
        output["accuracy"] = None
        output["place_id"] = None
        output["type"] = None
        output["postcode"] = None
        output["input_string"] = None
        output["number_of_results"] = None
        output["status"] = None
        output["response"] = None
        output["localidade"] = None
        output["distrito"] = None
        output["concelho"] = None
        output["freguesia"] = None
        output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']
        return output

    def google(self, addr, local, country, saveraw):

        output = self.initOutput()
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)

        # init service if not init yet
        if not self.geolocator_google:
            self.geolocator_google = GoogleV3(
                api_key=self.SERVICES[self.CURRENT_SERVICE]['key'])

        # geocode address
        location = self.geolocator_google.geocode(
            address, exactly_one=False)  #, components={"country": "PT"})
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["formatted_address"] = location[0].address
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["accuracy"] = answer.get('geometry').get('location_type')
            output["place_id"] = answer.get("place_id")
            output["type"] = ",".join(answer.get('types'))
            output["postcode"] = ",".join([
                x['long_name'] for x in answer.get('address_components')
                if 'postal_code' in x.get('types')
            ])
            output["input_string"] = address
            output["number_of_results"] = len(location)
            output["localidade"] = ",".join([
                x['long_name'] for x in answer.get('address_components')
                if 'locality' in x.get('types')
            ]).split(',')[0]

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    def tomtom(self, addr, local, country, saveraw):
        output = self.initOutput()
        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)
        # init service if not init yet
        if not self.geolocator_tomtom:
            self.geolocator_tomtom = TomTom(api_key=self.SERVICES[
                self.CURRENT_SERVICE]['key'])  #,default_scheme = 'https')

        # geocode address
        location = self.geolocator_tomtom.geocode(
            address, exactly_one=False)  #, components={"country": "PT"})
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude

            output["accuracy"] = answer.get('score')
            output["input_string"] = address
            output["number_of_results"] = len(
                location)  #answer.get("numResults")
            output["place_id"] = answer.get("id")

            if answer.get("address"):
                output["distrito"] = answer.get("address").get(
                    "countrySubdivision")
                # maybe?
                output["concelho"] = answer.get("address").get("municipality")
                output["freguesia"] = answer.get("address").get(
                    "municipalitySubdivision")
                output["formatted_address"] = answer.get('address').get(
                    'freeformAddress')
                CPext = answer.get("address").get('extendedPostalCode')
                CP = answer.get("address").get('postalCode')
                if CPext:
                    CPext = CPext.split(',')[0]
                    CPext = CPext[:4] + '-' + CPext[4:]
                    output["postcode"] = CPext
                elif CP:
                    output["postcode"] = CP.split(',')[0]

            output["type"] = answer.get('type')
            #output["query_type"] = answer.get("queryType")

            # maybe?
            #output["localidade"] = answer.get("address").get("municipality")

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    def nominatim(self, addr, local, country, saveraw):
        output = self.initOutput()

        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)
        '''
		query = {	'street': data[1],
					'city':data[2],
					'country': 'Portugal'
				}
		'''

        # init service if not init yet
        if not self.geolocator_nominatum:
            self.geolocator_nominatum = Nominatim(user_agent="tests_1")

        # geocode address
        location = self.geolocator_nominatum.geocode(address,
                                                     exactly_one=False,
                                                     addressdetails=True)
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["number_of_results"] = len(location)
            #output["accuracy"] = answer.get('importance')
            output["place_id"] = answer.get("osm_id")
            output["input_string"] = address
            if answer.get("address"):
                output["postcode"] = re.sub(
                    '[^0-9-]+', '',
                    answer.get("address").get("postcode"))  ###???
                output["freguesia"] = answer.get("address").get("suburb")
                output["localidade"] = answer.get("address").get("city")
                if not output["localidade"]:
                    output["localidade"] = answer.get("address").get("town")
                output["formatted_address"] = answer.get('address').get(
                    'display_name')

            output["type"] = answer.get('osm_type')

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    def bing(self, addr, local, country, saveraw):
        output = self.initOutput()

        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)

        # init service if not init yet
        if not self.geolocator_bing:
            self.geolocator_bing = Bing(
                api_key=self.SERVICES[self.CURRENT_SERVICE]['key'])

        # geocode address
        location = self.geolocator_bing.geocode(
            address,
            exactly_one=False)  #culture='PT',  include_neighborhood=True,
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["number_of_results"] = len(location)

            if answer.get("address"):
                output["formatted_address"] = answer.get('address').get(
                    'formattedAddress')
                output["localidade"] = answer.get("address").get("locality")
                output["distrito"] = answer.get("address").get("adminDistrict")
                output["concelho"] = answer.get("address").get(
                    "adminDistrict2")
                output["freguesia"] = answer.get("address").get("neighborhood")
                output["postcode"] = answer.get("address").get("postalCode")

            output["accuracy"] = answer.get('confidence')

            output["input_string"] = address

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    def here(self, addr, local, country, saveraw):
        output = self.initOutput()

        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)

        # init service if not init yet
        if not self.geolocator_here:
            self.geolocator_here = Here(
                app_id=self.SERVICES[self.CURRENT_SERVICE]['app_id'],
                app_code=self.SERVICES[self.CURRENT_SERVICE]['app_code'])

        # geocode address
        location = self.geolocator_here.geocode(address,
                                                exactly_one=False,
                                                language="pt-PT")
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["number_of_results"] = len(location)

            output["input_string"] = address

            output["accuracy"] = answer.get('Relevance')

            if answer.get("Location"):
                output["formatted_address"] = answer.get("Location").get(
                    'Address').get('Label')
                output["place_id"] = answer.get("Location").get("LocationId")

            if answer.get("Location"):
                if answer.get("Location").get("Address"):
                    output["postcode"] = answer.get("Location").get(
                        "Address").get("PostalCode")
                    # all 4 are not tghrustworthy
                    output["freguesia"] = answer.get("Location").get(
                        "Address").get("District")
                    output["distrito"] = answer.get("Location").get(
                        "Address").get("County")
                    output["concelho"] = answer.get("Location").get(
                        "Address").get("City")
                    output["localidade"] = answer.get("Location").get(
                        "Address").get("City")

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    ###

    def azure(self, addr, local, country, saveraw):
        output = self.initOutput()

        # create query
        address = "" if addr is None else addr
        address = address + ("" if local is None else "," + local)
        address = address + ("" if country is None else "," + country)

        # init service if not init yet
        if not self.geolocator_azure:
            self.geolocator_azure = AzureMaps(
                subscription_key=self.SERVICES[self.CURRENT_SERVICE]['key'])

        # geocode address
        location = self.geolocator_azure.geocode(address,
                                                 exactly_one=False,
                                                 language="pt-PT")
        if location is not None:
            answer = location[0].raw

            output['status'] = "OK"
            output["latitude"] = location[0].latitude
            output["longitude"] = location[0].longitude
            output["number_of_results"] = len(location)

            output["input_string"] = address

            output["accuracy"] = answer.get('score')

            output["place_id"] = answer.get("id")

            if answer.get("address"):
                output["formatted_address"] = answer.get('address').get(
                    'freeformAddress')
                output["distrito"] = answer.get("address").get(
                    "countrySubdivision")
                # maybe?
                output["concelho"] = answer.get("address").get("municipality")
                output["freguesia"] = answer.get("address").get(
                    "municipalitySubdivision")
                CPext = answer.get("address").get('extendedPostalCode')
                CP = answer.get("address").get('postalCode')
                if CPext:
                    CPext = CPext.split(',')[0]
                    CPext = CPext[:4] + '-' + CPext[4:]
                    output["postcode"] = CPext
                elif CP:
                    output["postcode"] = CP.split(',')[0]

            output["type"] = answer.get('type')

            output["service"] = self.SERVICES[self.CURRENT_SERVICE]['service']

            if saveraw:
                output["response"] = location[0].raw

        else:
            output['status'] = "ZERO_RESULTS"

        return output

    ############ PROCESS FILE ############

    def getService(self):

        if self.CURRENT_SERVICE >= len(self.SERVICES):
            raise UnableToGeocode("Unable to geocode entity.")

        if len(self.IGNORE) >= len(self.SERVICES):
            raise OutOfServices("No service available.")

        for i in self.SERVICES:
            if self.SERVICES[self.CURRENT_SERVICE]['service'] in self.IGNORE:
                self.CURRENT_SERVICE = self.CURRENT_SERVICE + 1
                if self.CURRENT_SERVICE >= len(self.SERVICES):
                    raise UnableToGeocode("Unable to geocode entity.")
            else:
                break

        if "GOOGLE" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.google
        elif "TOMTOM" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.tomtom
        elif "NOMINATUM" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.nominatim
        elif "BING" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.bing
        elif "HERE" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.here
        elif "AZURE" in self.SERVICES[self.CURRENT_SERVICE]['service']:
            return self.azure

        return None

    # service = None => all available
    def geocode(self,
                addr=None,
                local=None,
                country="Portugal",
                saveraw=True,
                service=None):
        geocoded = False
        self.CURRENT_SERVICE = 0
        geocode_result = None

        if service:
            for s in self.SERVICES:
                if s['service'] != service:
                    self.IGNORE.append(s['service'])

        while not geocoded:
            try:
                serv = self.getService()
                geocode_result = serv(addr, local, country, saveraw)
                if geocode_result['status'] == "OK":
                    geocoded = True
                    break
                else:
                    self.CURRENT_SERVICE = self.CURRENT_SERVICE + 1
                '''
						else:
							if DEBUG:
								logger.error ('\n--------------------------------------------------------------------')
								logger.error ('ERROR: no addr/name for id_localization [{}].'.format(address.split('|')[0]))
								logger.error ('Passing to next address.')
								logger.error ('--------------------------------------------------------------------')
							CURRENT_SERVICE = 0
							geocode_result = initOutput()
							geocode_result['id_localization'] = address.split('|')[0]
							geocode_result['status'] = "NO_DATA"				
							break	
				'''
            except UnableToGeocode as e:
                if self.SHOW_ERRORS:
                    pass
                    #logger.error ('\n--------------------------------------------------------------------')
                    #logger.error ('ERROR: Unable to geocode addr [{}].'.format(addr))
                    #logger.error ('Passing to next address.')
                    #logger.error ('--------------------------------------------------------------------')
                self.CURRENT_SERVICE = 0
                geocode_result = self.initOutput()
                geocode_result['status'] = "UNABLE"
                geocode_result['service'] = "ALL"
                break
            except OutOfServices as e:
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('ERROR: you reached the limit on all services. No more services available.')
                #	logger.error ('Saving the all sucessuful results and exiting the application.')
                #	logger.error ('--------------------------------------------------------------------')
                raise
                #return None
            except (GeocoderQueryError, GeocoderAuthenticationFailure,
                    GeocoderInsufficientPrivileges, ConfigurationError):
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('ERROR: something wrong with either the service or the query.')
                #	logger.error ('Check service: [{}]'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #	logger.error ('Passing to the next service.')
                #	logger.error ('--------------------------------------------------------------------')
                self.IGNORE.append(
                    self.SERVICES[self.CURRENT_SERVICE]['service'])
            except GeocoderQuotaExceeded:
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('ERROR: you have reached the end of your quota for service [{}].'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #	logger.error ('Passing to the next service.')
                #	logger.error ('--------------------------------------------------------------------')
                self.IGNORE.append(
                    self.SERVICES[self.CURRENT_SERVICE]['service'])
            except GeocoderTimedOut:
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('TIMEOUT: something went wrong with the geocoding the address: [{}].'.format(addr))
                #	logger.error ('while using service [{}].'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #	logger.error ('Passing to the next service.')
                #	logger.error ('--------------------------------------------------------------------')
                self.IGNORE.append(
                    self.SERVICES[self.CURRENT_SERVICE]['service'])
            except (GeocoderServiceError, GeocoderUnavailable):
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('ERROR: service unavailable or unknown error for service [{}].'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #	logger.error ('Passing to the next service.')
                #	logger.error ('--------------------------------------------------------------------')
                self.IGNORE.append(
                    self.SERVICES[self.CURRENT_SERVICE]['service'])
            except GeocoderNotFound:
                #if self.SHOW_ERRORS:
                #	logger.error ('\n--------------------------------------------------------------------')
                #	logger.error ('ERROR: unknown service > [{}].'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #	logger.error ('check if this service still exists!')
                #	logger.error ('Passing to the next service.')
                #	logger.error ('--------------------------------------------------------------------')
                self.IGNORE.append(
                    self.SERVICES[self.CURRENT_SERVICE]['service'])
            except Exception as e:
                #logger.error ('\n--------------------------------------------------------------------')
                #logger.error("Unknown catastrophic error while processing address: {}".format(addr))
                #logger.error('while using service > [{}].'.format(self.SERVICES[self.CURRENT_SERVICE]['id']))
                #logger.error("Check the error and correct it before restart the application.")
                #logger.error(str(e))
                #logger.error('--------------------------------------------------------------------')
                raise
                #return None

        return geocode_result
Esempio n. 24
0
 def test_user_agent_custom(self):
     geocoder = Here(
         apikey='DUMMYKEY1234',
         user_agent='my_user_agent/1.0'
     )
     assert geocoder.headers['User-Agent'] == 'my_user_agent/1.0'
Esempio n. 25
0
'''
Goes through every entry on the OHA Healthspace web site and
checks for those that aren't currently in the mapper database.
'''

from HealthSpaceDriver import HealthSpaceDriver
from HealthSpaceContainers import Facility
from HealthSpaceDB import HealthSpaceDB
from Converter import GeoJSONConverter
from geopy.geocoders import Here
from geopy.exc import GeocoderTimedOut
from geopy.exc import GeocoderServiceError
import config

geocoder = Here(config.GEOCODER_APP_ID, config.GEOCODER_APP_CODE)
database = HealthSpaceDB()
driver = HealthSpaceDriver()
facilityHolder = Facility()


def geocode_get(string_in):
    location = None
    try:
        location = geocoder.geocode(string_in)
    except GeocoderTimedOut:
        return geocode_get(string_in)
    except GeocoderServiceError:
        print(string_in + "Service Error")
        return None
    return location
Esempio n. 26
0
 def make_geocoder(cls, **kwargs):
     return Here(
         apikey=env['HERE_APIKEY'],
         timeout=10,
         **kwargs
     )
Esempio n. 27
0
import time

file = open(r"/Library/WebServer/Documents/JSConferences/data2019.json",
            "r",
            encoding="utf-8")
data = json.load(file)
features = data["features"]

geojsonFeatures = []

while len(features) > 0:
    feat = features.pop(0)
    address = feat["city"] + "+" + feat["country"]
    print(address)
    #geolocator = Nominatim(user_agent="Ralucas-app", ssl_context= ssl.SSLContext())
    geolocator = Here(app_id="", app_code="", ssl_context=ssl.SSLContext())
    location = geolocator.geocode(address)
    if location is not None:
        geojsonFeature = {
            "type": "Feature",
            "properties": feat,
            "geometry": {
                "type": "Point",
                "coordinates": [location.longitude, location.latitude]
            }
        }
        geojsonFeatures.append(geojsonFeature)
        print(geojsonFeature)
    time.sleep(5)
    print("------------------")
geojson = {"type": "FeatureCollection", "features": geojsonFeatures}
Esempio n. 28
0
    def here(self, address=None, city=None, country=None, key_here=None):

        if not key_here:
            raise RuntimeError(
                "Requires a key! Check https://developer.here.com/ for more information."
            )
        if not address and not city and not country:
            raise RuntimeError(
                "Requires an address and/or a city and/or a country!")

        addr = "" if address is None else address
        #addr = ("" if address is None else ", " + address)
        addr += ("" if city is None else ", " + city)
        addr += ("" if country is None else ", " + country)

        result = self.newResult()
        result['service'] = 'here'
        result['status'] = 'ZERO_RESULTS'

        try:
            geolocator_here = Here(apikey=key_here)
            location = geolocator_here.geocode(addr,
                                               exactly_one=False,
                                               language="pt-PT")

            if location is not None:
                answer = location[0].raw

                result['status'] = "OK"
                result["latitude"] = location[0].latitude
                result["longitude"] = location[0].longitude
                result["number_of_results"] = len(location)

                result["input_string"] = address

                result["accuracy"] = answer.get('Relevance')

                if answer.get("Location"):
                    result["formatted_address"] = answer.get("Location").get(
                        'Address').get('Label')
                    result["place_id"] = answer.get("Location").get(
                        "LocationId")

                if answer.get("Location"):
                    if answer.get("Location").get("Address"):
                        result["postcode"] = answer.get("Location").get(
                            "Address").get("PostalCode")
                        # all 4 are not tghrustworthy
                        result["freguesia"] = answer.get("Location").get(
                            "Address").get("District")
                        result["distrito"] = answer.get("Location").get(
                            "Address").get("County")
                        result["concelho"] = answer.get("Location").get(
                            "Address").get("City")
                        result["localidade"] = answer.get("Location").get(
                            "Address").get("City")

                #if saveraw:
                #	output["response"] = location[0].raw

        except (GeocoderQueryError, GeocoderAuthenticationFailure,
                GeocoderInsufficientPrivileges, ConfigurationError):
            result['status'] = 'ACCESS_ERROR'
        except GeocoderQuotaExceeded:
            result['status'] = 'QUOTA_EXCEEDED'
        except GeocoderTimedOut:
            result['status'] = 'TIME_OUT'
        except (GeocoderServiceError, GeocoderUnavailable, GeocoderNotFound):
            result['status'] = 'SERVICE_ERROR'
        except Exception as e:
            result['status'] = 'UNKNOWN_ERROR'

        return result
Esempio n. 29
0
from geopy.geocoders import Here

HERE_app_id = 'kO1cowFWMjjOgIKv6cFd'
HERE_app_code = '_llArvZJD-XXKWs4q41hkQ'

geolocator = Here(HERE_app_id, HERE_app_code)

states_fips = '../data/state_fips.csv'
county_fips = '../data/county_fips.csv'
county_locs = '../data/county_loc.csv'
Esempio n. 30
0
 def test_error_with_no_keys(self):
     with pytest.raises(exc.ConfigurationError):
         Here()