示例#1
0
def get_location_from_address(address):
    geolocator = OpenCage(api_key=settings.OPENCAGE_API_KEY)
    location = geolocator.geocode(address)

    if location.latitude and location.longitude:
        return (location.latitude, location.longitude)
    else:
        return None
示例#2
0
def geolocation():
    from geopy.geocoders import OpenCage
    geolocator = OpenCage('d1d1fbc618ef41b89d3ebde37f53d1b2')
    for p in Province.query.all():
        g = geolocator.geocode(p.name + ", Việt Nam", timeout=60)
        p.latitude = g.latitude
        p.longitude = g.longitude
        print((p.latitude, p.longitude))
    db.session.commit()
示例#3
0
 def __init__(self, name):
     self.city = name
     if self.city not in City.CITIES:
         #OpenCage Geocoder
         geolocator = OpenCage(api_key=KEY)
         location = geolocator.geocode(self.city, timeout=6)
         if location is None:
             raise TypeError('city does not exist')
         self.latitude = location.latitude
         self.longitude = location.longitude
         City.CITIES[self.city] = (location.latitude, location.longitude)
     else:
         self.latitude = City.CITIES[self.city][0]
         self.longitude = City.CITIES[self.city][1]
示例#4
0
 def opencage_decode(self):
     api_key = self.api_keys.get('OpenCage', None)
     if api_key is not None:
         geolocator_service = OpenCage(api_key=api_key)
         self.geosetter(geolocator_service)
     else:
         raise LookupError
示例#5
0
文件: opencage.py 项目: fitnr/geopy
 async def test_rate_limited_error(self, disable_adapter_retries):
     async with self.inject_geocoder(
             OpenCage(api_key=self.testing_tokens[429])):
         with pytest.raises(GeocoderRateLimited) as cm:
             await self.geocode_run({"query": "london"}, {},
                                    skiptest_on_errors=False)
         assert cm.type is GeocoderRateLimited
示例#6
0
文件: opencage.py 项目: fitnr/geopy
 async def test_api_key_disabled_error(self, disable_adapter_retries):
     async with self.inject_geocoder(
             OpenCage(api_key=self.testing_tokens[403])):
         with pytest.raises(GeocoderInsufficientPrivileges) as cm:
             await self.geocode_run({"query": "london"}, {},
                                    skiptest_on_errors=False)
         assert cm.type is GeocoderInsufficientPrivileges
示例#7
0
文件: opencage.py 项目: fitnr/geopy
 async def test_payment_required_error(self, disable_adapter_retries):
     async with self.inject_geocoder(
             OpenCage(api_key=self.testing_tokens[402])):
         with pytest.raises(GeocoderQuotaExceeded) as cm:
             await self.geocode_run({"query": "london"}, {},
                                    skiptest_on_errors=False)
         assert cm.type is GeocoderQuotaExceeded
示例#8
0
class OpenCageApi(object):
    def __init__(self, api_key):
        self.geolocator = OpenCage(api_key=api_key)

    def get_location_information(self, lat, long):
        location = self.geolocator.reverse(query=(lat, long))
        return {
            'country': location.raw.get('components').get('country'),
            'continent': location.raw.get('components').get('continent')
        }
示例#9
0
def make_main_figure(year, address): 

    if year is None:
        raise PreventUpdate
    if address is None: 
        raise PreventUpdate

    # process address input by user, get latitude and longitude 
    if address not in ADDRESS_CACHE:
        api_key = opencage_key
        geolocator = OpenCage(api_key, domain='api.opencagedata.com', scheme=None, user_agent='AQMap', format_string=None, timeout=4)
        ADDRESS_CACHE[address] = geolocator.geocode(address)
    location = ADDRESS_CACHE[address]
    user_lat = location.latitude
    user_lon = location.longitude
    # get  necessary dataframes from PostGIS database 
    ozone_df = qd.get_pollutant_annual_df(year, 'ozone', location.latitude, location.longitude)
    ozone_geo_df = qd.get_pollutant_annual_avg_df(year, 'ozone', location.latitude, location.longitude, 10) 
    pm25_df = qd.get_pollutant_annual_df(year, 'pm25', location.latitude, location.longitude)
    pm25_geo_df = qd.get_pollutant_annual_avg_df(year, 'pm25', location.latitude, location.longitude, 10)
    no2_df = qd.get_pollutant_annual_df(year, 'no2', location.latitude, location.longitude)
    no2_geo_df = qd.get_pollutant_annual_avg_df(year, 'no2', location.latitude, location.longitude, 10)
    temp_df = qd.get_temp_annual_df(year, location.latitude, location.longitude)
    temp_geo_df = qd.get_temp_annual_avg_df(year, location.latitude, location.longitude, 10)

    # making the ozone geo plots
    ozonefig = make_scatter(ozone_df, 'slateblue', 'Month', 'Concentration (ppm)', 'Ozone', 'month', 'conc')
    ozonegeo = make_geo_mapbox(ozone_geo_df, 'Mean Annual Ozone', 'st_x', 'st_y', 'conc', user_lon, user_lat)
    # making the pm2.5 plots 
    pm25fig = make_scatter(pm25_df, 'seagreen', 'Month', 'Concentration (μg/m^3)', 'PM2.5','month','conc')
    pm25geo = make_geo_mapbox(pm25_geo_df, 'Mean Annual PM2.5', 'st_x', 'st_y', 'conc', user_lon, user_lat)
    # making the no2 plots
    no2fig = make_scatter(no2_df, 'goldenrod', 'Month', 'Concentration (ppb)', 'Nitrogen Dioxide','month','conc')
    no2geo = make_geo_mapbox(no2_geo_df, 'Mean Annual Nitrogen Dioxide', 'st_x', 'st_y', 'conc', user_lon, user_lat)
    # making the temp plots 
    tempfig = make_scatter(temp_df, 'tomato', 'Month', '˚C', 'Maximum Temperature', 'month', 'tmax')
    tempgeo = make_geo_mapbox(temp_geo_df, 'Mean Annual Temperature', 'st_x', 'st_y', 'tmax', user_lon, user_lat)

    return tempfig, tempgeo, ozonefig, ozonegeo, pm25fig, pm25geo, no2fig, no2geo
def index():
    client = MongoClient()
    db = client.health_data
    
    form = AddressForm(request.form)
    if request.method == 'POST':

        """ Find closest bike station to given address """
        collection = db.bike_stations
        addr = request.form.get("address")
        geolocator = OpenCage('d79317ac2c4be89c2683778d8a95df49', timeout=None)
        location = geolocator.geocode(addr)
        compass = findLocation( location.latitude, location.longitude)
        closestBikeStation = compass.nearby(compass.coordinates, collection)

        """ Switch to finding closest corner store (farmer's market will be added later)  to bike station """
        compass = findLocation( closestBikeStation['geometry']['coordinates'][1], closestBikeStation['geometry']['coordinates'][0] )
        collection = db.healthy_corner_stores
        closestHealthyStore = compass.nearby(compass.coordinates, collection)

        """ Farmer's market """
        collection = db.farmers_markets
        closestFarmersMarket = compass.nearby(compass.coordinates, collection)

        """ Bike path's """
        collection = db.bike_network
        closestBikePath = compass.nearby(compass.coordinates, collection)

        """ Mapp generation"""
        currentMapp = Mapp(location.latitude, location.longitude, 
                closestBikeStation['geometry']['coordinates'], closestHealthyStore['geometry']['coordinates'],
                addr, closestBikeStation, closestHealthyStore, closestFarmersMarket,closestBikePath)
        currentMapp.generateMapp()

        """ Return new page with found values plugged into template """
        return render_template("result.html", bikestation=closestBikeStation['properties']['addressStreet'],
                market=closestHealthyStore['properties']['OFFICIAL_STORE_NAME'], farmersmarket=closestFarmersMarket['properties']['NAME'])
    else:
        return render_template("index.html", form=form)
示例#11
0
 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
示例#12
0
from insights.io import config

if __name__ != "__main__":
    configuration_files = config.import_yaml_files(".", ["constants", "keys", "countries.yaml"])
    constants = configuration_files[0]
    key_file = configuration_files[1]
    location_data = configuration_files[2]
else:
    configuration_files = config.import_yaml_files("..", ["constants", "keys", "countries.yaml"])
    constants = configuration_files[0]
    key_file = configuration_files[1]
    location_data = configuration_files[2]

rootdir = os.path.join(constants.get('dataDir'), "Takeout", "Location History")
opencage_geolocator = OpenCage(api_key=str(key_file.get('opencage').get('key')))
google_geolocator = GoogleV3()
nominatim_geolocator = Nominatim()

if can_load_last_position():
    with open(os.path.join(constants.get("outputDir"), "LocationsIndex"), "r") as checkpoint:
        start_pos_data = yaml.load(checkpoint)
        print(int(start_pos_data))
        with open(os.path.join(rootdir, "Location History.json"), 'r') as source:
            data = json.load(source)
        locations = data.get('locations')
        for key, location in enumerate(locations):
            if key < int(start_pos_data):
                print("Skipping Key " + str(key))
                continue
            time_stamp = location.get('timestampMs')
示例#13
0
from geopy.geocoders import OpenCage
key = 'd1d1fbc618ef41b89d3ebde37f53d1b2'

provincesList = ['An Giang','Bà Rịa - Vũng Tàu','Bạc Liêu','Bắc Kạn','Bắc Giang','Bắc Ninh','Bến Tre','Bình Dương','Bình Định','Bình Phước','Bình Thuận','Cà Mau','Cao Bằng','Cần Thơ','Đà Nẵng','Đắk Lắk','Đắk Nông','Đồng Nai','Đồng Tháp','Điện Biên','Gia Lai','Hà Giang','Hà Nam','Hà Nội','Hà Tĩnh','Hải Dương','Hải Phòng','Hòa Bình','Hậu Giang','Hưng Yên','Thành phố Hồ Chí Minh','Khánh Hòa','Kiên Giang','Kon Tum','Lai Châu','Lào Cai','Lạng Sơn','Lâm Đồng','Long An','Nam Định','Nghệ An','Ninh Bình','Ninh Thuận','Phú Thọ','Phú Yên','Quảng Bình','Quảng Nam','Quảng Ngãi','Quảng Ninh','Quảng Trị','Sóc Trăng','Sơn La','Tây Ninh','Thái Bình','Thái Nguyên','Thanh Hóa','Thừa Thiên - Huế','Tiền Giang','Trà Vinh','Tuyên Quang','Vĩnh Long','Vĩnh Phúc','Yên Bái']
coodinatesList = []

geolocator = OpenCage(key)
for p in provincesList:
    g = geolocator.geocode( p + ", Việt Nam", timeout=60)
    coodinatesList.append((g.latitude, g.longitude))

print(coodinatesList)
示例#14
0
文件: geocoder.py 项目: lvsz/bxllive
from django.contrib.gis.geos.point import Point
from geopy.geocoders import OpenCage

_opencage_api_key = '2effc381905143b7aa14e50ea7ff4fae'
_geocoder = OpenCage(_opencage_api_key, timeout=3)


class Geocoder:
    class Location:
        def __init__(self, location):
            self.point = Point(location.latitude, location.longitude)
            self.address_fr, self.address_nl = address_convert(
                location.address)

        def __iter__(self):
            return iter((self.address_fr, self.address_nl, self.point))

    def geocode(self, address):
        return self.Location(_geocoder.geocode(address))

    def reverse(self, point):
        return self.Location(_geocoder.reverse(point, exactly_one=True))


# using OpenCage geocoding, addresses in Brussels are returned as "French name - Dutch name"
def address_convert(address):

    street, town, country = address.split(', ')[-3:]
    postal_code = int(town[:4])

    if len(street.split(' - ')) > 1:
示例#15
0
from geopy.geocoders import Nominatim, ArcGIS, OpenCage, OpenMapQuest
from geopy.exc import GeocoderTimedOut, GeocoderQuotaExceeded, GeocoderInsufficientPrivileges
import os

nomatim = Nominatim(user_agent='locators')
arcgis = ArcGIS(timeout=10)
geocoders = [nomatim, arcgis]

if 'OPEN_CAGE_API_KEY' in os.environ:
    geocoders.append(OpenCage(os.environ['OPEN_CAGE_API_KEY']))

if 'OPEN_MAP_QUEST_API_KEY' in os.environ:
    geocoders.append(OpenMapQuest(os.environ['OPEN_MAP_QUEST_API_KEY']))

for i in range(len(geocoders)):
    geocoders[i] = geocoders[i].geocode


def address_to_lat_long(df):
    df['lat-long'] = df['address'].apply(convert)


def convert(address):
    i = 0

    if address is None:
        return None

    while i < len(geocoders):
        print(i, geocoders[i])
示例#16
0
 def setUpClass(cls):
     cls.geocoder = OpenCage(
         api_key=env['OPENCAGE_KEY'],
         timeout=10,
     )
     cls.delta_exact = 0.2
if __name__ == "__main__":
    #Variable Definitions
    threshold = 1  # Bin Size, i.e. events within x degrees latlong of an indexed location get binned

    # CSV File containing list of all DECO event ID's and countries of origin
    ids_countries = {}
    try:
        with open('../data/ids_countries.json') as data_file:
            ids_countries = json.load(data_file)
    except FileNotFoundError:
        pass

    # Import the geolocator program and use an API key
    ## Limitations: 2500/day, 1/second
    geolocator = OpenCage("78e7d0d08860206c27e848a7bc4f1451")
    #78e7d0d08860206c27e848a7bc4f1451 66ffcb477c3548c99085d0cf5e87954e KEYS

    # The last indexed event ID - used to determine 'new'/'old' data
    with open('../data/lastID.csv') as l:
        reader = csv.reader(l)
        for row in reader:
            lastID = row[0]

#locations of all DECO users
    users = []
    with open('../data/userLocations.csv') as u:
        data = csv.reader(u)
        for user in data:
            users.append(user)
示例#18
0
    def setUp(self):
        self.address = "Sunnersta"  #static address to be found

        self.address2 = "Mackenzie"  #static address for DataBC only

        self.userlocation = (59.8585107, 17.6368508)

        self.addrNone = "abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba"  #non-existing address
        self.scheme = "https"
        self.plainscheme = "http"
        self.geolocators = []

        #set up for Google
        self.geolocator1 = GoogleV3()
        self.googleurl = "https://maps.googleapis.com/maps/api/geocode/json"
        self.googledomain = "maps.googleapis.com"
        self.geolocators.append(self.geolocator1)

        #set up for ArcGIS
        self.geolocator2auth = ArcGIS("asailona", "uppsala00",
                                      "asailona.maps.arcgis.com")
        self.geolocator2 = ArcGIS()
        self.arcgisurl = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find"
        self.arcgisgenerate = "https://www.arcgis.com/sharing/generateToken?username=asailona&password=uppsala00&expiration=3600&f=json&referer=asailona.maps.arcgis.com"
        self.geolocators.append(self.geolocator2auth)
        self.geolocators.append(self.geolocator2)

        #set up for Bing
        self.geolocator3auth = Bing(
            "AjIo4Ums4724tF5U5V7t91SHwwvjm8GP8wf0b3HZmVJWVQLlGJtSwv04IlwJ6971")
        self.bingapikey = "AjIo4Ums4724tF5U5V7t91SHwwvjm8GP8wf0b3HZmVJWVQLlGJtSwv04IlwJ6971"
        self.bingurlapi = "https://dev.virtualearth.net/REST/v1/Locations"
        self.geolocators.append(self.geolocator3auth)

        #set up for Data BC
        self.geolocator4 = DataBC()
        self.databcurlapi = "https://apps.gov.bc.ca/pub/geocoder/addresses.geojson"
        self.geolocators.append(self.geolocator4)

        #set up for geocodeFarm
        self.geolocator5 = GeocodeFarm()
        self.geourlapi = "https://www.geocode.farm/v3/json/forward/"
        self.geolocators.append(self.geolocator5)

        #set up for GeoNames
        self.geolocator6 = GeoNames(None, "asailona")
        self.gnameapi = "http://api.geonames.org/searchJSON"
        self.geolocators.append(self.geolocator6)

        #set up for MapZen
        self.geolocator7 = Mapzen("mapzen-yJXCFyc")
        self.mapzenapikey = "mapzen-yJXCFyc"
        self.mapzenapi = "https://search.mapzen.com/v1/search"
        self.geolocators.append(self.geolocator7)

        #set up for OpenCage
        self.geolocator8 = OpenCage("1aea82c9f55149dc1acc6ae04be7747c")
        self.openapikey = "1aea82c9f55149dc1acc6ae04be7747c"
        self.opendomain = "api.opencagedata.com"
        self.openapi = "https://api.opencagedata.com/geocode/v1/json"
        self.geolocators.append(self.geolocator8)

        #set up for Open Street Map
        self.geolocator9 = Nominatim()
        self.osmdomain = "nominatim.openstreetmap.org"
        self.osmapi = "https://nominatim.openstreetmap.org/search"
        self.geolocators.append(self.geolocator9)

        #set up for Photon
        self.geolocator10 = Photon()
        self.photondomain = "photon.komoot.de"
        self.photonapi = "https://photon.komoot.de/api"
        self.geolocators.append(self.geolocator10)

        #set up for vincenty distance test cases
        self.myLocation = Point(59.849904, 17.621000)
        self.northPole = Point(90.0, 0.0)
        self.southPole = Point(-90.0, 0.0)
        self.antiPodal1 = Point(0.0, 0.0)
        self.antiPodal2 = Point(0.5, 179.7)
        self.earthCircunference = Distance(2 * math.pi * EARTH_RADIUS)
示例#19
0
import os
import schedule, time, threading
from config import db
from models import MetrobusDatos
from random import randint, seed
from datetime import datetime, timedelta
from geopy.geocoders import OpenCage
from opencage.geocoder import OpenCageGeocode

## Inicializar geopy
geolocators = OpenCage(api_key="fab369dfefcd443d918d5c91970e77d1")
geolocator = OpenCageGeocode("fab369dfefcd443d918d5c91970e77d1")

## Array de longitudes y latitudes del metrobus == 18 estaciones
lonLat = [
    "19.4930495,-99.1210605", "19.44674162268097,-99.15331646617256",
    "19.4236252,-99.1630861", "19.4096721,-99.1680921",
    "19.3858323,-99.175063", "19.3700691,-99.1797636", "19.364163,-99.1824701",
    "19.354986,-99.1860674", "19.3508802,-99.1863723",
    "19.3424829,-99.1897309", "19.3099899,-99.1845917",
    "19.3143883,-99.1872434", "19.3040286,-99.1860506",
    "19.2973346,-99.1847422", "19.2944284,-99.1839697",
    "19.2903981,-99.1774466", "19.282733,-99.1757382", "19.280384,-99.1717675"
]


def insertNewData():
    ## Datos para inicializar la base de datos
    DATOS = []
    for x in range(0, 50):
        update_time = datetime.now() - timedelta(hours=1)
示例#20
0
					   'ID':"Idaho","IL":"Illinois","IN":"Indiana","IA":"Iowa",
					   'KS':"Kansas",'KY':"Kentucky",'LA':'Louisiana','ME':'Maine',
					   'MD':'Maryland','MA':'Massachusetts','MI':'Michigan',
					   'MN':'Minnesota',"MS":'Mississippi','MO':'Missouri',
					   'MT':'Montana','NE':'Nebraska','NV':'Nevada','NH':'New Hampshire'
					   ,'NJ':'New Jersey','NM':'New Mexico','NY':'New York',
					   'NC':'North Carolina','ND':'North Dakota','OH':'Ohio',
					   'OK':'Oklahoma','OR':"Oregon",'PA':'Pennsylvania',
					   'RI':'Rhode Island','SC':'South Carolina','SD':'South Dakota',
					   'TN':'Tennessee','TX':'Texas','UT':'Utah','VT':'Vermont',
					   'VA':'Virginia','WA':'Washington','WV':'West Virginia',
					   'WI':'Wisconsin','WY':'Wyoming'}

# Import the geolocator and use an API key
## Limitations: 2500/day, 1/second
geolocator = OpenCage("e1b89ffd560a4b39b9d856904798fc7b")
#78e7d0d08860206c27e848a7bc4f1451 66ffcb477c3548c99085d0cf5e87954e KEYS
# e1b89ffd560a4b39b9d856904798fc7b NEW KEY

# Radius of location bins (in latitude/longitude degrees)
bin_size = 1

# Filepaths
main_data_path = '../data/db_hourly_safe.csv'
event_locations_path = '../data/event_locations.csv'
lastID_path = '../data/lastID.csv'
userLocations_path = '../data/userLocations.csv'
binnedLocations_path = '../data/binnedLocations.csv'
contributingCountries_path = '../data/contributingCountries.csv'
states_path = '../data/states.csv'
topofmonth_path = '../data/topOfMonth.csv'
示例#21
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:
示例#22
0
 def __init__(self, api_key):
     self.geolocator = OpenCage(api_key=api_key)
示例#23
0
文件: opencage.py 项目: fitnr/geopy
 def make_geocoder(cls, **kwargs):
     return OpenCage(api_key=env['OPENCAGE_KEY'], timeout=10, **kwargs)
示例#24
0
def get_location(query, format, api_key):
    """Get geographic data of a lab in a coherent way for all labs."""

    # Play nice with the API...
    sleep(1)
    geolocator = OpenCage(api_key=api_key, timeout=10)

    # Variables for storing the data
    data = {
        "city": None,
        "address_1": None,
        "postal_code": None,
        "country": None,
        "county": None,
        "state": None,
        "country_code": None,
        "latitude": None,
        "longitude": None,
        "continent": None
    }
    road = ""
    number = ""
    # Default None values
    location_data = {
        "city": None,
        "road": None,
        "house_number": None,
        "postcode": None,
        "country": None,
        "county": None,
        "state": None,
        "ISO_3166-1_alpha-2": None,
        "country_code": None,
        "lat": None,
        "lng": None
    }

    # Reverse geocoding ... from coordinates to address
    if format == "reverse":
        # If the query (coordinates) is not empty
        if query is None or len(query) < 3:
            pass
        else:
            location = geolocator.reverse(query)
            if location is not None:
                location_data = location[0].raw[u'components']
                location_data["lat"] = location[0].raw[u'geometry']["lat"]
                location_data["lng"] = location[0].raw[u'geometry']["lng"]
    # Direct geocoding ... from address to coordinates and full address
    if format == "direct":
        # If the query (address) is not empty
        if query is None or len(query) < 3:
            pass
        else:
            location = geolocator.geocode(query)
            if location is not None:
                location_data = location.raw[u'components']
                location_data["lat"] = location.raw[u'geometry']["lat"]
                location_data["lng"] = location.raw[u'geometry']["lng"]

    # Extract the meaningful data
    for component in location_data:
        if component == "town" or component == "city":
            data["city"] = location_data[component]
        if component == "road":
            road = location_data[component]
        if component == "house_number":
            number = location_data[component]
        if component == "postcode":
            data["postal_code"] = location_data[component]
        if component == "country":
            data["country"] = location_data[component]
        if component == "county":
            data["county"] = location_data[component]
        if component == "state":
            data["state"] = location_data[component]
        if component == "ISO_3166-1_alpha-2":
            data["country_code"] = location_data[component]
    # The address need to be reconstructed
    data["address_1"] = unicode(road) + " " + unicode(number)
    data["latitude"] = location_data["lat"]
    data["longitude"] = location_data["lng"]
    # Format the country code to three letters
    try:
        data["country_code"] = pycountry.countries.get(
            alpha_2=data["country_code"]).alpha_3
    except:
        data["country_code"] = None
    # Get the continent
    try:
        continent_code = pycountry.country_alpha2_to_continent_code(
            data["country_code"])
        data["continent"] = pycountry.convert_continent_code_to_continent_name(
            continent_code)
    except:
        data["continent"] = None

    # Return the final data
    return data
示例#25
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)]
示例#26
0
 def test_user_agent_custom(self):
     geocoder = OpenCage(api_key='DUMMYKEY1234',
                         user_agent='my_user_agent/1.0')
     self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
示例#27
0
# Have to do this because when the command is called from the import in any subfolder it cannot find the dbconfig
if __name__ == "__main__":
    with open(os.path.join("constants.yaml"), 'r') as ymlfile:
        constants = yaml.load(ymlfile)
        # Open continents.yaml to get location data
    with open(os.path.join("countries.yaml"), 'r') as loc_data:
        location_data = yaml.load(loc_data)
else:
    with open("constants.yaml", 'r') as ymlfile:
        constants = yaml.load(ymlfile)
    with open(os.path.join("countries.yaml"), 'r') as loc_data:
        location_data = yaml.load(loc_data)

rootdir = os.path.join(constants.get('dataDir'), "Takeout", "Location History")
opencage_geolocator = OpenCage(api_key="d1e2dc9584fd84b683ac13c5cf12cc98")
google_geolocator = GoogleV3()
nominatim_geolocator = Nominatim()

if can_load_last_position():
    with open(os.path.join(constants.get("outputDir"), "LocationsIndex"), "r") as checkpoint:
        start_pos_data = yaml.load(checkpoint)
        print(int(start_pos_data))
        with open(os.path.join(rootdir, "LocationHistory.json"), 'r') as source:
            data = json.load(source)
        locations = data.get('locations')
        for key, location in enumerate(locations):
            if key < int(start_pos_data):
                print("Skipping Key " + str(key))
                continue
            time_stamp = location.get('timestampMs')