示例#1
0
    def extractZipCodeInfo(self, entity):
        geolocator = Bing(api_key="Ar-aTc8AF9MnqnsJAbSHorGLYZUugT5Rd2gmD6Aq6Cd-z-kMzJ71u3Ku7h_s0MNh")
        zipcode =str(entity)
        location = geolocator.geocode(zipcode, include_country_code=True, include_neighborhood=True)
        resonseMSg = ""
        try :

            countryValues = location.raw
            country = countryValues['address']['countryRegionIso2']
            print("Country is ", country)
            try:
                state = countryValues['address']['adminDistrict']
                print("state is ",state)
                inputValues = str(state)+", "+str(country)
                compleleteList = geolocator.geocode(inputValues, include_country_code=True, include_neighborhood=True)
                correctStateName = str(compleleteList.raw['address']['formattedAddress'])
                #_, count, _ , country= self.getTotalConfirmedCasesInWorld(country,self.CCCases)
                resonseMSg, _, _, _ = self.getTotalCasesInfo(correctStateName,"All")

                return resonseMSg
            except Exception as ex:
                print(ex)
                resonseMSg1, _, _, _ = self.getTotalCasesInfo(country, "All")
                resonseMSg = "Sorry..!! I could not find the details for "+ zipcode.upper() +" \n Instead I am giving the below details " \
                               " as the "+zipcode.upper()+" belongs to "+country+"\n \n" \
                              ""+ resonseMSg1
                return resonseMSg

        except Exception as e:
            print(e)
            return "Sorry I am not able to recognize the zipcode "+ zipcode
def geocode(address, address2, city, state, postcode, country):
    ''' Geocode an address; returns dict with lat, lon, and postcode '''

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

    geoaddress = address

    if not is_empty(address2):
        geoaddress += "," + address2

    if not is_empty(city):
        geoaddress += "," + city

    if not is_empty(state):
        geoaddress += "," + state

    if not is_empty(postcode):
        geoaddress += "," + postcode

    if not is_empty(country):
        geoaddress += "," + country

    if config.bing_geocoder_api_key is not None:
        geolocator = Bing(api_key=config.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 config.google_geocoder_api_key is not None:
        geolocator = GoogleV3(api_key=config.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
    def reverse_geocode_flickr(self, user_info, wild_collection):
        # add the encounter locations to our user info dictionaries
        # which already contain user location
        for dic in user_info:
            doc_res = self.db[wild_collection].find({'id': dic['id']})
            for doc in doc_res:
                dic['enc_lat'] = doc['latitude']
                dic['enc_long'] = doc['longitude']

        #create a df from our user_info list of dictionaries
        df = pd.DataFrame(user_info)

        #use bing geocoder to convert cities/countries -> lat, long coords
        key = 'AsmRvBYNWJVq55NBqUwpWj5Zo6vRv9N_g6r96K2hu8FLk5ob1uaXJddVZPpMasio'
        locator = Bing(key)
        user_locs = []
        enc_locs = []

        #df where there are videos in tf that have BOTH encounter and user loc
        df_both_locs = df.loc[(df.enc_long != 0) & (df.user_location != None) &
                              (df.user_location != '')]
        df_both_locs = df_both_locs.reset_index(drop=True)

        #get user country lat long coords
        user_lat = []
        user_long = []
        for x in df_both_locs.user_location.values:
            if (x == ''):
                print('empty')
                continue
            try:
                user_lat.append(locator.geocode(x, timeout=3).latitude)
                user_long.append(locator.geocode(x, timeout=3).longitude)
            except AttributeError:
                user_lat.append(None)
                user_long.append(None)

        #drop rows in df where user_lat = None, user_long = None
#         rows = df_both_locs.loc[df_both_locs.user_lat == None or df_both_loc.user_location == '']
#         print(rows)
#         df_both_locs = df_both_locs.loc[df_both_locs.user_lat == None or df_both_loc.user_location == '']
#         df_both_locs.drop(rows)

#add enc_coords list and user_coords list to df_both_locs
        df_both_locs['user_lat'] = user_lat
        df_both_locs['user_long'] = user_long

        return df_both_locs
    def _clean_geo(self):
        lat = self.cleaned_data.get('lat')
        lng = self.cleaned_data.get('lng')

        if not lat or not lng:
            q = self.cleaned_data.get('q')

            try:
                geocoder = Bing(settings.BING_API_TOKEN, '%s, UK')
                geo_resp = geocoder.geocode(q)
            except GeopyError:
                raise UnexpectedException()

            if not geo_resp:
                raise UnexpectedException()

            placename, (lat, lng) = geo_resp

            # Bing Maps API turns a placename of "United Kingdom" for invalid
            # postcodes that _look_ like a valid postcode - e.g. first text
            # part (SW, SE) followed by regex-matching pattern. Along with the
            # placename, it returns a lat/lon of the middle of the country,
            # which is somewhere in Durham.
            if placename == 'United Kingdom':
                raise UnexpectedException()

            self.cleaned_data['lat'] = lat
            self.cleaned_data['lng'] = lng

        return (lat, lng)
示例#5
0
def add_coordinates():
    geolocator = Bing(
        api_key=
        "AjqWLHWVCEHcB9V7sO2ubMzKO7P1eARR-Zl6SBe0RmACT5RFMer3p6q6iJxiQ2Z9",
        timeout=3)
    # todo: add delay, api requests timing out
    with open('dataset/locations_export.csv') as csvDataFile:
        csvReader = csv.reader(csvDataFile)
        with open('dataset/location_coordinates_complete.csv', 'w') as csv_out:
            writer = csv.writer(csv_out,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
            csvReader = csv.reader(csvDataFile)
            row_count = 0
            for row in csvReader:
                if row_count % 100 == 0:
                    print(f"sleepy time {row_count}"
                          )  #just to check progress while it's running
                #time.sleep(1)
                row_count += 1
                if row:
                    if not row[1]:
                        location = geolocator.geocode(query=str(row[0]))
                        if location is not None:
                            new_row = [
                                row[0], location.latitude, location.longitude
                            ]
                        else:
                            new_row = row
                    else:
                        new_row = row
                else:
                    new_row = ["", "", ""]
                writer.writerow(new_row)
 def is_bing_ok(self):
         try:
             geocoder = Bing(settings.BING_API_TOKEN, '%s, UK')
             geo_resp = geocoder.geocode('SW1A 1AA')
             return True
         except GeopyError:
             return False
示例#7
0
    def getMapUrl(self, entity, zoom=7):
        dynamicMapUrl = "https://www.mapquestapi.com/staticmap/v4/getmap?key=lYrP4vF3Uk5zgTiGGuEzQGwGIVDGuy24" \
                        "&size=600,600&type=map&imagetype=jpg&zoom=#zoom&scalebar=false&traffic=false" \
                        "&center=#center" \
                        "&xis=https://s.aolcdn.com/os/mapquest/yogi/icons/poi-active.png,1,c,40.015831,-105.27927&ellipse=fill:0x70ff0000|color:0xff0000|width:2|40.00,-105.25,40.04,-105.30"

        try:
            zoom = str(zoom)
            dynamicMapUrl = requote_uri(dynamicMapUrl)
            geolocator = Bing(
                api_key=
                "Ar-aTc8AF9MnqnsJAbSHorGLYZUugT5Rd2gmD6Aq6Cd-z-kMzJ71u3Ku7h_s0MNh"
            )
            location = geolocator.geocode(entity, include_country_code=True)
            lat = location.raw['point']['coordinates'][0]
            long = location.raw['point']['coordinates'][1]
            latValues = str(lat) + "," + str(long)
            dynamicMapUrl = dynamicMapUrl.replace("#center", latValues)
            dynamicMapUrl = dynamicMapUrl.replace("#zoom", zoom)

            responseValue = "$$$" + dynamicMapUrl
            print(dynamicMapUrl)
            # response = requests.get(dynamicMapUrl)
            # if(response.status_code==200):
            #     responseValue = "$$$"+ response.text
            # else:
            #     responseValue= ""
        except Exception as ex:
            print(ex)
            responseValue = ""
        return responseValue
示例#8
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
                                })
示例#9
0
def get_lat_long(location):
    locator = Bing(_internal['geocode_key'])
    location = locator.geocode(location)
    coordinates = {}
    if location != None:
        coordinates['latitude'] = location.latitude
        coordinates['longitude'] = location.longitude
    return coordinates
示例#10
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
示例#11
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
示例#12
0
def findCoordinates(KEY, DATASET_PATH, SAVELOCATION_PATH, TOP_LIMIT=120, isNoRetweet=False):
    print("*****Outsourcing Work To Get Location's Coordinates*****")
    print("¥[*.*]¥ -- > Gonna Hire A Bangladeshi Guy")
    print("¥[*.*]¥ -- > #metooLAZY")
    print("¥[*.*]¥ -- > Also Trump don't deport my guy !!PLEASE!!")
    print("")
    print("")
    if isNoRetweet:
        DATASET_PATH = DATASET_PATH + "/withoutRT"
        SAVELOCATION_PATH = SAVELOCATION_PATH + "/withoutRT"
    os.makedirs(SAVELOCATION_PATH, exist_ok=True)
    geolocator = Bing(api_key=KEY)
    user_location = pd.read_csv(DATASET_PATH + '/user_location.csv')
    user_location = removeRow(user_location, "Earth")
    user_location = removeRow(user_location, "Worldwide")
    user_location = removeRow(user_location, "Global")
    user_location = removeRow(user_location, "Planet Earth")
    user_location = removeRow(user_location, "Everywhere")
    user_location = removeRow(user_location, ".")
    user_location = removeRow(user_location, "she/her")
    user_location = removeRow(user_location, "In Hearts of")
    user_location = removeRow(user_location, "Mars")
    user_location = removeRow(user_location, "Hogwarts")
    user_location = removeRow(user_location, "worldwide")
    user_location = removeRow(user_location, "Worldwide")
    df = pd.DataFrame(columns=['place', 'lat', 'long', 'type'])
    retryCount = 0
    for i in user_location.iloc[0:TOP_LIMIT, 0]:
        while True:
            try:
                location = geolocator.geocode(i)
                print((i.encode("utf-8"), location.latitude,
                       location.longitude, location.raw['entityType']))
                df = df.append({'place': i, 'lat': location.latitude, 'long': location.longitude,
                                'typegg': location.raw['entityType']}, ignore_index=True)
                time.sleep(0.5)
                retryCount = 0
                break
            except:
                retryCount += 1
                if retryCount > 20:
                    print("Stuck on Error for too long")
                    print("Exiting now")
                    retryCount = 0
                    break
                print("---------------------ERROR----------------")
                continue
    print("")
    print("( ^◡^)っ ---> I am done sir,")
    print("")
    print("¥[*.*]¥ -- > Thank you Sayeed")
    print("")
    print("( ^◡^)っ ---> Dhonno-baad")
    print("")
    print("")
    df.to_csv(SAVELOCATION_PATH + "/user_location_coordinates.csv")
示例#13
0
def _geocode_address_using_backup(address):
    '''
    Gets the longitude and latitude of address for plotting on a map from backup service (Bing)
    '''
    try:
        bing_locator = Bing(api_key=settings.BING_MAPS_API_KEY)

        return bing_locator.geocode(address)
    except:
        return
示例#14
0
def geocode_address(address):
    address = address.encode('utf-8')
    geocoder = Bing(
        api_key=
        'AmTOjxQXZeKHxacoFI3IoPyR6PHW0U-r0NgDSQ8F-x0dt97mnLMFKREiS96mkOkX')
    try:
        location = geocoder.geocode(address)
    except (URLError, GeocoderServiceError, ValueError):
        pass
    else:
        return location
示例#15
0
 def save(self, **kwargs):
     if not self.location:
         address = u'%s %s' % (self.city, self.address)
         address = address.encode('utf-8')
         geocoder = Bing(api_key='AmTOjxQXZeKHxacoFI3IoPyR6PHW0U-r0NgDSQ8F-x0dt97mnLMFKREiS96mkOkX')
         try:
             _, latlon = geocoder.geocode(address)
         except (URLError, GeocoderServiceError, ValueError):
             pass
         else:
             point = "POINT(%s %s)" % (latlon[1], latlon[0])
             self.location = geos.fromstr(point)
     super(Shop, self).save()
示例#16
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)
示例#17
0
    def getDetailsByPincode(self, entity):
        geolocator = Bing(
            api_key=
            "Ar-aTc8AF9MnqnsJAbSHorGLYZUugT5Rd2gmD6Aq6Cd-z-kMzJ71u3Ku7h_s0MNh")
        zipcode = str(entity)
        location = geolocator.geocode(zipcode,
                                      include_country_code=True,
                                      include_neighborhood=True)
        resonseMSg = ""
        try:
            countryValues = location.raw
            country = "IN" if (re.fullmatch(
                r'\d{6}',
                zipcode)) else countryValues['address']['countryRegionIso2']
            print("Country is ", country)

            try:
                if (country == "IN"):
                    response = requests.get(
                        "https://api.postalpincode.in/pincode/" + zipcode)
                    if (response.status_code == 200):
                        data = response.json()
                        correctStateName = data[0]['PostOffice'][0]['State']
                        correctStateName = "Puducherry" if (
                            str(correctStateName).lower()
                            == "pondicherry") else correctStateName
                        correctStateName = "Telengana" if (
                            str(correctStateName).lower()
                            == "telangana") else correctStateName
                        print(correctStateName)
                    else:
                        correctStateName = country
                resonseMSg, _, _, _ = self.getTotalCasesInfo(
                    correctStateName, "All")
                #resonseMSg +=self.getMapUrl(country)

                return resonseMSg
            except Exception as ex:
                print(ex)
                resonseMSg1, _, _, _ = self.getTotalCasesInfo(country, "All")
                resonseMSg = "Sorry..!! I could not find the details for "+ zipcode.upper() +" \n Instead I am giving the below details " \
                               " as the "+zipcode.upper()+" belongs to "+country+"\n \n" \
                              ""+ resonseMSg1
                resonseMSg += self.getMapUrl(country, zoom=4)
                return resonseMSg

        except Exception as e:
            print(e)
            return "Sorry I am not able to recognize the zipcode " + zipcode
示例#18
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)
示例#19
0
def geocode():

    if request.method == 'GET':
        return render_template('geocoding.html')
    else:
        # request was a POST
        #app.vars['pandas'] = request.form['keyword']
        file = request.files.get("data_file")
        if file.filename.endswith('.csv'):
            df = pd.read_csv(file)
        else:
            df = pd.read_excel(file)

    #api_key2 = request.form.get("api_key")
    #return "uploaded ok"
    #df['videoID'] = df['URL'].apply(getid)
    #vidids = df['videoID'].tolist()

        g = Bing(
            'AoGg6nfFsORF7WrNxkvEQpj2r-O7WTS5hoOYXg6fDynZIo4JkGcFZ-UPjPJ7HQda',
            timeout=5)

        ddict = {}
        #create a list of adresses from your column name, then loop through that list
        adr = df['Address']
        for inputAddress in adr:
            #get the geocode & append it to list
            try:
                location = g.geocode(inputAddress, timeout=10)
                ddict[inputAddress] = [location.latitude, location.longitude]
            except:
                ddict[inputAddress] = ["", ""]

        df2 = pd.DataFrame.from_dict(ddict,
                                     orient='index',
                                     columns=['lat', 'long']).reset_index()
        result = pd.merge(df,
                          df2,
                          how='left',
                          left_on='Address',
                          right_on='index').drop("index", 1)

        #df = df3.to_json()
        #session['data'] = df
        resp = make_response(result.to_csv())
        resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
        resp.headers["Content-Type"] = "text/csv"
        return resp
示例#20
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
示例#21
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
def convert_to_coordinates(dictionary):
    """
    (None) -> None

    Connects with API and returns dictionary with coordinates
    dictionary: name - coordinates in tuple

    >>> convert_to_coordinates({'Betrayal': 'New Orleans Louisiana USA'})
    {'Betrayal': (29.9536991119385, -90.077751159668)}
    """
    geolocator = Bing(
        "AomOhTUAKsV-5fu4bzeuBtVlx5VeMi_M86P4gODXuCd6f7S2dquidP7Aj2xtDoS0")
    dict_coordinates = {}
    for key in dictionary:
        point = dictionary[key]
        try:
            address, (latitude, longitude) = geolocator.geocode(point)
            dict_coordinates[key] = (latitude, longitude)
        except TypeError:
            continue
    return dict_coordinates
示例#23
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
示例#24
0
def makeGraph(dataset, model, districtStats, R, sigma):

    RADIUS_OF_EARTH = 6378

    dataFile = json.load(open(dataset))
    dates = [date for date in dataFile]

    # Saving locations from dictionary
    placesList = []
    for date in dates:
        for state in list(dataFile[date]):
            if state == 'TT':
                pass
            try:
                for district in list(dataFile[date][state]['districts']):
                    if district == 'Unknown' or district == 'Other State':
                        district = randomDistrict(state)
                    place = district + ',' + state + ',' + 'India'
                    if not place in placesList:
                        placesList.append(place)

            except KeyError:
                place = state + ',' + 'India'
                if not place in placesList:
                    placesList.append(place)
    print('Updated places')

    # Geolocator, we save stuff to geoUP.p
    geolocator = Bing(api_key=apis.bing())

    uniquePlacesList = list(unique_everseen(placesList))
    geocodedDistrictList = list(districtStats['Coordinates'])
    geocodedUniqueNearestDistrictList = list(
        np.zeros_like(uniquePlacesList).astype(str))

    # Initialize if not present
    if not os.path.exists('data/geoUP.p'):
        geocodedUniquePlacesList = list(
            np.zeros_like(uniquePlacesList).astype(str))
        with open('data/geoUP.p', 'wb') as f:
            pickle.dump(geocodedUniquePlacesList, f)

    # Add new locations if any
    with open('data/geoUP.p', 'rb') as f:
        geocodedUniquePlacesList = pickle.load(f)
        for i in range(len(uniquePlacesList)):
            if geocodedUniquePlacesList[i] == '':
                geocodedUniquePlacesList[i] = ((geolocator.geocode(
                    uniquePlacesList[i]).latitude), (geolocator.geocode(
                        uniquePlacesList[i]).longitude))
    print('Geo mapping stuff done')

    # Save to pickle
    with open('data/geoUP.p', 'wb') as f:
        pickle.dump(geocodedUniquePlacesList, f)

    for i in range(len(uniquePlacesList)):
        _, _, coordinate, _ = getNearestDistrictData(
            model, districtStats, geocodedUniquePlacesList[i])
        geocodedUniqueNearestDistrictList[i] = coordinate

    # Map stuff to different lists this got error
    numberOfDistricts = len(geocodedDistrictList)
    numberOfDates = len(dates)
    arrayFinal = np.zeros((numberOfDates, numberOfDistricts, 3))
    print('Making final time resolved array')

    for dateIndex in range(numberOfDates):
        for districtIndex in range(numberOfDistricts):
            date = dates[dateIndex]
            district = list(districtStats['Coordinates'])[districtIndex]
            try:
                place = uniquePlacesList[
                    geocodedUniqueNearestDistrictList.index(district)]

            # If that district is not enlisted in corona affected places
            except ValueError:
                pass

            if place:
                dump = place.split(',')
                number = 0

                # Check to see if district or state only data
                if len(dump) == 2:
                    try:
                        number = dataFile[date][dump[0]]['total']['confirmed']
                    # If that state does not exist on that date
                    except KeyError:
                        pass
                else:
                    try:
                        number = dataFile[date][dump[1]]['districts'][
                            dump[0]]['total']['confirmed']
                    # If that district does not exist in this state on the date
                    except KeyError:
                        pass

                arrayFinal[dateIndex, districtIndex, 0] = number
                arrayFinal[dateIndex, districtIndex, 1] = list(
                    districtStats['Literacy rate'])[districtIndex]
                arrayFinal[dateIndex, districtIndex, 2] = list(
                    districtStats['Population'])[districtIndex]

            else:
                pass

    print('Array made')

    E = radius_neighbors_graph(model,
                               R / RADIUS_OF_EARTH,
                               mode='distance',
                               metric='haversine').toarray()
    W = 1 - np.exp(-(E * E) / sigma)
    adj = np.where(W > 0, 1, 0)
    # edge = W.reshape(1, W.shape[0]*W.shape[1])
    return arrayFinal, W, adj
示例#25
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()
示例#26
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
示例#27
0
def lat(row):
    geolocator = Bing(bing_key, timeout=5)
    location = geolocator.geocode(row['Address'])
    return str(location.latitude)
示例#28
0
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:
    f.write(json.dumps(data, cls=JsonEncoder))
示例#29
0
        try:
            user = api.get_user(username)
        except:
            print("SLEEPING")
            time.sleep(900)
            try:
                user = api.get_user(username)
            except:
                continue

        if user.location in invalid_locations:
            continue

        try:
            loc = locator.geocode(user.location, timeout=10)
        except:
            continue

        if not loc:
            continue

        country = " ".join(loc.address.split(",")[-1].split())

        code = pycountry.countries.get(name=country)

        if code:
            if code.alpha_3 in nums.keys():
                nums[code.alpha_3] += 1
            else:
                nums[code.alpha_3] = 1
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
			column = 15
			for park in parks:		
				dist = vincenty(locationGeo, park[1]).feet	
				house[column] = dist
				column += 1
			mainWriter.writerow(house)
			# print (vincenty(locationGeo, park[1]).feet)
			# print ''


# Southern Boundaries Park - 100 THIRD FORK DRIVE -     (35.95625302831242, -78.92609419345702) + 1000 feet so within 2000, 2500, 3500

# Sherwood Park - 1720 CHEEK ROAD -     (36.001420062411256, -78.87215723968244) - standard 1000, 1500, 2500
示例#31
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
示例#32
0
                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='')
            

  
  
        
if __name__ == '__main__':
    # run a small test in this case
    import pprint
    BingAPIkey = "AoHjJBfB_lnIWNP201cRJ70AItSb0hRGuIv2YvGpEOtUmDe41W9kghqEdUlZcMQz"
    geolocator = Bing(BingAPIkey)

    cache = Cache('test.db')
    locationString = '1 Murphy St, Sunnyvale, CA'
    geoBLOB = cache.location_cached(locationString)
    if geoBLOB:
        print('was cached: {}\n{}'.format(geoBLOB, pprint.pformat(geoBLOB.raw)))
    else:
        print('was not cached, looking up and caching now')
        geoBLOB = geolocator.geocode(locationString)
        print('found as: {}\n{}'.format(geoBLOB, pprint.pformat(geoBLOB.raw)))
        cache.save_to_cache(locationString, geoBLOB)
        print('... and now cached.')
        
示例#33
0
            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(),
        style_function=lambda x: {
            'fillColor':
            'green' if x['properties']['POP2005'] < 10000000 else 'yellow'
            if 10000000 <= x['properties']['POP2005'] < 40000000 else 'orange'
示例#34
0
    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
from urllib.parse import *
示例#35
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
示例#36
0
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]
        n_locations += 1
        refactored_data += [refactored_movie]
    n_movies += 1
    print(refactored_movie)

print('# movies {}'.format(n_movies))

print('# locations {}'.format(n_locations))
示例#37
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)]
示例#38
0
# 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]
        n_locations += 1
        refactored_data += [refactored_movie]
    n_movies += 1
    print (refactored_movie)

print('# movies {}'.format(n_movies))

print('# locations {}'.format(n_locations))

# title
示例#39
0
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