예제 #1
0
def locate():
    """
    This method uses the Google Maps v3 API to convert and input query string
    into latitude-longitude coordinates and a string summary.
    This method will only return valid positional data if the Google Maps API
    call returns a single result (i.e. the query wasn't too vague and referred
    to a valid location).
    """
    query_list = request.args.getlist('query')
    if 0 < len(query_list):
        # Just take the first one
        query = query_list[0]

        # Plug the query into geopy's Google geolocator and see if we get a
        # unique result.
        g = GoogleV3()
        try:
            place, (lat, lon) = g.geocode(query)
            return jsonify({
                'place': place,
                'lat': lat,
                'lon': lon })
        except ValueError:
            return jsonify({'error': 'multiple results'})
        except GQueryError:
            return jsonify({'error': 'no results'})
        except GeocoderResultError:
            return jsonify({'error': 'unknown'})

    return jsonify({'error': 'no query'})
예제 #2
0
def geocode_address(address):
    address = address.encode('utf-8')
    # search(address)
    geocoder = GoogleV3(api_key=settings.GOOGLE_MAP_API_KEY)
    try:
        _, latlon = geocoder.geocode(address)
    except (URLError, GeocoderQueryError, ValueError):
        return None
    else:
        print(latlon)
        return latlon
예제 #3
0
    def save(self, *args, **kwargs):

        # save coordinates by geocoding address
        geolocator = GoogleV3(
            api_key='AIzaSyDwMQvVq5I887bnz3zAlz71Onjsq4_PYb0')
        location = None if not self.address else geolocator.geocode(
            self.address)
        if location:
            self.latitude = location.latitude
            self.longitude = location.longitude

        super().save(*args, **kwargs)
예제 #4
0
 def save(self, **kwargs):
     if not self.location:
         address = u'%s %s' % (self.city, self.address)
         address = address.encode('utf-8')
         geocoder = GoogleV3(api_key=settings.GOOGLE_MAP_API_KEY)
         try:
             _, latlon = geocoder.geocode(address)
         except (URLError, GeocoderQueryError, ValueError):
             pass
         else:
             point = "POINT(%s %s)" % (latlon[1], latlon[0])
             self.location = geos.fromstr(point)
     super(Restaurant, self).save()
예제 #5
0
 def setUp(self):
     from geopy.geocoders.googlev3 import GoogleV3
     self.geocoder = GoogleV3()
예제 #6
0
#! /bin/python3

import csv
import time
from geopy.geocoders.googlev3 import GoogleV3

geocoder = GoogleV3(api_key="AIzaSyAy6XiyZG-6u99q-qacOz-dtT9ILbYzb-4")
with open("../ReadingBusesOrig.csv") as cf:
    with open("../out.csv", "a") as cw:
        reader = csv.DictReader(cf)
        writer = csv.DictWriter(cw, ["latitude", "longitude", "date"])
        startrow = 0
        for i in range(0, startrow):
            row = reader[i]
            location = geocoder.geocode(row['Place of Event'],
                                        components={
                                            "locality": "Reading",
                                            "country": "GB"
                                        })

            print("Resolved Address: " + str(location.address))
            print("Latitude: " + str(location.latitude))
            print("Longitude: " + str(location.longitude))
            print('\n')
            writer.writerow({
                "latitude": location.latitude,
                "longitude": location.longitude,
                "date": row['Accident Date']
            })
            time.sleep(0.2)
예제 #7
0
def crawl():

    geolocator = GoogleV3(api_key='***REMOVED***')
    #geolocator = Nominatim(user_agent="tu-dortmund-research")
    #     print( get_poi('Consulate General of the Unites States, Recife, Brazil', gmaps=geolocator) )
    #     exit()

    pois = get_pois(BASE_PATH + PREP_FOLDER)
    pois_done = get_processed(BASE_PATH + CRAWL_FOLDER + EXPORT_FOLDER)

    pois = pois[~pois.poi.isin(pois_done)]

    #test
    pois = pois[:10]

    result_map = {}

    first = pois.poi.values[0]

    togo = len(pois)
    tstart = time.time()

    try:

        retries = RETRY
        f**k = False
        dump = DUMP_AFTER

        i = 0

        while not f**k:

            if i == len(pois):
                break

            try:

                item_str = pois.poi_str.values[i]
                item = pois.poi.values[i]

                res = get_poi(item_str, gmaps=geolocator)
                if res is not None:
                    result_map[item] = res
                #time.sleep(1)
                i += 1
                retries = RETRY
                togo -= 1
                dump -= 1

                if dump == 0:
                    pickle.dump(
                        result_map,
                        open(
                            BASE_PATH + CRAWL_FOLDER + EXPORT_FOLDER +
                            'pfrom_' + str(first) + '.pkl', 'wb'))
                    dump = DUMP_AFTER
                    first = item
                    result_map = {}

                if togo % 100 == 0:
                    spent = time.time() - tstart
                    done = i + 1
                    each = spent / done
                    left = each * togo
                    eta = datetime.timedelta(seconds=left)
                    spent = datetime.timedelta(seconds=spent)

                    print('done {} of {} in {}, {} left'.format(
                        done, len(pois), spent, eta))

            except Exception:
                retries -= 1
                print('retries ', retries)
                if retries <= 0:
                    raise
                wait = RETRY - retries + 1
                time.sleep(pow(2, wait))

    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("*** print_tb:")
        traceback.print_tb(exc_traceback)
        pickle.dump(
            result_map,
            open(
                BASE_PATH + CRAWL_FOLDER + EXPORT_FOLDER + 'pfrom_' +
                str(first) + '.pkl', 'wb'))

    pickle.dump(
        result_map,
        open(
            BASE_PATH + CRAWL_FOLDER + EXPORT_FOLDER + 'pfrom_' + str(first) +
            '.pkl', 'wb'))