def get_geolocaiton(weather_data_structure=[],Out_Location_File=[],city=[]):

    '''
    In order to generate city's lantitude and longtitude information.
    python GeoPy package is required.
    '''
    # Get this python program full path to let use can program anywhere.

    absolute_directory_path =  os.path.dirname(os.path.abspath(sys.argv[0]))
    geolocations_file_data = absolute_directory_path + Out_Location_File


    # Initial geolocation dictionary to store written data.

    geolocaiton = {}

    '''
     Instead of calling GeoPy use policy everytime
     we will write location into a file. In the next
     query request, the program will read the data from the file directly.
     Format in the file: [city],[lantitude],[longtitude]
    '''

    # Check whether the file exist or not
    if os.path.isfile(geolocations_file_data):
        filereaderlocation = open(geolocations_file_data,'r')
        for line in filereaderlocation:
            temp = line.strip().split(',')
            geolocaiton[temp[0]] = temp[1] + "," + temp[2]
        filereaderlocation.close()

    # Check whether the input city exists in the location file.
    if not geolocaiton.has_key(city):
        filewriterlocation = open(geolocations_file_data,'a')
        geolocator = Nominatim()
        location = perform_geocode(geolocator,city.lower())
        lan = location.latitude
        lon = location.longitude
        filewriterlocation.write(city + "," + str(round(float(lan),2)) + "," + str(round(float(lon),2)) + "\n")
        filewriterlocation.close()
    else:
        location = geolocaiton.get(city)
        lan = location.split(',')[0]
        lon = location.split(',')[1]


    # Write into the  weather data structure
    weather_data_structure =  weather_data_structure + str(round(float(lan),2)) + "," + str(round(float(lon),2)) + ","

    '''
    The following part generate city's altitude.
    geocoder package required.
    '''

    g = geocoder.elevation([lan,lon])
    alt = g.meters

    # Write into the  weather data structure
    weather_data_structure =  weather_data_structure + str(int(alt)) + "|"
    return weather_data_structure
Ejemplo n.º 2
0
def getaltitude(latitude, longitude):

    print latitude
    print longitude
    h = geocoder.elevation([latitude,longitude]).meters
    time.sleep(0.25)
    return str(h)
Ejemplo n.º 3
0
    def set_location(self, location,
                     language=None, altitude=None, unit="meters"):
        """Set location via Google geocoding service.

        :param tuple/str location:
        :param str language: language for Google geocoding
        :param str language: language e.g. 'en', 'ja'
        :param float altitude: altitude
        :param str unit: altitude unit e.g. 'meters', 'feet'
        """
        language = language or self.language
        unit = dict(m="meters", ft="feet").get(unit, "meters")
        kw = {"language": language}
        if isinstance(location, tuple):
            kw["method"] = "reverse"
        try:
            location = geocoder.google(location, **kw)
        except:
            self._location = dict(
                    address=location,
                    latitude=None, longitude=None, altitude=None)
            return
        if not location.ok:
            raise ValueError("unknown place")
        address = unicodedata.normalize(
                    "NFKC", location.address.replace(FWHYPHEN, HWHYPHEN))
        if not altitude:
            altitude = getattr(geocoder.elevation(location.latlng), unit)
        self._location = dict(
                address=address,
                latitude=location.latlng[0], longitude=location.latlng[1],
                altitude=altitude)
Ejemplo n.º 4
0
def GetNames(ko, fallback='Unkown'):
    """
        Method that returns information about the location
        of given kooardinates.

        Variabls:
            ko (tuple) : lat,lon of the location
        Keywords:
            fallback   : if the google api doesn't find information
                        which string should be returned
        Returns str
    """

    lat, lon = float(ko[0]), float(ko[1])
    request = r = geocoder.reverse([lat, lon], short_name=False)
    name = r.country
    if type(r.state) != type(None):
        name = '%s, %s' % (r.state, r.country)

    if type(r.state) is type(None) and type(r.country) is type(None):
        ele = geocoder.elevation([lat, lon], short_name=False).meters
        if ele < 0:
            if lon > 48 and lon < 76 and lat > 3.9 and lat < 27.9:
                name = 'Arabian Sea'
            elif lon > 79 and lon < 98 and lat > 3.9 and lat < 27.9:
                name = 'Bay of Bengal'
            elif lon > 40 and lon < 122:
                name = 'Indian Ocean'
            elif lon > 145 and lon < 163 and lat < -11 and lat > -30:
                name = 'Coral Sea'
            elif lon > 121 and lon < 180.1:
                if lat > 0:
                    name = 'North West Pacific'
                else:
                    name = 'South West Pacific'
            elif lon > -180.1 and lon < -70:
                if lat > 0:
                    name = 'North East Pacific'
                else:
                    name = "South East Pacific"
            elif lon > -97 and lon < -90 and lat < 30 and lat > 18:
                name = 'Gulf of Mexico'
            elif lon > -89 and lon < -80 and lat < 30 and lat > 21:
                name = "Gulf of Mexico"
            elif lon > -87 and lon < -73 and lat < 21 and lat > 9:
                name = 'Caribbean Sea'
            elif lon > -80 and lon < 13:
                if lat > 0:
                    name = 'North Atlantic'
                else:
                    name = 'South Atlantic'
            else:
                name = '%02f %02f' % (float(lat), float(lon))
        else:
            name = '%02f %02f' % (float(lat), float(lon))

    return name
Ejemplo n.º 5
0
 def get_location(self, given_location=None):
     ''' Get user location based on input '''
     print('Location input examples:')
     print('    "San Francisco, CA, USA"')
     print('    "37.7749295,  -122.4194155,  15.60"')
     print('    "37:46:29.7N, -122:25:09.9E, 15.60"')
     print('')
     if sys.version_info < (3, 0):
         input_function = raw_input  # pylint:disable=undefined-variable
     else:
         input_function = input
     self.home = ephem.Observer()
     coords = None
     while True:
         if given_location:
             print('Given location: "{}"'.format(given_location))
             location_keyword = given_location
             given_location = None
         else:
             location_keyword = input_function(
                 'Enter location (default="{}"): '.format(
                     self.default_location))
         if not location_keyword or location_keyword.isspace():
             location_keyword = self.default_location
         coords = self._parse_coords(location_keyword)
         if coords:
             self.location = location_keyword
             (self.latitude, self.longitude, self.elevation) = coords
             self.elevation = float(self.elevation)
             self.friendly_location = "coordinates"
             break
         else:
             gloc = geocoder.google(location_keyword)
             if gloc.status != 'OK':
                 print('Location not found: "{}"'.format(location_keyword))
                 location_keyword = None
             else:
                 self.location = gloc.address
                 self.elevation = geocoder.elevation(gloc.latlng).meters
                 (self.latitude, self.longitude) = gloc.latlng
                 #(self.latitude, self.longitude) = (gloc.lat, gloc.lng)
                 self.friendly_location = u"{}".format(
                     unicode_deprecator(self.location))
                 #print(gloc.json)
                 print()
                 break
     self.config['main']['default_location'] = self.location
     self.home.elevation = self.elevation  # meters
     self.home.lat = str(self.latitude)  # +N
     self.home.lon = str(self.longitude)  # +E
     print("Found: {}N, {}E, {:0.2f}m".format(self.home.lat, self.home.lon,
                                              self.home.elevation))
     self.friendly_location = "{} ({:4.7f}N, {:4.7f}E) {:0.2f}m".format(
         self.friendly_location, self.home.lat / ephem.degree,
         self.home.lon / ephem.degree, self.home.elevation)
     print("Location: {}".format(self.friendly_location))
     print()
Ejemplo n.º 6
0
def fix():
    prematrix_file = conf.data_process_file_prematrix
    heigth=[]
    fw = open(conf.data_process_file_prematrix,"w+")
    i=0
    #Open file an fix altitude of each station
    with open(conf.data_process_file_prematrix_error,) as f:

        for line in f:
            slots = line.split(";")
            # id = slots[0]
            # street = slots[1]
            # heigth = slots[2]
            lat = float(slots[3])
            lon = float(slots[4])
            # neighbours = slots[5]
            # type = slots[6]
            # status = slots[7]

            #print("old height: " + str(heigth))
            #fix heigth
            print [lat,lon]
            altura =geocoder.elevation([lat,lon]).meters
            heigth.append(altura)
            print("Fix height: " + str(heigth[i]))
            print  altura
            time.sleep(0.25)


            line = slots[0]+";"+slots[1]+";"+str(heigth[i])+";"+slots[3]+";"+slots[4]+";"+slots[5]+";"+slots[6]+";"+slots[7]+";\n"
            #print line
            fw.write(line)
            i+=1


    fw.close()
    f.close()

    fw = open(conf.data_process_file_vector,"w+")
    i=0
    #Open file an fix altitude of each station
    with open(conf.data_process_file_vector_error,) as f:

        for line in f:
            slots = line.split(";")
            # id = slots[0]
            line = slots[0]+";"+slots[1]+";"+slots[2]+";"+str(heigth[i])+";"+slots[4]+";"+slots[5]+";\n"
            #print line
            fw.write(line)
            i+=1


    fw.close()
    f.close()
Ejemplo n.º 7
0
def fix():
    prematrix_file = conf.data_process_file_prematrix
    heigth = []
    fw = open(conf.data_process_file_prematrix, "w+")
    i = 0
    #Open file an fix altitude of each station
    with open(conf.data_process_file_prematrix_error, ) as f:

        for line in f:
            slots = line.split(";")
            # id = slots[0]
            # street = slots[1]
            # heigth = slots[2]
            lat = float(slots[3])
            lon = float(slots[4])
            # neighbours = slots[5]
            # type = slots[6]
            # status = slots[7]

            #print("old height: " + str(heigth))
            #fix heigth
            print[lat, lon]
            altura = geocoder.elevation([lat, lon]).meters
            heigth.append(altura)
            print("Fix height: " + str(heigth[i]))
            print altura
            time.sleep(0.25)

            line = slots[0] + ";" + slots[1] + ";" + str(
                heigth[i]) + ";" + slots[3] + ";" + slots[4] + ";" + slots[
                    5] + ";" + slots[6] + ";" + slots[7] + ";\n"
            #print line
            fw.write(line)
            i += 1

    fw.close()
    f.close()

    fw = open(conf.data_process_file_vector, "w+")
    i = 0
    #Open file an fix altitude of each station
    with open(conf.data_process_file_vector_error, ) as f:

        for line in f:
            slots = line.split(";")
            # id = slots[0]
            line = slots[0] + ";" + slots[1] + ";" + slots[2] + ";" + str(
                heigth[i]) + ";" + slots[4] + ";" + slots[5] + ";\n"
            #print line
            fw.write(line)
            i += 1

    fw.close()
    f.close()
Ejemplo n.º 8
0
def get_coords(address):
    while True:
        try:
            geolocator = Nominatim()
            location = geolocator.geocode(address)
            ele = geocoder.elevation(address)
            break
        except GeocoderTimedOut as e:
            time.sleep(1)
            print 'Geocoder Service timed out'
    
    return location.latitude, location.longitude, ele.meters
#loc = get_coords("Yorkshire Gliding Club UK")
#print "latitude: ", loc[0], " longitude: ", loc[1], " elevation: ", loc[2]
Ejemplo n.º 9
0
 def setLocation(self, search):
     if len(search.split(" ")) == 2:
         f, s = [i.replace(',','') for i in search.split(" ")]
         # Input location is coordinates
         if is_float(f) and is_float(s):
             self.latitude = float(f)
             self.longitude = float(s)
             self.altitude = 8
             return self.latitude, self.longitude, self.altitude
     providers = ['google', 'osm', 'arcgis', 'freegeoip']
     for p in providers:
         geo = getattr(geocoder, p)(search)
         if geo.lat is not None and geo.lng is not None:
             elev = geocoder.elevation(geo.latlng)
             self.latitude, self.longitude, self.altitude = geo.lat, geo.lng, elev.meters or 8
             return self.latitude, self.longitude, self.altitude
     raise GeneralPogoException("Location could not be found")
def get_coords(address):
    try:
#        geolocator = Nominatim(timeout=3, scheme='http')  # Nominatim seems to have stopped working, see github geopy
        geolocator = geopy.geocoders.GoogleV3(timeout=3)
#        geolocator = geopy.geocoders.GeocodeFarm(timeout=20)
        try:   
            location = geolocator.geocode(address, timeout=3, exactly_one=True)  # Only 1 location for this address
            if location == None:
                print "Geocoder Service timed out or Airfield: ", address, " not known by geocode locator service. Check settings"
                return False
            ele = geocoder.elevation(address)
            print "Geolocator worked"
            return location.latitude, location.longitude, ele.meters
        except ERROR_CODE_MAP[400]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[400]
        except ERROR_CODE_MAP[401]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[401]
        except ERROR_CODE_MAP[402]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[402]
        except ERROR_CODE_MAP[403]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[403]
        except ERROR_CODE_MAP[407]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[407]
        except ERROR_CODE_MAP[412]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[412]
        except ERROR_CODE_MAP[413]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[413]
        except ERROR_CODE_MAP[414]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[414]
        except ERROR_CODE_MAP[502]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[502]
        except ERROR_CODE_MAP[503]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[503]
        except ERROR_CODE_MAP[504]:
            print " ERROR_CODE_MAP is: ",  ERROR_CODE_MAP[503]
        return False
    except GeocoderTimedOut as e:
        print "Geocoder Service timed out for Airfield: ", address
        return False
Ejemplo n.º 11
0
def page_not_found(error):
    with requests.Session() as session:
        requester_ip = request.access_route[0]

        if requester_ip == '127.0.0.1':
            place, latlng, elev = 'nc', [35.6921, -80.4357], 218.2
            address: str = u'On Library Park: 35\N{DEGREE SIGN} 41\' 31.9\"N 80\N{DEGREE SIGN} 26\' 8.67\"W'

        else:
            place, latlng = 'geocode', geocoder.ip(requester_ip, key=GOOGLE_API_KEY).latlng

        requester_geocode = geocoder.google(latlng, key=GOOGLE_API_KEY, method='reverse', session=session)

        # Use the defined address, implies we are using a static location, if undefined make it the geocoded one.
        try:
            address
        except NameError:
            address = str(requester_geocode.address)

        # Use the defined elevation, implies we are using a static location, if undefined make it the geocoded one.
        try:
            requester_geocode.elevation = lambda: None
            requester_geocode.elevation.meters = lambda: None
            setattr(requester_geocode.elevation, 'meters', elev)
        except NameError:
            requester_geocode.elevation = geocoder.elevation(latlng, key=GOOGLE_API_KEY, session=session)

        # Get the timezone
        requester_geocode.timeZoneId = geocoder.timezone(latlng, key=GOOGLE_API_KEY, session=session).timeZoneId

    return render_template('404.html',
                           place=place,
                           address=address,
                           latlng=latlng,
                           elevation=requester_geocode.elevation.meters,
                           ip=requester_ip), 404
Ejemplo n.º 12
0
sty_uni_lst.remove('')
sty_uni_lst.remove('Not Known')

for i in range(len(sty_uni_lst)):
    ustyles.append(0)

fin1 = open('/data/user_detail', 'r')

for ln1 in fin1:
    uid = ln1.split('|')[0].strip()
    loc = ln1.split('|')[1].strip().split(',')[0]
    if (loc != 'Not Known'):
        geolocator = Nominatim()
        location = geolocator.geocode(loc)
        g = geocoder.elevation([location.latitude, location.longitude])

        if (g.meters <= 30):
            nos = 10
        elif (g.meters > 30 and g.meters < 1000):
            nos = 100
        else:
            nos = 1000
    else:
        nos = 0

    vmonths = ln1.split('|')[2].strip().split(',')
    for i in range(len(vmonths)):
        if vmonths[i] == 'Jan':
            months[0] = months[0] + 1
        elif vmonths[i] == 'Feb':
Ejemplo n.º 13
0
import geocoder

g = geocoder.google('Mountain View, CA')
print g.latlng

g = geocoder.elevation([float(41.387953), float(2.150169)])
print(g.meters)

g = geocoder.google([45.15, -75.14], method='elevation')
print g.meters
Ejemplo n.º 14
0
def search_worker_thread(args, account_queue, account_failures, search_items_queue, pause_bit, status, dbq, whq):

    log.debug('Search worker thread starting')

    # The outer forever loop restarts only when the inner one is intentionally exited - which should only be done when the worker is failing too often, and probably banned.
    # This reinitializes the API and grabs a new account from the queue.
    while True:
        try:
            status['starttime'] = now()

            # Get account
            status['message'] = 'Waiting to get new account from the queue'
            log.info(status['message'])
            account = account_queue.get()
            status['message'] = 'Switching to account {}'.format(account['username'])
            status['user'] = account['username']
            log.info(status['message'])

            stagger_thread(args, account)

            # New lease of life right here
            status['fail'] = 0
            status['success'] = 0
            status['noitems'] = 0
            status['skip'] = 0
            status['location'] = False
            status['last_scan_time'] = 0

            # only sleep when consecutive_fails reaches max_failures, overall fails for stat purposes
            consecutive_fails = 0
            consecutive_empties = 0

            # Create the API instance this will use
            if args.mock != '':
                api = FakePogoApi(args.mock)
            else:
                api = PGoApi()

            if status['proxy_url']:
                log.debug("Using proxy %s", status['proxy_url'])
                api.set_proxy({'http': status['proxy_url'], 'https': status['proxy_url']})

            # api.activate_signature(encryption_lib_path)

            # The forever loop for the searches
            while True:

                # If this account has been messing up too hard, let it rest
                if consecutive_fails >= args.max_failures:
                    status['message'] = 'Account {} failed more than {} scans; possibly bad account. Switching accounts...'.format(account['username'], args.max_failures)
                    log.warning(status['message'])
                    account_failures.append({'account': account, 'last_fail_time': now(), 'reason': 'failures'})
                    break  # exit this loop to get a new account and have the API recreated

                while pause_bit.is_set():
                    status['message'] = 'Scanning paused'
                    time.sleep(2)

                # If this account has been running too long, let it rest
                if (args.account_search_interval is not None):
                    if (status['starttime'] <= (now() - args.account_search_interval)):
                        status['message'] = 'Account {} is being rotated out to rest.'.format(account['username'])
                        log.info(status['message'])
                        account_failures.append({'account': account, 'last_fail_time': now(), 'reason': 'rest interval'})
                        break

                # Grab the next thing to search (when available)
                status['message'] = 'Waiting for item from queue'
                step, step_location, appears, leaves = search_items_queue.get()

                # too soon?
                if appears and now() < appears + 10:  # adding a 10 second grace period
                    first_loop = True
                    paused = False
                    while now() < appears + 10:
                        if pause_bit.is_set():
                            paused = True
                            break  # why can't python just have `break 2`...
                        remain = appears - now() + 10
                        status['message'] = 'Early for {:6f},{:6f}; waiting {}s...'.format(step_location[0], step_location[1], remain)
                        if first_loop:
                            log.info(status['message'])
                            first_loop = False
                        time.sleep(1)
                    if paused:
                        search_items_queue.task_done()
                        continue

                # too late?
                if leaves and now() > (leaves - args.min_seconds_left):
                    search_items_queue.task_done()
                    status['skip'] += 1
                    # it is slightly silly to put this in status['message'] since it'll be overwritten very shortly after. Oh well.
                    status['message'] = 'Too late for location {:6f},{:6f}; skipping'.format(step_location[0], step_location[1])
                    log.info(status['message'])
                    # No sleep here; we've not done anything worth sleeping for. Plus we clearly need to catch up!
                    continue

                # Get the actual altitude of step_location
                altitude = geocoder.elevation([step_location[0], step_location[1]])
                step_location = (step_location[0], step_location[1], altitude.meters)

                # Let the api know where we intend to be for this loop
                # doing this before check_login so it does not also have to be done there
                # when the auth token is refreshed
                api.set_position(*step_location)

                # Ok, let's get started -- check our login status
                check_login(args, account, api, step_location, status['proxy_url'])

                # putting this message after the check_login so the messages aren't out of order
                status['message'] = 'Searching at {:6f},{:6f}'.format(step_location[0], step_location[1])
                log.info(status['message'])

                # Make the actual request (finally!)
                response_dict = map_request(api, step_location, args.jitter)

                # G'damnit, nothing back. Mark it up, sleep, carry on
                if not response_dict:
                    status['fail'] += 1
                    consecutive_fails += 1
                    status['message'] = 'Invalid response at {:6f},{:6f}, abandoning location'.format(step_location[0], step_location[1])
                    log.error(status['message'])
                    time.sleep(args.scan_delay)
                    continue

                # Got the response, check for captcha, parse it out, send todo's to db/wh queues
                try:
                    # Captcha check
                    if args.captcha_solving:
                        captcha_url = response_dict['responses']['CHECK_CHALLENGE']['challenge_url']
                        if len(captcha_url) > 1:
                            status['message'] = 'Account {} is encountering a captcha, starting 2captcha sequence'.format(account['username'])
                            log.warning(status['message'])
                            captcha_token = token_request(args, status, captcha_url)
                            if 'ERROR' in captcha_token:
                                log.warning("Unable to resolve captcha, please check your 2captcha API key and/or wallet balance")
                                account_failures.append({'account': account, 'last_fail_time': now(), 'reason': 'catpcha failed to verify'})
                                break
                            else:
                                status['message'] = 'Retrieved captcha token, attempting to verify challenge for {}'.format(account['username'])
                                log.info(status['message'])
                                response = api.verify_challenge(token=captcha_token)
                                if 'success' in response['responses']['VERIFY_CHALLENGE']:
                                    status['message'] = "Account {} successfully uncaptcha'd".format(account['username'])
                                    log.info(status['message'])
                                else:
                                    status['message'] = "Account {} failed verifyChallenge, putting away account for now".format(account['username'])
                                    log.info(status['message'])
                                    account_failures.append({'account': account, 'last_fail_time': now(), 'reason': 'catpcha failed to verify'})
                                    break

                    # Parse 'GET_MAP_OBJECTS'
                    parsed = parse_map(args, response_dict, step_location, dbq, whq, api)
                    search_items_queue.task_done()
                    if parsed['count'] > 0:
                        status['success'] += 1
                        consecutive_empties = 0
                    else:
                        status['noitems'] += 1
                        consecutive_empties += 1
                    consecutive_fails = 0
                    status['message'] = 'Search at {:6f},{:6f} completed with {} finds'.format(step_location[0], step_location[1], parsed['count'])
                    log.debug(status['message'])
                except KeyError:
                    parsed = False
                    status['fail'] += 1
                    consecutive_fails += 1
                    status['message'] = 'Map parse failed at {:6f},{:6f}, abandoning location. {} may be banned.'.format(step_location[0], step_location[1], account['username'])
                    log.exception(status['message'])

                # Get detailed information about gyms
                if args.gym_info and parsed:
                    # build up a list of gyms to update
                    gyms_to_update = {}
                    for gym in parsed['gyms'].values():
                        # Can only get gym details within 1km of our position
                        distance = calc_distance(step_location, [gym['latitude'], gym['longitude']])
                        if distance < 1:
                            # check if we already have details on this gym (if not, get them)
                            try:
                                record = GymDetails.get(gym_id=gym['gym_id'])
                            except GymDetails.DoesNotExist as e:
                                gyms_to_update[gym['gym_id']] = gym
                                continue

                            # if we have a record of this gym already, check if the gym has been updated since our last update
                            if record.last_scanned < gym['last_modified']:
                                gyms_to_update[gym['gym_id']] = gym
                                continue
                            else:
                                log.debug('Skipping update of gym @ %f/%f, up to date', gym['latitude'], gym['longitude'])
                                continue
                        else:
                            log.debug('Skipping update of gym @ %f/%f, too far away from our location at %f/%f (%fkm)', gym['latitude'], gym['longitude'], step_location[0], step_location[1], distance)

                    if len(gyms_to_update):
                        gym_responses = {}
                        current_gym = 1
                        status['message'] = 'Updating {} gyms for location {},{}...'.format(len(gyms_to_update), step_location[0], step_location[1])
                        log.debug(status['message'])

                        for gym in gyms_to_update.values():
                            status['message'] = 'Getting details for gym {} of {} for location {},{}...'.format(current_gym, len(gyms_to_update), step_location[0], step_location[1])
                            time.sleep(random.random() + 2)
                            response = gym_request(api, step_location, gym)

                            # make sure the gym was in range. (sometimes the API gets cranky about gyms that are ALMOST 1km away)
                            if response['responses']['GET_GYM_DETAILS']['result'] == 2:
                                log.warning('Gym @ %f/%f is out of range (%dkm), skipping', gym['latitude'], gym['longitude'], distance)
                            else:
                                gym_responses[gym['gym_id']] = response['responses']['GET_GYM_DETAILS']

                            # increment which gym we're on (for status messages)
                            current_gym += 1

                        status['message'] = 'Processing details of {} gyms for location {},{}...'.format(len(gyms_to_update), step_location[0], step_location[1])
                        log.debug(status['message'])

                        if gym_responses:
                            parse_gyms(args, gym_responses, whq)

                # Record the time and place the worker left off at
                status['last_scan_time'] = now()
                status['location'] = step_location

                # Always delay the desired amount after "scan" completion
                status['message'] += ', sleeping {}s until {}'.format(args.scan_delay, time.strftime('%H:%M:%S', time.localtime(time.time() + args.scan_delay)))
                time.sleep(args.scan_delay)

        # catch any process exceptions, log them, and continue the thread
        except Exception as e:
            status['message'] = 'Exception in search_worker using account {}. Restarting with fresh account. See logs for details.'.format(account['username'])
            time.sleep(args.scan_delay)
            log.error('Exception in search_worker under account {} Exception message: {}'.format(account['username'], e))
            account_failures.append({'account': account, 'last_fail_time': now(), 'reason': 'exception'})
Ejemplo n.º 15
0
# Find missing gps_height values from longitute and latitude column
#use command --> "pip install --user geocoder" to install Geocoder package
from time import sleep
import geocoder

#Store missing gps heights in a dict {id:gps_height}
gpsHeights = {}

for i in data_values.id:
    if (data_values.loc[data_values.id == i, "gps_height"] == 0).bool():
        latitude = data_values.loc[data_values.id == i, "latitude"]
        longitude = data_values.loc[data_values.id == i, "longitude"]
        skipped = 0
        while True:
            sleep(1)
            #GPS access key
            geoAccess = geocoder.elevation(
                [latitude, longitude],
                key="AIzaSyDk3VvbBmuUH5J4vGntZAinU6rynEEKGO4")
            if geoAccess.ok:
                gpsHeights[i] = int(geoAccess.meters)
                break
            else:
                skipped += 1
        #Writing the missing gps heights on to a newGpsheights.csv file
        pandas.DataFrame.from_dict(gpsHeights,
                                   orient="index").reset_index().to_csv(
                                       "../../../Datasets/heights.csv",
                                       header=["id", "gps_height"],
                                       index=False)
Ejemplo n.º 16
0
def load_stats(station_location_files=[],
               sfile='stations.csv',
               shp_file='../geo/limadmin-shp/LIMADMIN.shp'):
    if os.path.exists(sfile):
        print("Loading station infromation from saved file")
        blocs = pd.read_csv(sfile)
    else:
        print("Creating station information file with relevant information")
        import geocoder
        import geopandas as gpd
        import LatLon
        from unidecode import unidecode
        from shapely.geometry import Point
        loc_files = station_location_files
        loclist = []
        for ll in loc_files:
            loclist.append(load_loc(ll))
        blocs = pd.concat(loclist)
        blocs.drop_duplicates(subset=['code'], keep='last', inplace=True)
        downtown = LatLon.LatLon(LatLon.Latitude(45.504045),
                                 LatLon.Longitude(-73.569101))
        stat_loc = [
            LatLon.LatLon(LatLon.Latitude(blocs.loc[stat, 'latitude']),
                          LatLon.Latitude(blocs.loc[stat, 'longitude']))
            for stat in blocs.index
        ]

        print("finding elevation for each station")
        ggs = [
            geocoder.elevation(
                "%s, %s" %
                (blocs.loc[stat, 'latitude'], blocs.loc[stat, 'longitude']))
            for stat in blocs.index
        ]
        elevs = [g.meters for g in ggs]
        dist_dt = [downtown.distance(sl) for sl in stat_loc]
        blocs['distance_to_downtown'] = dist_dt
        blocs['LatLon'] = stat_loc
        blocs['elev'] = elevs

        nbl = []
        for bl in blocs['name']:
            t = unidecode(bl)
            t.encode(
                "ascii"
            )  #works fine, because all non-ASCII from s are replaced with their equivalents
            nbl.append(t)
        blocs['name fmt'] = nbl
        # remove names which are not easily written to file
        del blocs['name']
        blocs.index = blocs['code']
        # read shape file of region
        mtlbr = gpd.read_file(shp_file)

        pps = [Point(pt) for pt in zip(blocs['longitude'], blocs['latitude'])]
        nns = []
        for pp in pps:
            shp_indx = np.argmax(mtlbr['geometry'].contains(pp))
            nns.append(shp_indx)

        blocs['neighborhood code'] = nns
        nnames = np.array(
            mtlbr.loc[nns, 'NOM'].map(lambda x: unidecode(x).encode('ascii')))
        blocs['neighborhood'] = nnames

        blocs.to_csv(sfile)
    return blocs
		lan = location.latitude
		lon = location.longitude
		fwlocation.write(city + "," + str(round(float(lan),2)) + "," + str(round(float(lon),2)) + "\n")
		fwlocation.close()
	else:
		location = geolocaiton.get(city)
		lan = location.split(',')[0]
		lon = location.split(',')[1]
	# Write into the string data
	str_data = str_data + str(round(float(lan),2)) + "," + str(round(float(lon),2)) + ","

	'''
	The following part generate city's altitude.
	geocoder package required.
	'''
	g = geocoder.elevation([lan,lon])
	alt = g.meters
	# Write into the string data
	str_data = str_data + str(int(alt)) + "|"

	'''
	Get current time using standard format
	'''
	ts = time.time()
	st = datetime.datetime.fromtimestamp(ts,pytz.timezone(tz_loc.get(city))).strftime('%Y-%m-%dT%H:%M:%SZ')
	# Write into the string data
	str_data = str_data + st + "|"

	'''
	Randomly get conditions
	'''
Ejemplo n.º 18
0
#!/usr/bin/python3

## This code accesses the Google Elevation API to return the corresponding elevation from sea level using the LAT and LONG array

import geocoder
import pandas as pd
import numpy as np

proxies = {
    'http': 'http://*****:*****@172.27.16.154:3128',
    'https': 'http://*****:*****@172.27.16.154:3128',
    'HTTP': 'http://*****:*****@172.27.16.154:3128',
    'HTTPS': 'http://*****:*****@172.27.16.154:3128'
}

data = pd.read_csv('soilData.csv')
df = data.set_index('ID')

data = np.array(df)
pos = data[:, 0:2]

elevation = np.array([])
for i in range(pos.shape[0]):
    g = geocoder.elevation(list(pos[i]), proxies=proxies)
    elevation = np.append(elevation, [g.meters])

alt = pd.DataFrame({'Elevation': elevation
                    })  # Creating a DataFrame to store elevation values
alt.to_csv('elevation.csv')  #Exporting the data to a CSV
Ejemplo n.º 19
0
import geocoder
import sys

degree = 0.0025
lat = float(sys.argv[1])
lon = float(sys.argv[2])

w, h = 10, 10

arr = [[0 for x in range(w)] for y in range(h)]
fin = [[0 for x in range(w - 2)] for y in range(h - 2)]

for y in range(-5, 5):
    for x in range(-5, 5):
        g = geocoder.elevation([lat + (x * degree), lon + (y * degree)]).meters
        if (g == None):
            if (x == -5):
                arr[y + 5][x + 5] = arr[y + 4][4]
            else:
                arr[y + 5][x + 5] = arr[y + 5][x + 4]
        else:
            arr[y + 5][x + 5] = g

print arr

for j in range(8):
    for i in range(8):
        fin[j][i] = abs(arr[j + 1][i + 1] - arr[j][i + 1]) + abs(
            arr[j + 1][i + 1] - arr[j + 1][i + 2]) + abs(
                arr[j + 1][i + 1] -
Ejemplo n.º 20
0
import geocoder
import pandas as pd
import numpy as np
import sys, os

os.chdir("C:\Users\Shikhar\Desktop\MEMS\THESIS\RESEARCH")
station = pd.read_csv('C:\Users\Shikhar\Desktop\MEMS\THESIS\RESEARCH\ground_data_stations_eu.csv', engine='python')
station_parent_dict = station.set_index('stid').T.to_dict('list')
all_stid = station["stid"].tolist()

for stid in all_stid:
# stid='penn_state'
 lat, lon = round(station_parent_dict[stid][1],5), round(station_parent_dict[stid][2],5)
 a = '['+str(lat)+","+str(lon)+']'
 g = geocoder.elevation(a)
 b = g.meters
 if b==None:
  b=0
 df_tosave = pd.DataFrame({'country' : station_parent_dict[stid][0],'stid' : [str(stid)], 'nlat' : [round(station_parent_dict[stid][1],2)],'elon' : [round(station_parent_dict[stid][2],2)],'elev' : [round(b,0)]})
 cols=['country','stid', 'nlat','elon' ,'elev']
 df_tosave=df_tosave[cols]
 filename1 = "ground_data_correctelev_eu.csv"
 if stid == all_stid[0] :
  df_tosave.to_csv(filename1, sep=',',index=False)
 else:
  with open(filename1, 'a') as f:
   (df_tosave).to_csv(f, sep=',',index=False,header=False)
 
 os.chdir("C:\Users\Shikhar\Desktop\MEMS\THESIS\RESEARCH" + "\\Europe\\"+stid + "\\data\\")
 fake_station = pd.read_csv('station_info_noElev.csv',engine = 'python') ### for each fake region created, the elevations are downloaded
Ejemplo n.º 21
0
 def address2elev(self, address):
     g = geocoder.elevation(address)
     elevation = g.elevation
     if elevation is None:    
         elevation = 0    
     return elevation
        # plot initialization and display
        pltTitle = str(home.date)
        ax = plt.subplot(111, polar=True)
        ax.set_title(pltTitle, va='bottom')
        ax.set_theta_offset(np.pi / 2.0)  # Top = 0 deg = north
        ax.set_theta_direction(-1)  # clockwise
        ax.xaxis.set_ticklabels(['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'])
        ax.yaxis.set_ticklabels([])  # hide radial tick labels
        ax.grid(True)
        ax.scatter(theta_plot, r_plot, c=colors, alpha=0.5, picker=True)
        ax.set_rmax(1.0)
        fig.canvas.mpl_connect('pick_event', onpick)
        fig.canvas.mpl_connect('close_event', handle_close)
        plt.pause(0.25)  # A pause is needed here, but the loop is rather slow
        fig.clf()


if __name__ == "__main__":
    print()
    print('-'*79)
    print(TITLE)
    print('-'*79)
    print()
    savedsats = processTLEdata(tleSources)
    g = getLocation()
    (latitude, longitude) = g.latlng
    elevation = geocoder.elevation(g.latlng).meters
    plotSats(savedsats=savedsats, latitude=latitude,
             longitude=longitude, elevation=elevation)
Ejemplo n.º 23
0
def test_elevation():
    g = geocoder.elevation(location)
    assert g.ok
Ejemplo n.º 24
0
import geocoder


g = geocoder.google('Mountain View, CA')
print g.latlng

g = geocoder.elevation([float(41.387953),float(2.150169)])
print (g.meters)

g = geocoder.google([45.15, -75.14], method='elevation')
print g.meters
Ejemplo n.º 25
0
def print_ephemeris():
    # set the location to report for
    with requests.Session() as session:
        requester_ip = request.access_route[0]

        if str(request.path) == '/nc' or str(request.path) == '/erikshus':
            place, latlng, elev = 'nc', [35.6921, -80.4357], 218.2
            address: str = u'On Library Park: 35\N{DEGREE SIGN} 41\' 32\"N 80\N{DEGREE SIGN} 26\' 9\"W'
        elif str(request.path) == '/gammelhus':
            place, latlng, elev = 'gammelhus', [42.1064, -76.2624], 248.7168
            address: str = u'Under the streetlamp: 42\N{DEGREE SIGN} 06\' 23\"N 76\N{DEGREE SIGN} 15\' 45\"W'
        elif str(request.path) == '/kopernik':
            place, latlng, elev = 'kopernik', [42.0020, -76.0334], 528
            address: str = u'Kopernik Observatory: 42\N{DEGREE SIGN} 0\' 7\"N 76\N{DEGREE SIGN} 2\' 0\"W'
        elif str(request.path) == '/deetop':
            place, latlng, elev = 'deetop', [41.9700, -75.6700], 284
            address: str = u'Dee-Top Observatory: 41\N{DEGREE SIGN} 58\' 12\"N 75\N{DEGREE SIGN} 40\' 12\"W'
        elif str(request.path) == '/stjohns':
            place, latlng, elev = 'stjohns', [47.5675, -52.7072], 83
            address: str = u'St. John\'s: 47\N{DEGREE SIGN} 34\' 3\"N 52\N{DEGREE SIGN} 42\' 26\"W'
        elif str(request.path) == '/greenwich':
            place, latlng, elev = 'greenwich', [51.4768, -0.0005], 47.1526
            address: str = u'Greenwich Observatory: 51\N{DEGREE SIGN} 28\' 38\"N 0\N{DEGREE SIGN} 0\' 0\"'
        else:
            if requester_ip == '127.0.0.1':
                place, latlng, elev = 'nc', [35.6921, -80.4357], 218.2
                address: str = u'On Library Park: 35\N{DEGREE SIGN} 41\' 32\"N 80\N{DEGREE SIGN} 26\' 9\"W'
            else:
                place, latlng = 'geocode', geocoder.ip(requester_ip, key=GOOGLE_API_KEY, session=session).latlng

        # Start with a discovered geocode.
        requester_geocode = geocoder.google(latlng, method='reverse', key=GOOGLE_API_KEY, session=session)

        # Use the defined address, implies we are using a static location, if undefined make it the geocoded one.
        try:
            address
        except NameError:
            address = str(requester_geocode.address)

        # Use the defined elevation, implies we are using a static location, if undefined make it the geocoded one.
        try:
            requester_geocode.elevation = lambda: None
            requester_geocode.elevation.meters = lambda: None
            setattr(requester_geocode.elevation, 'meters', elev)
        except NameError:
            requester_geocode.elevation = geocoder.elevation(latlng, key=GOOGLE_API_KEY, session=session)

        # Get the timezone
        requester_geocode.timeZoneId = geocoder.timezone(latlng, key=GOOGLE_API_KEY, session=session).timeZoneId

    # noinspection PyPep8
    return render_template('print_times.html',
                           place=place,
                           sunset_string=twilight('sunset', requester_geocode),
                           sunrise_string=twilight('sunrise', requester_geocode),
                           civil_end_string=twilight('civil_end', requester_geocode),
                           civil_begin_string=twilight('civil_begin', requester_geocode),
                           nautical_end_string=twilight('nautical_end', requester_geocode),
                           nautical_begin_string=twilight('nautical_begin', requester_geocode),
                           amateur_end_string=twilight('amateur_end', requester_geocode),
                           amateur_begin_string=twilight('amateur_begin', requester_geocode),
                           astro_end_string=twilight('astronomical_end', requester_geocode),
                           astro_begin_string=twilight('astronomical_begin', requester_geocode),
                           moonrise_string=twilight('moonrise', requester_geocode),
                           moonset_string=twilight('moonset', requester_geocode),
                           moon_phase_string=twilight('moon_phase', requester_geocode),
                           moonset_ante_astro_noon_p=twilight('moonset_ante_astro_noon_p', requester_geocode),
                           address=address,
                           latlng=latlng,
                           elevation=requester_geocode.elevation.meters,
                           ip=requester_ip)
Ejemplo n.º 26
0
satellite = ''
for i in arg1:
    if (i[0] == '-z'):
        zip_code = i[1]
    else:
        satellite = i[1]

result = get_tle(satellite)

print ''
print "Satellite Info: "+result

result = result.split('\n')
geolocator = Nominatim()
location = geolocator.geocode(zip_code + ", United States")
g = geocoder.elevation((location.latitude, location.longitude))


times = get_times(result, g.meters, location)
#print times
clear_times = []

print 'NOTE: all times are in UTC time'
print ''

#loop through each time found and check to see if visible
for t in times:
    clear = is_weather_clear(str(location.latitude),str(location.longitude), t)
    night = is_night(str(location.latitude),str(location.longitude), t)
    if(clear[0] and night):
        print 'Clear time found! --> '+t
## use geopy to find latitude and longitude
lats, longs, elevations = [], [], []

## get lat,long and elevation
for loc in df["location"].values:

    print("\n", loc)

    geolocator = Nominatim()
    #g = geocoder.google(loc)

    location = geolocator.geocode(loc)
    if location:
        lats.append(location.latitude)
        longs.append(location.longitude)
        g = geocoder.elevation([location.latitude, location.longitude],
                               key=API_KEY)
        elevation = g.meters
        elevations.append(elevation)
        print("\t", location.latitude, location.longitude)
        print("\t", elevation)
    else:
        lats.append("NaN")
        longs.append("NaN")
        elevations.append(elevation)
        print("did not find it")

df['lat'] = lats
df['long'] = longs
df['elevation'] = elevations

df.to_csv("snowfall.csv", sep=';')
        pltTitle = str(home.date)
        ax = plt.subplot(111, polar=True)
        ax.set_title(pltTitle, va='bottom')
        ax.set_theta_offset(np.pi / 2.0)  # Top = 0 deg = north
        ax.set_theta_direction(-1)  # clockwise
        ax.xaxis.set_ticklabels(['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'])
        ax.yaxis.set_ticklabels([])  # hide radial tick labels
        ax.grid(True)
        ax.scatter(theta_plot, r_plot, c=colors, alpha=0.5, picker=True)
        ax.set_rmax(1.0)
        fig.canvas.mpl_connect('pick_event', onpick)
        fig.canvas.mpl_connect('close_event', handle_close)
        plt.pause(0.25)  # A pause is needed here, but the loop is rather slow
        fig.clf()


if __name__ == "__main__":
    print()
    print('-' * 79)
    print(TITLE)
    print('-' * 79)
    print()
    savedsats = processTLEdata(tleSources)
    g = getLocation()
    (latitude, longitude) = g.latlng
    elevation = geocoder.elevation(g.latlng).meters
    plotSats(savedsats=savedsats,
             latitude=latitude,
             longitude=longitude,
             elevation=elevation)
Ejemplo n.º 29
0
def test_elevation():
    g = geocoder.elevation(location)
    assert g.ok
Ejemplo n.º 30
0
# -*- coding: utf-8 -*-
"""
Created on Thu May  9 11:33:24 2019

@author: nicho
"""

import geocoder

g = geocoder.elevation()
topo_points = []
topo_points.append(g)
    def get_location(self, given_location=None):
        ''' Get user location based on input '''
        print('Location input examples:')
        print('    "San Francisco, CA, USA"')
        print('    "37.7749295,  -122.4194155,  15.60"')
        print('    "37:46:29.7N, -122:25:09.9E, 15.60"')
        print('')
        input_function = input
        self.home = ephem.Observer()
        coords = None
        while True:
            if given_location:
                print('Given location: "{}"'.format(given_location))
                location_keyword = given_location
                given_location = None
            else:
                location_keyword = input_function(
                    'Enter location ["{}"]: '.format(self.default_location))
            if not location_keyword or location_keyword.isspace():
                location_keyword = self.default_location
            coords = self._parse_coords(location_keyword)
            if coords:
                self.location = location_keyword
                (self.latitude, self.longitude, self.elevation) = coords
                self.elevation = float(self.elevation)
                self.friendly_location = "coordinates"
                break
            else:
                gloc = geocoder.google(location_keyword, key=SECRET_API_KEY)
                print(location_keyword, gloc.status)
                if gloc.status != 'OK':
                    print('Location not found: "{}"'.format(location_keyword))
                    location_keyword = None
                else:
                    self.location = gloc.address
                    if SECRET_API_KEY == '':
                        print(
                            "No API key - using default elevation {}m".format(
                                DEFAULT_ELEVATION))
                        self.elevation = DEFAULT_ELEVATION
                    else:
                        for _ in range(MAX_RETRIES):
                            self.elevation = geocoder.elevation(
                                gloc.latlng, key=SECRET_API_KEY).meters
                            if self.elevation is not None:
                                break
                            time.sleep(RETRY_DELAY)
                        else:
                            print(
                                "Retries exceeded - using default elevation {}m"
                                .format(DEFAULT_ELEVATION))
                            self.elevation = DEFAULT_ELEVATION

                    (self.latitude, self.longitude) = gloc.latlng
                    # (self.latitude, self.longitude) = (gloc.lat, gloc.lng)
                    self.friendly_location = u"{}".format(self.location)
                    # print(gloc.json)
                    print()
                    break
        self.config['main']['default_location'] = self.location
        print(self.elevation)
        self.home.elevation = self.elevation  # meters
        self.home.lat = str(self.latitude)  # +N
        self.home.lon = str(self.longitude)  # +E
        print("Found: {}N, {}E, {:0.2f}m".format(self.home.lat, self.home.lon,
                                                 self.home.elevation))
        self.friendly_location = "{} ({:4.7f}N, {:4.7f}E) {:0.2f}m".format(
            self.friendly_location, self.home.lat / ephem.degree,
            self.home.lon / ephem.degree, self.home.elevation)
        print("Location: {}".format(self.friendly_location))
        print()