Beispiel #1
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
Beispiel #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)
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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)
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)
Beispiel #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')
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
Beispiel #14
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")
Beispiel #15
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)
Beispiel #16
0
 def setUpClass(cls):
     cls.geocoder = Here(
         app_id=env['HERE_APP_ID'],
         app_code=env['HERE_APP_CODE'],
         timeout=10,
     )
Beispiel #17
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}
Beispiel #18
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))
Beispiel #19
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)

Beispiel #20
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
Beispiel #21
0
 def make_geocoder(cls, **kwargs):
     return Here(
         app_id=env['HERE_APP_ID'],
         app_code=env['HERE_APP_CODE'],
         timeout=10,
     )
Beispiel #22
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'
Beispiel #23
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')
Beispiel #24
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
Beispiel #25
0
 def make_geocoder(cls, **kwargs):
     return Here(
         apikey=env['HERE_APIKEY'],
         timeout=10,
         **kwargs
     )
Beispiel #26
0
 def test_error_with_no_keys(self):
     with pytest.raises(exc.ConfigurationError):
         Here()
Beispiel #27
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
Beispiel #28
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}
Beispiel #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'
Beispiel #30
0
 def test_no_warning_with_apikey(self):
     with warnings.catch_warnings(record=True) as w:
         Here(
             apikey='DUMMYKEY1234',
         )
     assert len(w) == 0