예제 #1
0
def openCSV(fileName):
    with open('addresses.csv', 'r') as infile:
        with open(fileName + '.csv', 'w') as outfile:
            reader = csv.DictReader(infile)
            fnames = reader.fieldnames + ['latitude', 'longitude', 'county']
            print('headers are: ' + fnames)
            write = csv.DictWriter(outfile, fieldnames = fnames)
            write.writeheader()
            print('wrote header')
            for row in reader:
                address = row['address']
                city = row['city']
                state = row['state']
                zipcode = row['zip']
                fullAddress = address + ' ' + city + ' ' + state + ' ' + zipcode
                from geopy.geocoders import Bing
                geocoder = Bing(api_key = '')
                loc = geocoder.geocode(fullAddress)
                county = loc.raw['address']['adminDistrict2']
                lat = loc.latitude
                lng = loc.longitude
                write.writerow({'address' : address,
                                'city' : city,
                                'state' : state,
                                'zip' : zipcode,
                                'latitude' : lat,
                                'longitude' : lng,
                                'county' : county
                                })
예제 #2
0
def bing(query):
	p = Point((bounds[0]+bounds[2]/2,(bounds[1]+bounds[3])/2))
	b = Bing( os.environ.get('BING_API_KEY'))
	try:
		place, (lat, lng) = b.geocode(query, user_location=p)
		return place, (lat, lng)
	except:
		return None, None
예제 #3
0
def get_countryname(region_name):
    geolocator = Bing(api_key='')  # необходимо указать api_key
    location = geolocator.geocode(region_name, timeout=10)
    if location:
        return str(location.address).split(', ')[-1]
    elif location == '':
        return None
    else:
        return None
예제 #4
0
def geocoding(addr, flag='b'):
    res = None
    if flag == 'b':
        locator = Bing(BING_API_KEY)
        na = locator.geocode(query=addr, timeout=10, culture='en')
        if na is not None:
            q = na.address.split(",")
            n = len(q)
            if n < 4:
                res = addr
            else:
                res = ",".join([q[0], q[1]])
        else:
            res = addr
    elif flag == 'g':
        locator = GoogleV3()
    elif flag == 'n':
        locator = Nominatim()

    return res
예제 #5
0
 def get_coder(d):
     if isinstance(d, str):
         if d == "Nominatim":
             return Nominatim()
         else:
             raise ValueError("Unknown geocoder '{0}'".format(d))
     elif isinstance(d, tuple):
         name, key = d
         if name == "bing":
             return Bing(key)
         else:
             raise ValueError("Unknown geocoder '{0}'".format(d))
     else:
         raise TypeError("Unexpected type '{0}'".format(type(d)))
예제 #6
0
 def get_geoBLOB(self, locationString, geolocator = Bing("AoHjJBfB_lnIWNP201cRJ70AItSb0hRGuIv2YvGpEOtUmDe41W9kghqEdUlZcMQz")):
     try:
         #first check cache for geoBLOB
         geoBLOB = self.location_cached(locationString)
         #if no row found, call geocoder and save results to cache
         if geoBLOB == False:
             print("querying: \'", locationString, "\'", sep='')
             geoBLOB = geolocator.geocode(query = locationString.translate({ord(c): None for c in '<>'}), #<> cause errors in Bing
                                          exactly_one=True,
                                          include_country_code=True)
             geoBLOB = self.save_to_cache(locationString, geoBLOB)
         return(geoBLOB)
     except Exception as inst:
         print("ERROR: For locationString = \'",locationString,"\', the following error was encountered: ", inst, sep='')
예제 #7
0
def getGeo(street, city, state, zipCode = ""):
    #Nominatim is fussier at street level than Bing.
    time.sleep(1)
    zipCode = zipCode if zipCode != None else ""
    with open('../keys/bingKey.txt') as f:
        bingKey = f.read()
    geoloc = Bing(bingKey)
    place = ("%s, %s, %s %s" % (street, city, state, zipCode)).strip()
    address = geoloc.geocode(query = place, timeout = 10)

    if not address: #If fails, abandon street and just go for the city's coords
        print("Bing maps can't find %s.  Trying again without street." % place)
        address = geoloc.geocode(query = "%s, %s %s" % (city, state, zipCode))

    #print (address.raw)
    #Return original zipcode if it exists, otherwise computes it.
    zipCode = zipCode if zipCode != "" else (address.raw['address']['postalCode'])
    try:    
        county = address.raw['address']['adminDistrict2'][:-1]+'unty'
    except:
        county = None

    return (address.latitude, address.longitude, zipCode, county)
예제 #8
0
def getGeo(street, city, state, zipCode=""):
    #Nominatim is fussier at street level than Bing.
    time.sleep(1)
    zipCode = zipCode if zipCode != None else ""
    with open('../keys/bingKey.txt') as f:
        bingKey = f.read()
    geoloc = Bing(bingKey)
    place = ("%s, %s, %s %s" % (street, city, state, zipCode)).strip()
    address = geoloc.geocode(query=place, timeout=10)

    if not address:  #If fails, abandon street and just go for the city's coords
        print("Bing maps can't find %s.  Trying again without street." % place)
        address = geoloc.geocode(query="%s, %s %s" % (city, state, zipCode))

    #print (address.raw)
    #Return original zipcode if it exists, otherwise computes it.
    zipCode = zipCode if zipCode != "" else (
        address.raw['address']['postalCode'])
    try:
        county = address.raw['address']['adminDistrict2'][:-1] + 'unty'
    except:
        county = None

    return (address.latitude, address.longitude, zipCode, county)
예제 #9
0
 def search_bing(self, address):
     try:
         self.geolocator_bing = Bing(api_key='AuqD7e7LRzuD5Wzmn2HqTQPIipUyZrbgz2y_'
                                     'efTS9YtGhio37GkJr9IWmnRV4EOB',
                                     proxies={'http': self.get_proxy()})
         location = self.geolocator_bing.geocode(address, timeout=20)
         if location:
             lat = location.latitude
             lon = location.longitude
             full_address = location.address
             if 40.485808 < lat < 40.917691 and -73.699206 > lon > -74.260380:
                 return lat, lon, full_address
             else:
                 return None, None
         else:
             return None, None
     except (GeocoderTimedOut, AttributeError):
         return None, None
예제 #10
0
    def __init__(self, config_path='geopyplus.cfg'):
        config = None
        with open(config_path, 'r') as config_file:
            config = json.load(config_file)

        self.providers = []
        self.current_index = -1

        # The APIs will be used in the order that they are in the configuration file
        for provider, attributes in config['providers'].iteritems():
            if provider == 'google':
                self.providers.append(GoogleV3(api_key=attributes['api_key']))
            elif provider == 'bing':
                self.providers.append(Bing(api_key=attributes['api_key']))
            elif provider == 'mapquest':
                self.providers.append(MapQuest(api_key=attributes['api_key']))
            else:
                continue

            self.current_index += 1

        self.current_index = 0
예제 #11
0
# import the geocoding services you'd like to try
# sourced at https://gist.github.com/rgdonohue/c4beedd3ca47d29aef01

from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GeocoderDotUS, GoogleV3, OpenMapQuest
import csv, sys

print('creating geocoding objects!')

arcgis = ArcGIS(timeout=100)
bing = Bing('EskjSss5VoskMkVH9455~Mw_sz22GdAR8PAJf_yOcRw~Ak7zshEQvx8HunHbrUjh7l9PsVYxVjAMd9q-2R3cNm9L4J8IQeqt4meCt-1esehh',timeout=100)
nominatim = Nominatim(timeout=100)
opencage = OpenCage('f579fc0ccbf975c8d822196eca92cf64',timeout=100)
googlev3 = GoogleV3(timeout=100)
openmapquest = OpenMapQuest(timeout=100)

# choose and order your preference for geocoders here
geocoders = [googlev3, bing, nominatim, arcgis, opencage, openmapquest]

def geocode(address):
    i = 0
    try:
        while i < len(geocoders):
            # try to geocode using a service
            location = geocoders[i].geocode(address)

            # if it returns a location
            if location != None:

                # return those values
                return [location.latitude, location.longitude]
            else:
예제 #12
0
        try:
            com_ind = line[5:].index(",")
            name = line[5:5 + com_ind]
            if line[6 + com_ind:].count(",") <= 1:
                place = line[6 + com_ind:]
            else:
                place = ",".join(line[6 + com_ind:].split(",")[-2:])[1:]
            if place not in dict:
                dict[place] = (name)
            elif name not in dict[place]:
                dict[place] += "," + name
        except:
            pass
map = folium.Map(zoom_start=1)
fg_m = folium.FeatureGroup(name='Movies locations ' + year)
geolocator = Bing(
    "AhIHYq_lkTbr8aO0OobYnJkFJVVUwnxjH18nJBHul38z5qEwn985vF4HqFoi4QMW")
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
for key in dict:
    try:
        loc = geolocator.geocode(key)

        fg_m.add_child(
            folium.Marker(location=[loc.latitude, loc.longitude],
                          popup=dict[key],
                          icon=folium.Icon()))
    except:
        pass
fg_p = folium.FeatureGroup(name="Population")
fg_p.add_child(
    folium.GeoJson(
        data=open('world.json', 'r', encoding='utf-8-sig').read(),
예제 #13
0
class GeoSearch:
    def __init__(self, dao=None):
        self.geolocator_nominatim = None
        self.geolocator_google = None
        self.geolocator_opencage = None
        self.geolocator_bing = None
        self.dao = dao

    def search_nominatim(self, address):
        try:
            self.geolocator_nominatim = Nominatim(proxies={'http': self.get_proxy()})
            location = self.geolocator_nominatim.geocode(address, timeout=20)
            if location:
                lat = location.latitude
                lon = location.longitude
                full_address = location.address
                if 40.485808 < lat < 40.917691 and -73.699206 > lon > -74.260380:
                    return lat, lon, full_address
                else:
                    return None, None
            else:
                return None, None
        except (GeocoderTimedOut, AttributeError):
            return None, None

    def search_google(self, address):
        try:
            self.geolocator_google = GoogleV3(api_key='AIzaSyAwv1G4XIza'
                                              '5IIlwucRjWZlFA3lbynGG_8',
                                              proxies={'http': self.get_proxy()})
            location = self.geolocator_google.geocode(address, timeout=20)
            if location:
                lat = location.latitude
                lon = location.longitude
                full_address = location.address
                if 40.485808 < lat < 40.917691 and -73.699206 > lon > -74.260380:
                    return lat, lon, full_address
                else:
                    return None, None
            else:
                return None, None
        except (GeocoderTimedOut, AttributeError):
            return None, None

    def search_opencage(self, address):
        try:
            self.geolocator_opencage = OpenCage(api_key='d65ff98d8e33b1b85210ed4a400fc3a1',
                                                proxies={'http': self.get_proxy()})
            location = self.geolocator_opencage.geocode(address, timeout=20)
            if location:
                lat = location.latitude
                lon = location.longitude
                full_address = location.address
                if 40.485808 < lat < 40.917691 and -73.699206 > lon > -74.260380:
                    return lat, lon, full_address
                else:
                    return None, None
            else:
                return None, None
        except (GeocoderTimedOut, AttributeError):
            return None, None

    def search_bing(self, address):
        try:
            self.geolocator_bing = Bing(api_key='AuqD7e7LRzuD5Wzmn2HqTQPIipUyZrbgz2y_'
                                        'efTS9YtGhio37GkJr9IWmnRV4EOB',
                                        proxies={'http': self.get_proxy()})
            location = self.geolocator_bing.geocode(address, timeout=20)
            if location:
                lat = location.latitude
                lon = location.longitude
                full_address = location.address
                if 40.485808 < lat < 40.917691 and -73.699206 > lon > -74.260380:
                    return lat, lon, full_address
                else:
                    return None, None
            else:
                return None, None
        except (GeocoderTimedOut, AttributeError):
            return None, None

    def search_dao(self, address, borough):
        results = self.dao.do("SELECT ST_X(geomout), ST_Y(geomout), "
                              "(addy).zip FROM geocode('" + address + "') As g LIMIT 1;")
        return self.verify_address(address, results, borough)

    @staticmethod
    def verify_address(adress, results, borough):
        zips = Normalizer.select_zipcode_class(Normalizer.get_neighborhood(borough))
        for r in results:
            zip3dig = int(r[2]) / 100
            if zip3dig in zips:
                return r[0], r[1], adress+", "+r[2]
        return None

    def get_proxy(self):
        proxies = ['durianproxy.gq', 'proxyrocket.org', 'apricotproxy.gq',
                   'technoproxy.cf', 'mawoop.ml', 'proxyfree.party', 'accessproxy.org',
                   'proxyeuro.pw', 'zqal.xyz', 'bukus.ga', 'popeyeprox.info', 'b2bproxy.cf',
                   'buzy.ml', 'limeproxy.gq', 'web.proxygogo.info', 'broccoliproxy.gq',
                   'xyzproxy.gq', 'franceproxy.pw', 'ispvpn.com'
                   ]
        return proxies[randint(0, len(proxies) - 1)]
예제 #14
0
    def getTotalCasesInfo(self, entity='All', type="All"):
        ccases= 0
        rcases = 0
        dcases = 0
        indiaFlag = True
        entity =str(entity).lower()
        try:
            if((entity == None) | (entity== "") | (entity == 'globe') | (entity=="world") |
                    (entity=="All")):
                url = "https://corona.lmao.ninja/v2/all"
                entity = "World"

            else:
                url1 = "https://api.rootnet.in/covid19-in/stats/latest"
                responseValue = requests.get(url1)
                ind_data = responseValue.json()
                regionalData= ind_data['data']['regional']
                statesList= []
                data = {}
                for x in regionalData:
                    states = str(x['loc']).lower()
                    statesList.append(states)
                entity = str(entity).lower()
                try:
                    indexMatch = statesList.index(entity)
                    if(indexMatch >= 0):
                        entity = str(entity).title()
                        entity = "**" + entity + "**"
                        data['cases'] =regionalData[indexMatch]['confirmedCasesIndian']
                        data['recovered'] =regionalData[indexMatch]['discharged']
                        data['deaths'] =regionalData[indexMatch]['deaths']
                        data['active'] = 0
                        #data['countryInfo']['flag'] = "https://raw.githubusercontent.com/NovelCOVID/API/master/assets/flags/in.png"
                        responeMsg,ccases,rcases,dcases = self.formCasesString(data, type, entity)
                        indiaFlag= False
                except Exception as ex :
                    geolocator = Bing(api_key="Ar-aTc8AF9MnqnsJAbSHorGLYZUugT5Rd2gmD6Aq6Cd-z-kMzJ71u3Ku7h_s0MNh")
                    zipcode = str(entity)
                    location = geolocator.geocode(zipcode, include_country_code=True, include_neighborhood=True)
                    try:
                        entity = location.raw['address']['countryRegion']
                        url = "https://corona.lmao.ninja/v2/countries/" + entity
                    except Exception as exp:
                        print(exp)
                    print(ex)


            if(indiaFlag):
                response = requests.get(url)
                if (response.status_code == 200):
                    data = response.json()
                    entity = entity if (entity == "World") else data['country']
                    entity = str(entity).title()
                    entity = "**" + entity + "**"
                    if(type=="All"):
                        ccases = data['cases']
                        rcases = data['recovered']
                        dcases = data['deaths']
                        accases = data['active']
                        responeMsg = "Hey, the latest covid-19 cases of " + f"{entity}" + " with \n \n" + "Confirmed cases as " + f"{ccases}" + "\n \n" \
                                        "and the Recovered Cases counts to " + f"{rcases}" + "\n \n" + "and finally the Death Cases are " + f"{dcases}"
                    elif(type=="confirmed"):
                        ccases = data['cases']
                        responeMsg = "The total confirmed covid-19 cases of " + f"{entity}" + " is " + f"{ccases}"
                    elif (type == "deaths"):
                        dcases = data['deaths']
                        responeMsg = "The total death cases of covid-19 in " + f"{entity}" + " is " + f"{dcases}"
                    elif (type == "recovered"):
                        rcases = data['recovered']
                        responeMsg = "The recovered cases for covid-19 in " + f"{entity}" + " is Recovered Cases " + f"{rcases}"
                    if( 'countryInfo' in data):
                        responeMsg+="$$$"+data['countryInfo']['flag']

                else:
                    responeMsg = "Sorry!! I could not reach the api.."
        except Exception as ex:
            print(ex)
            responeMsg = "Sorry!! I could not recognized you.."


        return responeMsg, ccases,rcases,dcases
예제 #15
0
place = geo.bing(
    g.latlng,
    method='reverse',
    key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL')

# baidu
g = geo.baidu('中国人民大学', key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')
#place = geo.baidu(g.latlng,method='reverse',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')

from geopy.geocoders import Baidu, Bing

geoBaidu = Baidu(r'DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')
location = geoBaidu.geocode("中国人民大学")
place = geoBaidu.reverse((location.latitude, location.longitude))

geoBing = Bing(
    'AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL')
location = geoBing.geocode("中国人民大学")
place = geoBing.reverse((location.latitude, location.longitude),
                        exactly_one=False)
'''
百度 API 直接开发 地理位置坐标转换
1.构建查询字符串  http://api.map.baidu.com/geocoder/v2/
2.查询gis 坐标
3.利用gis 坐标 reverse 查询附近地理位置  http://api.map.baidu.com/geosearch/v3/nearby/ 

baidu api key  DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO 终于好了,我去!
'''
import pandas as pd
import numpy as np

import requests
예제 #16
0
파일: bing.py 프로젝트: fitnr/geopy
 def make_geocoder(cls, **kwargs):
     return Bing(
         api_key=env['BING_KEY'],
         **kwargs
     )
예제 #17
0
             ('Lonely,Track,road	Albany', 'North,Shore,City'),
             ('1102/30,Beach,Road	City,Centre', 'Auckland,City'),
             ('Otara	Otara', 'Manukau,City'),
             ('16,Burton,Street	Grafton', 'Auckland,City'),
             ('304/184,Symonds,Street	City,Centre', 'Auckland,City'),
             ('30/145,Quay,Street	City,Centre', 'Auckland,City'),
             ('Maplesden	Clendon,Park', 'Manukau,City'),
             ('152,Hobson,Street	City,Centre', 'Auckland,City'),
             ('732,Massey,Road	Mangere', 'Manukau,City'),
             ('Woodward	Mount,Albert', ' Auckland,City'),
             ('1,courthouse,lane', 'City,Centre, Auckland,City'),
             ('507/2,Dockside,Lane', 'City,Centre, Auckland,City')]

# key = 'ApfjtgVg2-JtMzi3b-sdJYnHNE6bpkRIdug30idpvaB-cF51TsA_BnppDBXRSujo'
key = ""
geolocator = Bing(key)

f = open(r'Output\addCoordinates.txt', "w")

loc = geolocator.geocode('10D/8 Scotia place, Auckland City , New Zealand')
f.write('10D/8 Scotia place, Auckland City , New Zealand;' +
        str(loc.latitude) + ";" + str(loc.longitude) + "\n")
print(loc.latitude, loc.longitude)

for add in addresses:
    loc = geolocator.geocode(add[0] + ' ' + add[1] + ' New Zealand')
    f.write(add[0] + ' ' + add[1] + ' New Zealand;' + str(loc.latitude) + ";" +
            str(loc.longitude) + "\n")
    print(loc.latitude, loc.longitude)

f.close()
예제 #18
0
import csv, gmaps, geopy
from geopy.geocoders import Bing

bing = Bing("AtvTgjJ5FOjkgB5I291TXiDZFVZ7XK2_o4mqIBekDtcg0tDSBqmJpfjJckYqd2la",
            timeout=20)

STATES = [
    "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI",
    "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN",
    "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH",
    "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA",
    "WI", "WV", "WY"
]

LAST = """06/30/1971 14:00,Yellowstone National Park,WY,Egg,not known,Old photos from 1871 show what appears to be objects in the air.,12/12/2009,44.6204948425,-110.490531921""".split(
    ",")


def fetch():
    with open("data/full-data.csv") as csvin:
        reader = csv.reader(csvin)

        with open("data/full-data-geodata.csv", "ab") as csvout:
            writer = csv.writer(csvout)

            head = reader.next()
            head += ['lat', 'lon']

            #writer.writerow(head)

            candidates = (row for row in reader
예제 #19
0
def geocode(address, address2, city, state, zip, country):

    if address == None or address == "":
        print("Need an address to geocode!")
        return None

    geoaddress = address

    if address2 != None and address2 != "":
        geoaddress += "," + address2

    if city != None and city != "":
        geoaddress += "," + city

    if state != None and state != "":
        geoaddress += "," + state

    if zip != None and zip != "":
        geoaddress += "," + zip

    if country != None and country != "":
        geoaddress += "," + country

    geolocator = Nominatim(timeout=30)
    location = geolocator.geocode(geoaddress,
                                  exactly_one=True,
                                  addressdetails=True)
    if location is not None:
        return {
            "lat": location.latitude,
            "lon": location.longitude,
            "zip": get_zip_from_nominatim_location(location)
        }
    else:
        print("Nominatim could not geocode address")

    if bing_geocoder_api_key != None:
        geolocator = Bing(api_key=bing_geocoder_api_key, timeout=30)
        location = geolocator.geocode(geoaddress, exactly_one=True)
        if location is not None:
            return {
                "lat": location.latitude,
                "lon": location.longitude,
                "zip": get_zip_from_bing_location(location)
            }
        else:
            print("Bing could not geocode address")
    else:
        print("Skipping Bing geocoder because we don't have a free API key")

    if google_geocoder_api_key != None:
        geolocator = GoogleV3(api_key=google_geocoder_api_key, timeout=30)
        location = geolocator.geocode(geoaddress)
        if location is not None:
            return {
                "lat": location.latitude,
                "lon": location.longitude,
                "zip": get_zip_from_google_location(location)
            }
        else:
            print("Google could not geocode address")
    else:
        print("Skipping Google geocoder because we don't have a free API key")

    return None
예제 #20
0
from functools import partial
import json
from typing import Tuple, Iterable
import os
import os.path

from geopy.geocoders import Bing
from geopy.extra.rate_limiter import RateLimiter
from geopy.point import Point

from lac_covid19.geo.paths import DIR_DATA
import lac_covid19.const as const
import lac_covid19.current_stats as current_stats

LAC_CENTER = Point(34.0, -118.2)
BING_GEOCODER = Bing(os.environ.get('BINGMAPSKEY'))
request_geocode = RateLimiter(
    partial(BING_GEOCODER.geocode, user_location=LAC_CENTER),
    min_delay_seconds=2
)

APPEND_ZIP = {
    '14208 MULBERRY AVE, WHITTIER, CA': 90604,
}


ADDRESS_CACHE_PATH = os.path.join(DIR_DATA, 'addresses.json')
def load_addresses_cache():
    address_dict = {}
    if os.path.isfile(ADDRESS_CACHE_PATH):
        with open(ADDRESS_CACHE_PATH) as f:
"""
Created on Tue Sep  1 10:30:28 2020

@author: nowak
"""


# import the geocoding services you'd like to try
from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GoogleV3, OpenMapQuest #, GeocoderDotUS
import csv, sys
import pandas as pd

print ('creating geocoding objects!')

arcgis = ArcGIS(timeout=100)
bing = Bing('AsL7FxfxahbZIKcrKexOBUWvaPCBLrJYhQNJGb-qO8ApL65PKLSi40n-7Fc1yRWc',timeout=100)
nominatim = Nominatim(user_agent="*****@*****.**", timeout=100)
#opencage = OpenCage('your-API-key',timeout=100)
#geocoderDotUS = GeocoderDotUS(timeout=100)
#googlev3 = GoogleV3(timeout=100) #since 2018 api key
#openmapquest = OpenMapQuest(timeout=100) #reqires api key

# choose and order your preference for geocoders here
geocoders = [nominatim, bing]

def geocode(Address1):
    i = 0
    try:
        while i < len(geocoders):
            # try to geocode using a service
            location = geocoders[i].geocode(address)
예제 #22
0
import json
from geopy.geocoders import GoogleV3
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
from geopy.geocoders import Bing
import time

with open('data.json') as data_file:
    data = json.load(data_file)

# let's remove the meta informations
data = data['data']

geolocator_google = GoogleV3(api_key='AIzaSyCaAXe1p_-gIMrXlKuBDzfkJxJ8187GGD4')
geolocator_open_maps = Nominatim()
geolocator_microsoft = Bing(
    api_key='Ath1_nHTp4oY34uzWDMRpQ6Yxh_He-x2SB_av7ZT0QP5Ef5Mj8tVRygUBmjkKdvg')

refactored_data = []
n_movies = 0
n_locations = 0

for movie in data:
    refactored_movie = {}
    movie_description = '{} , San Francisco, California, USA'.format(movie[10])
    try:
        location = geolocator_microsoft.geocode(movie_description, timeout=10)
    except (GeocoderTimedOut):
        print('nope for {}'.format(movie[10]))
    if location:
        refactored_movie['loc'] = [location.longitude, location.latitude]
        refactored_movie['title'], refactored_movie['year'], refactored_movie[
예제 #23
0
# Simple map and weather app

from string import Template
import pyowm
owm = pyowm.OWM('YOURAPIKEY')
from flask import Flask, render_template, request
from geopy.geocoders import Bing
geolocator = Bing("YOURAPIKEY",
                  format_string=None,
                  scheme=None,
                  user_agent=None)

app = Flask(__name__)


@app.route("/<city>")
def create_homepage(city):
    loca = geolocator.geocode(city)  # geocodeing: address to coordinates
    latlong = str(loca.latitude) + "," + str(loca.longitude)
    observation = owm.weather_around_coords(loca.latitude, loca.longitude)
    w = observation[0].get_weather()
    weather = str(round(w.get_temperature('celsius')["temp"])) + " °C, " + str(
        w.get_detailed_status())

    return Template(render_template('standard.html')).substitute(
        latlong=latlong, title=city.capitalize(), weather=weather)


@app.route("/")
def places():
    return render_template('my-form.html')
예제 #24
0
import pandas as pd
import glob
import os
import numpy as np
import mysql.connector as sqlcon
from geopy.geocoders import ArcGIS
from geopy.geocoders import Bing
from datetime import datetime

n = ArcGIS()
nom = Bing(api_key="{BING_KEY}")

# conn = mysql.connector.connect(user = '', password = '', host = '', port = '3306')
mydb = sqlcon.connect(user='******', password='', host='localhost', port='3306')
now = datetime.now()
dt_string = now.strftime("%Y/%m/%d %H:%M:%S")

dir = "/Users/Rock/Desktop/data/"
file_paths = []
for file in glob.iglob(f'{dir}/*.csv'):
    file_paths.append(file)
print(file_paths)


def file_to_df(path):
    df = pd.read_csv(path, index_col=0)
    df.drop(columns=['pollutant_min', 'pollutant_max', 'last_update'],
            inplace=True)

    city_df = df[['city', 'station', 'state', 'country']]
    by_city = city_df.groupby(['city', 'station', 'state'])
예제 #25
0
    def bing(self, address=None, city=None, country=None, key_bing=None):

        if not key_bing:
            raise RuntimeError(
                "Requires a key! Check https://www.bingmapsportal.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'] = 'bing'
        result['status'] = 'ZERO_RESULTS'

        try:
            geolocator_bing = Bing(api_key=key_bing)
            location = geolocator_bing.geocode(
                address,
                exactly_one=False)  #culture='PT',  include_neighborhood=True,
            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)

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

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

                result["input_string"] = address

            #	if saveraw:
            #		result["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
예제 #26
0
    def __init__(self, *args):

        self.m_args = args
        api_key = 'AtbIr0Y1MYUHAdGI4spe2NLMCA0TOWUG1RRloo0NbZykVFjUdvEVqOYnSMxxuaJX'
        self.m_geolocator = Bing(api_key, timeout=4)
예제 #27
0
# baidu
g = geo.baidu('中国人民大学',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')
#place = geo.baidu(g.latlng,method='reverse',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')



from geopy.geocoders import Baidu,Bing 

geoBaidu = Baidu(r'DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO')
location = geoBaidu.geocode("中国人民大学")
place= geoBaidu.reverse((location.latitude,location.longitude))




geoBing = Bing('AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL')
location = geoBing.geocode("中国人民大学")
place= geoBing.reverse((location.latitude,location.longitude),exactly_one=False)





'''
百度 API 直接开发 地理位置坐标转换
1.构建查询字符串  http://api.map.baidu.com/geocoder/v2/
2.查询gis 坐标
3.利用gis 坐标 reverse 查询附近地理位置  http://api.map.baidu.com/geosearch/v3/nearby/ 

baidu api key  DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO 终于好了,我去!
'''
예제 #28
0
def bing_geocoder(street,
                  city,
                  state_prov,
                  postal_code,
                  country=None,
                  min_quality=QUALITY_RANGE_INTERPOLATED):
    '''Call the Bing geocoder to get the address's Lat / Lon.'''

    if min_quality != QUALITY_RANGE_INTERPOLATED:
        logging.warn(
            "Bing Geocoder has not yet implemented the min_quality threshold!")

    response = response_components()

    # Bing requires the address to be in a comma-separated string.
    geocoder_address = ','.join([street, city, state_prov, postal_code])

    # Initialize the Bing geocoder class using the Bing Map API key.
    geocoder = Bing('AgpUigcyyCn4A4ZJHo2lvXqt_4BrH0xr5wbGzfrcqgZUt6jaVG'
                    'vORIBM6SwqeEYd')

    # Call the Bing geocoder.  Note that the geocoder can time out for various
    # reasons (if the geocoder is down, if the geocoder is very confused by the
    # given address, etc.).
    try:
        location = geocoder.geocode(geocoder_address)

        # if location is None, then the geocoder did not return a match
        if not location:
            error = 'Bing could not geocode address. Zero results returned'
            logging.info(error)
            response['error_msg'] = error
            return response
        else:
            location = location.raw

        # Get the Bing geocoder match codes and confidence level from the raw
        # return.
        bing_geo_match_codes = location['matchCodes']
        bing_geo_conf_level = location['confidence']

        # The Bing geocoder match codes is always a list, even if there is only
        # one element.
        # In order for us to consider this match valid, there must be only one
        # element in the list.
        if len(bing_geo_match_codes) == 1:

            # Get the single match code from the match codes list.
            bing_geo_match_code = bing_geo_match_codes[0]

            # If the Bing geocoder match code is "Good" and the confidence is
            # "High", then in most cases we can have some confidence in the
            # accuracy of the geocoded coordinates, although Bing has returned
            # "Good" and "High" for an address that contains a post office box.
            if bing_geo_match_code == 'Good' and bing_geo_conf_level == 'High':
                latitude = location['point']['coordinates'][0]
                longitude = location['point']['coordinates'][1]

                if latitude and longitude:
                    response['latitude'] = str(latitude)
                    response['longitude'] = str(longitude)
                    response['street'] = location['address']['addressLine']
                    response['city'] = location['address']['locality']
                    response['state_prov'] = location['address'][
                        'adminDistrict']
                    response['postal_code'] = location['address']['postalCode']
                    response['country'] = location['address']['countryRegion']
                    response['quality'] = QUALITY_ROOFTOP

            else:

                # Provide the Bing geocoder match type in the error message
                # (and for logging).
                error_msg = 'Bing geocoder match is not Good / High; was ' \
                            'instead: {0} / {1}.'.format(bing_geo_match_code,
                                                         bing_geo_conf_level)
                logging.info(error_msg)
                response['error_msg'] = error_msg

        else:
            # Provide the Bing geocoder match count in the error message (and
            # for logging).
            error_msg = 'Bing geocoder - {0} match codes - ' \
                        'invalid.'.format(len(bing_geo_match_codes))
            logging.info(error_msg)
            response['error_msg'] = error_msg

    except:

        # The error message is different for an exception (time-out, etc.) than
        # for an inaccurate match.
        error_msg = 'Bing geocoder timed out or otherwise failed.'
        logging.info(error_msg)
        response['error_msg'] = error_msg
        response['latitude'] = None
        response['longitude'] = None

    return response
예제 #29
0
    for loc in loc_set:
        geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
        location = geocode(f"{loc}, USA")
        try:
            coord_dict[loc] = {
                "latitude": location.latitude,
                "longitude": location.longitude
            }
        except Exception as e:
            # Skip errors
            print(loc)
            print(e)
            pass
    return coord_dict


if __name__ == "__main__":
    geolocator = Bing(api_key=os.getenv('BING_API_KEY'),
                      user_agent="GABF_analysis")

    df_winners = pd.read_csv('data/cleaned_gabf_winners.csv', index_col=0)
    df_winners = df_winners[["City", "State"]]
    df_winners["Location"] = df_winners['City'] + ", " + df_winners['State']

    coord_dict = geocode_cities(list(df_winners["Location"]), geolocator)

    # save to a pickle file so we don't have to do this every time
    f = open("data/coordinates.pkl", "wb")

    pickle.dump(coord_dict, f)
    f.close()
예제 #30
0
파일: mapsadd.py 프로젝트: liteburn/map
                    color="red",
                    fill_opacity=0.3,
                    radius=20,
                    fill_color="red"))

    except GeocoderTimedOut as error:
        pass
    except GeocoderServiceError as error:
        pass


if __name__ == "__main__":
    map = folium.Map()

    geolocator = Bing(
        api_key=
        "Ag0zrFeOxLpmBE5EdTPEQp2laoQ8HoaXLEHObPUMdWhm1RF_QbCG5v64zk1eGJft ")

    year = int(input("Year of films u want to see on map(1890-2019): "))

    a = str(
        input(
            "Do you want to see one more year of films on the map(Write Yes or No)? - "
        ))
    if a == "Yes":
        year1 = int(input("Year of films u want to see on map(1890-2019): "))

    b = str(
        input(
            "Do you want to see the most inhabited regions(write Yes or No)? - "
        ))
예제 #31
0
import json
import twint
import tweepy
import pycountry
import datetime
import time
from geopy.geocoders import Bing

events = ["San Francisco", "New York", "Cowes"]
data_files = ["San_Francisco.json", "New_York.json", "Cowes.json"]

invalid_locations = ["Global", ""]

locator = Bing(
    "Ah0nnAJPsuz_GDQuwBEQ0xplFqJy7MsoCwLGDmTyZwA_RcTKsQKPY0X0MFf3rh5j")

with open("key.txt", "r") as f:
    keys = f.read().split("\n")

# api = twitter.Api(consumer_key=keys[0], consumer_secret=keys[1], access_token_key=keys[2], access_token_secret=keys[3])

auth = tweepy.OAuthHandler(keys[0], keys[1])
auth.set_access_token(keys[2], keys[3])

api = tweepy.API(auth)

for i, event in enumerate(events):
    nums = {}
    with open(data_files[i], "r", encoding="utf8") as f:
        data = f.read().split("\n")
예제 #32
0
from willyweather import *
from openweathermap import *
from geopy.geocoders import Nominatim
from geopy.geocoders import Bing
import datetime
import csv
from tqdm import tqdm
import os

#a = AccuWeather(os.environ.get('ACCUWEATHER_API_KEY'))
w = willyweather(os.environ.get('WILLYWEATHER_API_KEY'))
a = AccuWeather(os.environ.get('ACCUWEATHER_API_KEY'))
o = OpenWeatherMap(os.environ.get('OPENWEATHERMAP_API_KEY'))

#geolocator = Nominatim(user_agent=os.environ.get('NOMINATIM_USER_AGENT'))
geolocator = Bing(os.environ.get('BING_API_KEY'))
#addresses = []

results = []

# postcode, suburb, state, lat, lon
# 0800,DARWIN,NT, x, y

#with open("addresses.csv", "r") as f:
with open("au-suburbs-postcodes-coords.csv", "r") as f:
    num_rows = sum(1 for line in f)

#with open("addresses.csv", "r") as infile:
with open("au-suburbs-postcodes-coords.csv", "r") as infile:
    reader = csv.reader(infile)
    next(reader)
예제 #33
0
파일: bing.py 프로젝트: zzzz123321/geopy
 def setUpClass(cls):
     cls.geocoder = Bing(format_string='%s', api_key=env['BING_KEY'])
예제 #34
0
import json
import time
import math


class JsonEncoder(json.JSONEncoder):
    def default(self, z):  # pylint: disable=E0202
        return z.__dict__


with open(r'Output\data.txt') as f:
    data = json.load(f)

# key = 'ApfjtgVg2-JtMzi3b-sdJYnHNE6bpkRIdug30idpvaB-cF51TsA_BnppDBXRSujo'
key = ""
geolocator = Bing(key, timeout=None)

totalData = len(data)
counter = 0

for dataItem in data:
    counter = counter + 1
    print(
        str(counter) + "[" + str(math.floor((counter / totalData) * 100)) +
        '%]' + dataItem['address'])
    loc = geolocator.geocode(dataItem['address'] + ' ' + dataItem['area'] +
                             ' New Zealand')
    dataItem['latitude'] = loc.latitude
    dataItem['longitude'] = loc.longitude

with open(r'Output\GeoCoder\out' + str(time.time()) + ".txt", "w") as f:
예제 #35
0
파일: bing.py 프로젝트: fitnr/geopy
 def test_user_agent_custom(self):
     geocoder = Bing(
         api_key='DUMMYKEY1234',
         user_agent='my_user_agent/1.0'
     )
     assert geocoder.headers['User-Agent'] == 'my_user_agent/1.0'
예제 #36
0
 def setUpClass(cls):
     cls.geocoder = Bing(api_key=env['BING_KEY'])
예제 #37
0
sherwood = ('sherwood', (36.001420062411256, -78.87215723968244))#, (1000, 1500, 2500))
rockwood = ('rockwood',(35.972950743249214, -78.9218170838534))#, (1000, 1500, 2500))
cornwallis = ('cornwallis', (35.97845591554436,-78.95247102687885))#, (1200, 1700, 2700))
northgate = ('northgate', (36.0242222222222, -78.8996944444444))#, (1000, 1500, 2500))
lakeshoreG = ('lakeshoreG', (35.932350, -78.835439))#, (1500, 2000, 3000))
crossingG = ('crossingG', (35.980455, -78.806038))#, (1000, 1500, 2500))

parks = [southernBoundaries, sherwood, rockwood, cornwallis, northgate, lakeshoreG, crossingG]
#print parks[0][2][0]

#def checkAndWrite():


with open('noGeoSourceFullPart4.csv','rb') as houses:
	reader = csv.reader(houses)
	geolocator = Bing('AiSlSBllhsR2rq49e4u4IEUe4l6YQXqYoyeifW4nZS2a0sa5kGLAERyI8Bt4Bdma', timeout = None)
	f = open('geoFullPart4.csv','wb')
	mainWriter = csv.writer(f)
	firstRow = 0
	for house in reader:
		if firstRow == 1:
			mainWriter.writerow(house)
			firstRow = 0
		elif house[10] == '0':
			print 'Skipped Row'
		else:			
			address = house[1] + ' Durham NC'
			print house[0]
			location = geolocator.geocode(address)
			locationGeo = ((location.latitude), (location.longitude))
			house[14] = locationGeo
예제 #38
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
예제 #39
0
from geopy.geocoders import Bing
from geopy.extra.rate_limiter import RateLimiter

import pandas as pd
from tqdm import tqdm

tqdm.pandas()

geolocator = Bing(api_key='AtlqFZkg8aISlN-CNOpURU3oLthMK6g166C9gDCO1sc9Cl5njVi1NcGaqwIW21vd')
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=0.1)

df = pd.read_excel('../data/MCM_NFLIS_Data.xlsx', sheet_name='Data')

df = df.groupby('FIPS_Combined').first().reset_index()

df['name'] = df['COUNTY'] + ', ' + df['State']
df['location'] = df['name'].progress_apply(geocode)
df['latitude'] = df['location'].apply(lambda loc: loc and loc.latitude or 0)
df['longitude'] = df['location'].apply(lambda loc: loc and loc.longitude or 0)

df = df.reindex(columns=['FIPS_Combined', 'State', 'COUNTY', 'latitude', 'longitude'])

df.to_csv('geocode.csv', index=False)
예제 #40
0
import json
from geopy.geocoders import GoogleV3
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
from geopy.geocoders import Bing
import time

with open('data.json') as data_file:
    data = json.load(data_file)

# let's remove the meta informations
data = data['data']

geolocator_google = GoogleV3(api_key='AIzaSyCaAXe1p_-gIMrXlKuBDzfkJxJ8187GGD4')
geolocator_open_maps = Nominatim()
geolocator_microsoft = Bing(api_key='Ath1_nHTp4oY34uzWDMRpQ6Yxh_He-x2SB_av7ZT0QP5Ef5Mj8tVRygUBmjkKdvg')

refactored_data = []
n_movies = 0
n_locations = 0

for movie in data:
    refactored_movie = {}
    movie_description = '{} , San Francisco, California, USA'.format(movie[10])
    try:
        location = geolocator_microsoft.geocode(movie_description, timeout = 10)
    except(GeocoderTimedOut):
        print('nope for {}'.format(movie[10]))
    if location:
        refactored_movie['loc'] = [location.longitude, location.latitude]
        refactored_movie['title'], refactored_movie['year'], refactored_movie['location'] = movie[8], movie[9], movie[10]