예제 #1
0
class RateLimitErrorTestCase(unittest.TestCase):
    def setUp(self):
        httpretty.enable()

        self.geocoder = OpenCageGeocode('abcde')

    def tearDown(self):
        httpretty.disable()
        httpretty.reset()

    def testNoRateLimit(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"status":{"code":200,"message":"OK"},"thanks":"For using an OpenCage Data API","total_results":0,"we_are_hiring":"http://lokku.com/#jobs","licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"rate":{"reset":1402185600,"limit":"2500","remaining":2479},"results":[],"timestamp":{"created_http":"Sat, 07 Jun 2014 10:38:45 GMT","created_unix":1402137525}}')

        # shouldn't raise an exception
        self.geocoder.geocode("whatever")


    def testRateLimitExceeded(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"status":{"code":429,"message":"OK"},"thanks":"For using an OpenCage Data API","total_results":0,"we_are_hiring":"http://lokku.com/#jobs","licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"rate":{"reset":1402185600,"limit":"2500","remaining":0},"results":[],"timestamp":{"created_http":"Sat, 07 Jun 2014 10:38:45 GMT","created_unix":1402137525}}',
            status=429,
            adding_headers={'X-RateLimit-Limit': '2500', 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': '1402185600'},
        )

        self.assertRaises(RateLimitExceededError, self.geocoder.geocode, "whatever")

        # check the exception
        try:
            self.geocoder.geocode("whatever")
        except RateLimitExceededError as ex:
            self.assertEqual(str(ex), 'Your rate limit has expired. It will reset to 2500 on 2014-06-08T00:00:00')
            self.assertEqual(ex.reset_to, 2500)
예제 #2
0
파일: models.py 프로젝트: dwdyer/familytree
 def save(self, *args, **kwargs):
     if not (self.latitude and self.longitude):
         try:
             geocoder = OpenCageGeocode(settings.OPENCAGE_API_KEY)
             query = '{0}, {1}, {2}'.format(self.name,
                                            self.county_state_province,
                                            self.country.name)
             result = geocoder.geocode(query)
             geometry = result[0].get('geometry')
             self.latitude = geometry.get('lat')
             self.longitude = geometry.get('lng')
         except Exception as e:
             # If something goes wrong, there's not much we can do, just leave
             # the coordinates blank.
             print (e)
     super(Location, self).save(*args, **kwargs)
예제 #3
0
    def get(self):
        query = self.request.get('q')
        key = OPEN_CAGE_KEY

        geocoder = OpenCageGeocode(key)

        if not query:
            latlong = self.request.headers.get('X-AppEngine-CityLatLong')
            latlong = string.split(latlong, ',')

            result = geocoder.reverse_geocode(latlong[0], latlong[1])
        else:
            result = geocoder.geocode(query, limit=4, language='fr')

        self.response.headers['Access-Control-Allow-Origin'] = ACCESS_CONTROL_ALLOW_ORIGIN_URL
        self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
        self.response.write(json.dumps(result))
예제 #4
0
class Geocode:

	def __init__(self):
		key = 'Insert token here!'
		self.geocoder = OpenCageGeocode('995f38d3b781b46526e1b3fd7e9a78a7')


	def getNearestStation(self, address):
		result = self.geocoder.geocode(address, format='json',language='fr')
		if (result == []):
			return "Aucun resultat pour cette adresse"

		else:
			print "*------ Result : "
			print result
			latitude = str(result[0]['geometry']['lat'])
			print latitude
			longitude = str(result[0]['geometry']['lng'])
			print longitude
			url = "http://localhost:8080/restserver/station/nearest-station?latitude=" + latitude + "&longitude=" + longitude
			print url
			request = Request(url)

			try:
				response = urlopen(request)
				stationRoot = ET.fromstring(response.read())
				m_id = stationRoot.find("id").text
				name = stationRoot.find("name").text
				howbig = stationRoot.find("howbig").text
				latitude = stationRoot.find("coordinates/latitude").text
				longitude = stationRoot.find("coordinates/longitude").text
				postal_code = stationRoot.find("postalCode").text
				city = stationRoot.find("city").text
				department = stationRoot.find("department").text
				region =stationRoot.find("region").text
				station = TrainStation()
				station.initialize(m_id, name, howbig, latitude, longitude, postal_code, city, department, region)
				return station

			except URLError, e:
				print "Erreur lors de la communication avec le service distant :" , e
				return -1
예제 #5
0
# print(df4.columns)
# print(df4.location.to_string())
import pandas as pd
import numpy as np
import sys
from opencage.geocoder import OpenCageGeocode

key = '7c4cedcd01d7455db554757f30d3be36'
geocoder = OpenCageGeocode(key)
addressfile = '/home/muema/Downloads/addresses.csv'

try:
    with open(addressfile, 'r') as f:
        for line in f:
            address = line.strip()
            results = geocoder.geocode(address, no_annotations='1')

            if results and len(results):
                longitude = results[0]['geometry']['lng']
                latitude = results[0]['geometry']['lat']
                print(u'%f;%f;%s' % (latitude, longitude, address))
                # 40.416705;-3.703582;Madrid,Spain
                # 45.466797;9.190498;Milan,Italy
                # 52.517037;13.388860;Berlin,Germany
            else:
                sys.stderr.write("not found: %s\n" % address)
except IOError:
    print('Error: File %s does not appear to exist.' % addressfile)
except RateLimitExceededError as ex:
    print(ex)
    #Your rate limit has expired. It will reset to 2500 at midnight UTC timezone
예제 #6
0
def fetch(entries, verbosity=1):
    '''
    looks up tuples of (city,state) strings coming from load_data,
    and returns a latitude/longitude pair of the "expected" center
    of the city.

    Input:
        entries: list-like; each entry must itself be a list-like of the form of
            two strings ["city", "state"]; e.g. ["flint", "mi"]. I expect
            the format can be flexible, but this is up to the OpenCage API.
    Output:
        latlon: numpy array shape (n,2) of the center of the bounding box
            for each location.

    Loosely follows https://amaral.northwestern.edu/blog/getting-long-lat-list-cities

    '''
    if isinstance(entries, str):
        print(
            'Warning: expecting list-like of cities; will return a list of a single tuple.'
        )
        entries = [entries]
    #
    from opencage.geocoder import OpenCageGeocode
    import opencage_apikey  # get your own.
    import numpy as np

    geocoder = OpenCageGeocode(opencage_apikey.key)

    import pdb
    #pdb.set_trace()
    latlons = np.zeros((len(entries), 2), dtype=float)
    for j, query in enumerate(entries):
        #        query = ', '.join(cs)
        print('fetching %s...' % query)
        try:
            results = geocoder.geocode(query)
        except:
            results = []
        #
#        ri = 0
#        hit = results[ri]
#        while hit['components']['country'].lower() not in ['united states of america', 'usa', 'us']:

        if len(results) == 0:
            print(
                "Warning: failed to find data for entry %s, using NaN coordinates."
                % query)
            latlon = np.array([np.nan, np.nan])
        else:
            for ri, result in enumerate(results):
                if result['components']['country'].lower() not in [
                        'united states of america', 'usa', 'us'
                ]:
                    continue
                hit = result
                break
            #
            if hit['components']['country'].lower() not in [
                    'united states of america', 'usa', 'us'
            ]:
                print('entry not in the US: %s' %
                      str(hit['components']['country'].lower()))
                continue
#            ne = hit['bounds']['northeast']
#            sw = hit['bounds']['southwest']
#            latlon = np.mean([[ne['lat'], ne['lng']],[sw['lat'],sw['lng']]], axis=0)
            latlon = np.array([hit['geometry']['lat'], hit['geometry']['lng']])
        #
        latlons[j] = latlon
        if verbosity > 0:
            print('done, ', query, latlon)
    #
    return latlons
예제 #7
0
def location():
    key = 'b109193b45cf4444b0115e2f16e911f1'
    geocoder = OpenCageGeocode(key)
    query = input("Enter location(ex:-delhi,punjab,hyd):")
    results = geocoder.geocode(query)
    return results
예제 #8
0
    values = {}
    for row in reader:
        print(row['Start Node'], row['End Node'], row['Via'])
        values['Start_Node_Cords'] = row['Start Node']
        values['End_Node_Cords'] = row['End Node']
        values['Via_Cords'] = row['Via']
        results[i] = {}
        results[i]['Start_Node_Cords'] = "No Coords"
        results[i]['End_Node_Cords'] = "No Coords"
        results[i]['Via_Cords'] = "No Coords"

        for key,value in values.iteritems():
            query = value            
            print "Querying . . . "
            try:
            	result = geocoder.geocode(query)
            except Exception as e:
            	continue
            for res in result:
                if "Tanzania" in res['formatted'] and row['District'] in res['formatted']:
                    lat = res['geometry']['lat']
                    lng = res['geometry']['lng']
                    results[i][key] = str(lat)+','+str(lng)
        if i is 50:
            break
        i = i + 1

   
with open('Results.csv', 'w') as csvfile:
    fieldnames = ['Start_Node_Cords', 'End_Node_Cords', 'Via_Cords']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames, lineterminator = '\n')
예제 #9
0
from opencage.geocoder import OpenCageGeocode
import requests

key = 'd82859da6a3d4320b056ab23ffe5d627'
geocoder = OpenCageGeocode(key)

query = u'Bratislava'
results = geocoder.geocode(query)

url = 'https://api.opencagedata.com/geocode/v1/json?q=Bratislava&key=d82859da6a3d4320b056ab23ffe5d627&pretty=1'

r = requests.get(url).json()

geo = {
    'lat': r['results'][0]['geometry']['lat'],
    'lng': r['results'][0]['geometry']['lng']
}

print(geo)

#print(u'%f;%f;%s;%s' % (results[0]['geometry']['lat'],
#                        results[0]['geometry']['lng'],
#                        results[0]['components']['country_code'],
#                        results[0]['annotations']['timezone']['name']))
key = 'add your own Open Cage Code key here'
geocoder = OpenCageGeocode(key)


file = pathlib.Path('South_Baltimore_Business_Status_COVID19_Update.csv')
if not file.exists ():
  lat = []
  lng = []

  df0 = pd.read_csv('South_Baltimore_Business_Status_COVID19.csv',encoding = "ISO-8859-1")
  df1 = df0.loc[:, ['Business_Name', 'Address']]
  df1['Address'] = df1['Address']+', Baltimore, MD'

  for i in range(len(df1)):
    ADDRESS = df1['Address'][i]
    result = geocoder.geocode(ADDRESS, no_annotations='1')
    temp = result[0]['geometry']['lat']
    lat.append(result[0]['geometry']['lat'])
    lng.append(result[0]['geometry']['lng'])
    time.sleep(0.5)

  lat = pd.DataFrame(lat)
  lng = pd.DataFrame(lng)
  df0['Latitude'] = lat
  df0['Longitude'] = lng
  df0.to_csv('South_Baltimore_Business_Status_COVID19_Update.csv')


df2 = pd.read_csv('South_Baltimore_Business_Status_COVID19_Update.csv',encoding = "ISO-8859-1")
df2 = df2[df2.Status != 'CLOSED']
# df2['category'] = 'other'
예제 #11
0
key = '<your api key>'
geocoder = OpenCageGeocode(key)


# SQL query 
cur.execute("TRUNCATE TABLE  <table where data will be insert>;")
cur.execute("<query to colect address from your db>")
rows = cur.fetchall()


# Address conversion -> lat, lng and insert query
for elem in rows:
    if None in elem:
        pass
    else:
        try:
            # Address conversion -> lat, lng
            results = geocoder.geocode(str(elem))
            pos = u'%f;%f' % (results[0]['geometry']['lat'], results[0]['geometry']['lng'])
            data = pos.split(";")
            
            # Insert query
            insert_query = "INSERT INTO <your table>(<collumns name>) Values (%s, %s)"
            data_to_insert = (data[0], data[1])
            cur.execute(insert_query, data_to_insert)
            conn.commit()

        except:
            print('/!\ incorect address: ' + str(elem))

print("\n'pos' collection finished")
class OpenCageAddressService(AddressService):
    """
    Class represents Open Cage implementation of AddressService class. It 
    provides reverse and forward geocoding. 

    Open cage is rate limited based on API access tier, so request 
    response time will be variable. 
    """
    def __init__(self):
        self.client = None
        self.num_addresses_processed = 0

    def load_config(self, config_file, usage):
        """Resonsible for loading configs and setting up client"""
        config = configparser.ConfigParser()
        config.read(config_file)
        auth_key = config.get('OPEN CAGE', 'auth_key')
        self.client = OpenCageGeocode(auth_key)

    def send_request(self, params, address_data):
        """Responsible for sending a request to service"""
        try:
            # forward geocoding
            if int(params["options"]) == 2:
                address_query = u'{address_data.input_string}'
                results = self.client.geocode(address_query)
                print(f'{type(self).__name__} request sent.' \
                       'Waiting on rate limit...')
                return results
            # reverse geocoding
            elif int(params["options"]) == 3:
                latitude = address_data.latitude
                longitude = address_data.longitude
                results = self.client.reverse_geocode(latitude,
                                                      longitude,
                                                      language='eng',
                                                      no_annotations='1')
                print(f'<{type(self).__name__} request sent.' \
                       'Waiting on rate limit...>')
                return results
        except RateLimitExceededError as rate_err:
            print(rate_err)
        except UnknownError as unkwn_err:
            print("\n Error: An unspecified server issue occured \n")
            print(unkwn_err)
        except InvalidInputError as invalid_err:
            print(invalid_err)
            raise

    def forward_geocode(self, params, address_input_data):
        """ 
        Reponsible for forward geocoding input addresses in stream or batch form.
        
        returns a list containing a single Address object for stream input 
        and multiple for batch input.
        """
        processed_address_list = []
        for address in address_input_data:
            result = self.send_request(params, address)
            self.num_addresses_processed += 1
            if result and len(result):
                address.latitude = result[0]['geometry']['lat']
                address.longitude = result[0]['geometry']['lng']
                address.is_valid = True
            else:
                address.is_valid = False
            processed_address_list.append(address)

        print(f'< {self.num_addresses_processed} addresses processed >')
        return processed_address_list

    def reverse_geocode(self, params, address_input_data):
        """ 
        Reponsible for forward geocoding input addresses in stream or batch form.
        
        returns a list containing a single Address object for stream input 
        and multiple for batch input.
        """
        processed_address_list = []
        for address in address_input_data:
            result = self.send_request(params, address)
            self.num_addresses_processed += 1
            if result and len(result):
                address.line_1 = result[0]['formatted']
                address.is_valid = True
            else:
                address.is_valid = False
            processed_address_list.append(address)

        print(f'< {self.num_addresses_processed} addresses processed >')
        return processed_address_list
예제 #13
0
def index(request):

    dests = Destination.objects.all()
    if request.user.is_authenticated:
        global df
        engine = create_engine(
            'postgresql+psycopg2://postgres:postgres@localhost:5432/telusko')
        df_d = pd.read_sql_table(
            "travello_destination",
            con=engine,
            schema='public',
            coerce_float=True,
            columns=['name', 'img', 'desc', 'state', 'city', 'typeofplace'])
        df = pd.DataFrame(df_d)
        geocoder = OpenCageGeocode('ea7fd5e689b149c38ef13cbed352bff5')
        list_lat = []
        list_long = []
        for index, row in df.iterrows():

            name = get_name_from_index(index, df)

            state = get_state_from_index(index, df)
            city = get_city_from_index(index, df)
            query = str(name) + ',' + str(city) + ',' + str(state)
            print("hi")
            results = geocoder.geocode(query)
            print('$$$$$$', results)
            if len(results) != 0:
                lat = results[0]['geometry']['lat']
                longi = results[0]['geometry']['lng']
            else:
                print("results is empty")

            print("hello", index, name, state)
            list_lat.append(lat)
            list_long.append(longi)
        df['lat'] = list_lat
        df['lon'] = list_long
        print(df)
        features = ['desc', 'state', 'typeofplace']
        for feature in features:
            df[feature] = df[feature].fillna('')

        df['combined_features'] = df.apply(combine_features, axis=1)
        cv = CountVectorizer()
        count_matrix = cv.fit_transform(df['combined_features'])
        cosine_sim = cosine_similarity(count_matrix)
        custom = CustomPreferences.objects.all()
        for c in custom:
            if str(c.user) == str(request.user):
                user_prefer = c.preferences
                user_prefer = user_prefer.split(",")
                rows_data = []
                for up in user_prefer:
                    place_index = get_index_from_title(up, df)
                    similar_places = list(enumerate(cosine_sim[place_index]))
                    sorted_similar_places = sorted(similar_places,
                                                   key=lambda x: x[1],
                                                   reverse=True)
                    i = 0
                    for place in sorted_similar_places:
                        row_data = get_title_from_index(place[0], df)
                        rows_data.append(row_data)
                        i = i + 1
                        if i > 3:
                            break
                final_data = []
                for dest in dests:
                    for lists in rows_data:
                        if dest.name in lists:
                            result = TextBlob(dest.desc)
                            polar = result.sentiment.polarity
                            if polar > 0.0:
                                final_data.append(dest)

    else:
        user_prefer = []
        final_data = []

    return render(request, "index.html", {
        'dests': dests,
        'recommendations': final_data
    })
예제 #14
0
파일: main.py 프로젝트: cateban/gymy
def main():
    if request.method == 'POST':
        try:
            filtro_usuario = request.form.get('dropdown')
            input_user = request.form['input_usuario']
            #inicializo GeoCage para localizar input del usuario
            tokengeo = token('geocagetoken.txt')
            #tokengeo = token(PATH+'/geocagetoken.txt') -> For pythonanywhere
            geocoder = OpenCageGeocode(tokengeo)
            results = geocoder.geocode(input_user)
            kmeters = 2
            fcoordinates = []
            coordinates = []
            coordinates.append(results[0]['geometry']['lat'])
            coordinates.append(results[0]['geometry']['lng'])
            country = results[0]['components']['country']
            city = results[0]['components']['city']
            localidad = country + ', ' + city
            fcoordinates.append(coordinates)
            fcoordinates.append(kmeters)
            fcoordinates.append(localidad)
            fcoordinates.append(input_user)

            #se separa la string
            latlong = fcoordinates[0]
            radio = fcoordinates[1]
            localidad = fcoordinates[2]
            direccion_solicitud = fcoordinates[3]

            gymy_modelo = gymy.copy()

            if filtro_usuario != 'Todas las categorías':
                gymy_modelo['filtro'] = gymy_modelo.category.apply(
                    filtro, form_user_input=filtro_usuario)
            else:
                gymy_modelo['filtro'] = True

            gymy_modelo = gymy_modelo[gymy_modelo.filtro == True]

            gymy_modelo['distance'] = gymy_modelo.latlong.apply(
                lambda x, y=latlong: distance.distance(x, y).km)
            display_df = gymy_modelo[gymy_modelo.distance < radio]
            display_df.reset_index(inplace=True)
            n_gyms = len(display_df)

            if n_gyms > 0:
                #creating map object
                tooltip = 'Dirección elegida: {} \n {}'.format(
                    direccion_solicitud, localidad)
                mapa = folium.Map(latlong,
                                  zoom_start=15,
                                  width='100%',
                                  height='70%')
                folium.Marker(latlong, tooltip=tooltip,
                              icon=folium.Icon()).add_to(mapa)
                for i in range(len(display_df)):
                    htmlpopup = """
                            <font face = Verdana size = "1"> <label ><b>{}</b></label> <br> </font>
                            <p>
                            <font face= Verdana size = "1"><label><b> Teléfono:</b> {}</label> <br>
                            <label><b>Dirección:</b> {}</label> <br>
                            <label><b>Web:</b> {}</label> <br>
                            <label><b>Categorías: </b>{}</label>
                            </font>
                                </p>
                            """.format(display_df.names[i],
                                       display_df.phone[i],
                                       display_df.address[i],
                                       display_df.web[i],
                                       display_df.category[i])

                    icongym = folium.features.CustomIcon(
                        '/Users/EstebanCardona/Documents/gymy/static/gymyicon.png',
                        icon_size=(40, 40))
                    iframe = folium.IFrame(html=htmlpopup,
                                           width=225,
                                           height=125)
                    popup = folium.Popup(iframe)

                    folium.Marker([display_df.lat[i], display_df.long[i]],
                                  popup=popup,
                                  tooltip=display_df.names[i],
                                  icon=icongym).add_to(mapa)
                mapa.save('templates/{}{}.html'.format(direccion_solicitud,
                                                       filtro_usuario))
                #mapa.save(PATH+'/templates/{}.html'.format(direccion_solicitud))  -> For pythonanywhere

                devuelta = 'Existen {} GYMYs de {} cerca de {}'.format(
                    n_gyms, filtro_usuario, direccion_solicitud)

                #agrega el jinja de block al html de folium
                with open(
                        'templates/{}{}.html'.format(direccion_solicitud,
                                                     filtro_usuario),
                        'a') as f:
                    #with open(PATH+'/templates/{}.html'.format(direccion_solicitud), 'a') as f: -> For pythonanywhere
                    f.write('\n{% block content %} {% endblock %}')

                return render_template('index.html',
                                       gyms_template=devuelta,
                                       mapatrue='{}{}.html'.format(
                                           direccion_solicitud,
                                           filtro_usuario),
                                       dropdown=filtro_usuario)

            else:
                devuelta = 'No hay Gymys cerca'
                return render_template('index.html',
                                       gyms_template=devuelta,
                                       mapatrue='nomapa.html',
                                       dropdown='Todas las categorías')

        except:
            devuelta = 'Dirección Inválida. Prueba con otra'
            return render_template('index.html',
                                   gyms_template=devuelta,
                                   mapatrue='nomapa.html',
                                   dropdown='Todas las categorías')

    else:
        return render_template('index.html',
                               gyms_template='',
                               mapatrue='nomapa.html',
                               dropdown='Todas las categorías')
class ZeitOpenCage(object):
    """


    ZeitSci relavant tools for interacting with the OpenCage API.

    """

    def __init__(self, api_key=None):
        """

        :param key: an OpenSci API key; defaults to None.
        """

        if api_key == None:
            raise ValueError("An OpenCage API key must be supplied via api_key.")

        self.geocoder = OpenCageGeocode(key=api_key)

    def parser(self, result, request):
        """

        :param result: JSON from geocoder.geocode(query)
        :param request: 'address', 'geo' or 'conf', all --> formatted addgress, longditude, confidence, all three
        :return:
        """

        if len(result) == 0:
            raise ValueError("The result param is invalid")
        else:
            if request == 'address':
                return result[0]['formatted']
            if request == 'conf':
                return result[0]['confidence']
            if request == 'geo':
                return (result[0]['geometry']['lng'], result[0]["geometry"]["lat"])
            if request == 'all':
                loc_dict = {
                    'Address': result[0]['formatted']
                    , 'Coordinates': (result[0]['geometry']['lng'], result[0]["geometry"]["lat"])
                    , 'Confidence': result[0]['confidence']
                }
                return loc_dict

    def lookup(self, query, request='all', double_check=False, min_conf=1, sleep_bounds=(1, 3)):
        """

        TO CHANGE: double_check --> validate_list, e.g., ['Harvard', 'University', 'United States of America']

        :param query: the search term
        :param request: 'address', 'geo' or 'conf', all --> formatted addgress, (long, lat), confidence, all three
        :param double_check:
        :param min_conf:
        :param sleep_bounds: (min, max) wait time (select from a uniform probability density function).
        :return:
        """

        # Pause between calls
        sleep(uniform(sleep_bounds[0], sleep_bounds[1]))

        if cln(query, 2) == "":
            raise ValueError("query is an emptry string.")

        try:
            parse_return = self.parser(self.geocoder.geocode(query), request)
        except:
            return {}

        # Validate that the country is in the address.

        # Improve this check and/at least refactor the code...
        if double_check and "," in query and not \
                partial_list_match(parse_return['Address'].lower(), \
                                   [cln(i, 1).lstrip().rstrip().lower() for i in query.split(",")]) \
                and int(parse_return["Confidence"]) < min_conf:
            return {}

        return parse_return
예제 #16
0
# Formats the data into a data frame, remove duplicates, sort by postcode alphabetic order
depots = pd.DataFrame(depot, columns=["Name", "Address", "Postcode"])
depots = depots.drop_duplicates()
depots = depots.sort_values("Postcode")
depots = depots.reset_index(drop=True)
#------------------------------------#

#------------------------------------#
# Geocodes the depots
#------------------------------------#
UPS_depot = []
for index in range(len(depots)):
    loc = depots.iloc[index]['Address'] + depots.iloc[index]['Postcode']
    print(loc)
    location = geolocator.geocode(loc)  # calls the geocoder
    if location:  # if the geocoder succeeded
        lat = location[0]['geometry']['lat']
        lng = location[0]['geometry']['lng']
        if (location[0]['components']['country'] != 'United Kingdom'
            ):  # checks that the obtained location is in the UK
            print("country issue", loc)
        elif (
                location[0]['confidence'] < 8
        ):  # returns a warning if the confidence level of the geocoder is low. The latitude and longitude are not returned and set to the default value 42
            print(depots.iloc[index]['Postcode'], ":",
                  location[0]['confidence'])
            UPS_depot.append([
                depots.iloc[index]['Name'],
                depots.iloc[index]['Address'].replace(",", ""),
                depots.iloc[index]['Postcode'], 42, 42
예제 #17
0
        countDict['Tests'] = tdList[11].get_text()
        data.append(countDict)
        count = count + 1
        print(tdList[0].get_text())
    return data


# In[13]:

data = getDataFromTableRows(trList, 'World')

# In[14]:

for point in data:
    print(point['Country'])
    result = geocoder.geocode(point['Country'])
    point['Country Code'] = result[0]['components']['country_code']
    point['coordinates'] = []
    point['coordinates'].append(result[0]['geometry']['lat'])
    point['coordinates'].append(result[0]['geometry']['lng'])
    point['lat'] = result[0]['geometry']['lat']
    point['lng'] = result[0]['geometry']['lng']

# In[15]:

worldCountersTable = pd.DataFrame(data)

# In[16]:

worldCountersTable
예제 #18
0
def get_hotel(place):
    """

    :param place_code: suggests the best hotel deal based on the city selected
    :return:
    """
    client = Client(client_id='YhUHykQeccgseWSSARlxtfIAzhdGgTM2',
                    client_secret='IWBwclckwNgBKNWr')

    key = "25cff282b29f43b9ac3aad454f1945a6"
    geocoder = OpenCageGeocode(
        key)  # Gecoding Service to get coordinates of city
    result = geocoder.geocode(place,
                              no_annotations='1')  # Geo-coding Service API key
    # here we get only london (there will be many london in the world)
    longitude = result[0]['geometry']['lng']  # Parsing Latitude
    latitude = result[0]['geometry']['lat']  # Parsing Longitude
    print(latitude)
    print(longitude)
    response = client.shopping.hotel_offers.get(latitude=latitude,
                                                longitude=longitude)

    print(response.data)
    # get all hotel number in london
    hotel_number = len(response.data)
    # use a list to store hotel information
    all_hotel = []
    hotel_contact = "NULL"
    hotel_address = "NULL"
    hotel_name = "NULL"
    hotel_rating = "4"
    hotel_price = 95
    hotel_latitude = 0
    hotel_longitude = 0
    hotel_unit = 'GBP'
    # only choose top 3
    if (hotel_number <= 3):
        for temp_hotel in response.data:
            if ('name' in temp_hotel['hotel']):
                hotel_name = temp_hotel['hotel']['name']
            if ('rating' in temp_hotel['hotel']):
                hotel_rating = temp_hotel['hotel']['rating']
            if ('address' in temp_hotel['hotel']
                    and 'lines' in temp_hotel['hotel']['address']):
                hotel_address = temp_hotel['hotel']['address']['lines'][0]
            if ('contact' in temp_hotel['hotel']
                    and 'phone' in temp_hotel['hotel']['contact']):
                hotel_contact = temp_hotel['hotel']['contact']['phone']
            if ('latitude' in temp_hotel['hotel']):
                hotel_latitude = temp_hotel['hotel']['latitude']
            if ('longitude' in temp_hotel['hotel']):
                hotel_longitude = temp_hotel['hotel']['longitude']
            # if ('price' in temp_hotel['offers'][0] and 'total' in temp_hotel['offers'][0]['price']):
            hotel_price = temp_hotel['offers'][0]['price']['total']
            if ('price' in temp_hotel['offers'][0]
                    and 'currency' in temp_hotel['offers'][0]['price']):
                hotel_unit = temp_hotel['offers'][0]['price']['currency']
            hotel_prase = hotel(name=hotel_name,
                                unit=hotel_unit,
                                rate=hotel_rating,
                                price=hotel_price,
                                latitude=hotel_latitude,
                                longitude=hotel_longitude,
                                communication=hotel_contact,
                                position=hotel_address)
            all_hotel.append(hotel_prase)

    else:
        for temp_hotel in response.data[0:3]:
            if ('name' in temp_hotel['hotel']):
                hotel_name = temp_hotel['hotel']['name']
            if ('rating' in temp_hotel['hotel']):
                hotel_rating = temp_hotel['hotel']['rating']
            if ('address' in temp_hotel['hotel']
                    and 'lines' in temp_hotel['hotel']['address']):
                hotel_address = temp_hotel['hotel']['address']['lines'][0]
            if ('contact' in temp_hotel['hotel']
                    and 'phone' in temp_hotel['hotel']['contact']):
                hotel_contact = temp_hotel['hotel']['contact']['phone']
            if ('latitude' in temp_hotel['hotel']):
                hotel_latitude = temp_hotel['hotel']['latitude']
            if ('longitude' in temp_hotel['hotel']):
                hotel_longitude = temp_hotel['hotel']['longitude']
            if ('price' in temp_hotel['offers'][0]
                    and 'total' in temp_hotel['offers'][0]['price']):
                hotel_price = temp_hotel['offers'][0]['price']['total']
            if ('price' in temp_hotel['offers'][0]
                    and 'currency' in temp_hotel['offers'][0]['price']):
                hotel_unit = temp_hotel['offers'][0]['price']['currency']
            hotel_prase = hotel(name=hotel_name,
                                unit=hotel_unit,
                                rate=hotel_rating,
                                price=hotel_price,
                                latitude=hotel_latitude,
                                longitude=hotel_longitude,
                                communication=hotel_contact,
                                position=hotel_address)
            all_hotel.append(hotel_prase)
    all_hotel.sort(key=lambda x: x.rate, reverse=True)
    return all_hotel[:3]
        print("Tweepy Rate Limit Error")
        break
print('No of followers fetched:',len(user_followers))

pickle.dump(user_followers,open("C:/Users/Jatin/Desktop/MidsemHack/darak_followers.pkl", "wb"))

user_followers=pickle.load(open("C:/Users/Jatin/Desktop/MidsemHack/darak_followers.pkl","rb"))

lat=[]
long=[]
geocoder = OpenCageGeocode(key)
name=[]
counter=0
for user in user_followers:
    print(counter)
    loaction= geocoder.geocode(user.location)
    if len(loaction)!=0:
        lat.append(loaction[0]['geometry']['lat'])
        long.append(loaction[0]['geometry']['lng'])
        name.append(user.screen_name)
        print(counter,name[counter],lat[counter],long[counter])
        counter+=1

coordinates=(name,lat,long)
pickle.dump(coordinates,open("C:/Users/Jatin/Desktop/MidsemHack/darak_followers_location.pkl", "wb"))

####################################################################################33
coordinates=pickle.load(open("C:/Users/Jatin/Desktop/MidsemHack/shah_followers_location.pkl","rb"))

def getPointCoords(row, geom, coord_type):
    """Calculates coordinates ('x' or 'y') of a Point geometry"""
def get_data():  # 定义获取数据并写入csv文件里的函数
    url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"  # 请求网址
    response = requests.get(url).json()  # 发出请求并json化处理
    # print(response) #测试一下是否获取数据了
    data = json.loads(response['data'])  # 提取数据部分
    # print(data.keys()) #获取数据组成部分['chinaTotal', 'chinaAdd', 'lastUpdateTime', 'areaTree', 'chinaDayList', 'chinaDayAddList']
    update_time = data["lastUpdateTime"]

    recordDIR = "./data_record/"

    geokey = '0902cbf7651440a7b1b837648ac2eea2'  # get api key from:  https://opencagedata.com

    dayTime = update_time.split(" ", 2)[0]
    # timeStampNow = time.mktime(time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
    with open("day_update_record", "rb") as dayUpdateRecord:
        # 加载目前已经获取的天数的字典
        dayDict = pickle.load(dayUpdateRecord)
    with open("cityCord", "rb") as cordFile:
        cordDict = pickle.load(cordFile)

    dataAggDir = "./data_agg/"

    if dayTime not in dayDict.keys():
        # 获得新的一天的数据
        print("---------------------------")
        print("Add the data for a new day")
        areaTree = data["areaTree"]  # 各地方数据
        fileName = time.strftime(
            "%Y-%m-%d-%H-%M", time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
        if len(os.listdir(dataAggDir)) == 0:
            work_book = xlwt.Workbook(encoding='gbk')
            sheet = work_book.add_sheet(fileName)
        else:
            rb = xlrd.open_workbook(dataAggDir + "data.xls")
            wb = copy(rb)
            sheet = wb.add_sheet(fileName)
        sheet.write(0, 0, "Province/State")
        sheet.write(0, 1, "Country/Region")
        sheet.write(0, 2, "Last Update")
        sheet.write(0, 3, "Confirmed")
        sheet.write(0, 4, "Recovered")
        sheet.write(0, 5, "Deaths")
        with open(recordDIR + fileName + "_data" + ".csv", "w+",
                  newline="") as csv_file:
            writer = csv.writer(csv_file)
            header = [
                "Province/State", "Country/Region", "Last Update", "Confirmed",
                "Recovered", "Deaths", "Date_last_updated", "lat", "lon"
            ]
            Last_update = time.strftime(
                "%d/%m/%Y %H:%M",
                time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
            writer.writerow(header)
            china_data = areaTree[0]["children"]  # 中国数据
            geocoder = OpenCageGeocode(geokey)
            count = 0
            for j in range(len(china_data)):
                province = china_data[j]["name"]  # 省份
                city_list = china_data[j]["children"]  # 该省份下面城市列表
                for k in range(len(city_list)):
                    city_name = province + city_list[k]["name"]  # 城市名称
                    total_confirm = city_list[k]["total"]["confirm"]  # 总确认病例
                    # total_suspect = city_list[k]["total"]["suspect"]  # 总疑似病例
                    total_dead = city_list[k]["total"]["dead"]  # 总死亡病例
                    total_heal = city_list[k]["total"]["heal"]  # 总治愈病例
                    # today_confirm = city_list[k]["today"]["confirm"]  # 今日确认病例
                    # today_suspect = city_list[k]["total"]["suspect"]  # 今日疑似病例
                    # today_dead = city_list[k]["today"]["dead"]  # 今日死亡病例
                    # today_heal = city_list[k]["today"]["heal"]  # 今日治愈病例
                    # print(province, city_name, total_confirm, total_suspect, total_dead, total_heal, today_confirm,
                    #       today_suspect, today_dead, today_heal, update_time)
                    if city_name in cordDict.keys():
                        lat = cordDict[city_name][0]
                        lon = cordDict[city_name][1]
                    else:
                        query = city_list[k][
                            "name"] + ',' + province + "," + "china"
                        results = geocoder.geocode(query)
                        lat = results[0]['geometry']['lat']
                        lon = results[0]['geometry']['lng']
                    data_row = [
                        city_name, "China", Last_update, total_confirm,
                        total_heal, total_dead, update_time, lat, lon
                    ]
                    writer.writerow(data_row)
                    count += 1
                    add_line(sheet, data_row, count)

        dayDict[dayTime] = True
        with open("day_update_record", "wb") as dayUpdateRecord:
            pickle.dump(dayDict, dayUpdateRecord)


#     wb.save(dataDir + 'Excel表.xls')
        print("day {dayName} has been added".format(dayName=dayTime))
        print("---------------------------")

    else:
        # 一天中更新数据
        fileName = time.strftime(
            "%Y-%m-%d-%H-%M", time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
        rb = xlrd.open_workbook(dataAggDir + "data.xls")
        wb = copy(rb)
        if fileName + "_data.csv" not in os.listdir("./data_record"):
            sheet = wb.add_sheet(
                time.strftime("%Y-%m-%d-%H-%M",
                              time.strptime(update_time, "%Y-%m-%d %H:%M:%S")))
            sheet.write(0, 0, "Province/State")
            sheet.write(0, 1, "Country/Region")
            sheet.write(0, 2, "Last Update")
            sheet.write(0, 3, "Confirmed")
            sheet.write(0, 4, "Recovered")
            sheet.write(0, 5, "Deaths")
            print("---------------------------")
            print("update the data for day {dayname}".format(dayname=dayTime))
            areaTree = data["areaTree"]  # 各地方数据
            fileName = time.strftime(
                "%Y-%m-%d-%H-%M",
                time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
            with open(recordDIR + fileName + "_data" + ".csv",
                      "w+",
                      newline="") as csv_file:
                writer = csv.writer(csv_file)
                header = [
                    "Province/State", "Country/Region", "Last Update",
                    "Confirmed", "Recovered", "Deaths", "Date_last_updated",
                    "lat", "lon"
                ]
                Last_update = time.strftime(
                    "%d/%m/%Y %H:%M",
                    time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
                writer.writerow(header)
                china_data = areaTree[0]["children"]  # 中国数据
                geocoder = OpenCageGeocode(geokey)
                count = 0
                for j in range(len(china_data)):
                    province = china_data[j]["name"]  # 省份
                    city_list = china_data[j]["children"]  # 该省份下面城市列表
                    for k in range(len(city_list)):
                        city_name = province + city_list[k]["name"]  # 城市名称
                        total_confirm = city_list[k]["total"][
                            "confirm"]  # 总确认病例
                        total_dead = city_list[k]["total"]["dead"]  # 总死亡病例
                        total_heal = city_list[k]["total"]["heal"]  # 总治愈病例
                        if city_name in cordDict.keys():
                            lat = cordDict[city_name][0]
                            lon = cordDict[city_name][1]
                        else:
                            query = city_list[k][
                                "name"] + ',' + province + "," + "china"
                            results = geocoder.geocode(query)
                            lat = results[0]['geometry']['lat']
                            lon = results[0]['geometry']['lng']
                        data_row = [
                            city_name, "China", Last_update, total_confirm,
                            total_heal, total_dead, update_time, lat, lon
                        ]
                        writer.writerow(data_row)
                        count += 1
                        add_line(sheet, data_row, count)

            print("day {dayName} has been updated at {uptime}".format(
                dayName=dayTime, uptime=update_time))
            print("---------------------------")
    if len(os.listdir(dataAggDir)) == 0:
        work_book.save(dataAggDir + "data.xls")
    else:
        os.remove(dataAggDir + "data.xls")
        wb.save(dataAggDir + "data.xls")
예제 #21
0
def on_chat_message(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(msg)
    # response = requests.get('127.0.0.1:8001/person/{}'.format(msg['from']['id']))
    # update['message']['chat']['first_name']
    # bot.sendMessage(-310500080,'привіт @newskit_bot ,як в тeбe справи?')
    if content_type == 'sticker' and 'set_name' == 'bundblyad':
        bot.sendMessage(chat_id,
                        '*О ннііі,не бунтуйте будь-ласка*',
                        parse_mode='Markdown')
    if content_type != 'text':
        bot.sendMessage(chat_id,
                        "*Вибач,я розумію тільки текстові повідомлення😢*",
                        parse_mode='Markdown')
        return
    command = msg['text']
    app_id = "f996f51b"
    app_key = "cdfb503fe8f18fe3784de8cdabf67581"

    if command == 'Тижнева Погода':
        conn = sqlite3.connect('main_db.db')
        cur = conn.cursor()
        com_sql = "INSERT OR REPLACE INTO commands (user_id, location) VALUES (?, ?)"
        cur.execute(com_sql, (msg['from']['id'], '7'))
        conn.commit()
        bot.sendMessage(chat_id, 'Напиши своє місцезназодження')
    if command == 'Погода на завтра':
        conn = sqlite3.connect('main_db.db')
        cur = conn.cursor()
        com_sql = "INSERT OR REPLACE INTO commands (user_id, location) VALUES (?, ?)"
        cur.execute(com_sql, (msg['from']['id'], '5'))
        conn.commit()
        bot.sendMessage(chat_id, 'Напиши своє місцезназодження')
    if command == '/start':
        markup = ReplyKeyboardMarkup(keyboard=[[
            KeyboardButton(text='Поточна Погода'),
            KeyboardButton(text='Погода на завтра'),
            KeyboardButton(text='Тижнева Погода')
        ]],
                                     resize_keyboard=True)
        bot.sendMessage(chat_id,
                        '*Привіііт*',
                        reply_markup=markup,
                        parse_mode='Markdown')

    conn = sqlite3.connect('main_db.db')
    cur = conn.cursor()
    # cur.execute("SELECT EXISTS(SELECT * FROM commands WHERE user_id=msg['from']['id'])")
    print(msg['from']['id'])
    cur.execute(
        "SELECT EXISTS(SELECT user_id,location FROM commands WHERE user_id={} and location='1')"
        .format(msg['from']['id']))
    result = cur.fetchall()
    print(result)
    if result == [
        (1, )
    ] and command != '/start' and command != 'Поточна Погода' and command != 'Тижнева Погода' and command != 'Погода на завтра':
        print(command)
        #deletion of smth in table
        conn = sqlite3.connect('main_db.db')
        sql = 'DELETE FROM commands WHERE user_id=?'
        cur = conn.cursor()
        cur.execute(sql, (msg['from']['id'], ))
        conn.commit()
        #end deletion

        try:
            # encoding location
            key = 'a279f5ab7cc1464da34cec0183dde7a0'
            geocoder = OpenCageGeocode(key)
            query = command
            result = geocoder.geocode(query)
            if result and len(result):
                longitude = result[0]['geometry']['lng']
                latitude = result[0]["geometry"]["lat"]

            # end encoding location
            #getting weather
            r = requests.get(
                'http://api.weatherunlocked.com/api/trigger/{},{}/current temperature gt 16 includecurrent?app_id=f996f51b&app_key=cdfb503fe8f18fe3784de8cdabf67581'
                .format(latitude, longitude))
            print(r.json())
            Weather = r.json()
            FinalWeather = '*' + 'Поточна погода у ' + command + '*' + ':' + '\n' + '🌡️ Температура: ' + str(
                round(Weather['CurrentWeather']['temp_c'])
            ) + '°C' + '\n' + '🌡️ Відчувається як: ' + str(
                round(Weather['CurrentWeather']['feelslike_c'])
            ) + '°C' + '\n' + '💦 Вологість: ' + str(
                round(Weather['CurrentWeather']['humid_pct'])
            ) + '%' + '\n' + '💨 Вітер: ' + str(
                round(Weather['CurrentWeather']['windspd_ms'])) + 'м/с'
            # bot.sendMessage(chat_id,FinalWeather,parse_mode='HTML')
            # bot.sendPhoto(chat_id,'https://techcrunch.com/wp-content/uploads/2017/05/onedrive-illo3.jpg?w=730&crop=1',caption=FinalWeather,parse_mode='MARKDOWN')
            icon = Weather['CurrentWeather']['wx_icon']
            final_icon = icon.replace('.gif', '.png') or icon.replace(
                '.png', '.png')
            print(final_icon)
            bot.sendPhoto(chat_id,
                          open('icons/{}'.format(final_icon), 'rb'),
                          caption=FinalWeather,
                          parse_mode='MARKDOWN')
            admin = msg['from'][
                'first_name'] + command + "Успішно запитав поточну погоду"
            bot.sendMessage(462005869, admin)
            print(FinalWeather)
            #end getting weather
        except UnboundLocalError or ConnectionError:
            bot.sendMessage(
                chat_id,
                'Ти неправильно написав(-ла) місцезназодження.Натисни на кнопку *Поточна Погода* щоб спробувати щераз',
                parse_mode='Markdown')

    if result == [(0, )]:

        # bot.sendMessage(chat_id,"test no location")
        pass
    conn = sqlite3.connect('main_db.db')
    cur = conn.cursor()
    # cur.execute("SELECT EXISTS(SELECT * FROM commands WHERE user_id=msg['from']['id'])")
    print(msg['from']['id'])
    cur.execute(
        "SELECT EXISTS(SELECT user_id,location FROM commands WHERE user_id={} and location='7')"
        .format(msg['from']['id']))
    result2 = cur.fetchall()
    print('Week' + str(result2))
    if result2 == [
        (1, )
    ] and command != '/start' and command != 'Поточна Погода' and command != 'Тижнева Погода' and command != 'Погода на завтра':
        print(command)
        #deletion of smth in table
        conn = sqlite3.connect('main_db.db')
        sql = 'DELETE FROM commands WHERE user_id=?'
        cur = conn.cursor()
        cur.execute(sql, (msg['from']['id'], ))
        conn.commit()
        #end deletion

        try:
            # encoding location
            key = 'a279f5ab7cc1464da34cec0183dde7a0'
            geocoder = OpenCageGeocode(key)
            query = command
            result = geocoder.geocode(query)
            if result and len(result):
                longitude = result[0]['geometry']['lng']
                latitude = result[0]["geometry"]["lat"]
            # end encoding location
            #getting weather
            r = requests.get(
                'http://api.weatherunlocked.com/api/trigger/{},{}/forecast tomorrow temperature gt 16 include7dayforecast?app_id=f996f51b&app_key=cdfb503fe8f18fe3784de8cdabf67581'
                .format(latitude, longitude))

            # r = requests.get('http://api.weatherunlocked.com/api/trigger/{},{}/current temperature gt 16 includecurrent?app_id=f996f51b&app_key=cdfb503fe8f18fe3784de8cdabf67581'.format(latitude,longitude))
            print(r.json())
            Weather = r.json()
            Days = Weather['ForecastWeather']['Days']
            Today = '🔶 ' + 'Сьогодні,' + '*' + str(
                Days[0]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[0]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[0]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[0]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[0]['humid_min_pct'])) + ' - ' + str(
                            round(Days[0]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[0]['windgst_max_ms'])) + ' м/с'
            Day2 = '\n' + '🔶 ' + 'Завтра,' + '*' + str(
                Days[1]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[1]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[1]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[1]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[1]['humid_min_pct'])) + ' - ' + str(
                            round(Days[1]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[1]['windgst_max_ms'])) + ' м/с'
            Day3 = '\n' + '🔶 ' + '*' + str(
                Days[2]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[2]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[2]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[2]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[2]['humid_min_pct'])) + ' - ' + str(
                            round(Days[2]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[3]['windgst_max_ms'])) + ' м/с'
            Day4 = '\n' + '🔶 ' + '*' + str(
                Days[3]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[3]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[3]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[3]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[3]['humid_min_pct'])) + ' - ' + str(
                            round(Days[3]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[3]['windgst_max_ms'])) + ' м/с'
            Day5 = '\n' + '🔶 ' + '*' + str(
                Days[4]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[4]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[4]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[4]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[4]['humid_min_pct'])) + ' - ' + str(
                            round(Days[4]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[4]['windgst_max_ms'])) + ' м/с'
            Day6 = '\n' + '🔶 ' + '*' + str(
                Days[5]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[5]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[5]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[5]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[5]['humid_min_pct'])) + ' - ' + str(
                            round(Days[5]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[5]['windgst_max_ms'])) + ' м/с'
            Day7 = '\n' + '🔶 ' + '*' + str(
                Days[6]
                ['date']) + '*' + ':' + '\n' + '      🌡️ Температура: ' + str(
                    round(Days[6]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[6]['temp_max_c'])
                    ) + '°C' + '\n' + '      💧 Ймов. опадів: ' + str(
                        round(Days[6]['prob_precip_pct'])
                    ) + '%' + '\n' + '      💦 Вологість: ' + str(
                        round(Days[6]['humid_min_pct'])) + ' - ' + str(
                            round(Days[6]['humid_max_pct'])
                        ) + '%' + '\n' + '      💨 Вітер: ' + str(
                            round(Days[6]['windgst_max_ms'])) + ' м/с'
            print(Today)
            FinalWeather = '*' + 'Тижнева погода у ' + command + '*' + '\n' + Today + Day2 + Day3 + Day4 + Day5 + Day6 + Day7
            # bot.sendMessage(chat_id,FinalWeather,parse_mode='HTML')
            # bot.sendPhoto(chat_id,'https://techcrunch.com/wp-content/uploads/2017/05/onedrive-illo3.jpg?w=730&crop=1',caption=FinalWeather,parse_mode='MARKDOWN')
            print(FinalWeather)
            bot.sendMessage(chat_id, FinalWeather, parse_mode='MARKDOWN')
            admin = msg['from'][
                'first_name'] + "Успішно запитав тижневу погоду,місто - " + command
            bot.sendMessage(462005869, admin)
            print(FinalWeather)
            #end getting weather
        except UnboundLocalError or ConnectionError:
            bot.sendMessage(
                chat_id,
                'Ти неправильно написав(-ла) місцезназодження.Натисни на кнопку *Тижнева Погода* щоб спробувати щераз',
                parse_mode='Markdown')

    if command == 'Поточна Погода':

        conn = sqlite3.connect('main_db.db')
        cur = conn.cursor()
        com_sql = "INSERT OR REPLACE INTO commands (user_id, location) VALUES (?, ?)"
        cur.execute(com_sql, (msg['from']['id'], '1'))
        conn.commit()
        bot.sendMessage(chat_id, 'Напиши своє місцезнаходження')

    conn = sqlite3.connect('main_db.db')
    cur = conn.cursor()
    # cur.execute("SELECT EXISTS(SELECT * FROM commands WHERE user_id=msg['from']['id'])")
    print(msg['from']['id'])
    cur.execute(
        "SELECT EXISTS(SELECT user_id,location FROM commands WHERE user_id={} and location='5')"
        .format(msg['from']['id']))
    result3 = cur.fetchall()
    print('Week' + str(result3))
    if result3 == [
        (1, )
    ] and command != '/start' and command != 'Поточна Погода' and command != 'Тижнева Погода' and command != 'Погода на завтра':
        #deletion of smth in table
        conn = sqlite3.connect('main_db.db')
        sql = 'DELETE FROM commands WHERE user_id=?'
        cur = conn.cursor()
        cur.execute(sql, (msg['from']['id'], ))
        conn.commit()
        #end deletion

        try:
            # encoding location
            key = 'a279f5ab7cc1464da34cec0183dde7a0'
            geocoder = OpenCageGeocode(key)
            query = command
            result = geocoder.geocode(query)
            if result and len(result):
                longitude = result[0]['geometry']['lng']
                latitude = result[0]["geometry"]["lat"]
            # end encoding location
            #getting weather
            r = requests.get(
                'http://api.weatherunlocked.com/api/trigger/{},{}/forecast tomorrow temperature gt 16 include7dayforecast?app_id=f996f51b&app_key=cdfb503fe8f18fe3784de8cdabf67581'
                .format(latitude, longitude))

            # r = requests.get('http://api.weatherunlocked.com/api/trigger/{},{}/current temperature gt 16 includecurrent?app_id=f996f51b&app_key=cdfb503fe8f18fe3784de8cdabf67581'.format(latitude,longitude))
            print(r.json())
            Weather = r.json()
            Days = Weather['ForecastWeather']['Days']
            Day2 = '*' + str(
                Days[1]['date']) + '*' + ':' + '\n' + '🌡️ Температура: ' + str(
                    round(Days[1]['temp_min_c'])) + '°' + ' - ' + str(
                        round(Days[1]['temp_max_c'])
                    ) + '°C' + '\n' + '💧 Ймов. опадів: ' + str(
                        round(Days[1]['prob_precip_pct'])
                    ) + '%' + '\n' + '💦 Вологість: ' + str(
                        round(Days[1]['humid_min_pct'])) + ' - ' + str(
                            round(Days[1]['humid_max_pct'])
                        ) + '%' + '\n' + '💨 Вітер: ' + str(
                            round(Days[1]['windgst_max_ms'])) + ' м/с'
            print(Day2)
            FinalWeather = '*' + 'Завтрашня погода у ' + command + '*' + '\n' + Day2

            bot.sendMessage(chat_id, FinalWeather, parse_mode='MARKDOWN')
            admin = msg['from'][
                'first_name'] + "Успішно запитав тижневу погоду,місто - " + command
            bot.sendMessage(462005869, admin)
            #end getting weather
        except UnboundLocalError or ConnectionError:
            bot.sendMessage(
                chat_id,
                'Ти неправильно написав(-ла) місцезназодження.Натисни на кнопку *Завтрашня Погода* щоб спробувати щераз',
                parse_mode='Markdown')
예제 #22
0
def findMyHome(pincode='612104'):
    key = '858c794be631439582229839e2816bd4'
    geocoder = OpenCageGeocode(key)
    results = geocoder.geocode(pincode)
    refLoc = [results[0]['geometry']['lat'], results[0]['geometry']['lng']]
    return refLoc
예제 #23
0
def search(request):
    """
    The bulk of the work is performed in this function, which runs once the user enters a start and end address
    and clicks Submit. In a nutshell, here's what it does:
    1. Retrieve the start and end address that the user submitted using HTTP GET
    2. Query the OpenCage Geocoding API to find the coordinates corresponding to the start and end addresses
    3. Find the start segment, the sidewalk edge closest to the start coordinates. Also find the end segment, the
       sidewalk edge closest to the end coordinates
    4. Split the start segment at the start coordinates, creating two shorter edges. Repeat for end segment.
    5. Create a temporary table containing the four shorter edges in addition to the rest of the sidewalk segments
    6. Run a PgRouting query on the temporary table to find the shortest accessible route. The PgRouting query returns
       the route as a Geojson string.
    7. Query for elevation data at various points along the route and generate a Geojson string that contains both
       the route and the elevation data.
    8. Render an HTML page, inserting the generated Geojson into the page.

    """
    # Get the start and end addresses that the user sent
    address = request.GET['inputaddress'].strip()
    dest = request.GET['inputdest'].strip()
    error=""
    startfound = True
    destfound = True
    lat=0
    lng=0
    destlat=0
    destlng=0
    routegeojson = """{
    "type": "FeatureCollection",
    "features": []
    }"""
    # Query OpenCage to get coordinates for this address
    key = '7945028e977fa9593e5b02378cbf0f27'
    geocoder = OpenCageGeocode(key)
    # Make sure neither start address nor end address is blank first
    if (address != '' and dest != ''):
        # Get coordinates for start address
        # Opencage returns the result in json format
        jsonresult = geocoder.geocode(address)
        
        try:
            # Get the start lat/lng coordinates out of the results sent by OpenCage
            resultdict = jsonresult[0]
            lat = resultdict["geometry"]["lat"]
            lng = resultdict["geometry"]["lng"]
        except IndexError:
            # The start address was not found
            startfound = False
        
        # Repeat the process for destination address
        jsonresult = geocoder.geocode(dest)
        try:
            # Get the end lat/lng coordinates out of the results sent by OpenCage
            resultdict = jsonresult[0]
            destlat = resultdict["geometry"]["lat"]
            destlng = resultdict["geometry"]["lng"]
        except IndexError:
            # The destination address was not found
            destfound = False
        # Display appropriate errors if one or both addresses were not found
        if not startfound and destfound:
            error = "Error: The start address you entered could not be found."
        elif startfound and not destfound:
            error = "Error: The end address you entered could not be found."
        elif not startfound and not destfound:
            error = "Error: Neither addresses you entered could be found."
    else:
        # Display error if one or more fields were left blank
        error = "Error: One or more fields were left blank."

    cursor = connection.cursor()
    cursor.execute("SET search_path TO sidewalk,public;")  # Allow the subsequent queries to access tables under the sidewalk schema without a qualifier (i.e., sidewalk.XXX) http://www.postgresql.org/docs/9.2/static/ddl-schemas.html
    # Find the sidewalk edge closest to the start location and store the value in its 'source' column as start_source_id
    cursor.execute("SELECT source FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [lng, lat])
    row = cursor.fetchone()
    start_source_id = row[0]
    # Find the sidewalk edge closest to the end location and store the value in its 'target' column as end_target_id
    cursor.execute("SELECT target FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [destlng, destlat])
    row = cursor.fetchone()
    end_target_id = row[0]
    
    # Find the sidewalk edge closest to the start location and store its 'sidewalk_edge_id' as start_edge_id
    cursor.execute("SELECT sidewalk_edge_id FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [lng, lat])
    row = cursor.fetchone()
    start_edge_id = row[0]
    # Find the sidewalk edge closest to the end location and store its 'sidewalk_edge_id' as end_edge_id
    
    cursor.execute("SELECT sidewalk_edge_id FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [destlng, destlat])
    row = cursor.fetchone()
    end_edge_id = row[0]

    # Find location in the middle of the route for centering the map
    average_lat = (lat + destlat)/2.0
    average_lng = (lng + destlng)/2.0

    # The following gigantic SQL query creates a temporary table called combined_sidewalk_edge which contains
    # four new edges resulting from splitting the start segment at the start coordinates and the end segment at the
    # end coordinates, in addition to all the original sidewalk edges. This is necessary because we need to route
    # from the exact start point to the exact end point, not from the start segment to the end segment.
    cursor.execute("DISCARD TEMP;")
    create_temp_query = """
    CREATE TEMP TABLE combined_sidewalk_edge AS
    SELECT sidewalk_edge_id, geom, target, source  FROM sidewalk_edge
    UNION

    SELECT -1000 as sidewalk_edge_id, (
    ST_Line_Substring(  (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),
        (
            SELECT ST_Line_Locate_Point((SELECT geom FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1), (
                SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326), (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) ))
            )
        )
    ,1) 
    ) as geom, (SELECT target FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1) as target, '-123' as source

    UNION
    SELECT -1001 as sidewalk_edge_id, (
    ST_Line_Substring(  (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),0,
        (
            SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), (
                SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) ))
            )
        )
    ) 
    ) as geom, '-123' as target, (SELECT source FROM sidewalk_edge WHERE sidewalk_edge_id=%s LIMIT 1) as source
    
    UNION

    SELECT -1002 as sidewalk_edge_id, (
    ST_Line_Substring(  (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),0,
        (
            SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), (
                SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) ))
            )
        )
    ) 
    ) as geom, '-124' as target, (SELECT source FROM sidewalk_edge WHERE sidewalk_edge_id=%s LIMIT 1) as source
    
    UNION

    SELECT -1003 as sidewalk_edge_id, (
    ST_Line_Substring(  (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),
        (
            SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), (
                SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) ))
            )
        )
    ,1) 
    ) as geom, (SELECT target FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1) as target, '-124' as source
    ;
    """

    cursor.execute(create_temp_query, [start_edge_id, start_edge_id, lng, lat, start_edge_id, start_edge_id,
                                       start_edge_id, start_edge_id, lng, lat, start_edge_id, start_edge_id,
                                       end_edge_id, end_edge_id, destlng, destlat, end_edge_id, end_edge_id,
                                       end_edge_id, end_edge_id, destlng, destlat, end_edge_id, end_edge_id])
    cursor.execute("select * from combined_sidewalk_edge")

    # Now that the temporary table combined_sidewalk_edge has been created, we can query for a route from the start
    # location to the end location. This query will return a route as a Geojson string.

    # Todo. I changed calculate_accessible_cost to 1 for now.
    routesql = """
     SELECT ST_AsGeoJSON(st_union) FROM (
     SELECT ST_Union(geom) FROM (
     SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.geom, dt.sidewalk_edge_id FROM pgr_dijkstra('
                 SELECT sidewalk_edge_id AS id,
                          source::integer,
                          target::integer,
                          calculate_accessible_cost(sidewalk_edge_id)::double precision AS cost
                         FROM combined_sidewalk_edge',
                 %s, %s, false, false) as route
		 		join combined_sidewalk_edge  dt
		 		on route.id2 = dt.sidewalk_edge_id
     ) as routegeometries
     ) as final; """
    #routesql = """
    #SELECT ST_AsGeoJSON(route_geom) FROM (
    #SELECT ST_Union(geom) as route_geom FROM (
    #    SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.geom, dt.sidewalk_edge_id FROM pgr_dijkstra('SELECT sidewalk_edge_id AS id,
    #            source::int4, target::int4, 1.0::float8 AS cost FROM combined_sidewalk_edge', -123, -124, false, false
    #            ) as route
    #    join combined_sidewalk_edge dt on route.id2 = dt.sidewalk_edge_id
    #) as routegeometries
#) as final;""".replace("\t", "").replace("\n", "")
    # The source will always be -123 and the target will always be -124 because those are the source/target values we
    # assigned to the newly generated edges in the gigantic SQL query above.

    #cursor.execute(routesql)
    cursor.execute(routesql, ["-123", "-124"])
    row = cursor.fetchone()
    # Store the geojson string describing the route as routejs
    routejs = row[0]

    # Unfortunately, the paths that make up the route are not ordered in the geojson returned by PostGIS. Before
    # we can query for elevation, we need to order the paths.

    """
    Path sorting algorithm description:
    - Parse the json from PostGIS into a list named 'data'. The 'data' list is nested: It is a list of paths,
      each path is a list of points, and each point is a list containing lat and lng. So 'data' looks like this:
      [
      [ [5,6][7,8] ]
      [ [9,10][11,12][13,14] ]
      ]
    - Create an empty list 'data_ordered'. It will have the same structure as the 'data' list, but of course
      it will store the paths in order.
    - Remove a path from 'data' and put it into data_ordered
    - Find the lat/lng coordinates of the LAST point in this path
    - Search through 'data' for a path that either begins or ends at the coordinates found in the last step. This is
      the path that goes next, so append it to data_ordered and remove it from 'data'. If needed, reverse the order of
      the points in the newly appended path so that the common coordinates "meet". For instance, if the original path
      is [ [6,7][12,14][3,6] ] and the new path to append is [ [3,8][3,4][3,6] ], here's what data_ordered should look
      like after this step:
      [
      [ [6,7][12,14][3,6] ]
      [ [3,6][3,4][3,8] ]
      ]
    - Keep repeating this until no new path to append is found, at which point we have reached the end of the route.
      Now do this again, but prepending paths that should go before the first path currently in 'data_ordered'
      (rather than appending paths that go after the last one). The process continues until the 'data' list contains
      no more paths.

    - Create a new geojson string from 'data_ordered'
    """
    
    data = json.loads(routejs)
    points = []

    data_ordered = []
    begin_found = False
    end_found = False
    # Add the first path to data_ordered
    data_ordered.append(data['coordinates'].pop())

    while not begin_found or not end_found:
        # If the last segment hasn't been found yet
        if not end_found:
            # Find the path that goes after the last one, and append it to data_ordered
            search_lng = data_ordered[-1][-1][0]
            search_lat = data_ordered[-1][-1][1]
            next_segment_found = False
            for path in data['coordinates']:
                start_point = path[0]
                end_point = path[-1]
                if start_point[0] == search_lng and start_point[1] == search_lat:
                    # We found the path that goes next
                    next_segment_found = True
                    # Append it to data_ordered
                    data_ordered.append(path)
                    # Remove it from data
                    data['coordinates'].remove(path)
                elif end_point[0] == search_lng and end_point[1] == search_lat:
                    # Same as above, but the points in the path are in the opposite order
                    next_segment_found = True
                    data_ordered.append(path[::-1]) # Reverse the order of points in path before appending it
                    data['coordinates'].remove(path)
            # If the path that goes next was not found, we have reached the end of the route.
            if not next_segment_found:
                end_found = True
        # Now repeat this process backward until we reach the beginning of the route
        if not begin_found:
            # Find the path that goes before the first one, and prepend it to data_ordered
            search_lng = data_ordered[0][0][0]
            search_lat = data_ordered[0][0][1]
            previous_segment_found = False
            for path in data['coordinates']:
                start_point = path[0]
                end_point = path[-1]
                if start_point[0] == search_lng and start_point[1] == search_lat:
                    # We've found the path that goes before the first path currently in data_ordered
                    previous_segment_found = True
                    # Prepend the path to data_ordered. Order of the points in the path need to be reversed first.
                    data_ordered.insert(0, path[::-1])
                    # Remove the path from data
                    data['coordinates'].remove(path)
                elif end_point[0] == search_lng and end_point[1] == search_lat:
                    # Same as above but order of the points in the path does not need to be reversed.
                    previous_segment_found = True
                    data_ordered.insert(0, path)
                    data['coordinates'].remove(path)
            if not previous_segment_found:
                begin_found = True

    firstpath = data_ordered[0]
    lastpath = data_ordered[-1]
    route_start_lng = firstpath[0][0]
    route_start_lat = firstpath[0][1]
    route_end_lng = lastpath[-1][0]
    route_end_lat = lastpath[-1][1]

    # Sometimes, the first path in data_ordered will be the end segment and the last one will be the start
    # segment, so the entire data_ordered list may need to be reversed. Check if this is necessary and if so,
    # reverse the order of the paths and the order of the points in each path.

    # Determine if the order of path in data_ordered needs to be reversed
    # Find distance from start point to first point in data_ordered
    start_to_first_dist = math.hypot(lng - route_start_lng, lat - route_start_lat)
    # Now find distance from start point to last point in data_ordered
    start_to_last_dist = math.hypot(lng - route_end_lng, lat - route_end_lat)
    # If the latter is less than the former, data_ordered needs to be reversed
    if start_to_last_dist < start_to_first_dist:
        # Reverse order of the paths
        data_ordered.reverse()
        for path in data_ordered:
            # Also reverse order of the points in each path
            path.reverse()
        firstpath = data_ordered[0]
        lastpath = data_ordered[-1]
        route_start_lng = firstpath[0][0]
        route_start_lat = firstpath[0][1]
        route_end_lng = lastpath[-1][0]
        route_end_lat = lastpath[-1][1]

    # Finally, query for elevation data

    # Make a list of all the points along the route
    for path in data_ordered:
        for point in path:
            points.append(point)

    # Split the route into many shorter segments (so that there are more points to query elevation for)
    split_path = split(points)
    # Get elevation for each point on the split route; store in list
    elevation_list = get_elevations(split_path)
    # Generate a geojson string that contains both the split route and the elevation data
    output_string = output_geojson(split_path, elevation_list)

    routegeojson = routejs
    
    # Get nearby accessibility features to mark on map
    
    
    nearby_feature_sql = """ SELECT * FROM accessibility_feature
    WHERE ST_Distance_Sphere(feature_geometry, ST_MakePoint(%s, %s)) <= 3 * 1609.34 AND feature_type=%s; """
    # Get "construction" features
    cursor.execute(nearby_feature_sql, [lng, lat, 2])
    construction_features = cursor.fetchall()
    construction_points_list = []
    print("construction features")
    for feature in construction_features:
        feature_lng = feature[3]
        feature_lat = feature[4]
        feature_point = Point((feature_lng, feature_lat))
        streetview_img_code = "<h3>Reported construction</h3><a target='_blank' href='http://maps.google.com/?cbll="+str(feature_lat)+","+str(feature_lng)+"&cbp=12,235,,0,5&layer=c'><img src='https://maps.googleapis.com/maps/api/streetview?size=200x200&location="+str(feature_lat)+","+str(feature_lng)+"&fov=90&heading=235&pitch=10' /></a>"
        feature = geojson.Feature(geometry=feature_point, properties={"markertype": "construction","popupContent":streetview_img_code, "markerAddress":address})
        construction_points_list.append(feature)
    construction_collection = geojson.FeatureCollection(construction_points_list, featureid=2)
    construction_geojson = geojson.dumps(construction_collection, sort_keys=True)
    logger.debug(construction_geojson)
    # (Note: Repeat the above code for other kinds of features, if desired.)
    
    # print(routejs)
    return render(request, 'routeapp/homepage.html', {'submitted_start':address, 'submitted_dest':dest,'constructionfeatures':construction_geojson, 'routestartlng':route_start_lng, 'routestartlat':route_start_lat, 'routeendlng':route_end_lng, 'routeendlat':route_end_lat, 'elevationjson':output_string, 'centerlat':average_lat, 'centerlng':average_lng, 'defaultzoom': '17', 'lat':lat, 'lng':lng, 'destlat': destlat, 'destlng':destlng, 'start_textbox_value': address, 'dest_textbox_value': dest, 'error_message':error, 'routegeojson':routegeojson, })
            results = page_py.summary[0:2000]
            print(results)
            # text to speech
            language = 'bn'
            myobj = gTTS(
                text=results, lang=language,
                slow=False)  # Passing the text and language to the engine,
            myobj.save(
                "welcome.mp3"
            )  # Saving the converted audio in a mp3 file named  'welcome'
            os.system("start welcome.mp3")  # Playing the converted file

        elif 'আবহাওয়া বলুন' in query:
            geocoder = OpenCageGeocode(key)
            place = query.replace('আবহাওয়া বলুন', '')
            result = geocoder.geocode(place)

            a = result[0]['geometry']['lat']
            b = result[0]['geometry']['lng']
            print('Lat:', a, 'Lng:', b)
            # print(result[0])

            Approximity = a, b
            weekday = date.today()
            with forecast('35eaf94a35d0825031e72d5fb524bf50',
                          *Approximity) as area:
                temp = (((area.temperature) - 32) / 1.8)
                tempt = "%.2f" % temp
                print('TimeZone:', area.timezone)
                print('Temperature:', "%.2f" % temp, 'Degree Celcius')
                print('Humidity', area.humidity)
예제 #25
0
def predict():
    distance_time_speed_dataset = pd.read_csv('distance_time_speed_dataset.csv')
    s = (distance_time_speed_dataset.dtypes == 'object')
    object_cols = list(s[s].index)
    label_encoder = LabelEncoder()  # encoding all Object columns
    list_of_encoding = []
    for col in object_cols:
        distance_time_speed_dataset[col] = label_encoder.fit_transform(distance_time_speed_dataset[col])
        le_name_mapping = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))
        list_of_encoding.append(le_name_mapping)  # mapping the labels
    z_scores = zscore(distance_time_speed_dataset)
    abs_z_scores = np.abs(z_scores)
    filtered_entries = (abs_z_scores < 3).all(axis=1)  # filtering rows that are three std deviations away from the mean
    new_df = distance_time_speed_dataset[filtered_entries]

    distribution = []
    carrier = new_df['carrier']
    counter = Counter(carrier)
    for key, value in counter.items():
        percent = value / len(carrier) * 100
        df = {'Carrier': key, 'Count of unique values': value, 'Percentage': percent}
        distribution.append(df)
    dist = pd.DataFrame(distribution)
    dist = dist.sort_values(by=['Carrier'], ignore_index=True)

    group = new_df.groupby('carrier')
    df1 = group.apply(lambda x: len(x['source_city'].unique()))
    df2 = group.apply(lambda x: len(x['destination_city'].unique()))
    df3 = group.apply(lambda x: sum(x['time_to_delivery_hours']) / len(x['time_to_delivery_hours']))
    df4 = group.apply(lambda x: sum(x['speed']) / len(x['speed']))
    df5 = group.apply(lambda x: sum(x['distance']) / len(x['distance']))

    dist['Unique source cities'] = df1.values
    dist['Unique destination cities'] = df2.values
    dist['Average delivery time'] = df3.values
    dist['Average speed'] = df4.values
    dist['Average distance'] = df5.values
    key = '7d84bfd479184b6fb58f0c46bfc4debc'  # API Key
    geocoder = OpenCageGeocode(key)
    src = request.form.get("sourceCity", False)
    dest = request.form.get("destinationCity", False)
    src_location = geocoder.geocode(src)
    if src_location and len(src_location):
        src_longitude = src_location[0]['geometry']['lng']
        src_latitude = src_location[0]['geometry']['lat']
        src_code = src_location[0]['components']['country_code']
    else:
        return render_template('index.html',pred='Please enter correct source location')
    dest_location = geocoder.geocode(dest)
    if dest_location and len(dest_location):
        dest_longitude = dest_location[0]['geometry']['lng']
        dest_latitude = dest_location[0]['geometry']['lat']
        dest_code = dest_location[0]['components']['country_code']
    else:
        return render_template('index.html',pred='Please enter correct destination location')

    distance = vincenty((src_latitude, src_longitude), (dest_latitude, dest_longitude))

    srcC, destC = 100, 100
    srcS, destD = 396, 2207

    if src in list_of_encoding[0]:
        srcS = list_of_encoding[0].get(src)

    if dest in list_of_encoding[1]:
        destD = list_of_encoding[1].get(dest)

    if src_code in list_of_encoding[2]:
        srcC = list_of_encoding[2].get(src_code)

    if dest_code in list_of_encoding[3]:
        destC = list_of_encoding[3].get(dest_code)

    carriers = ['Aramex', 'BlueDart', 'Delhivery', 'DHL Express', 'dotzot', 'Ecom Express', 'FedEx', 'XpressBees']

    min_value = math.inf
    best_carrier = ""
    for id in range(8):
        item = [[srcS, destD, id, distance, srcC, destC, dist["Average speed"][id]]]
        value = model.predict(item)
        if value < min_value and value>0:
            min_value = value
            best_carrier = carriers[id]

    answer =  best_carrier + " is predicted to deliver the fastest!"
    #answer = best_carrier + "is predicted to deliver fastest in" + min_value[0][0] + "hours /" + min_value[0][0] // 24 + "days"
    return render_template('index.html', pred = answer)
예제 #26
0
def home():
    global first_frame, sec_frame, nearest_cols, nearest_rows, temp_cols, temp_rows, \
           v_city, v_Land, v_number_of_stations, station_id, start_date, end_date, result_single
    formFirst = FirstForm(request.form)
    formSec = SecondForm(request.form)

    # falls alle Variablen befüllt sind, die Felder mit diesen Namen vorausfüllen
    if v_city != "":
        formFirst.city.data = v_city
    if v_Land != "":
        formFirst.land.data = v_Land
    if station_id != "":
        formSec.stationId.data = station_id
    if start_date != "":
        start_date_split = start_date.split("-")
        formSec.start.data = datetime.date(int(start_date_split[0]), int(start_date_split[1]), int(start_date_split[2]))
    if end_date != "":
        end_date_split = end_date.split("-")
        formSec.end.data = datetime.date(int(end_date_split[0]), int(end_date_split[1]), int(end_date_split[2]))

    # hier geht er rein, wenn ein POST Event, also bspw. ein Button Klick auftritt
    # alle Buttons werden über ihre Namen angesprochen
    if request.method == 'POST':
        # Zurücksetzen der Eingaben in allen Feldern
        if 'resetAll' in request.form:
            first_frame = False
            sec_frame = False
            nearest_cols = 0
            nearest_rows = 0
            temp_cols = 0
            temp_rows = 0
            v_city = ""
            v_Land = ""
            station_id = ""
            start_date = ""
            end_date = ""
            return redirect(url_for("main.home"))

        # Submit der ersten Zeile, also City und Land
        if 'firstSubmit' in request.form:
            geocoder = OpenCageGeocode(API_key_OpenCageGeocoding)
            v_city = formFirst.city.data
            v_Land = formFirst.land.data
            print("City: " + v_city)
            try:
                temp_coordinates = geocoder.geocode((v_city + ',' + v_Land), no_annotations='1')
                temp_longitude = temp_coordinates[0]['geometry']['lng']
                temp_latitude = temp_coordinates[0]['geometry']['lat']
            except IOError:
                print('Error: City does not appear to exist.')
            except RateLimitExceededError as ex:
                print(ex)

            print(v_city + ',' + v_Land)
            df_nearest = get_nearest_weatherstation(temp_longitude, temp_latitude, 5)
            nearest_cols = df_nearest.columns.values
            nearest_rows = list(df_nearest.values.tolist())
            first_frame = True

        # Submit der zweiten Zeile, also Station ID , Start Date und End Date
        if 'SecSubmit' in request.form:
            station_id = formSec.stationId.data
            start_date = str(formSec.start.data)
            end_date = str(formSec.end.data)

            data_temp = get_hourly_weatherdata(station_id, start_date, end_date, v_city)

            range_date = pd.DataFrame(pd.date_range(start=str(start_date+' 00:00:00'), end=str(end_date+' 23:00:00'), freq='H'),
                                      columns=['time'])
#            range_date = pd.DataFrame(pd.date_range(start='2019-01-01 00:00:00', end='2019-12-31 23:00:00', freq='H'),
#                                      columns=['time'])
            data_temp = range_date.merge(data_temp, on='time', how='left')
            data_temp = data_temp.assign(missing=np.nan)
            data_temp.missing[data_temp.temperature.isna()] = 1
            result_statistics = pd.DataFrame({'recordings':range_date['time'].value_counts().sum(),
                                  '#_conditions':data_temp['condition'].value_counts().sum(),
                                  '#_dewpoint':data_temp['dewpoint'].value_counts().sum(),
                                  '#_humidity':data_temp['humidity'].value_counts().sum(),
                                  '#_missing':data_temp['missing'].value_counts().sum(),
                                  '#_peakgust':data_temp['peakgust'].value_counts().sum(),
                                  '#_precipitation':data_temp['precipitation'].value_counts().sum(),
                                  '#_precipitation_3':data_temp['precipitation_3'].value_counts().sum(),
                                  '#_precipitation_6':data_temp['precipitation_6'].value_counts().sum(),
                                  '#_pressure':data_temp['pressure'].value_counts().sum(),
                                  '#_snowdepth':data_temp['snowdepth'].value_counts().sum(),
                                  '#_station_id':data_temp['WS_id'].value_counts().sum(),
                                  '#_time':data_temp['time'].value_counts().sum(),
                                  '#_winddirection':data_temp['winddirection'].value_counts().sum(),
                                  '#_windspeed':data_temp['windspeed'].value_counts().sum(),
                                  '#temperature':data_temp['temperature'].value_counts().sum()
                                  },index=['selected_location'])
            temp_cols = result_statistics.columns.values
            temp_rows = list(result_statistics.values.tolist())
            sec_frame = True
            result_single = data_temp

        if 'ThirdSubmit' in request.form:
        ##### Alternative, damit alte Excel nicht überschrieben wird
            # Datei würde ich eher nicht direkt in derm Directory der App speichern,
            # sondern bspw. auf dem Display, einfach
            with tempfile.TemporaryDirectory() as folder:
                time = str(strftime("%Y%m%d-%H%M%S"))
                generated_file_name = time + '_df_hourly_weatherdata_' + v_city + '.xlsx'
                generated_file_path = os.path.join(folder, generated_file_name)
                result_single.to_excel(generated_file_path)
                return send_from_directory(folder, filename=generated_file_name, as_attachment=True)
        # deine ursprüngliche Lösung, liegt dann automatisch im Projekt-Ordner, wo auch die run.py liegt
        # result_single.to_excel('df_hourly_weatherdata_selected_city.xlsx')
        ### optional: wenn Excel erzeugt wurde, alle Daten zurück setzen (also Tabellen und Felder)?
            # first_frame = False
            # sec_frame = False
            # nearest_cols = 0
            # nearest_rows = 0
            # temp_cols = 0
            # temp_rows = 0
            #v_city = ""
            # v_Land = ""
            # station_id = ""
            # start_date = ""
            # end_date = ""

    return render_template('home.html', formFirst=formFirst, formSec=formSec,
                           first_frame=first_frame, sec_frame=sec_frame,
                           nearest_cols=nearest_cols, nearest_rows=nearest_rows,
                           temp_cols=temp_cols, temp_rows=temp_rows, zip=zip)
예제 #27
0
import opencage
from opencage.geocoder import OpenCageGeocode
import json

key = 'your-key-here'

geocoder = OpenCageGeocode(key)

query = '182 Clerkenwell Road, London'
ret = geocoder.geocode(query)
print(ret._content)
예제 #28
0
def get_coordinates_from_addr(addr):
    geocoder = OpenCageGeocode(opencagedata_key)
    results = geocoder.geocode(addr)
    return results[0]['geometry']['lng'], results[0]['geometry'][
        'lat'], results[0]['components']['city']
예제 #29
0
def search(request):
    
    address = request.GET['inputaddress'].strip()
    dest = request.GET['inputdest'].strip()
    error=""
    startfound = True
    destfound = True
    lat=0
    lng=0
    destlat=0
    destlng=0
    routegeojson = """{
    "type": "FeatureCollection",
    "features": []
    }"""
    # Query OpenCage to get coordinates for this address
    key = '7945028e977fa9593e5b02378cbf0f27'
    geocoder = OpenCageGeocode(key)
    if (address != '' and dest != ''):
        # Get coordinates for start address
        jsonresult = geocoder.geocode(address)
        
        try:
            resultdict = jsonresult[0]
            lat = resultdict["geometry"]["lat"]
            lng = resultdict["geometry"]["lng"]
        except IndexError:
            startfound = False
        
        # Get coordinates for destination address
        jsonresult = geocoder.geocode(dest)
        try:
            resultdict = jsonresult[0]
            destlat = resultdict["geometry"]["lat"]
            destlng = resultdict["geometry"]["lng"]
        except IndexError:
            destfound = False
        
        if not startfound and destfound:
            error = "Error: The start address you entered could not be found."
        elif startfound and not destfound:
            error = "Error: The end address you entered could not be found."
        elif not startfound and not destfound:
            error = "Error: Neither addresses you entered could be found."
    else:
        error = "Error: One or more fields were left blank."
    # Perform raw query to postgres database to find sidewalk closest to start coordinates
    cursor = connection.cursor()
    
    cursor.execute("SELECT source FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), wkb_geometry) ASC LIMIT 1", [lng, lat])
    row = cursor.fetchone()
    start_edge_id = row[0]
    # Repeat to find sidewalk closest to end coordinates
    cursor.execute("SELECT target FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), wkb_geometry) ASC LIMIT 1", [destlng, destlat])
    row = cursor.fetchone()
    end_edge_id = row[0]
    print("Start edge id is "+str(start_edge_id))
    print("End edge id is   "+str(end_edge_id))
    # Find location in the middle of the route for centering the map
    average_lat = (lat + destlat)/2.0
    average_lng = (lng + destlng)/2.0
    # Create geojson route
    routesql = """
    SELECT ST_AsGeoJSON(st_union) FROM (
    SELECT ST_Union(wkb_geometry) FROM (
    SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.wkb_geometry, dt.sidewalk_edge_id FROM pgr_dijkstra('
                SELECT sidewalk_edge_id AS id,
                         source::integer,
                         target::integer,
                         calculate_accessible_cost(sidewalk_edge_id)::double precision AS cost
                        FROM sidewalk_edge',
                %s, %s, false, false) as route
				join sidewalk_edge  dt
				on route.id2 = dt.sidewalk_edge_id
    ) as routegeometries
    ) as final; """
    cursor.execute(routesql, [start_edge_id, end_edge_id])
    row = cursor.fetchone()
    routejs = row[0]
    # We now have the geojson representing the route, but we need to clean it up a little
    geojson_dirty = json.loads(routejs)
    print("Info from route:")
    print(geojson_dirty['coordinates'][-1])
    '''
    # Take all coordinates and combine into a single large list
    coordinates_all = []
    for path in geojson_dirty['coordinates']:
        for point in path:
            pointlng = point[0]
            pointlat = point[1]
            coordinates_all.append(round(pointlng,4))
            coordinates_all.append(round(pointlat,4))
    print(coordinates_all)
    for path in geojson_dirty['coordinates']:
        for point in path:
            pointlng = point[0]
            poitnlng = point[1]
            # See if this is the only occurrence of this point
            if (coordinates_all.count(round(pointlng,4)) == 1 and coordinates_all.count(round(pointlat,4)) == 1):
                print("Found point that only occurs once")
                print(point[0],point[1])
    '''
    # Query the elevation table to get geojson with elevation data
    '''
    data = json.loads(routejs)
    points = []
    for path in data['coordinates']:
        for point in path:
            points.append(point)
    split_path = split(points)
    elevation_list = get_elevations(split_path)
    output_string = output_geojson(input_path, input_elevation_list)
    logger.debug(output_string)
    print(output_string)
    '''
    
    logger.error(routejs)
    routegeojson = routejs
    # print(routejs)
    return render(request, 'routeapp/homepage.html', {'centerlat':average_lat, 'centerlng':average_lng, 'defaultzoom': '17', 'lat':lat, 'lng':lng, 'destlat': destlat, 'destlng':destlng, 'start_textbox_value': address, 'dest_textbox_value': dest, 'error_message':error, 'routegeojson':routegeojson, })
예제 #30
0
		self.m_id=m_id
		self.prettyName=name
		self.latitude=latitude
		self.longitude=longitude

	def _get_prettyName(self):
		return self.prettyName

	def _set_prettyName(self, name):
		self.prettyName = name
#--------------------------------------------------#
key = 'Insert token here'
geocoder = OpenCageGeocode(key)

query = raw_input("Entrez l'adresse recherchee ici : ")
result = geocoder.geocode(query, format='json',language='fr')

latitude = str(result[0]['geometry']['lat'])
longitude = str(result[0]['geometry']['lng'])

print ""
print ""
print "Nous allons rechercher la Gare la plus proche de l'adresse : " + query
url = "http://localhost:8080/restserver/station/nearest-station?latitude=" + latitude + "&longitude=" + longitude

request = Request(url)

try:
	response = urlopen(request)
	stationRoot = ET.fromstring(response.read())
	m_id = stationRoot.find("id").text
#a.to_csv("xyz.tsv",sep="\t",index=None)

#retrieving coordinates and creating lat_long file
logging.info("Connecting server to retrieve coordinates of locations...")

region = a["region"].tolist()
regions = [x for x in region if str(x) != "?"]
regionlist = []
for i in regions:
    if i not in regionlist:
        regionlist.append(i)
regionlats = []
regionlngs = []
for i in regionlist:
    query = i
    result = geocoder.geocode(query)
    lat = result[0]["geometry"]["lat"]
    lng = result[0]["geometry"]["lng"]
    regionlats.append(lat)
    regionlngs.append(lng)
regiondata = {
    "info": "region",
    "loc": regionlist,
    "lat": regionlats,
    "lng": regionlngs
}
df1 = pd.DataFrame(regiondata)

countrie = a["country"].tolist()
countries = [x for x in countrie if str(x) != "?"]
countrylist = []
예제 #32
0
class OpenCageGeocodeTestCase(unittest.TestCase):
    def setUp(self):
        httpretty.enable()

        self.geocoder = OpenCageGeocode('abcde')

    def tearDown(self):
        httpretty.disable()
        httpretty.reset()

    def testUKPostcode(self):
        httpretty.register_uri(
            httpretty.GET,
            self.geocoder.url,
            body='{"total_results":10,"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","rate":{"limit":"2500","remaining":2487,"reset":1402185600},"results":[{"annotations":{},"components":{"country_name":"United Kingdom","region":"Islington","locality":"Clerkenwell"},"formatted":"Clerkenwell, Islington, United Kingdom","geometry":{"lat":"51.5221558691","lng":"-0.100838524406"},"bounds":null},{"formatted":"82, Lokku Ltd, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RF, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"82","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"Lokku Ltd","postcode":"EC1M 5RF"},"annotations":{},"bounds":{"northeast":{"lng":"-0.1023889","lat":"51.5226795"},"southwest":{"lat":"51.5225795","lng":"-0.1024889"}},"geometry":{"lat":"51.5226295","lng":"-0.1024389"}},{"components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lat":"51.5225346","lng":"-0.1027003"},"bounds":{"northeast":{"lat":"51.5225759","lng":"-0.1020597"},"southwest":{"lat":"51.5225211","lng":"-0.103223"}}},{"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Craft Central","annotations":{},"components":{"postcode":"EC1M 6DS","arts_centre":"Craft Central","state":"England","suburb":"Clerkenwell","country":"United Kingdom","city":"London Borough of Islington","country_code":"gb","road":"Clerkenwell Road","state_district":"Greater London","county":"London"},"bounds":{"northeast":{"lat":"51.52246","lng":"-0.1027652"},"southwest":{"lng":"-0.1028652","lat":"51.52236"}},"geometry":{"lng":"-0.1028152","lat":"51.52241"}},{"components":{"county":"London","state_district":"Greater London","restaurant":"Noodle Express","road":"Albemarle Way","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Noodle Express, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lng":"-0.10255386845056","lat":"51.5228195"},"bounds":{"southwest":{"lng":"-0.102621","lat":"51.5227781"},"northeast":{"lat":"51.5228603","lng":"-0.1024869"}}},{"geometry":{"lat":"51.5229424","lng":"-0.102380530769224"},"bounds":{"northeast":{"lat":"51.5229759","lng":"-0.1023064"},"southwest":{"lng":"-0.1024639","lat":"51.5229046"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","cafe":"PAR","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"formatted":"PAR, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"},{"formatted":"Workshop Coffee Co., 27, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RN, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"27","cafe":"Workshop Coffee Co.","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 5RN"},"annotations":{},"bounds":{"southwest":{"lng":"-0.1024422","lat":"51.5222246"},"northeast":{"lng":"-0.1022307","lat":"51.5224408"}},"geometry":{"lat":"51.52234585","lng":"-0.102338899572156"}},{"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"Franco & Co","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Franco & Co","geometry":{"lng":"-0.1024118","lat":"51.5231165"},"bounds":{"southwest":{"lng":"-0.1024618","lat":"51.5230665"},"northeast":{"lng":"-0.1023618","lat":"51.5231665"}}},{"bounds":{"northeast":{"lng":"-0.1023218","lat":"51.5231688"},"southwest":{"lat":"51.5229634","lng":"-0.1024934"}},"geometry":{"lng":"-0.102399365567707","lat":"51.5230257"},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, MacCarthy","annotations":{},"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"MacCarthy","state":"England","postcode":"EC1M 6DS"}},{"geometry":{"lng":"-0.102730855172415","lat":"51.52267345"},"bounds":{"northeast":{"lng":"-0.1025498","lat":"51.5227315"},"southwest":{"lat":"51.5226068","lng":"-0.1028931"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","house_number":"84","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"The Printworks","postcode":"EC1M 6DS"},"formatted":"84, The Printworks, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"}],"timestamp":{"created_unix":1402133768,"created_http":"Sat, 07 Jun 2014 09:36:08 GMT"},"we_are_hiring":"http://lokku.com/#jobs"}',
        )

        results = self.geocoder.geocode("EC1M 5RF")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 51.5201666) < 0.05 and abs(result['geometry']['lng'] - -0.0985142) < 0.05) for result in results),
            msg="Bad result"
        )

    def testAustralia(self):
        httpretty.register_uri(
            httpretty.GET,
            self.geocoder.url,
            body='{"licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","results":[{"geometry":{"lng":"149.5886383","lat":"-32.5980702"},"components":{"country_code":"au","state":"New South Wales","country":"Australia","town":"Mudgee"},"formatted":"Mudgee, New South Wales, Australia, au","annotations":{},"bounds":{"southwest":{"lng":"149.5486383","lat":"-32.6380702"},"northeast":{"lng":"149.6286383","lat":"-32.5580702"}}},{"formatted":"Mudgee, Mid-Western Regional, New South Wales, Australia","components":{"state":"New South Wales","country":"Australia","county":"Mid-Western Regional","town":"Mudgee"},"bounds":{"southwest":{"lng":"149.573196411","lat":"-32.6093025208"},"northeast":{"lng":"149.602890015","lat":"-32.5818252563"}},"annotations":{},"geometry":{"lng":149.5871,"lat":-32.59426}}],"total_results":2,"rate":{"reset":1402185600,"limit":"2500","remaining":2489},"we_are_hiring":"http://lokku.com/#jobs","timestamp":{"created_http":"Sat, 07 Jun 2014 09:31:50 GMT","created_unix":1402133510}}',
        )

        results = self.geocoder.geocode("Mudgee, Australia")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - -32.5980702) < 0.05 and abs(result['geometry']['lng'] - 149.5886383) < 0.05) for result in results),
            msg="Bad result"
        )


    def testMustBeUnicodeString(self):
        # dud mock so this goes quick
        httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"results":{}}')

        # Should not give errors
        self.geocoder.geocode('xxx')    # ascii convertable
        self.geocoder.geocode(six.u('xxx'))   # unicode
        self.geocoder.geocode(six.u('xxá'))   # unicode

        # But if it isn't a unicode string, it should give error
        utf8_string = six.u("xxá").encode("utf-8")
        latin1_string = six.u("xxá").encode("latin1")

        self.assertRaises(InvalidInputError, self.geocoder.geocode, utf8_string)

        # check the exception
        try:
            self.geocoder.geocode(utf8_string)
        except InvalidInputError as ex:
            self.assertEqual(ex.bad_value, utf8_string)
            self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(utf8_string))

        self.assertRaises(InvalidInputError, self.geocoder.geocode, latin1_string)

        # check the exception
        try:
            self.geocoder.geocode(latin1_string)
        except InvalidInputError as ex:
            self.assertEqual(ex.bad_value, latin1_string)
            self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(latin1_string))


    def testMunster(self):
        httpretty.register_uri(
            httpretty.GET,
            self.geocoder.url,
            body='{"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"thanks":"For using an OpenCage Data API","we_are_hiring":"http://lokku.com/#jobs","total_results":10,"status":{"message":"OK","code":200},"results":[{"formatted":"M\u00fcnster, M\u00fcnster, 48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de","geometry":{"lat":"51.9625101","lng":"7.6251879"},"components":{"country_code":"de","postcode":"48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","city":"M\u00fcnster","county":"M\u00fcnster","country":"Germany"},"bounds":{"southwest":{"lng":"7.4651879","lat":"51.8025101"},"northeast":{"lng":"7.7851879","lat":"52.1225101"}},"annotations":{}},{"bounds":{"northeast":{"lat":"52.0600706","lng":"7.7743708"},"southwest":{"lat":"51.8402214","lng":"7.4738051"}},"annotations":{},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","county":"M\u00fcnster","country":"Germany"},"formatted":"M\u00fcnster, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de"},{"formatted":"Munster, Ireland, ie","annotations":{},"bounds":{"southwest":{"lat":"51.4199027","lng":"-10.6891099"},"northeast":{"lng":"-6.9497829","lat":"53.1689062"}},"components":{"country_code":"ie","state_district":"Munster","country":"Ireland"},"geometry":{"lng":"-8.57089717629267","lat":"52.3076216"}},{"formatted":"Germany, de, M\u00fcnster","annotations":{},"bounds":{"southwest":{"lat":"51.8402214","lng":"7.4738051"},"northeast":{"lat":"52.0600706","lng":"7.7743708"}},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","address100":"M\u00fcnster","country":"Germany"}},{"formatted":"M\u00fcnster, M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","components":{"city":"Stuttgart","state":"Baden-W\u00fcrttemberg","city_district":"M\u00fcnster","country":"Germany","country_code":"de","state_district":"Regierungsbezirk Stuttgart","suburb":"M\u00fcnster","county":"Stuttgart"},"geometry":{"lat":"48.8212962","lng":"9.2200016"},"bounds":{"northeast":{"lng":"9.2400016","lat":"48.8412962"},"southwest":{"lng":"9.2000016","lat":"48.8012962"}},"annotations":{}},{"geometry":{"lng":"8.8671181","lat":"49.9229236"},"components":{"country_code":"de","state_district":"Regierungsbezirk Darmstadt","state":"Hesse","city":"M\u00fcnster","county":"Landkreis Darmstadt-Dieburg","country":"Germany"},"annotations":{},"bounds":{"northeast":{"lat":"49.9438725","lng":"8.9161067"},"southwest":{"lat":"49.9056973","lng":"8.7705856"}},"formatted":"M\u00fcnster, Landkreis Darmstadt-Dieburg, Regierungsbezirk Darmstadt, Hesse, Germany, de"},{"formatted":"M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","geometry":{"lat":"48.8272797","lng":"9.2024402537349"},"components":{"country_code":"de","state_district":"Regierungsbezirk Stuttgart","state":"Baden-W\u00fcrttemberg","city":"Stuttgart","city_district":"M\u00fcnster","county":"Stuttgart","country":"Germany"},"bounds":{"northeast":{"lat":"48.8384709","lng":"9.2273738"},"southwest":{"lng":"9.1883711","lat":"48.8152795"}},"annotations":{}},{"bounds":{"southwest":{"lng":"10.8788966","lat":"48.5896428"},"northeast":{"lat":"48.6515558","lng":"10.9314006"}},"annotations":{},"geometry":{"lng":"10.9008883","lat":"48.6242219"},"components":{"country_code":"de","state_district":"Swabia","state":"Free State of Bavaria","city":"M\u00fcnster","county":"Rain (Schwaben)","country":"Germany"},"formatted":"M\u00fcnster, Rain (Schwaben), Swabia, Free State of Bavaria, Germany, de"},{"formatted":"Munster, Lake County, Indiana, United States of America, us","bounds":{"northeast":{"lat":"41.5814003","lng":"-87.4802388"},"southwest":{"lng":"-87.5254509","lat":"41.522608"}},"annotations":{},"geometry":{"lat":"41.5644798","lng":"-87.5125412"},"components":{"country_code":"us","state":"Indiana","city":"Munster","county":"Lake County","country":"United States of America"}},{"bounds":{"northeast":{"lng":"12.5929086","lat":"48.9728073"},"southwest":{"lng":"12.5529086","lat":"48.9328073"}},"annotations":{},"geometry":{"lng":"12.5729086","lat":"48.9528073"},"components":{"country_code":"de","state_district":"Lower Bavaria","village":"M\u00fcnster","state":"Free State of Bavaria","county":"Landkreis Straubing-Bogen","country":"Germany"},"formatted":"M\u00fcnster, Landkreis Straubing-Bogen, Lower Bavaria, Free State of Bavaria, Germany, de"}],"timestamp":{"created_unix":1402135758,"created_http":"Sat, 07 Jun 2014 10:09:18 GMT"},"rate":{"remaining":2485,"reset":1402185600,"limit":"2500"}}',
        )

        results = self.geocoder.geocode(six.u("Münster"))
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 51.9625101) < 0.05 and abs(result['geometry']['lng'] - 7.6251879) < 0.05) for result in results),
            msg="Bad result"
        )

    def testDonostia(self):
        httpretty.register_uri(
            httpretty.GET,
            self.geocoder.url,
            body='{"thanks":"For using an OpenCage Data API","status":{"message":"OK","code":200},"rate":{"remaining":2482,"limit":"2500","reset":1402185600},"we_are_hiring":"http://lokku.com/#jobs","total_results":7,"results":[{"geometry":{"lat":"43.3213324","lng":"-1.9856227"},"annotations":{},"components":{"postcode":"20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018","county":"Donostialdea/Donostia-San Sebasti\u00e1n","state":"Basque Country","country":"Spain","city":"San Sebasti\u00e1n","country_code":"es"},"formatted":"San Sebasti\u00e1n, Donostialdea/Donostia-San Sebasti\u00e1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es","bounds":{"southwest":{"lat":"43.2178373","lng":"-2.086808"},"northeast":{"lng":"-1.8878838","lat":"43.3381344"}}},{"formatted":"Donostia, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lat":"43.3422299","lng":"-1.8022744"},"northeast":{"lng":"-1.8013452","lat":"43.3449598"}},"geometry":{"lng":"-1.8019153","lat":"43.3432784"},"annotations":{}},{"annotations":{},"geometry":{"lng":"-1.8022744","lat":"43.3422299"},"formatted":"Donostia, Anaka, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","suburb":"Anaka","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lng":"-1.8022971","lat":"43.3421635"},"northeast":{"lng":"-1.8022744","lat":"43.3422299"}}},{"geometry":{"lng":"-2.69312049872164","lat":"42.868297"},"annotations":{},"bounds":{"southwest":{"lng":"-2.6933154","lat":"42.8681484"},"northeast":{"lat":"42.8684357","lng":"-2.6929252"}},"formatted":"Donostia kalea, Ibaiondo, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Ibaiondo","country_code":"es","road":"Donostia kalea"}},{"bounds":{"southwest":{"lng":"-2.6889534","lat":"42.8620967"},"northeast":{"lat":"42.8623764","lng":"-2.6885774"}},"formatted":"Donostia kalea, Lakua, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Lakua","country_code":"es","road":"Donostia kalea"},"geometry":{"lat":"42.8622515","lng":"-2.68876582144679"},"annotations":{}},{"annotations":{},"geometry":{"lat":"51.5146888","lng":"-0.1609307"},"components":{"restaurant":"Donostia","country":"United Kingdom","state_district":"Greater London","country_code":"gb","county":"London","state":"England","suburb":"Marylebone","city":"City of Westminster","road":"Great Cumberland Mews"},"formatted":"Donostia, Great Cumberland Mews, Marylebone, City of Westminster, London, Greater London, England, United Kingdom, gb","bounds":{"northeast":{"lng":"-0.1608807","lat":"51.5147388"},"southwest":{"lat":"51.5146388","lng":"-0.1609807"}}},{"geometry":{"lat":43.31283,"lng":-1.97499},"annotations":{},"bounds":{"northeast":{"lng":"-1.92020404339","lat":"43.3401603699"},"southwest":{"lat":"43.2644081116","lng":"-2.04920697212"}},"formatted":"San Sebastian, Gipuzkoa, Basque Country, Spain, Donostia / San Sebasti\u00e1n","components":{"county":"Gipuzkoa","state":"Basque Country","country":"Spain","town":"San Sebastian","local administrative area":"Donostia / San Sebasti\u00e1n"}}],"timestamp":{"created_unix":1402136556,"created_http":"Sat, 07 Jun 2014 10:22:36 GMT"},"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}]}',

        )

        results = self.geocoder.geocode("Donostia")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 43.300836) < 0.05 and abs(result['geometry']['lng'] - -1.9809529) < 0.05) for result in results),
            msg="Bad result"
        )

        # test that the results are in unicode
        self.assertEqual(results[0]['formatted'], six.u('San Sebasti\xe1n, Donostialdea/Donostia-San Sebasti\xe1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es'))
예제 #33
0
import phonenumbers
from opencage.geocoder import OpenCageGeocode
from numberLocation import actualLocation

#API Key from OpenCageGeocode
Key = "36aa769420c748bbb122b55f3423d7c3"
geocoder = OpenCageGeocode(Key)

query = str(actualLocation)

output = geocoder.geocode(query)

#print(output)

lat = output[0]['geometry']['lat']
lng = output[0]['geometry']['lng']

print(lat, lng)

예제 #34
0
class OpenCageGeocodeTestCase(unittest.TestCase):
    def setUp(self):
        httpretty.enable()

        self.geocoder = OpenCageGeocode('abcde')

    def tearDown(self):
        httpretty.disable()
        httpretty.reset()

    def testUKPostcode(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"total_results":10,"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","rate":{"limit":"2500","remaining":2487,"reset":1402185600},"results":[{"annotations":{},"components":{"country_name":"United Kingdom","region":"Islington","locality":"Clerkenwell"},"formatted":"Clerkenwell, Islington, United Kingdom","geometry":{"lat":"51.5221558691","lng":"-0.100838524406"},"bounds":null},{"formatted":"82, Lokku Ltd, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RF, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"82","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"Lokku Ltd","postcode":"EC1M 5RF"},"annotations":{},"bounds":{"northeast":{"lng":"-0.1023889","lat":"51.5226795"},"southwest":{"lat":"51.5225795","lng":"-0.1024889"}},"geometry":{"lat":"51.5226295","lng":"-0.1024389"}},{"components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lat":"51.5225346","lng":"-0.1027003"},"bounds":{"northeast":{"lat":"51.5225759","lng":"-0.1020597"},"southwest":{"lat":"51.5225211","lng":"-0.103223"}}},{"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Craft Central","annotations":{},"components":{"postcode":"EC1M 6DS","arts_centre":"Craft Central","state":"England","suburb":"Clerkenwell","country":"United Kingdom","city":"London Borough of Islington","country_code":"gb","road":"Clerkenwell Road","state_district":"Greater London","county":"London"},"bounds":{"northeast":{"lat":"51.52246","lng":"-0.1027652"},"southwest":{"lng":"-0.1028652","lat":"51.52236"}},"geometry":{"lng":"-0.1028152","lat":"51.52241"}},{"components":{"county":"London","state_district":"Greater London","restaurant":"Noodle Express","road":"Albemarle Way","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Noodle Express, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lng":"-0.10255386845056","lat":"51.5228195"},"bounds":{"southwest":{"lng":"-0.102621","lat":"51.5227781"},"northeast":{"lat":"51.5228603","lng":"-0.1024869"}}},{"geometry":{"lat":"51.5229424","lng":"-0.102380530769224"},"bounds":{"northeast":{"lat":"51.5229759","lng":"-0.1023064"},"southwest":{"lng":"-0.1024639","lat":"51.5229046"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","cafe":"PAR","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"formatted":"PAR, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"},{"formatted":"Workshop Coffee Co., 27, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RN, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"27","cafe":"Workshop Coffee Co.","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 5RN"},"annotations":{},"bounds":{"southwest":{"lng":"-0.1024422","lat":"51.5222246"},"northeast":{"lng":"-0.1022307","lat":"51.5224408"}},"geometry":{"lat":"51.52234585","lng":"-0.102338899572156"}},{"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"Franco & Co","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Franco & Co","geometry":{"lng":"-0.1024118","lat":"51.5231165"},"bounds":{"southwest":{"lng":"-0.1024618","lat":"51.5230665"},"northeast":{"lng":"-0.1023618","lat":"51.5231665"}}},{"bounds":{"northeast":{"lng":"-0.1023218","lat":"51.5231688"},"southwest":{"lat":"51.5229634","lng":"-0.1024934"}},"geometry":{"lng":"-0.102399365567707","lat":"51.5230257"},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, MacCarthy","annotations":{},"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"MacCarthy","state":"England","postcode":"EC1M 6DS"}},{"geometry":{"lng":"-0.102730855172415","lat":"51.52267345"},"bounds":{"northeast":{"lng":"-0.1025498","lat":"51.5227315"},"southwest":{"lat":"51.5226068","lng":"-0.1028931"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","house_number":"84","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"The Printworks","postcode":"EC1M 6DS"},"formatted":"84, The Printworks, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"}],"timestamp":{"created_unix":1402133768,"created_http":"Sat, 07 Jun 2014 09:36:08 GMT"},"we_are_hiring":"http://lokku.com/#jobs"}',
        )

        results = self.geocoder.geocode("EC1M 5RF")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 51.5201666) < 0.05 and abs(result['geometry']['lng'] - -0.0985142) < 0.05) for result in results),
            msg="Bad result"
        )

    def testAustralia(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","results":[{"geometry":{"lng":"149.5886383","lat":"-32.5980702"},"components":{"country_code":"au","state":"New South Wales","country":"Australia","town":"Mudgee"},"formatted":"Mudgee, New South Wales, Australia, au","annotations":{},"bounds":{"southwest":{"lng":"149.5486383","lat":"-32.6380702"},"northeast":{"lng":"149.6286383","lat":"-32.5580702"}}},{"formatted":"Mudgee, Mid-Western Regional, New South Wales, Australia","components":{"state":"New South Wales","country":"Australia","county":"Mid-Western Regional","town":"Mudgee"},"bounds":{"southwest":{"lng":"149.573196411","lat":"-32.6093025208"},"northeast":{"lng":"149.602890015","lat":"-32.5818252563"}},"annotations":{},"geometry":{"lng":149.5871,"lat":-32.59426}}],"total_results":2,"rate":{"reset":1402185600,"limit":"2500","remaining":2489},"we_are_hiring":"http://lokku.com/#jobs","timestamp":{"created_http":"Sat, 07 Jun 2014 09:31:50 GMT","created_unix":1402133510}}',
        )

        results = self.geocoder.geocode("Mudgee, Australia")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - -32.5980702) < 0.05 and abs(result['geometry']['lng'] - 149.5886383) < 0.05) for result in results),
            msg="Bad result"
        )


    def testMustBeUnicodeString(self):
        # dud mock so this goes quick
        httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"results":{}}')

        # Should not give errors
        self.geocoder.geocode('xxx')    # ascii convertable
        self.geocoder.geocode(six.u('xxx'))   # unicode
        self.geocoder.geocode(six.u('xxá'))   # unicode

        # But if it isn't a unicode string, it should give error
        utf8_string = six.u("xxá").encode("utf-8")
        latin1_string = six.u("xxá").encode("latin1")

        self.assertRaises(InvalidInputError, self.geocoder.geocode, utf8_string)

        # check the exception
        try:
            self.geocoder.geocode(utf8_string)
        except InvalidInputError as ex:
            self.assertEqual(ex.bad_value, utf8_string)
            self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(utf8_string))

        self.assertRaises(InvalidInputError, self.geocoder.geocode, latin1_string)

        # check the exception
        try:
            self.geocoder.geocode(latin1_string)
        except InvalidInputError as ex:
            self.assertEqual(ex.bad_value, latin1_string)
            self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(latin1_string))


    def testMunster(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"thanks":"For using an OpenCage Data API","we_are_hiring":"http://lokku.com/#jobs","total_results":10,"status":{"message":"OK","code":200},"results":[{"formatted":"M\u00fcnster, M\u00fcnster, 48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de","geometry":{"lat":"51.9625101","lng":"7.6251879"},"components":{"country_code":"de","postcode":"48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","city":"M\u00fcnster","county":"M\u00fcnster","country":"Germany"},"bounds":{"southwest":{"lng":"7.4651879","lat":"51.8025101"},"northeast":{"lng":"7.7851879","lat":"52.1225101"}},"annotations":{}},{"bounds":{"northeast":{"lat":"52.0600706","lng":"7.7743708"},"southwest":{"lat":"51.8402214","lng":"7.4738051"}},"annotations":{},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","county":"M\u00fcnster","country":"Germany"},"formatted":"M\u00fcnster, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de"},{"formatted":"Munster, Ireland, ie","annotations":{},"bounds":{"southwest":{"lat":"51.4199027","lng":"-10.6891099"},"northeast":{"lng":"-6.9497829","lat":"53.1689062"}},"components":{"country_code":"ie","state_district":"Munster","country":"Ireland"},"geometry":{"lng":"-8.57089717629267","lat":"52.3076216"}},{"formatted":"Germany, de, M\u00fcnster","annotations":{},"bounds":{"southwest":{"lat":"51.8402214","lng":"7.4738051"},"northeast":{"lat":"52.0600706","lng":"7.7743708"}},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","address100":"M\u00fcnster","country":"Germany"}},{"formatted":"M\u00fcnster, M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","components":{"city":"Stuttgart","state":"Baden-W\u00fcrttemberg","city_district":"M\u00fcnster","country":"Germany","country_code":"de","state_district":"Regierungsbezirk Stuttgart","suburb":"M\u00fcnster","county":"Stuttgart"},"geometry":{"lat":"48.8212962","lng":"9.2200016"},"bounds":{"northeast":{"lng":"9.2400016","lat":"48.8412962"},"southwest":{"lng":"9.2000016","lat":"48.8012962"}},"annotations":{}},{"geometry":{"lng":"8.8671181","lat":"49.9229236"},"components":{"country_code":"de","state_district":"Regierungsbezirk Darmstadt","state":"Hesse","city":"M\u00fcnster","county":"Landkreis Darmstadt-Dieburg","country":"Germany"},"annotations":{},"bounds":{"northeast":{"lat":"49.9438725","lng":"8.9161067"},"southwest":{"lat":"49.9056973","lng":"8.7705856"}},"formatted":"M\u00fcnster, Landkreis Darmstadt-Dieburg, Regierungsbezirk Darmstadt, Hesse, Germany, de"},{"formatted":"M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","geometry":{"lat":"48.8272797","lng":"9.2024402537349"},"components":{"country_code":"de","state_district":"Regierungsbezirk Stuttgart","state":"Baden-W\u00fcrttemberg","city":"Stuttgart","city_district":"M\u00fcnster","county":"Stuttgart","country":"Germany"},"bounds":{"northeast":{"lat":"48.8384709","lng":"9.2273738"},"southwest":{"lng":"9.1883711","lat":"48.8152795"}},"annotations":{}},{"bounds":{"southwest":{"lng":"10.8788966","lat":"48.5896428"},"northeast":{"lat":"48.6515558","lng":"10.9314006"}},"annotations":{},"geometry":{"lng":"10.9008883","lat":"48.6242219"},"components":{"country_code":"de","state_district":"Swabia","state":"Free State of Bavaria","city":"M\u00fcnster","county":"Rain (Schwaben)","country":"Germany"},"formatted":"M\u00fcnster, Rain (Schwaben), Swabia, Free State of Bavaria, Germany, de"},{"formatted":"Munster, Lake County, Indiana, United States of America, us","bounds":{"northeast":{"lat":"41.5814003","lng":"-87.4802388"},"southwest":{"lng":"-87.5254509","lat":"41.522608"}},"annotations":{},"geometry":{"lat":"41.5644798","lng":"-87.5125412"},"components":{"country_code":"us","state":"Indiana","city":"Munster","county":"Lake County","country":"United States of America"}},{"bounds":{"northeast":{"lng":"12.5929086","lat":"48.9728073"},"southwest":{"lng":"12.5529086","lat":"48.9328073"}},"annotations":{},"geometry":{"lng":"12.5729086","lat":"48.9528073"},"components":{"country_code":"de","state_district":"Lower Bavaria","village":"M\u00fcnster","state":"Free State of Bavaria","county":"Landkreis Straubing-Bogen","country":"Germany"},"formatted":"M\u00fcnster, Landkreis Straubing-Bogen, Lower Bavaria, Free State of Bavaria, Germany, de"}],"timestamp":{"created_unix":1402135758,"created_http":"Sat, 07 Jun 2014 10:09:18 GMT"},"rate":{"remaining":2485,"reset":1402185600,"limit":"2500"}}',
        )

        results = self.geocoder.geocode(six.u("Münster"))
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 51.9625101) < 0.05 and abs(result['geometry']['lng'] - 7.6251879) < 0.05) for result in results),
            msg="Bad result"
        )

    def testDonostia(self):
        httpretty.register_uri(httpretty.GET,
            self.geocoder.url,
            body='{"thanks":"For using an OpenCage Data API","status":{"message":"OK","code":200},"rate":{"remaining":2482,"limit":"2500","reset":1402185600},"we_are_hiring":"http://lokku.com/#jobs","total_results":7,"results":[{"geometry":{"lat":"43.3213324","lng":"-1.9856227"},"annotations":{},"components":{"postcode":"20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018","county":"Donostialdea/Donostia-San Sebasti\u00e1n","state":"Basque Country","country":"Spain","city":"San Sebasti\u00e1n","country_code":"es"},"formatted":"San Sebasti\u00e1n, Donostialdea/Donostia-San Sebasti\u00e1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es","bounds":{"southwest":{"lat":"43.2178373","lng":"-2.086808"},"northeast":{"lng":"-1.8878838","lat":"43.3381344"}}},{"formatted":"Donostia, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lat":"43.3422299","lng":"-1.8022744"},"northeast":{"lng":"-1.8013452","lat":"43.3449598"}},"geometry":{"lng":"-1.8019153","lat":"43.3432784"},"annotations":{}},{"annotations":{},"geometry":{"lng":"-1.8022744","lat":"43.3422299"},"formatted":"Donostia, Anaka, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","suburb":"Anaka","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lng":"-1.8022971","lat":"43.3421635"},"northeast":{"lng":"-1.8022744","lat":"43.3422299"}}},{"geometry":{"lng":"-2.69312049872164","lat":"42.868297"},"annotations":{},"bounds":{"southwest":{"lng":"-2.6933154","lat":"42.8681484"},"northeast":{"lat":"42.8684357","lng":"-2.6929252"}},"formatted":"Donostia kalea, Ibaiondo, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Ibaiondo","country_code":"es","road":"Donostia kalea"}},{"bounds":{"southwest":{"lng":"-2.6889534","lat":"42.8620967"},"northeast":{"lat":"42.8623764","lng":"-2.6885774"}},"formatted":"Donostia kalea, Lakua, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Lakua","country_code":"es","road":"Donostia kalea"},"geometry":{"lat":"42.8622515","lng":"-2.68876582144679"},"annotations":{}},{"annotations":{},"geometry":{"lat":"51.5146888","lng":"-0.1609307"},"components":{"restaurant":"Donostia","country":"United Kingdom","state_district":"Greater London","country_code":"gb","county":"London","state":"England","suburb":"Marylebone","city":"City of Westminster","road":"Great Cumberland Mews"},"formatted":"Donostia, Great Cumberland Mews, Marylebone, City of Westminster, London, Greater London, England, United Kingdom, gb","bounds":{"northeast":{"lng":"-0.1608807","lat":"51.5147388"},"southwest":{"lat":"51.5146388","lng":"-0.1609807"}}},{"geometry":{"lat":43.31283,"lng":-1.97499},"annotations":{},"bounds":{"northeast":{"lng":"-1.92020404339","lat":"43.3401603699"},"southwest":{"lat":"43.2644081116","lng":"-2.04920697212"}},"formatted":"San Sebastian, Gipuzkoa, Basque Country, Spain, Donostia / San Sebasti\u00e1n","components":{"county":"Gipuzkoa","state":"Basque Country","country":"Spain","town":"San Sebastian","local administrative area":"Donostia / San Sebasti\u00e1n"}}],"timestamp":{"created_unix":1402136556,"created_http":"Sat, 07 Jun 2014 10:22:36 GMT"},"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}]}',

        )

        results = self.geocoder.geocode("Donostia")
        self.assertTrue(
            any((abs(result['geometry']['lat'] - 43.300836) < 0.05 and abs(result['geometry']['lng'] - -1.9809529) < 0.05) for result in results),
            msg="Bad result"
        )

        # test that the results are in unicode
        self.assertEqual(results[0]['formatted'], six.u('San Sebasti\xe1n, Donostialdea/Donostia-San Sebasti\xe1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es'))
예제 #35
0
"""#####Creating a new Data frame with Lat, Lng being fetched from OpenCage geocoder"""

Latitude = []
Longitude = []
Borough = []
Neighbourhood = vnc_ws_neigh['Neighbourhood'].unique()



key = '830323b5ca694362904814ff0a11b803'
geocoder = OpenCageGeocode(key)

for i in range(len(Neighbourhood)):
    address = '{}, Vancouver, BC, Canada'.format(Neighbourhood[i])
    location = geocoder.geocode(address)
    Latitude.append(location[0]['geometry']['lat'])
    Longitude.append(location[0]['geometry']['lng'])
    Borough.append('West Side')
print(Latitude, Longitude)

#print('The geograpical coordinate of Vancouver City are {}, {}.'.format(latitude, longitude))

"""####Glimpse of the new Data Frame with Neighborhoods in West Side Borough of Vancoouver along with centroid of their co-ordinates"""

ws_neig_dict = {'Neighbourhood': Neighbourhood,'Borough':Borough,'Latitude': Latitude,'Longitude':Longitude}
ws_neig_geo = pd.DataFrame(data=ws_neig_dict, columns=['Neighbourhood', 'Borough', 'Latitude', 'Longitude'], index=None)

ws_neig_geo

"""####Fetching the Geographical co-ordiantes of Vancouver to plot on Map"""