Esempio n. 1
1
    def getLocationOfLargestPublishedMessages(self):
        res = self.collection.aggregate([{'$group': {'_id': {'lat': "$geo_lat", 'long': "$geo_lng"},
                                        'count': {'$sum': 1}}},
                                        {'$sort': {'count': -1}},
                                        {'$limit': 1}],
                                        allowDiskUse=True)
        dct = {}
        lat = ''
        lng = ''
        count = 0
        valid = False
        for doc in res:
            lat = doc['_id']['lat']
            lng = doc['_id']['long']
            count = int(doc['count'])
            valid = True

        if valid:
            geolocator = Nominatim()
            location = geolocator.reverse('{}, {}'.format(lat, lng))
            dct['loc'] = [lat, lng]
            dct['address'] = location.address
            dct['count'] = count
            return dct

        return None
Esempio n. 2
0
def set_description(sender, instance, created=False, **kwargs):
    if created:
        try:
            geolocator = Nominatim()
            lat = instance.location.latitude
            lon = instance.location.longitude
            print('.....{}, {}........'.format(lat, lon))
            loc = geolocator.reverse([lat, lon])
            address = loc.address
            print(address)
            instance.description = address
            instance.save()
        except:
            try:
                geolocator = Nominatim()
                lat = instance.location.latitude
                lon = instance.location.longitude
                print('..........{}, {}........'.format(lat, lon))
                loc = geolocator.reverse([lat, lon])
                address = loc.address
                print(address)
                instance.description = address
                instance.save()
            except:
                print('......didnt work.......')
                instance.description = 'Location created on {}'.format(instance.date_added)
                instance.save()
                pass
Esempio n. 3
0
    def post(self, request):
        post_identifier = request.POST.get('post_identifier')
        if post_identifier == 'create':
            form = CatastropheForm(request.POST)
            if not form.is_valid():
                return self.get(request, create_new_form=form)
            catastrophe = form.save(commit=False)
            geolocator = Nominatim()
            location = geolocator.reverse(str(catastrophe.location_x) + " , " + str(catastrophe.location_y))
            if location.address is not None:
                catastrophe.locationString = location.address
            else :
                form.add_error('radius', 'Wähle eine gültige Position')
                return self.get(request, create_new_form=form)
            catastrophe.save()

            ngos = form.cleaned_data['ngos']
            governments = form.cleaned_data['governments']
            for ngo in ngos:
                catastrophe.ngos.add(ngo)
            for government in governments:
                catastrophe.governments.add(government)      

        if post_identifier == 'delete':
            catastrophe = get_object_or_404(Catastrophe, id = request.POST.get('catastrophe_id'))
            catastrophe.delete()

        return HttpResponseRedirect(reverse('dcp:CatastropheOverview'))
Esempio n. 4
0
def moveToSubfolders(path):
	p = re.compile('.*_(\d{8})_.*')
	filesForDate = {}
	for subdir, dirs, files in os.walk(path):
		for file in files:
			filepath = subdir + os.sep + file
			m = p.match(filepath)
			if not m is None:
				date = m.group(1)
				if not date in filesForDate:
					filesForDate[date] = []
				filesForDate[date].append(filepath)

	geolocator = Nominatim()
	for date, files in filesForDate.iteritems():
		if len(files) > 5:
			movedir = os.path.join(os.path.dirname(files[0]), date)
			location = None
			if not os.path.exists(movedir):
				os.makedirs(movedir)
			for f in files:
				exif_data = get_exif_data(f)
				if not exif_data is None:
					lat, lon = get_lat_lon(exif_data)
					if not lat is None and not lon is None:
						location = geolocator.reverse("{},{}".format(lat, lon))
			if not location is None:
				print(location.address)
Esempio n. 5
0
def postprocess_move(move):
    gps_samples = [sample for sample in move.samples if sample.sample_type and sample.sample_type.startswith('gps-')]

    if gps_samples:
        gps_center = calculate_gps_center(gps_samples)
        move.gps_center_latitude = gps_center[0]
        move.gps_center_longitude = gps_center[1]

        gps_center_degrees = [radian_to_degree(x) for x in gps_center]

        gps_center_max_distance = 0
        for sample in gps_samples:
            point = (sample.latitude, sample.longitude)
            point_degrees = [radian_to_degree(x) for x in point]
            distance = vincenty(gps_center_degrees, point_degrees).meters
            gps_center_max_distance = max(gps_center_max_distance, distance)

        move.gps_center_max_distance = gps_center_max_distance

        first_sample = gps_samples[0]
        latitude = first_sample.latitude
        longitude = first_sample.longitude

        geolocator = Nominatim()
        location = geolocator.reverse("%f, %f" % (radian_to_degree(latitude), radian_to_degree(longitude)), timeout=60)
        move.location_address = location.address
        move.location_raw = location.raw
Esempio n. 6
0
def retrieve_location_from_coordinates(points):
	"""Given a list of coordinates, uses the geocoder and finds the corresponding
	locations and returns them in a dictionary. It requires internet connection
	to retrieve the addresses from openstreetmap.org. The geolocation service provider
	can be changed according to geopy documentation.
	
	Args:
		points (iterable): an iterable (e.g. a list) of tuples in the form of (lat, lon) where coordinates are of type float e.g. [(1.2343, -10.239834r),(5.634534, -12.47563)]
	Returns:
		co_loc (dict): a dictionary of the form point: location where location is itself a dictionary including the corresponding address of the point.
	
	"""
	from geopy.geocoders import Nominatim
	geocoder = Nominatim(timeout=10)
	coordinate_location = {}
	
	for coordinate in points:
		try:
			location = geocoder.reverse(coordinate)
		except:
			location = 'unknown'
		coordinate_location[coordinate] = location
	co_loc = {k:v.raw for k,v in coordinate_location.iteritems()}
	
	return co_loc
def distance_handler(location_obj):
    geolocator = Nominatim()
    input_data = str(location_obj['latitude']) + ", " + str(location_obj['longitude'])
    log("[Input location str:" + input_data)
    location = geolocator.reverse(input_data)
    adres = location.address
    log("\nLOCATION ADRESS:" + adres)
    # log("\nLOCATION RAW:"+location.address)

    POINT_A = (SYRKOMLI['latitude'], SYRKOMLI['longitude'])
    POINT_B = (location_obj['latitude'], location_obj['longitude'])

    vincentkm = (vincenty(POINT_A, POINT_B).kilometers)
    circlekm = (great_circle(POINT_A, POINT_B).kilometers)
    log("VINCENT KM:" + str(vincentkm))
    log("CIRCLE KM:")
    log(circlekm)

    vincent = (vincenty(POINT_A, POINT_B).meters)
    circle = (great_circle(POINT_A, POINT_B).meters)
    log("\nVINCENT meters:" + str(vincent))
    log("CIRCLE meters:")
    log(circle)
    return {
        'vincent': vincent,
        'circle': circle,
        'adres': adres
    }
Esempio n. 8
0
def geocode_item(**kwargs):
    """
    Geocode address or reverse geocode coordinate pair using OSM's Nominatim geocoder.
    :param kwargs: Dictionary of key/value pairs which vary depending on specific requirements. To geocode an address
    you need to supply an "address" item. To reverse geocode a coordinate pair, you need to supply a lat/lon pair and
    have the "reverse" item set to true. In either case, the coordinate pair must be in WGS84.
    :return: A dictionary of result key/value pairs
    """

    try:
        import geopy
        from geopy.geocoders import Nominatim
    except ImportError as e:
        print(e)
        return None

    try:
        geolocator = Nominatim(timeout=10)
        if (not kwargs["reverse"]) and kwargs["address"]:
            location = geolocator.geocode(kwargs["address"])
            if hasattr(location, 'raw'):
                return location.raw
            else:
                return None
        elif (kwargs["reverse"]) and (kwargs["lon"] and kwargs["lat"]):
            location = geolocator.reverse((kwargs["lat"], kwargs["lon"]))
            if hasattr(location, 'raw'):
                return location.raw
            else:
                return None
        else:
            return None
    except geopy.exc.GeopyError as e:
        print(str(e))
        return None
Esempio n. 9
0
def add_location(request):
	user = request.POST.get('user')
	action = request.POST.get('action')
	lattitude = request.POST.get('lattitude')
	longitude = request.POST.get('longitude')
	string = ""
	string += lattitude
	string += ", " + longitude
	if user and action:
		if lattitude and longitude:
			try:
				profile = Profile.objects.get(id=user)
				geolocator = Nominatim()
				spot = geolocator.reverse(string)
				if action == 'Get Location':
					profile.location = spot
					profile.save()
				else:
					profile.location = spot
					profile.save()
					return JsonResponse({'status' : 'ko'})
				return JsonResponse({'status' : 'ok'})
			except:
				pass
		else:
			messages.error(request, 'Your brouser might not have navigation support! Please upgrade..')
	return JsonResponse({'status' : 'ko'})
def getTrackCountryOfOrigin(billboard_df_final):
    geolocator = Nominatim()
    track_state_of_origin = []
    track_country_of_origin = []
    for index_artist, row in billboard_df_final.iterrows():
        if (not pd.isnull(row['latitude'])) & (not pd.isnull(row['longitude'])):
            try:
                location = geolocator.reverse(str(row['latitude']) +',' + str(row['longitude']), language='en')
                state = location.raw['address']['state']
                if state == "Puerto Rico":
                    country = "Puerto Rico"
                else:
                    country = location.raw['address']['country']
                    if country == "The Netherlands":
                        country = "Netherlands"
            except:
                print row["Artist(s)"]
                country = "" 
                state = ""
        else:
            country = ""
            state = ""
        
        track_country_of_origin.append(country)
        if country == "United States of America":
            track_state_of_origin.append(state)
        else:
            track_state_of_origin.append("")

    return [track_country_of_origin, track_state_of_origin]
def distance_handler(location_obj):
    log("[[DISTAMCE HANDLER START]]")
    geolocator = Nominatim()
    input_data = str(location_obj['latitude']) + ", " + str(location_obj['longitude'])
    log("[Input location str:" + input_data)
    location = geolocator.reverse(input_data)
    adres = location.address
    try:
        log("LOCATION ADRESS:" + adres)
    except Exception:
        try:
            log("LOCATION ADRESS:" + adres.encode("utf8"))
        except:
            pass
    # log("\nLOCATION RAW:"+location.address)
    POINT_A = (SOURCE_LOCATION_CORDINANTES['latitude'], SOURCE_LOCATION_CORDINANTES['longitude'])
    POINT_B = (location_obj['latitude'], location_obj['longitude'])

    vincentkm = (vincenty(POINT_A, POINT_B).kilometers)
    circlekm = (great_circle(POINT_A, POINT_B).kilometers)
    log("VINCENT KM:" + str(vincentkm))
    log("CIRCLE KM:")
    log(circlekm)

    vincent = (vincenty(POINT_A, POINT_B).meters)
    circle = (great_circle(POINT_A, POINT_B).meters)
    log("\nVINCENT meters:" + str(vincent))
    log("CIRCLE meters:")
    log(circle)
    log("[[DISTAMCE HANDLER END BEFORE RETURN]]")
    return {
        'vincent': vincent,
        'circle': circle,
        'adres': adres
    }
Esempio n. 12
0
	def getGeo(self, lon, lat):
		if self.geocoderCache:
			try:
				nodeObj = self.db.get(GeoAssign,{'lat' : lat, 'lon' : lon})
				nodeObj['cached'] = True
				return nodeObj
			except GeoAssign.DoesNotExist:
				pass
		if self.printStatus:
			print('lon: '+str(lon)+', lat: '+str(lat)+' not in cache - start lookup at Nominatim-API')
		geolocator = Nominatim()
		location = geolocator.reverse([lat, lon], timeout=20)
		if 'address' in location.raw:
			location = location.raw['address']
			nodeObj = GeoAssign({
				'lat' : lat,
				'lon' : lon,
				'payload' : location
				})
			self.db.save(nodeObj)
			self.db.commit()
			nodeObj['cached'] = False
			return nodeObj
		else:
			# got no results (i.e. coordinates are incorrect)
			return None
Esempio n. 13
0
def get_adr(gps):
    geolocator = Nominatim()
    try:
        location = geolocator.reverse(gps, timeout=5)
    except:
        return None
    return location.address
Esempio n. 14
0
 def address (self):
     geolocator = Nominatim()
     address = geolocator.reverse("%lf, %lf" % (self.location.y, self.location.x))
     if (address != None):
         return ((address))
     else:
         return ("")
Esempio n. 15
0
def get_address(location):
    geolocator = Nominatim()    
    latitude, longitude = location
    address = ""
    if latitude is not None:
        address = geolocator.reverse(latitude, longitude).address
    return address
def update_node_postcode():

    # Some nodes that have house number don't have post code. Since we want all nodes which have house number to have "street address",
    # "city", "state", "country", and "post code", we have to fill in "post code". By using "geopy" library to optain address from "geocode" API
    # knowing latitude and longitude, we can extract post code from there.

    import pymongo
    import unicodedata
    from pymongo import MongoClient
    from bson.objectid import ObjectId
    from geopy.geocoders import Nominatim

    client = MongoClient()
    db = client.examples
    geolocator = Nominatim()
    result = db.osm.test.find(
        {
            "lat": {"$exists": True},
            "lon": {"$exists": True},
            "tag.addr:housenumber": {"$exists": True},
            "tag.addr:postcode": {"$exists": False},
            "$or": [{"tag.addr:street": {"$exists": True}}, {"tag.addr:city": {"$exists": True}}],
        }
    )
    for doc in result:
        i = doc["_id"]
        pos = doc["lat"] + "," + doc["lon"]
        location = geolocator.reverse(pos, timeout=10)
        postcode = location.address.split(",")[-2].strip(" ")
        db.osm.test.update({"_id": i}, {"$set": {"tag.addr:postcode": postcode}})
Esempio n. 17
0
def location(word):
    word_m = re.sub(r"(\w)([A-Z])", r"\1 \2", word) #to break words like NewYork = New York
    geolocator = Nominatim(country_bias='USA', timeout=4)
    geocoded = geolocator.geocode(word_m, exactly_one=True)
    locat = ""
    lat = 0
    lon = 0
    actual = word_m
    if geocoded is not None:
        # success
        lat = geocoded.latitude
        lon = geocoded.longitude
        print("Coordinates Fetched: ",lat,lon)
        print("Checking whether the coordinates lie in NY\n\n")
        if(lat<up[0] and lat>low[0] and lon<up[1] and lon>low[1]):
            locat = "New York"
            tempadd = geolocator.reverse(str(lat)+","+str(lon)).address
            print("Actual Location: ",tempadd)
            tempadd = tempadd.split(",")
            for i in tempadd:
                m = len(word_m)
                n = len(i)
                if (subseq.isSubSequence(word_m, i, m, n) and len(tempadd)>5):
                    actual = i
        else:
            locat = "Not in New York"
        location = True
    else:
        # Here, perhaps you can run the geocoding function again,
        # after cleaning your input a little bit
        # Then you have recursion until you get a result!
        location = False
    return [location,locat,actual]
Esempio n. 18
0
    def post(self):
        parameter_data = request.get_json(force=True)
        if parameter_data.get('cityname') and parameter_data.get('lat') and parameter_data.get('lng'):
            #do city stuff
            main_response = []
            dest_city_name = parameter_data.get('cityname');
            city_lat = parameter_data.get('lat')
            city_lng = parameter_data.get('lng')
            
            g = geocoder.google(parameter_data.get('cityname'))
            
            current_city = [city_lat, city_lng]
            
            destination_city = g.latlng
            
            distance = vincenty(current_city, destination_city).miles
            geolocator = Nominatim()
            location = geolocator.reverse([city_lat, city_lng])
            
            response_data = {}
            if(distance < 100):
                #do uber
                url = 'https://api.uber.com/v1/estimates/price'
                parameters = {
                'server_token': 'KwHygnd9UVQOSWT8qJB9fhm2OoStOILx4IrBH_I6',
                'start_latitude': city_lat,
                'start_longitude': city_lng,
                'end_latitude': g.latlng[0],
                'end_longitude': g.latlng[1]
                }
                response = requests.get(url, params=parameters)
                data = response.json()
                response_data['uber'] = data
                print(response_data['uber'])
                #print(data);
            else:
                #do flights
                ###
                #base_url = "http://www.hitlistapp.com/flights/cheap-flights-from-"
                #split_dest_name = str(dest_city_name).lower().split(",")
                #url = (base_url+'philadelphia-PHL'+'-to-'+split_dest_name[0].replace(' ', '-')+'-'+split_dest_name[1]+'/')
                #print(url);
                
                #hdr = {'User-Agent': 'Mozilla/5.0'}
                #req = urllib2.Request(url,headers=hdr)
                #page = urllib2.urlopen(req)
                #soup = BeautifulSoup(urllib2.urlopen(url).read(), 'lxml')
                #soup = BeautifulSoup(page, 'lxml')
                #if(soup):
                 #   for div in soup.select('div#flightslist'):
                  #      for href in div.find('a'):
                   #         response_data['flight'] = [href]
               # param_start, param_end
                cities = { "Paris,France": "629",  "Agra,India": "1415", "Rome,Italy": "661", "Karachi,Pakistan": "1349", "Bangkok,Thailand": "958", "Catalonia,Spain": "634", "Guangzhou,China": "829", "Lagos,Nigeria": "849",  "Tokyo,Japan": "829", "Mumbai,India": "940", "Instanbul,Turkey": "876", "Dhaka,Bangladesh" : "1137", "Lahore,Pakistan": "1089", "Lima,Peru": "653", "MexicoCity,Mexico": "322"}

                for city in cities:
                    print(city)
        else:
             return make_response(jsonify( { 'error': 'error' } ), 400)
Esempio n. 19
0
 def geocoderGeoPyLatLon(self, lat, lon):
     geolocator = Nominatim()
     if lat and lon:
         coord = "%s, %s" % (lat, lon)
         location = geolocator.reverse(coord)
         if location.raw:
             return location.raw['address']
         return None
Esempio n. 20
0
def geopy_get_address_by_coordinates_using_nominatum(coordinates):
    from geopy.geocoders import Nominatim
    geolocator = Nominatim()
    address = geolocator.reverse(coordinates)
    print 'address: ', address.address
    print 'coordinates:', address.latitude, address.longitude
    print 'location raw:', address.raw
    return address
Esempio n. 21
0
def get_country_code(latitude, longitude):
    geolocator = Nominatim()
    location = geolocator.reverse("%f, %f" % (latitude, longitude))
    try:
        country_code = location.raw["address"]["country_code"]
    except KeyError:
        country_code = None
    return country_code
Esempio n. 22
0
def genAddrByGeo(datafile, outputfile):
    f = open(datafile, 'rb')
    reader = csv.reader(f)
    reader.next() # Skip the header. If no header, please comment out this line.

    outfile = open(outputfile, 'w')
    wr = csv.writer(outfile, quoting=csv.QUOTE_ALL)

    headers = ['id', 'country', 'country_code' ]
    errorcounter = 0
    mode = 0
    cc = countries.CountryChecker('TM_WORLD_BORDERS-0.3')
    
    for row in reader:
        geoLatitude = float(row[0])
        geoLongitude = float(row[1])
        country = ''
        country_code = ''

        if (len( geoLatitude ) != 0) and (len( geoLongitude ) != 0 ):
            breakmark = 0
            while breakmark == 0:
                if errorcounter == 10000 :
                    errorcounter = 0
                    break
                try:
                    # Check the offline method first.
                    if mode == 0:
                        country = cc.getCountry(countries.Point(geoLatitude, geoLongitude)).shape.GetField('NAME')
                        country_code = cc.getCountry(countries.Point(geoLatitude, geoLongitude)).iso
                    else:
                        if mode == 2:
                            geolocator = Nominatim()
                            location = geolocator.reverse(str(geoLatitude) + ", " + str(geoLongitude))
                            d = location.raw
                            country = d['address']['country'].encode('utf-8').strip()
                            country_code = d['address']['country_code'].encode('utf-8').strip()
                            mode = 0
                        else:
                            info = get_level1_locations(geoLatitude, geoLongitude)
                            country = info[0]
                            country_code = info[1]
                            mode = 0
                    querycount = querycount + 1
                    breakmark = 1
                    errorcounter = 0
                except Exception as e:
                    errorcounter = errorcounter + 1
                    mode = mode + 1
                    if mode == 3:
                        mode = 1
                    continue
        newrow = [ country, country_code ]
        wr.writerow(newrow)
    print('Done!')
    f.close()
    outfile.close()
def locate_country_code(latitude, longitude):
    geo_locator = Nominatim()
    location = geo_locator.reverse(query=(latitude, longitude), language='en', timeout=120)
    if location != None:
        try:
            # print location
            return location.raw['address']['country_code']
        except:
            return False
Esempio n. 24
0
def get_postcode(longitude, latitude):
        try:
            geolocator = Nominatim()
            location = geolocator.reverse("{0}, {1}".format(latitude, longitude))
            postal_code = location.raw.get('address', ).get('postcode')
        except (AttributeError, GeocoderServiceError, GeocoderUnavailable):
            postal_code = ''
        else: 
            postal_code = postal_code
Esempio n. 25
0
    def generate_fake(count=20, center_lat=39.951021, center_long=-75.197243):
        """Generate a number of fake resources for testing."""
        from sqlalchemy.exc import IntegrityError
        from random import randint
        from faker import Faker
        from geopy.geocoders import Nominatim

        geolocater = Nominatim()
        fake = Faker()

        num_options = 5
        options = []

        for i in range(num_options):
            options.append(Descriptor(
                name=fake.word(),
                values=['True', 'False'],
                is_searchable=fake.boolean()
            ))

        for i in range(count):

            # Generates random coordinates around Philadelphia.
            latitude = str(fake.geo_coordinate(
                center=center_lat,
                radius=0.01
            ))
            longitude = str(fake.geo_coordinate(
                center=center_long,
                radius=0.01
            ))

            location = geolocater.reverse(latitude + ', ' + longitude)
            resource = Resource(
                name=fake.name(),
                address=location.address,
                latitude=latitude,
                longitude=longitude
            )

            oa = OptionAssociation(option=randint(0, 1))
            oa.descriptor = options[randint(0, num_options - 1)]
            resource.option_descriptors.append(oa)

            ta = TextAssociation(text=fake.sentence(nb_words=10))
            ta.descriptor = Descriptor(
                name=fake.word(),
                values=[],
                is_searchable=fake.boolean()
            )
            resource.text_descriptors.append(ta)

            db.session.add(resource)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
Esempio n. 26
0
def get_country_code(latitude, longitude):
    geolocator = Nominatim()
    try:
        location = geolocator.reverse("{}, {}".format(latitude, longitude))
        country_code = location.raw['address']['country_code']
    except KeyError:
        country_code = None
    except GeopyError:
        country_code = None
    return country_code
Esempio n. 27
0
def get_address_from_coordinates(latitude, longitude):
    """ Get a street address from coordinates

    :param latitude: Starting latitude
    :param longitude: Starting longitude
    :return:
    """
    geolocator = Nominatim()
    location = geolocator.reverse("%.2f, %.2f" % (float(latitude), float(longitude)))
    return location.address
Esempio n. 28
0
def get_geolocation_for_ip(ip):
    lat, lon = get_lat_lon(ip)
    if not (float(lat)) and not (float(lon)):
        my_ip = requests.get(MY_IP_URL).content.decode("UTF-8")
        lat, lon = get_lat_lon(my_ip)

    geolocator = Nominatim()
    location = geolocator.reverse("%s, %s" % (lat, lon))

    return location.address
Esempio n. 29
0
def fetchPlaceName(place):
    geolocator = Nominatim()
    try:
        location = geolocator.reverse((str(place[0])+','+str(place[1])), language='en')
        address = location.address
    except GeocoderTimedOut:
        address = "Not Found"
        pass

    print address
    return address
Esempio n. 30
0
def find_poi(api, lat, lng, slack_client):
    poi = {'pokemons': {}, 'forts': []}
    coords = coords_square(lat, lng)
    for coord in coords:
        lat = coord['lat']
        lng = coord['lng']
        api.set_position(lat, lng, 0)

        from geopy.geocoders import Nominatim
        geolocator = Nominatim()
        cell_ids = get_cell_ids(lat, lng)
        timestamps = [0,] * len(cell_ids)
        response_dict = api.get_map_objects(latitude = util.f2i(lat), longitude = util.f2i(lng), since_timestamp_ms = timestamps, cell_id = cell_ids)
        if (response_dict['responses']):
            if 'status' in response_dict['responses']['GET_MAP_OBJECTS']:
                if response_dict['responses']['GET_MAP_OBJECTS']['status'] == 1:
                    for map_cell in response_dict['responses']['GET_MAP_OBJECTS']['map_cells']:
                        if 'wild_pokemons' in map_cell:
                            for pokemon in map_cell['wild_pokemons']:
                                pokekey = get_key_from_pokemon(pokemon)
                                pokemon['name'] = pokemonNames[pokemon['pokemon_data']['pokemon_id']-1]
                                pokemon['hides_at'] = (pokemon['time_till_hidden_ms']/1000)/60
                                if pokemon['name'] not in unwantedPokemon and pokemon['hides_at'] >= MINIMUM_ALERT_TIME_LB and pokemon['hides_at'] <= MINIMUM_ALERT_TIME_UB:
                                    address = geolocator.reverse(repr(pokemon['latitude'])+", "+repr(pokemon['longitude'])).address
                                    sep = ', Financial District'
                                    rest = address.split(sep, 1)[0]
                                    pokemon['location'] = rest  
                                    poi['pokemons'][pokekey] = pokemon
                                    for user in specialAlertUser:
                                        pokemonList = specialAlertUser[user]
                                        searchFor =  pokemon['name'].lower()
                                        if searchFor in  pokemonList:
                                            text = searchFor+" at "+pokemon['location']+"hidinf for"+"{0:.2f}".format(pokemon['hides_at']) + " minutes \n"
                                            slack_client.api_call(
                                                                "chat.postMessage", channel=user, text=text,
                                                                username='******', as_user = True
                                                        )
                                    
                                    
                    
            time.sleep(0.7)
    textSlack = ""
    for pokemon in poi['pokemons']:
        print(pokemon)
        textSlack += poi['pokemons'][pokemon]['name']+" at " + poi['pokemons'][pokemon]['location']+ " hiding for "+"{0:.2f}".format(poi['pokemons'][pokemon]['hides_at']) + " minutes \n"
            
            
    slack_client.api_call(
                "chat.postMessage", channel="#pokemon", text=textSlack,
                username='******', as_user = True
        )
    print('POI dictionary: \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(poi)))
    print('Open this in a browser to see the path the spiral search took:')
    print_gmaps_dbug(coords)
Esempio n. 31
0
        def update_lightning_strikes():
            """Get the latest data from FMI and update the states."""

            loc_time_list = []
            home_cords = (self.latitude, self.longitude)

            start_time = datetime.today() - timedelta(days=7)

            ## Format datetime to string accepted as path parameter in REST
            start_time = str(start_time).split(".")[0]
            start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
            start_time = "starttime=" + str(start_time.date()) + "T" + str(
                start_time.time()) + "Z"

            base_url = BASE_URL + start_time + "&"

            ## Fetch data
            response = requests.get(base_url)

            root = ET.fromstring(response.content)

            for child in root.iter():
                if child.tag.find("positions") > 0:
                    clean_text = child.text.lstrip()
                    val_list = clean_text.split("\n")
                    for loc_indx, val in enumerate(val_list):
                        if val != "":
                            val_split = val.split(" ")
                            lightning_coords = (float(val_split[0]),
                                                float(val_split[1]))
                            distance = 0
                            try:
                                distance = geodesic(lightning_coords,
                                                    home_cords).km
                            except:
                                _LOGGER.info(
                                    f"Unable to find distance between {lightning_coords} and {home_cords}"
                                )
                            add_tuple = (val_split[0], val_split[1],
                                         val_split[2], distance, loc_indx)
                            loc_time_list.append(add_tuple)

                elif child.tag.find("doubleOrNilReasonTupleList") > 0:
                    clean_text = child.text.lstrip()
                    val_list = clean_text.split("\n")
                    for indx, val in enumerate(val_list):
                        if val != "":
                            val_split = val.split(" ")
                            exist_tuple = loc_time_list[indx]
                            if indx == exist_tuple[4]:
                                add_tuple = (exist_tuple[0], exist_tuple[1],
                                             exist_tuple[2], exist_tuple[3],
                                             val_split[0], val_split[1],
                                             val_split[2], val_split[3])
                                loc_time_list[indx] = add_tuple
                            else:
                                print("Record mismtach - aborting query!")
                                break

            ## First sort for closes entries and filter to limit
            loc_time_list = sorted(loc_time_list,
                                   key=(lambda item: item[3]))  ## distance
            loc_time_list = loc_time_list[:LIGHTNING_LIMIT]

            ## Second Sort based on date
            loc_time_list = sorted(loc_time_list,
                                   key=(lambda item: item[2]),
                                   reverse=True)  ## date

            geolocator = Nominatim(user_agent="fmi_hassio_sensor")
            ## Reverse geocoding
            op_tuples = []
            for indx, v in enumerate(loc_time_list):
                loc = str(v[0]) + ", " + str(v[1])
                loc_time = time.strftime('%Y-%m-%d %H:%M:%S',
                                         time.localtime(int(v[2])))
                try:
                    location = geolocator.reverse(loc, language="en").address
                except:
                    _LOGGER.info(
                        f"Unable to reverse geocode for address-{loc}")
                    location = loc

                ## Time, Location, Distance, Strikes, Peak Current, Cloud Cover, Ellipse Major
                op = FMILightningStruct(time_val=loc_time,
                                        location=location,
                                        distance=v[3],
                                        strikes=v[4],
                                        peak_current=v[5],
                                        cloud_cover=v[6],
                                        ellipse_major=v[7])
                op_tuples.append(op)

            self.lightning_data = op_tuples

            return
Esempio n. 32
0
def send_link(item: dict, bot, chat_id: int) -> None:
    geolocator = Nominatim(user_agent='RoyalSwingClub')
    location = geolocator.reverse('{}, {}'.format(item['lat'], item['lon']))
    address = location.raw['address']

    link = None
    for branch in LINKS:
        if address['city'] in branch['cities'] and branch['state'] in address[
                'state']:
            group_chat_id = branch['chat_id']
            about_chat = bot.get_chat(group_chat_id)
            if not about_chat.invite_link:
                link = bot.export_chat_invite_link(group_chat_id)
            else:
                link = about_chat.invite_link
            break

    text = ('Итак, подведем итоги:\n\n'
            '\t- Вы знакомитесь как: {}\n'
            '\t- Ваш контактный номер телефона: +{}\n'
            '\t- Вы находитесь: {}, {}, {}\n')
    if item['sex'] == 'C':
        text += (
            # '\t- Вы представились как: {} и {}\n'
            '\t- Ваши дни рождения: {} и {}\n\n')
        text = text.format(
            'Пара',
            item['phone'],
            location.raw['address']['country'],
            location.raw['address']['state'],
            location.raw['address']['city'],
            # item['real_name'],
            # item['real_name_second'],
            item['birthday'],
            item['birthday_second'],
        )
    else:
        text += (
            # '\t- Вы представились как: {}\n'
            '\t- Ваш день рождения: {}\n\n')
        text = text.format(
            ('Парень' if item['sex'] == 'M' else 'Девушка'),
            item['phone'],
            location.raw['address']['country'],
            location.raw['address']['state'],
            location.raw['address']['city'],
            # item['real_name'],
            item['birthday'])

    keyboard = [
        [
            telegram.InlineKeyboardButton('Общий чат Royal Swing',
                                          url='https://t.me/royal_swing_chat')
        ],
        [
            telegram.InlineKeyboardButton('Официальный канал Royal Swing',
                                          url='https://t.me/royal_swing')
        ],
    ]

    if link:
        text += 'Если все правильно, тогда кликай по кнопке и присоединяйся к закрытому чату.\n\n' \
                'А если требуется пригласить друга, подругу или пару, кидай им ссылку на меня: ' \
                'https://t.me/royalswingmasterbot\n\nА если нужно исправить какие-то данные, используй комманды, ' \
                'начни набирать в сообщении: `/`, появится список комманда с подсказками.'
        keyboard.insert(0, [
            telegram.InlineKeyboardButton('Иркутский чат Royal Swing',
                                          url=link)
        ])
    else:
        text += (
            'К сожалению пока в вашем городе нет специального чата, но вы можете присоедениться к общему чату, '
            'в котором не менее интересно. А также читайте наш канал, может оказаться, '
            'что ваш город скоро появиться в списке. Сейчас у нас только:\n\n'
            '\t- Иркутск (Ангарск, Шелехов, Усолье-Сибирское)\n\n'
            'ВНИМАНИЕ! Мы предоставляем франшизу по тем города, которых нет в списке выше, '
            'напишите администратору @ArsenySokolov, если вы хотите проводить вечеринки в вашем городе.\n\n'
            'Также администратору можно написать, если вы из другого города, '
            'но тем не менее собираетесь приходить на вечеринки не в вашем городе.'
        )

    reply_markup = telegram.InlineKeyboardMarkup(keyboard)
    bot.sendMessage(chat_id,
                    text,
                    reply_markup=reply_markup,
                    parse_mode=ParseMode.MARKDOWN,
                    disable_web_page_preview=True)
Esempio n. 33
0
            text = TextBlob(string)
            if text.sentiment.polarity < 0:
                sentiment = "negative"
            elif text.sentiment.polarity == 0:
                sentiment = "neutral"
            else:
                sentiment = "positive"
            #print 'Sentiment: ', sentiment
            #question
            if '?' in string:
                question = 'Yes'
            else:
                question = 'No'
            #print 'Question: ', question
            #location
            place = geolocator.reverse(str(tweet.coordinates))
            #print 'Place: ', place
            latitude = tweet.coordinates[0]
            longitude = tweet.coordinates[1]
            #print 'Coords:', tweet.coordinates
            #print "==========="

            #save tweets
            saveThis = str(moment) + '; ' + str(
                string.encode('ascii', 'ignore')
            ) + '; ' + user + '; ' + sentiment + '; ' + question + '; ' + place + '; ' + str(
                latitude) + '; ' + str(longitude) + '; '
            saveFile.write(saveThis + '\n')
            print "---Tweet saved---"

        if tweet.place:
Esempio n. 34
0
    df = df.drop_duplicates(subset=['email'])
    
    return df

df_clean = clean_people(people)
df_clean.inactive = df_clean.last_seen < datetime.datetime(2018,10,23)
df_clean = df_clean.dropna(subset=['phone'])
df_clean.phone=df_clean.phone.str.replace('.','')
df_clean = df_clean[df_clean.phone.str.contains('[0-9]{10}')]
df_clean['phoneStarting06/07']=df_clean.phone.str[:2].isin(["06","07"])
#Marche pas cette librairie

from geopy.geocoders import Nominatim
import time
geolocator = Nominatim(user_agent="Cours 23/10")
location = geolocator.reverse("49.6326, 18.2841")
location.raw['address']['village'] in df_clean.address.iloc[0]
l = []
for i,j,a in df_clean[['lat','lon','address']].values:
    try : 
        from geopy.geocoders import Nominatim
        geolocator = Nominatim(user_agent="iteration")
        time.sleep(0.4)
        location = geolocator.reverse(""+str(i)+", "+str(j))
        if ((location.raw['address']['country'] in a) or (location.raw['address']['village'] in a)) :
            l.append(True)
        else : l.append(False)
    except : 
        l.append(False)
        
df_clean['LatLongToCountry'] = l
Esempio n. 35
0
    def get_property_pages(self, files):
        # default map size and zoom factor
        sizeMap = '400x400'
        zoomMap = '8'

        # if dealing with multiple selection, return
        if len(files) != 1: return

        # if not dealing with file, return
        file = files[0]
        if file.get_uri_scheme() != 'file': return

        # if mimetype corresponds to JPG image, read data and populate tab
        mimetype = file.get_mime_type().split('/')
        if mimetype[0] in ('image'):

            # get filename
            uri = file.get_uri()
            gvfs = Gio.Vfs.get_default()
            filename = gvfs.get_file_for_uri(uri).get_path()

            # get GPS position
            with exiftool.ExifTool() as et:
                # read GPS data
                strLatitude = str(et.get_tag("EXIF:GPSLatitude", filename))
                refLatitude = str(et.get_tag("EXIF:GPSLatitudeRef", filename))
                strLongitude = str(et.get_tag("EXIF:GPSLongitude", filename))
                refLongitude = str(et.get_tag("EXIF:GPSLongitudeRef",
                                              filename))
                strAltitude = str(et.get_tag("EXIF:GPSAltitude", filename))

            # if no GPS data, return
            if strLatitude == "None": return
            if strLongitude == "None": return

            # trunk latitude to 6 digits and sign it
            parts = strLatitude.split(".")
            strLatitude = parts[0] + "." + parts[1][:6]
            if refLatitude == "S": strLatitude = '-' + strLatitude

            # trunk longitude to 6 digits and sign it
            parts = strLongitude.split(".")
            strLongitude = parts[0] + "." + parts[1][:6]
            if refLongitude == "W": strLongitude = '-' + strLongitude

            # generate GPS position
            strPosition = strLatitude + ',' + strLongitude

            # generate Google Maps links
            urlMaps = 'https://www.google.com/maps/place/' + strPosition + '/@' + strPosition + ',' + zoomMap + 'z/'
            urlJpeg = 'https://maps.googleapis.com/maps/api/staticmap?maptype=hybrid&zoom=' + zoomMap + '&size=' + sizeMap + '&center=' + strPosition + '&markers=' + strPosition

            # generate cache filenames
            dirHomeCache = os.environ['HOME'] + '/.cache'
            dirGeotagCache = os.getenv('XDG_CACHE_HOME',
                                       dirHomeCache) + '/geotag'

            # if cache directory doesn't exist, create it
            if not os.path.exists(dirGeotagCache):
                os.makedirs(dirGeotagCache)

            # generate cache file names
            fileMap = dirGeotagCache + '/map_' + strLatitude + '_' + strLongitude + '_' + sizeMap + '.png'
            fileDesc = dirGeotagCache + '/map_' + strLatitude + '_' + strLongitude + '.txt'
            fileLog = dirGeotagCache + '/map_' + strLatitude + '_' + strLongitude + '.log'

            # write log
            file = codecs.open(fileLog, "w", "utf-8")
            file.write(urlMaps)
            file.write(" ")
            file.write(urlJpeg)
            file.close()

            # if description is not in the cache, retrieve it from Nominatim
            if not os.path.exists(fileDesc):
                # retrieve place description
                geolocator = Nominatim(user_agent="nautilus-exif-geotag")
                location = geolocator.reverse(strPosition)
                strDescription = location.address
                strDescription = strDescription[:90]

                # write description to cache
                file = codecs.open(fileDesc, "w", "utf-8")
                file.write(strDescription)
                file.close()
            else:
                # read description from cache
                file = codecs.open(fileDesc, "r", "utf-8")
                strDescription = file.read()
                file.close()

            # if map is not in the cache, retrieve it from Google Maps
            if not os.path.exists(fileMap):
                urllib.urlretrieve(urlJpeg, fileMap)

            # create table
            self.table = Gtk.Table(4, 3)

            # set spacing
            self.table.set_col_spacings(10)
            self.table.set_row_spacings(5)

            # set margins
            self.table.set_margin_start(10)
            self.table.set_margin_end(10)
            self.table.set_margin_top(10)
            self.table.set_margin_bottom(10)

            # populate table
            self.SetLabel("<b>Longitude</b>", 0, 0, 1, "center")
            self.SetLabel("<b>Latitude</b>", 0, 1, 1, "center")
            self.SetLabel("<b>Altitude</b>", 0, 2, 1, "center")
            self.SetLabel(strLongitude, 1, 0, 1, "center")
            self.SetLabel(strLatitude, 1, 1, 1, "center")
            self.SetLabel(strAltitude, 1, 2, 1, "center")
            self.SetImage(fileMap, 2, 0, 3, "center")
            self.SetLabel(
                "<a href='" + urlMaps + "'>" + strDescription + "</a>", 3, 0,
                3, "center")

            # set tab content (scrolled window -> table)
            tab_win = Gtk.ScrolledWindow()
            tab_win.add_with_viewport(self.table)
            tab_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER)
            tab_win.show_all()

            # set tab label
            tab_label = Gtk.Label('GPS')

            # return label and tab content
            return Nautilus.PropertyPage(name="NautilusPython::geotag_info",
                                         label=tab_label,
                                         page=tab_win),
Esempio n. 36
0
        dx = d1.decode("utf-8")

        d2 = dx.replace("u'", '"')
        d3 = d2.replace("'", '"')
        # data = json.loads(d2.decode("utf-8"))

        data = json.loads(d3)
        timestamp = k[4:]
        lat = data["latitude"]
        lon = data["longitude"]
        latlon = str(lat) + "," + str(lon)

        while True:
            try:
                location = geolocator.reverse(latlon, language='en')
                break
            except:
                pass

        if location.address == None:
            addr = ""
        else:
            addr = location.address
#			print (latlon)
#			print (addr)

        timestamp_human = datetime.datetime.fromtimestamp(
            int(timestamp)).strftime('%Y-%m-%d %H:%M:%S')
        # print (str(timestamp) + "," + str(timestamp_human) + " UTC," + str(lat) + "," + str(lon) + ',"' +  '"')
        print(
Esempio n. 37
0
    def post(self, request, *args, **kwargs):
        logger.debug('Find location post called')
        logger.debug(request.data)
        serializer = FindLocationSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            data = serializer.data
            geolocator = Nominatim(user_agent="_user_panel.uaccounts")
            # location=geolocator.reverse("52.509669, 13.376294")
            location = geolocator.reverse(data['lat'] + ', ' + data['lon'],
                                          language='en')
            if 'address' in location.raw:
                address = location.raw['address']

                #Translate address to English
                # translator= Translator(to_lang="English")
                # translation = translator.translate(address['city'])
                # print(translation)

                country = ''
                state = ''
                city = ''
                street = ''
                postcode = ''

                for k in address:
                    if k == 'country':
                        country = address[k]
                    elif k == 'state' or k == 'state_district':
                        state = address[k]
                    elif k == 'city' or k == 'city_district':
                        city = address[k]
                    elif k == 'postcode':
                        address[k] = re.findall('\d+', address[k])[0]
                        postcode = address[k]
                    else:
                        street = street + ' ' + address[k] + ','

                if not city or city == '':
                    if data['city']:
                        city = data['city']

                #-------------Arabic Save()-----------------------------------------
                tdata = []
                tdata.append(postcode)
                tdata.append(street)
                tdata.append(state)
                tdata.append(city)
                tdata.append(country)

                tres = translate_text_ar(tdata)

                RegisteredUser.objects.filter(user=request.user).update(
                    zipcode=postcode,
                    street=street,
                    city=city,
                    area=state,
                    country=country,
                    lat=data['lat'],
                    lon=data['lon'],
                    ar_zipcode=tres[0].text,
                    ar_street=tres[1].text,
                    ar_area=tres[2].text,
                    ar_city=tres[3].text,
                    ar_country=tres[4].text,
                )

                return Response(
                    {
                        'message': 'location found successfully',
                        'success': 'True',
                        # 'data':data,
                        'address': address,
                    },
                    status=HTTP_200_OK,
                )
            else:
                return Response(
                    {
                        'message': 'location not found',
                        'success': 'False',
                    },
                    status=HTTP_400_BAD_REQUEST,
                )
Esempio n. 38
0
def datareceive(request):
       
    if(request.method == 'GET'):
        device_serial = request.GET.get("device_serial")
        vb_meter = request.GET.get("vb_meter")
        va_meter = request.GET.get("va_meter")
        vin_house = request.GET.get("vin_house")
        cb_meter = request.GET.get("cb_meter")
        ca_meter = request.GET.get("ca_meter")
        cin_house = request.GET.get("cin_house")
        energy = request.GET.get("energy")
        latitude = request.GET.get("latitude")
        longitude = request.GET.get("longitude")   
        
        
        print(device_serial)
        print(vb_meter)
        print(va_meter)
        print(vin_house)
        print(cb_meter)
        print(ca_meter)
        print(cin_house)
        print(energy)
        print(latitude)
        print(longitude)   
                
        if(device_serial and vb_meter and va_meter and
            vin_house and cb_meter and ca_meter and cin_house and 
                energy and latitude and longitude):
            
            latfloat = float(latitude)
            longfloat = float(longitude)
            
            print('LATITUDE:', latfloat)
            print('LONGITUDE:', longfloat)

            if((latfloat == 0) and (longfloat == 0)):
                location_address = 'Null'  
            else:
                latlng = latitude + ',' + longitude
                geolocator = Nominatim(user_agent='gpslocator')
                location = geolocator.reverse(latlng)
                location_address = location.address
                 
                
            client_object = Client.objects.get(device_serial = device_serial)    
                
            Data(
                client = client_object, 
                vb_meter = vb_meter,
                va_meter = va_meter, 
                vin_house = vin_house, 
                cb_meter = cb_meter,  
                ca_meter = ca_meter,
                cin_house = cin_house, 
                energy = energy, 
                latitude = latitude, 
                longitude = longitude, 
                location = location_address
            ).save()
                  
            return HttpResponse("<br><h3> Pass </h3>")
        else:
            return HttpResponse("<br><h3> Data Missing </h3>")
Esempio n. 39
0
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("-37.8811,145.0969")
print(location.raw["address"]["suburb"])
# Chadstone
Esempio n. 40
0
def get_current_location(latitude, longitude):
    locator = Nominatim(user_agent="myGeocoder")
    coordinates = f"{latitude}, {longitude}"
    location = locator.reverse(coordinates)
    return location.raw
def reverseGEO(long, lat):
    geolocator = Nominatim(user_agent="Skill")
    location = geolocator.reverse(str(long) + "," + str(lat))
    Location = str(location.address)
    return (",".join(Location.split(",", 2)[:2]))
Esempio n. 42
0
class muniScraper(object):
    """ Opens a chrome driver with selenium and navigates to google maps; samples
    locations in San Francisco; and collects and returns: travel times between locations
    for multiple methods of transport (by car, bus, bike, and foot), for both
    outbound and return travel, plus timestamp and street addresses.
    
    -------------------------------------------------------------------------

    Note: Usage of the data collected through muniScraper is intended to show
    routes in SF where public transportation is at a particular disadvantage,
    relative to travel by car or bike.
    (E.g., pub_transit_factor = [transit time] / [driving time])
    This could be used...
    a) As a sorted dataframe and visualized from there...
    b) A user could select a location in SF, and then the travel times to all
        other locations can be displayed. The implementation could look as follows:
        1. Select a subset of data where the depart location is within some
            radius of the selected location.
            Note also that, because latitude and longitude are not
            on the same scale (especially at SF's latitude), in order to draw
            a radius of N meters around the depart location on the surface
            of the earth, the analyst must draw an ellipse in the data!
        2. Use the arrive location for the travel times
        3. Pick a granularity for datapoints within SF: the subset of data selected
            in step 1 will be incomplete. Therefore, interpolate the missing data.
            It is the analyst's responsibility to make sure there is enough data
            in the subset (i.e., the radius around the depart location is big enough)
            that the interpolated travel times are robust.
        4. The data are now ready to be visualized
    
    -------------------------------------------------------------------------
    
    Note: google maps has an API, through which this would be A LOT EASIER.
    HOWEVER: this has a cap on the number of free requests per day: 1,500.
    Each arrive, depart trip would require four requests: one for each travel
    mode (car, transit, bike, foot). Thherefore this method is too slow to ever
    collect a big enough sample for analysis.
    
    for googlemaps package see: https://pypi.python.org/pypi/googlemaps/
    for keys & pricing see: https://developers.google.com/api-client-library/python/
    
    -------------------------------------------------------------------------

    Usage example for muniScraper.run():
    
    # the user may have to pip install [packages used in this class]
    
    import sys
    sys.path.append('/path/to/muni_scraper/')
    
    import muni_scraper

    import numpy as np
    
    kwargs = {'chrome_options_arg':'--incognito',
              'base_url':'https://www.google.com/maps/dir/',
              'driver_path':[path/to/chromedriver]}
    
    ms = muni_scraper.muniScraper(**kwargs)
    
    # open a db path with Database() class
    db_path = '/path/to/database.db'
    gdb = muni_scraper.Database(db_path, buffer_size=10)
    
    # run for some n of N samples (collects <= two records per n)
    for n in range(int(1e5)):
        # second for is because ms.run() returns a df with two rows
        for dat in np.array(ms.run()):
            gdb.record(dat)
        time.sleep(1)    
    """

    # For finding elements with selenium
    dirsb = "directions-searchbox-"
    tsbi = "tactile-searchbox-input"
    wdi_reverse = ".widget-directions-icon.reverse"
    wpsdtd = "widget-pane-section-directions-trip-duration"
    dtme = "directions-travel-mode-expander"
    wpsdept = "widget-pane-section-directions-error-primary-text"

    # column names
    columns = ("arrive_add", "arrive_lat", "arrive_lon", "bicycle",
               "depart_add", "depart_lat", "depart_lon", "driving", "transit",
               "walk", "timestamp")

    # dictionary of directions method:selenium element key
    dir_dict = {
        "driving": ".directions-travel-mode-icon.directions-drive-icon",
        "transit": ".directions-travel-mode-icon.directions-transit-icon",
        "walk": ".directions-travel-mode-icon.directions-walk-icon",
        "bicycle": ".directions-travel-mode-icon.directions-bicycle-icon"
    }

    # for each run(), use this df structure and print these cols
    timesdf = pd.DataFrame(columns=columns, index=(0, 1))
    print_cols = [
        "timestamp", "depart_lat", "depart_lon", "arrive_lat", "arrive_lon",
        "driving", "transit", "bicycle", "walk"
    ]

    # lat-lon coordinates for the perimeter of SF
    sf_perim = [(37.708266, -122.393657), (37.708440, -122.485511),
                (37.724103, -122.485019), (37.726958, -122.483816),
                (37.729338, -122.485621), (37.730333, -122.489504),
                (37.728905, -122.491802), (37.729554, -122.493661),
                (37.729121, -122.496561), (37.731501, -122.498748),
                (37.725358, -122.503452), (37.727045, -122.506133),
                (37.732658, -122.507427), (37.735427, -122.506825),
                (37.775045, -122.511305), (37.778698, -122.513720),
                (37.779853, -122.509438), (37.780845, -122.509633),
                (37.781737, -122.493314), (37.787528, -122.493787),
                (37.787908, -122.491871), (37.787112, -122.491174),
                (37.788406, -122.489796), (37.788844, -122.489989),
                (37.790350, -122.485654), (37.810905, -122.477056),
                (37.809361, -122.476045), (37.808615, -122.471663),
                (37.803555, -122.459563), (37.804439, -122.453015),
                (37.805638, -122.453891), (37.806730, -122.447791),
                (37.805212, -122.447251), (37.805851, -122.442263),
                (37.806650, -122.442499), (37.807502, -122.435893),
                (37.806277, -122.435657), (37.804999, -122.433769),
                (37.806064, -122.425545), (37.807901, -122.421703),
                (37.808061, -122.417793), (37.808301, -122.415838),
                (37.809073, -122.415939), (37.808860, -122.412669),
                (37.807768, -122.407479), (37.806384, -122.404715),
                (37.803774, -122.401547), (37.792636, -122.390954),
                (37.791064, -122.389047), (37.789640, -122.388360),
                (37.787995, -122.387738), (37.778371, -122.387480),
                (37.777149, -122.390399), (37.776547, -122.389927),
                (37.776615, -122.387459), (37.771519, -122.386676),
                (37.768839, -122.385056), (37.765330, -122.386579),
                (37.763624, -122.387109), (37.763124, -122.386537),
                (37.763081, -122.385244), (37.762142, -122.385285),
                (37.761762, -122.383279), (37.759570, -122.381891),
                (37.759547, -122.381410), (37.758147, -122.381303),
                (37.757705, -122.381607), (37.755286, -122.381178),
                (37.755058, -122.384233), (37.754531, -122.384091),
                (37.754464, -122.383106), (37.754479, -122.383072),
                (37.753239, -122.383082), (37.753120, -122.384104),
                (37.748432, -122.382347), (37.748126, -122.386309),
                (37.747807, -122.390386), (37.748806, -122.392968),
                (37.747466, -122.393263), (37.746633, -122.390767),
                (37.746935, -122.375659), (37.740055, -122.367982),
                (37.739688, -122.374093), (37.732913, -122.375795),
                (37.732139, -122.374232), (37.734052, -122.372238),
                (37.731735, -122.369059), (37.731634, -122.365114),
                (37.729890, -122.362227), (37.728079, -122.362141),
                (37.728514, -122.357474), (37.726166, -122.357899),
                (37.725731, -122.365537), (37.723636, -122.362980),
                (37.722097, -122.364197), (37.719901, -122.363036),
                (37.716778, -122.364195), (37.724967, -122.377806),
                (37.722607, -122.381104), (37.724809, -122.387145),
                (37.721685, -122.383194), (37.720367, -122.383779),
                (37.716056, -122.376343), (37.709382, -122.381688),
                (37.710458, -122.390474), (37.708142, -122.393783)]

    def __init__(self,
                 driver_path,
                 base_url,
                 chrome_options_arg,
                 wait_time=1.5e7):

        self.driver_path = driver_path
        self.wait_time = wait_time

        chrome_options = webdriver.ChromeOptions()
        if chrome_options_arg:
            chrome_options.add_argument(chrome_options_arg)

        self.chrome_options = chrome_options

        self.driver = webdriver.Chrome(executable_path=self.driver_path,
                                       chrome_options=self.chrome_options)

        # Note: selenium can be run headless, with phantomJS
        # however, this does not seem to work for google maps
        # self.driver = webdriver.PhantomJS(executable_path=self.driver_path, service_log_path=os.path.devnull)
        # self.driver.set_window_size(1366,768)

        self.base_url = base_url
        self.driver.get(self.base_url)

        self.geolocator = Nominatim(timeout=None)

        # the perim of SF in matplotlib
        self.sfPath = mplPath.Path(np.array(self.sf_perim))

        # initialize some other views of the sf perim for easy use later
        self.lats = [x for x, _ in self.sf_perim]
        self.lons = [x for _, x in self.sf_perim]
        self.ylim = self.ymin, self.ymax = np.array(self.lats).min(), np.array(
            self.lats).max()
        self.xlim = self.xmin, self.xmax = np.array(self.lons).min(), np.array(
            self.lons).max()

    def get_address(self, latlon):
        """ Uses geolocator to get a street address
        'latlon' = (latitude, longigude) tuple """
        loc = self.geolocator.reverse(latlon)
        return loc.address

    def get_cn_text(self, cn_name):
        """ Finds elements by class name and returns all non-empty text """
        return [
            elmt.text
            for elmt in self.driver.find_elements_by_class_name(self.wpsdtd)
            if elmt.text != ""
        ]

    def get_times(self):
        """ Semi-redundant function for handling slow loading of page.
        It allows for get_traveltimes to run until it is populated with the text
        from at least one element, or until self.wait_time is exceded. """
        gtt = None
        wttm = 0
        t0 = dt.datetime.now()
        while not gtt and wttm <= self.wait_time:
            try:
                gtt = self.get_traveltimes()
                wttm = (dt.datetime.now() - t0).microseconds
            except:
                next
        return gtt

    def get_traveltimes(self):
        """ Gets travel times for each method of transportation, from a->b """
        onewaytimes = dict()
        for key in self.dir_dict.keys():
            # hover over the travel mode expander
            dtme = self.driver.find_element_by_class_name(self.dtme)
            # this ensures all travel mode elements are clickable
            ActionChains(self.driver).move_to_element(dtme).perform()
            self.driver.find_element_by_css_selector(
                self.dir_dict[key]).click()
            # wait for the data to load
            time.sleep(1)
            #sometimes there are no directions available
            if not [
                    x.text for x in self.driver.find_elements_by_class_name(
                        self.wpsdept) if x.text
            ]:
                next
            wait_counter = 0
            while wait_counter < 10 and len(self.get_cn_text(self.wpsdtd)) < 1:
                time.sleep(1 / math.e)
                wait_counter += 1
            text = self.get_cn_text(self.wpsdtd)
            if text:
                onewaytimes[key] = self.parse_times(text[0])
            else:
                onewaytimes[key] = None

        return onewaytimes

    def in_sf(self, tup):
        """ Tests if the supplied tuple of geocordinates is inside the perimeter of SF.
        'tup' = tuple of geocoordinates. """
        return self.sfPath.contains_point(tup) == 1

    def initialize_directions(self, depart, arrive):
        """ Requests initial directions from google maps (transportation method agnostic).
        'depart' = 'depart' = tuple of latitude, longitutde coordinates """
        sb_dict = dict()
        for location, i in zip((depart, arrive), (0, 1)):
            i = str(i)
            sb_dict["dsb" + i] = self.driver.find_element_by_id(self.dirsb + i)
            sb_dict["tsi" + i] = sb_dict["dsb" + i].find_element_by_class_name(
                self.tsbi)
            sb_dict["tsi" + i].clear()
            sb_dict["tsi" + i].send_keys(str(location)[1:-1])
            sb_dict["tsi" + i].send_keys(Keys.RETURN)

    def parse_times(self, timestr):
        """ Turns the travel times from string to integer.
        'timestr' = single travel time, as a string
        Example:
        parse_times('47m') returns 47
        parse_times('1h 12m') returns 72 """

        hh = mm = 0
        if "h" in timestr:
            hh = int(re.search("[1-9] h", timestr).group().replace(" h", ""))
            hh *= 60
        if "m" in timestr:
            mm = int(
                re.search("[0-9]{0,2} m", timestr).group().replace(" m", ""))
        return hh + mm

    def plot_sf(self, title="SF Perimeter"):
        """ Plot the permieter of SF """
        plt.figure(figsize=(8, 8))
        plt.plot(self.lons, self.lats)
        plt.ylim(self.ylim)
        plt.xlim(self.xlim)
        plt.title(title)
        plt.show()

    def restart_driver(self):
        self.driver.close()
        self.driver = webdriver.Chrome(executable_path=self.driver_path,
                                       chrome_options=self.chrome_options)
        self.driver.get(url=self.base_url)
        time.sleep(math.e)

    def reverse_direction(self):
        """ Finds the element to reverse directions, and simualates a click on the
        'reverse directions' button. """
        self.driver.find_element_by_css_selector(self.wdi_reverse).click()

    def run(self):
        """ Uses two randomly sampled latitude, longitude locations in SF,
        (depart & arrive), gets their street addresses (for later use in analysis),
        collects travel times to and from the locations, and returns a dataframe
        with two rows (typically; in the event of nulls, that row is excluded),
        one for outbound, one for inbound. For each trip, the tiemstamp is recorded. """
        timesdf = self.timesdf
        arrive, depart = self.sample_locations()
        for dirstr, dirtpl in zip(("arrive", "depart"), (arrive, depart)):
            timesdf.loc[0, [dirstr + "_lat", dirstr + "_lon"]] = dirtpl
            timesdf.loc[0, dirstr + "_add"] = self.get_address(dirtpl)
        self.initialize_directions(arrive, depart)
        gt = self.get_times()
        timesdf.loc[0, "timestamp"] = pd.to_datetime(dt.datetime.now())
        if gt:
            for key, val in gt.items():
                timesdf.loc[0, key] = val
        # reverse
        arrive, depart = depart, arrive
        for dirstr, dirtpl in zip(("arrive", "depart"), (arrive, depart)):
            timesdf.loc[1, [dirstr + "_lat", dirstr + "_lon"]] = dirtpl
        timesdf.loc[1,
                    "arrive_add"], timesdf.loc[1, "depart_add"] = timesdf.loc[
                        0, "depart_add"], timesdf.loc[0, "arrive_add"]
        self.reverse_direction()
        gt = self.get_times()
        timesdf.loc[1, "timestamp"] = pd.to_datetime(dt.datetime.now())
        if gt:
            for key, val in gt.items():
                timesdf.loc[1, key] = val

        timesdf = timesdf.ix[timesdf.isnull().sum(1) == 0]
        # take only full columns, change datatype, return
        for colname in [
                "arrive_lat", "arrive_lon", "depart_lat", "depart_lon"
        ]:
            timesdf[colname] = timesdf[colname].astype(np.float64)
        for colname in ["bicycle", "driving", "transit", "walk"]:
            timesdf[colname] = timesdf[colname].astype(np.int64)
        timesdf["timestamp"] = timesdf["timestamp"].astype(str)
        print(timesdf.loc[:, self.print_cols])
        return timesdf

    def sample_locations(self):
        """ Returns lat, lon for two random locations in SF """
        return [self.sample_sf() for _ in range(2)]

    def sample_sf(self):
        """ Returns a randomly sampled lat, lon location in SF """
        insf = 0
        while not insf == 1:
            sfpoint = tuple(
                map(lambda x: random.uniform(*x), [self.ylim, self.xlim]))
            insf = self.sfPath.contains_point(sfpoint)
        return sfpoint
Esempio n. 43
0
for measurement in measurements_dict:  # for each measurement
    # get the measurement ID
    print('Measurement', measurement, 'Probe ', measurements_dict[measurement])
    m = Measurement(id=measurement)  # get metadata for this measurement
    p = Probe(
        id=measurements_dict[measurement]
    )  # Get all the properties of the individual probe used in this individual measurement
    coords = p.geometry['coordinates']
    coords_str = ','.join([str(elem) for elem in coords])

    print(coords_str)
    # Create a list of Coordinates

    # Discover the physical address of the target location
    try:
        location = geolocator.reverse(coords_str)
    except:
        location = "unknown"
    print(location)
    latitude = p.geometry['coordinates'][1]
    longitude = p.geometry['coordinates'][0]
    is_anchor = p.is_anchor
    print("probe", measurements_dict[measurement], "is anchor", is_anchor)
    target_address = location
    target_probe_id = measurements_dict[measurement]
    print(measurement)

    kwargs = {
        "msm_id": measurement,  # my south africa measurement 
        #"start": datetime(2015, 05, 19), # just testing date filtering
        #"stop": datetime(2015, 05, 20),  # just testing date filtering
Esempio n. 44
0
def end_trip(message):
    global markers_tho, price
    global loc_tho
    idMain = message.chat.id
    groan_address = "Гродно"
    # trip_b= message.text
    geolocator = Nominatim(user_agent="specify_your_app_name_here")
    loc_tho = groan_address + " " + message.text
    # Получаем координаты для 2 точки
    loc_tho_chat = geolocator.geocode(loc_tho)
    lan_tho = loc_tho_chat.latitude
    lon_tho = loc_tho_chat.longitude
    lat_lon = str(lan_tho) + "," + str(lon_tho)  # для геокодинга
    startAndEnd[str(idMain)].append(lat_lon)  # шаг 2 точка старта
    zoom = 17  # Масштаб карты на старте. Изменяется от 1 до 19
    size = str(650) + "x" + str(450)
    markers_tho = "color:red%7Clabel:B%7C" + startAndEnd[str(idMain)][1]
    distance_trip = round(distance.distance(startAndEnd[str(idMain)][0], startAndEnd[str(idMain)][1]).km, 1)
    map_request_b = "https://maps.googleapis.com/maps/api/staticmap?size={size}&zoom={z}&center={ll}&markers={markers_tho}&key=AIzaSyC87S3ttehSCmIa76r7IE_omWk-3dEH1Rg".format(
        ll=startAndEnd[str(idMain)][1], size=size, z=zoom,
        markers_tho=markers_tho)
    response_b = requests.get(map_request_b)
    map_file_tho = "map_tho.png"
    try:
        with open(map_file_tho, "wb") as file_tho:
            file_tho.write(response_b.content)
    except IOError as error:
        print("Ошибка записи временного файла:", error)
    now = datetime.now()
    gmaps = googlemaps.Client(key='AIzaSyC87S3ttehSCmIa76r7IE_omWk-3dEH1Rg')
    result = gmaps.directions(startAndEnd[str(idMain)][0], startAndEnd[str(idMain)][1], mode="driving",
                              departure_time=now)
    raw = result[0]['overview_polyline']['points']
    points = polyline.decode(raw)
    pl = "|".join(["{0},{1}".format(p[0], p[1]) for p in points])
    path = "color:0xff0000ff |weight:5|" + pl
    map_request_c = "https://maps.googleapis.com/maps/api/staticmap?size={size}&markers={markers}&markers={markers_tho}&path={path}&key=AIzaSyC87S3ttehSCmIa76r7IE_omWk-3dEH1Rg".format(
        size=size, markers=startAndEnd[str(idMain)][0],
        markers_tho=markers_tho, path=path)
    response_c = requests.get(map_request_c)
    map_file_c = "map_c.png"
    try:
        with open(map_file_c, "wb") as file_c:
            file_c.write(response_c.content)
    except IOError as error:
        print("Ошибка записи временного файла:", error)
    photo_b = open('map_tho.png', 'rb')
    bot.send_photo(message.chat.id, photo_b)
    photo_c = open('map_c.png', 'rb')
    bot.send_photo(message.chat.id, photo_c)
    price = round(distance_trip)
    if price < 1:
        price = 1
    loc = geolocator.reverse(startAndEnd[str(idMain)][0])
    taxi_time = round(distance_trip / 0.6)
    markup = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    button_dop = types.KeyboardButton(text="Дополнительная точка поездки")
    button_ok = types.KeyboardButton(text="Поехали")
    button_exit_dop = types.KeyboardButton(text="Отменить поездку ❌")
    markup.add(button_dop, button_ok, button_exit_dop,)
    bot.send_message(message.chat.id,
                     f'Ваш маршрут:{loc}'
                     f'===>{loc_tho}\n'
                     f'Расcтояние маршрута = {distance_trip}/км\n'
                     f'Ожидайте машину🚕 \n'
                     f'Стоимость поездки: {price}P\n'
                     f'Примерное время ожидания {taxi_time}/мин', reply_markup=markup)
    bot.register_next_step_handler(message, dop_start_medium)
Esempio n. 45
0
from geopy.geocoders import Nominatim
print("Write your coordinates like: 60.016666666666666,30.322")
s = input()
geolocator = Nominatim(user_agent="*****@*****.**")
location = geolocator.reverse(s)
print('location: ', location.address)
print('Goggle Maps URL: https://www.google.com/maps/search/?api=1&query=',
      s,
      sep="")
Esempio n. 46
0
def dop_trip(message):
    global price
    idMain = message.chat.id
    groan_address = "Гродно"
    geolocator = Nominatim(user_agent="specify_your_app_name_here")
    address_trip_dop = groan_address + " " + message.text
    # Получаем координаты для 2 точки
    loc_dop = geolocator.geocode(address_trip_dop)
    lan_dop = loc_dop.latitude
    lon_dop = loc_dop.longitude
    lat_dop = str(lan_dop) + "," + str(lon_dop)  # для геокодинга
    startAndEnd[str(idMain)].append(lat_dop)  # шаг 2 точка старта
    markers_dop = "color:red%7Clabel:B%7C" + startAndEnd[str(idMain)][2]
    size = str(650) + "x" + str(450)
    distance_trip_one = round(distance.distance(startAndEnd[str(idMain)][0], startAndEnd[str(idMain)][2]).km, 1)
    distance_trip_tho = round(distance.distance(startAndEnd[str(idMain)][2], startAndEnd[str(idMain)][1]).km, 1)
    distance_trip = round(distance_trip_one + distance_trip_tho)
    now = datetime.now()
    gmaps = googlemaps.Client(key='AIzaSyC87S3ttehSCmIa76r7IE_omWk-3dEH1Rg')
    result = gmaps.directions(startAndEnd[str(idMain)][0], startAndEnd[str(idMain)][2], mode="driving",
                              departure_time=now)
    raw = result[0]['overview_polyline']['points']
    points = polyline.decode(raw)
    pl = "|".join(["{0},{1}".format(p[0], p[1]) for p in points])
    result_tho = gmaps.directions(startAndEnd[str(idMain)][2], startAndEnd[str(idMain)][1],
                                  mode="driving",
                                  departure_time=now)
    raw_tho = result_tho[0]['overview_polyline']['points']
    points_tho = polyline.decode(raw_tho)
    pl_tho = "|".join(["{0},{1}".format(p[0], p[1]) for p in points_tho])
    pl_general = pl + "|" + pl_tho
    path = "color:0xff0000ff |weight:5|" + pl_general
    map_request_c = "https://maps.googleapis.com/maps/api/staticmap?size={size}&markers={markers}&markers={markers_tho}&markers={markers_dop}&path={path}&key=AIzaSyC87S3ttehSCmIa76r7IE_omWk-3dEH1Rg".format(
        size=size, markers=startAndEnd[str(idMain)][0],
        markers_tho=markers_tho, path=path, markers_dop=markers_dop)
    response_c = requests.get(map_request_c)
    map_file_dop = "map_dop.png"
    try:
        with open(map_file_dop, "wb") as file_c:
            file_c.write(response_c.content)
    except IOError as error:
        print("Ошибка записи временного файла:", error)
    photo_dop = open('map_dop.png', 'rb')
    price = round(distance_trip)
    if price < 1:
        price = 1
    taxi_time = round(distance_trip / 0.6)
    markup = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    button_ok = types.KeyboardButton(text="Поехали")
    button_no = types.KeyboardButton(text="Отменить поездку ❌")
    loc = geolocator.reverse(startAndEnd[str(idMain)][0])
    loc_tho = geolocator.reverse(startAndEnd[str(idMain)][1])
    markup.add(button_ok, button_no)
    bot.send_photo(message.chat.id, photo_dop)
    bot.send_message(message.chat.id,
                     f'Ваш маршрут перестроен: {loc}\n'
                     f'===>{address_trip_dop}\n'
                     f'===>{loc_tho}\n'
                     f'Расcтояние маршрута = {distance_trip}/км\n'
                     f'Ожидайте машину🚕\n'
                     f'Стоимость поездки: {price}\n'
                     f'Примерное время ожидания {taxi_time}', reply_markup=markup)
    bot.register_next_step_handler(message, exit_trip)
 Contact = pd.DataFrame(columns=['AgencyID','FacilityID','Phone','Email','Website'])
 Address = pd.DataFrame(columns=['FacilityID','RouteID','Province','City','Street','Number','Cap','Location'])
 Location = pd.DataFrame(columns=['AddressID','StopId','Latitude','Longitude','Altitude'])
 Calendar = pd.DataFrame(columns=['FacilityID','StopTimesID','ServiceID','Monday','Tuesday','Wednesday',
                                  'Thursday','Friday','Saturday','Sunday','StartDate','EndDate','Exceptions'])
 CalendarDates = pd.DataFrame(columns=['CalendarId','ServiceId','Date','ExceptionType'])
 
 #%% Loop
 print('Start iterating dataset ...')
 
 for i in stazioni.itertuples():
     idx = i.Index +400
     geolocator = Nominatim(user_agent="me")
     tempLoc =''
     tempLoc += str(i.geometry.y)+','+str(i.geometry.x)
     rawData = geolocator.reverse(tempLoc)
     #set price
     Price.loc[idx] = [None,None,None,None,None]
     Contact.loc[idx]= [None,idx,None,None,None]
     Address.loc[idx] = [idx,None,
                         'Trento',
                         'Trento',
                         rawData.raw['address']['road'] if 'road' in rawData.raw['address'] else ''+','+rawData.raw['address']['suburb'] if 'suburb' in rawData.raw['address'] else None,
                         rawData.raw['address']['house_number'] if 'house_number' in rawData.raw['address'] else '',
                         rawData.raw['address']['postcode'] if 'postcode' in rawData.raw['address'] else '',
                         idx]
     #set Location
     Location.loc[idx] = [idx,None,rawData.latitude,rawData.longitude,0]
     # location = Location(rawData.latitude,rawData.longitude,None)
     Calendar.loc[idx]= [None,None,None,None,None,None,None,None,None,None,None,None,None]
     Facility.loc[idx] = [None,idx,idx,2,idx,idx]
Esempio n. 48
0
		for elem in repo['aliyevaa_jgtsui.' + dbName].find({}):
			djson.append(elem)
		
				
		#mrepo.dropPermanent("liquor_data")
		#mrepo.createPermanent("liquor_data")

		geolocator = Nominatim()
		for entry in djson:
			location=entry['location']
			if location['latitude']=='0.0':
				djson.remove(entry)		
			else:
				a=[]
				strLoc=location['latitude']+' , '+ location['longitude']
				location = geolocator.reverse(strLoc)
				try:
					addr=location.address
					a=addr.split(',')
					try:
						del entry ['location']
						entry['zip']=a[-2]
						#print(entry)
					except:
						djson.remove(entry)
						#if (isinstance(int(a[-2]), int))
				except:		
						pass

				mrepo['aliyevaa_jgtsui.liquor_data'].insert_many(djson)
		mrepo.logout()
Esempio n. 49
0
    with open('locations.json') as fh:
        locations = json.loads(fh.read())

# Parse the data we need from upstream.
parsed = []
for item in resp.json():
    lat = item.get('latitude')
    lon = item.get('longitude')

    if lat is not None:
        key = str(lat) + "," + str(lon)
        loc = locations.get(key)

        if loc is None:
            geolocator = Nominatim(user_agent="test")
            location = geolocator.reverse(key)
            check = location.address.split(',')[0]

            if check[0].isalnum():
                location_str = location.address.split(',')[1].strip()
            else:
                location_str = location.address.split(',')[0].strip()
            locations[key] = location_str

        try:
            parsed.append({'id': item.get('id'),
                           'lat': item.get('latitude'),
                           'lon': item.get('longitude'),
                           'name': locations[key],
                           'temp': round(item.get('temperature'), 1),
                           'hum': round(item.get('humidity'), 1)})
Esempio n. 50
0
def gateway_update_process(client, mycursor, VCO_CUSTOMER_EDGE):
    date = datetime.utcnow()
    date_before = date - timedelta(hours=1)
    kwargs = {"timeout": 200}
    params = {"with": ["edgeCount", "edgeConfigUpdate"]}
    get_customers_reply = client.call_api('network/getNetworkEnterprises',
                                          params, **kwargs)
    #print (get_customers_reply)
    kwargs = {"timeout": 200}
    params = {
        "with": [
            "site", "roles", "pools", "dataCenters", "certificates",
            "enterprises", "handOffEdges", "enterpriseAssociationCounts"
        ]
    }
    get_gateways = client.call_api('network/getNetworkGateways', params,
                                   **kwargs)
    #print json.dumps(get_gateways, indent=4, sort_keys=True)
    local_logger.info("Pulled Gateway API Call")

    for gw in get_gateways:
        local_logger.info(gw["name"])
        #if gw["name"] == "vcg162-usil1":
        if gw["gatewayState"] == "CONNECTED":
            #if gw["gatewayState"]:
            Date = datetime.now().strftime('%Y-%m-%d 00:00:00')
            GatewayName = gw["name"]
            GatewayID = gw["logicalId"]
            GWVersion = gw["buildNumber"]
            #GWCity = gw["site"]["city"]
            #GWState = gw["site"]["state"]
            #GWCountry = gw["site"]["country"]
            GWCity = "Not set"
            GWState = "Not set"
            GWCountry = "Not set"
            geospecific = "Not set"
            GWPostalCode = "Not set"
            geolocator = Nominatim(user_agent="get link details")
            geolocator.urlopen = uo
            #print (gw["ipAddress"])
            # Try to get location using geolocation
            try:
                geos = [
                    json.loads(line)
                    for line in open('DataFiles/country.json', 'r')
                ]
                try:
                    ### NOTE THIS CODE NEEDS TO BE IMPROVED, WE SHOULD TAKE IN ACCOUNT WHEN WE DONT HAVE LAT AND LONG BUT WE HAVE AN ADDRESS THAT WE CAN USE TO DETERMINE LAT AND LON
                    if gw["site"]["lat"] != None and gw["site"]["lon"] != None:
                        lat = gw["site"]["lat"]
                        lon = gw["site"]["lon"]
                        geoval = '%s,%s' % (gw["site"]["lat"],
                                            gw["site"]["lon"])
                        location = geolocator.reverse(geoval,
                                                      language="en-US,en")
                        sleep(
                            10
                        )  # sleeping since there is a limit of quota usage
                        data = location.raw
                        data = data['address']
                        local_logger.info(data)
                        if 'state' in data:
                            GWState = str(data['state'])
                        elif gw["site"]["state"] != None:
                            GWState = gw["site"]["state"]

                        if 'city' in data:
                            GWCity = str(data['city'])
                        elif 'county' in data:
                            GWCity = str(data['county'])
                        elif gw["site"]["city"] != None:
                            GWCity = gw["site"]["city"]

                        if 'country' in data:
                            GWCountry = str(data["country"])
                        elif gw["site"]["country"] != None:
                            GWCountry = gw["site"]["country"]

                        if 'postcode' in data:
                            str(data['postcode'])
                            GWPostalCode = str(data['postcode'])
                            if re.findall('[^A-Za-z0-9_  .-]', GWPostalCode):
                                GWPostalCode = gw["site"]["postalCode"]
                            else:
                                local_logger.info("regular string")
                                GWPostalCode = GWPostalCode
                        else:
                            GWPostalCode = gw["site"]["postalCode"]

                        for geo in geos:
                            #if geo["Country"] == Country or geo["ISO"] == Country:
                            if geo["ISO"].lower(
                            ) == data['country_code'].lower():
                                geospecific = geo["REG"]

                    else:
                        logger.info("using maxmind")
                        client = geoip2.webservice.Client(
                            73615, 'WZgmKOkO3ywZ')
                        response = client.insights(gw['ipAddress'])
                        lat = response.location.latitude
                        lon = response.location.longitude
                        geoval = '%s,%s' % (lat, lon)
                        location = geolocator.reverse(geoval,
                                                      language="en-US,en")
                        sleep(10)
                        data = location.raw
                        data = data['address']
                        local_logger.info(data)
                        if 'state' in data:
                            GWState = str(data['state'])
                        elif gw["site"]["state"] != None:
                            GWState = gw["site"]["state"]

                        if 'city' in data:
                            GWCity = str(data['city'])
                        elif 'county' in data:
                            GWCity = str(data['county'])
                        elif gw["site"]["city"] != None:
                            GWCity = gw["site"]["city"]

                        if 'country' in data:
                            GWCountry = str(data["country"])
                        elif gw["site"]["country"] != None:
                            GWCountry = gw["site"]["country"]

                        if 'postcode' in data:
                            str(data['postcode'])
                            GWPostalCode = str(data['postcode'])
                            if re.findall('[^A-Za-z0-9_  .]', GWPostalCode):
                                GWPostalCode = gw["site"]["postalCode"]
                            else:
                                local_logger.info("regular string")
                                GWPostalCode = GWPostalCode
                        else:
                            GWPostalCode = gw["site"]["postalCode"]

                        for geo in geos:
                            #if geo["Country"] == Country or geo["ISO"] == Country:
                            if geo["ISO"].lower(
                            ) == data['country_code'].lower():
                                geospecific = geo["REG"]

                except Exception as e:
                    local_logger.critical(e)
            except:
                local_logger.critical("UNABLE TO BUILD LOCATION")
            GWLAT = gw["site"]["lat"]
            GWLON = gw["site"]["lon"]
            GWActivationtime = gw["activationTime"]
            GWActivationState = gw["activationState"]
            GWCurrentstatus = gw["gatewayState"]
            GWLogicalID = gw["logicalId"]
            GWuptime = gw["systemUpSince"]
            if gw["connectedEdges"] != None:
                GWconnectededges = gw["connectedEdges"]
            else:
                GWconnectededges = "0"
            if gw["utilizationDetail"]["cpu"] != None:
                GWCPU = gw["utilizationDetail"]["cpu"]
            else:
                GWCPU = "0"
            if gw["utilizationDetail"]["load"] != None:
                GWload = gw["utilizationDetail"]["load"]
            else:
                GWload = "0"
            if gw["utilizationDetail"]["memory"] != None:
                GWMemory = gw["utilizationDetail"]["memory"]
            else:
                GWMemory = "0"
            if gw["site"]["contactEmail"] == "*****@*****.**" and gw[
                    "handOffDetail"] is None:
                local_logger.info("Cloud gateway")
                GatewayType = "None"
            else:
                local_logger.info("partner gateway")
            GatewayType = "ALLOW"
            GWpki = gw["endpointPkiMode"]
            gwpool = gw["pools"]
            current_time = datetime.now()
            start_new = current_time - timedelta(hours=24)
            try:
                kwargs = {"timeout": 200}
                params = {
                    "gatewayId":
                    gw["id"],
                    "interval": {
                        "start": start_new
                    },
                    "metrics": [
                        "cpuPct", "memoryPct", "flowCount",
                        "handoffQueueDrops", "tunnelCount"
                    ]
                }
                get_met = client.call_api('metrics/getGatewayStatusMetrics',
                                          params, **kwargs)
                #print json.dumps(get_met, indent=4, sort_keys=True)
                local_logger.info("Gateway Metrics API call pulled")
                GWCPU = get_met["cpuPct"]["max"]
                gw_flow_count = get_met["flowCount"]["max"]
                gw_handoff = get_met["handoffQueueDrops"]["max"]
                GWMemory = get_met["memoryPct"]["max"]
                gw_tunnel = get_met["tunnelCount"]["max"]
            except:
                gw_flow_count = 0
                gw_handoff = 0
                gw_tunnel = 0
            #Date = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
            Date = datetime.now().strftime('%Y-%m-%d 00:00:00')
            query = """INSERT IGNORE INTO Gateways (Date,GatewayID, GatewayName, GWVersion, GWCity, GWState, GWCountry, GWLAT, GWLON, GWActivationtime,
                   GWActivationState, GWCurrentstatus, GWuptime, GWconnectededges, GWCPU, GWMemory,GWload, GWpki, GatewayType ,gw_flow_count, gw_handoff,  gw_tunnel, geospecific, GWPostalCode)
                                     VALUES (%s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s,%s)
                                     ON DUPLICATE KEY UPDATE
                                     Date = VALUES(DATE),
                                     GatewayName = VALUES(GatewayName),
                                     GWVersion = VALUES(GWVersion),
                                     GWCity = VALUES(GWCity),
                                     GWState = VALUES(GWState),
                                     GWCountry = VALUES(GWCountry),
                                     GWLAT = VALUES(GWLAT),
                                     GWLON = VALUES(GWLON),
                                     GWActivationtime = VALUES(GWActivationtime),
                                     GWActivationState = VALUES(GWActivationState),
                                     GWCurrentstatus = VALUES(GWCurrentstatus),
                                     GWuptime = VALUES(GWuptime),
                                     GWconnectededges = VALUES(GWconnectededges),
                                     GWCPU = VALUES(GWCPU),
                                     GWMemory = VALUES(GWuptime),
                                     GWload = VALUES(GWload),
                                     GWpki = VALUES(GWpki),
                                     GatewayType = VALUES(GatewayType),
                                     gw_flow_count = VALUES(gw_flow_count),
                                     gw_handoff = VALUES(gw_handoff),
                                     gw_tunnel = VALUES(gw_tunnel),
                                     geospecific = VALUES(geospecific),
                                     GWPostalCode  = VALUES(GWPostalCode)
                                      ;
                           """
            print(Date, GatewayID, GatewayName, GWVersion, GWCity, GWState,
                  GWCountry, GWLAT, GWLON, GWActivationtime, GWActivationState,
                  GWCurrentstatus, GWuptime, GWconnectededges, GWCPU, GWMemory,
                  GWload, GWpki, GatewayType, gw_flow_count, gw_handoff,
                  gw_tunnel, geospecific, GWPostalCode)
            if GatewayID:
                val = (Date, GatewayID, GatewayName, GWVersion, GWCity,
                       GWState, GWCountry, GWLAT, GWLON, GWActivationtime,
                       GWActivationState, GWCurrentstatus, GWuptime,
                       GWconnectededges, GWCPU, GWMemory, GWload, GWpki,
                       GatewayType, gw_flow_count, gw_handoff, gw_tunnel,
                       geospecific, GWPostalCode)
            mycursor.execute(query, val)
            cnx.commit()
            local_logger.info("Updated Gateway details")
            try:
                for edgelist in gw["connectedEdgeList"]:
                    EdgeID = edgelist["vceid"]
                    GatewayName = gw["name"]
                    GatewayID = gw["logicalId"]
                    #Date = date_start_string
                    Date = datetime.now().strftime('%Y-%m-%d 00:00:00')
                    #Date = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
                    query = """INSERT IGNORE INTO gatewayrelation (EdgeID, GatewayID, Date) 
                                     VALUES (%s, %s, %s)
                                     ON DUPLICATE KEY UPDATE
                                     Date= VALUES(Date)
                                      ;
                           """
                    #print (Date,GatewayID, EdgeID)
                    if EdgeID:
                        val = (EdgeID, GatewayID, Date)
                    mycursor.execute(query, val)
                    cnx.commit()
            except:
                pass
            local_logger.info("Updated Gateway Edge relation details")

    cnx.close()
Esempio n. 51
0
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    #print content_type, chat_type, chat_id
    if content_type is 'text':
        global is_chatting
        number = ""
        res = ""
        global location_area
        chat_id = msg['chat']['id']
        command = msg['text']
        print 'Got command: %s' % command
        is_chatting = True
        location_area = False
        if command.startswith('who are you') or command.startswith(
                '/start') or command.startswith('/help'):
            bot.sendMessage(
                chat_id,
                "How this works: \n1.We process your text and check for the service. \n2. We prompt you for your phone no. and location(google map). \n3.We confirm and deliver the service via OTP.... And its Done! "
            )
        if command.startswith('/human'):
            if not command[7:]:
                bot.sendMessage(
                    chat_id,
                    "Give me an input for example: /human Are you human?")
            else:
                s = EasyProcess(
                    "zenity --entry --text=' Revert Response for: " +
                    command[7:] + "'").call(timeout=7).stdout
                #inputText = subprocess.check_output("zenity --entry --text=' Revert Response for: "+command[7:]+"' || exit 0",shell=True,timeout=None)
                if not s:
                    bot.sendMessage(chat_id, "Admin not Available")
                else:
                    bot.sendMessage(chat_id, s)
        if not command.startswith('/') and is_chatting:
            command = command.lower()
            phraseExtracted = phe.extract_phrase(command)
            for i in range(len(phraseExtracted)):
                print phraseExtracted[i]
                if 'food' in phraseExtracted[i].split(" "):
                    phraseExtracted = ['cuisines']
                if 'vehicle' in phraseExtracted[i].split(" "):
                    phraseExtracted - ['vehicle_repair']
                if phraseExtracted[i] in open('location.txt').read():
                    loc_area = phraseExtracted[i]
                    someFunctoStoreValue(loc_area, "loc_area", chat_id)
                    location_area = True
                    goto.loction_acc
            prevTestValue, bit = [], False
            if phraseExtracted:
                print phraseExtracted
                for i in range(len(phraseExtracted)):
                    testValue, some_list, some_list, index = serv_decrp.match_serv_menu(
                        phraseExtracted[i].encode('ascii', 'ignore'))
                    prevTestValue.append(testValue)
                print testValue, some_list, some_list, index
                if True in prevTestValue:
                    if False in prevTestValue:
                        responseStr = "we tried to interpret your request, you meant " + phraseExtracted[
                            prevTestValue.index(
                                True
                            )] + "? but could'nt acknowledge this  " + phraseExtracted[
                                prevTestValue.index(
                                    False
                                )] + " try requesting without " + phraseExtracted[
                                    prevTestValue.index(False)]
                        bot.sendMessage(chat_id, responseStr)
                        testValue, bit = False, True
            else:
                testValue = False
            if len(phraseExtracted) == 0 or testValue is False:
                xs = command.strip()

                if xs.isdigit() and len(str(xs)) == 10:
                    number = str(xs)
                    goto.checkno
                else:
                    xs = command
                    listedcommand = command.split(" ")
                    count = 1
                    possibleValidReq = "i would send me some like to order would like to have want send me what is the score do you "
                    for word in range(len(listedcommand)):
                        if listedcommand[word] in possibleValidReq:
                            count = count + 1
                    print bit
                    if count > 2:
                        if bit is False:
                            print califResponse()
                            bot.sendMessage(chat_id, califResponse())
                            bit = True
                    if bit is False:
                        res = sillybot.respond(command, chat_id)
                        print "AIML resp> " + str(res)
                        if not res:
                            res = "Exploring web\n" + search.do_a_search(
                                command)
                            print "WEB Resp> " + str(res)
                        bot.sendMessage(chat_id, res)
                    dump(chat_id, command)
                    dump(chat_id, phraseExtracted)
                    dump(chat_id, res)
                    goto.exit
            if len(phraseExtracted) != 0 and testValue is True and isinstance(
                    some_list, list):
                #", ".join(str(e) for e in s)
                bot.sendMessage(
                    chat_id,
                    "\n".join(str(e).replace("`@`", '') for e in some_list))
                goto.exit
            if len(phraseExtracted) != 0 or testValue is True:
                if os.path.isfile(str(chat_id) + ".txt"):
                    dumpinfo(chat_id)
                someFunctoStoreValue(phraseExtracted, "phraseExtracted",
                                     chat_id)
                someFunctoStoreValue(command, "command", chat_id)
            if not location_area:
                print("Location is ", location_area)
                bot.sendMessage(chat_id, "Provide us with your location")
                label.loction_acc
                phraseExtracted = someFunctoFetchValue("phraseExtracted",
                                                       chat_id)
                command = someFunctoFetchValue("command", chat_id)
            if len(phraseExtracted) != 0 and location_area:
                print phraseExtracted, command, location_area
                #print (db_handle.queryCollection(phraseExtracted,command,loc_area))
                #if db_handle.queryCollection(phraseExtracted,command,loc_area)[0] == "Did you mean":
                #bot.sendMessage(chat_id, db_handle.queryCollection(phraseExtracted,command,loc_area))
                #print db_handle.queryCollection(phraseExtracted,command,loc_area)
                if db_handle.queryCollection(
                        phraseExtracted, command,
                        loc_area)[0] == "Item/Service not avail":
                    dump(chat_id, "Sorry, Item/Service is not availiable")
                    bot.sendMessage(chat_id,
                                    "Sorry, Item/Service is not availiable")
                    os.remove(str(chat_id) + ".txt")
                    goto.exit
                    print "exiting chat error service NA"
                elif isinstance(
                        db_handle.queryCollection(phraseExtracted, command,
                                                  loc_area), basestring):
                    bot.sendMessage(
                        chat_id,
                        db_handle.queryCollection(phraseExtracted, command,
                                                  loc_area))
                    goto.exit

                bot.sendMessage(
                    chat_id,
                    "Provide us your phone no, shortly we will be sending an OTP for verifying your identity"
                )
                goto.exit
                label.dispResult
                phraseExtracted = someFunctoFetchValue("phraseExtracted",
                                                       chat_id)
                command = someFunctoFetchValue("command", chat_id)
                loc_area = someFunctoFetchValue("loc_area", chat_id)
                Cust_phone = someFunctoFetchValue("cust_phone", chat_id)
                #print Cust_phone
                location = someFunctoFetchValue("cust_location", chat_id)
                Cust_location = ",".join(location.split(",")[1:4])
                #print phraseExtracted,command,loc_area,Cust_phone

                intrmList = db_handle.queryCollection(phraseExtracted, command,
                                                      loc_area)
                print intrmList
                strop = ""
                for i in range(len(intrmList)):
                    for k, v in intrmList[i].items():
                        if str(v) == '9998':
                            v = 'NA Req SERV'
                        strop += str(k) + " : " + str(v) + "\n"
                serv_prov_phone = re.findall("phone : ([0-9]{10})", strop)
                requirement = "CUST_PH: " + Cust_phone + "\n" + "SRV/ITM: " + ",".join(
                    phraseExtracted) + "\n" + "ADD: " + Cust_location
                #print serv_prov_phone,requirement
                for ser_phon in serv_prov_phone:
                    otp_sms.sendsmses(ser_phon, requirement)
                bot.sendMessage(chat_id, strop)
                dumpinfo(chat_id)
                label.checkno
            if len(number) != 0:
                phn_number = number
                someFunctoStoreValue(phn_number, "cust_phone", chat_id)
                otp_sms.get_otp(phn_number, chat_id)
                bot.sendMessage(
                    chat_id,
                    'Please type "/otp" and enter the 6-digit OTP you have recieved. For e.g. /otp 123456'
                )
                label.exit

        elif command.startswith('/otp') and is_chatting:
            print "Recieved OTP"
            if otp_sms.valid_otp(
                    int(re.match(r'/otp (\S+)', command).group(1)),
                    chat_id) is True:
                print "isValid OTP"
                bot.sendMessage(
                    chat_id,
                    'Your number has been verified, you will be contacted by the service provider shortly'
                )
                goto.dispResult
            else:
                print "isNOTValid OTP"
                bot.sendMessage(chat_id,
                                "Try again, send us your number again.")
        elif command.startswith('/search') or command.startswith(
                '/s') and not command.startswith('/start'):
            print "searching web"
            if command.startswith('/search'):
                term_to_search = command[8:]
                res = "Exploring web\n" + search.do_a_search(term_to_search)
                bot.sendMessage(chat_id, res)
            elif command.startswith('/s'):
                term_to_search = command[3:]
                res = "Exploring web\n" + search.do_a_search(term_to_search)
                bot.sendMessage(chat_id, res)

        else:
            pass
    elif content_type is 'location':
        loc = msg['location']
        loc_list = loc.values()
        loc_list_lat, loc_list_long = loc_list[0], loc_list[1]
        loc_str = str(loc_list_lat) + ',' + str(loc_list_long)

        geolocator = Nominatim()
        location = geolocator.reverse(loc_str, timeout=10)
        #location_area = str(str(location).split(",")[-5])
        someFunctoStoreValue(str(location), "cust_location", chat_id)
        location_list = str(location).split(',')
        max_index = len(location_list) - 1
        location_area = location_list[max_index - 4]
        print 'Location is %s' % location_area
        loc_area = location_area.lower().strip()
        someFunctoStoreValue(loc_area, "loc_area", chat_id)
        location_area = True
        goto.loction_acc

    elif content_type is 'audio':
        pass
    elif content_type is 'document':
        pass
    elif content_type is 'photo':
        pass
    elif content_type is 'sticker':
        pass
    elif content_type is 'video':
        pass
    elif content_type is 'voice':
        pass
    elif content_type is 'contact':
        pass
    elif content_type is 'venue':
        pass
    elif content_type is 'new_chat_member':
        pass
    elif content_type is 'left_chat_member':
        pass
Esempio n. 52
0
def staticLoc():
    #sends the city to main.py
    staticLoc.locationtomain = ""
    enterMode = True
    while enterMode:
        searchMode = input(
            "1 for entering city in CA, 2 for detecting your current location "
        )
        #Search specific city in CA
        if searchMode == '1':
            #print("u pressed 1")
            enterMode = False
            searchLocation = input(
                "Which place do u wanna search? (city in California) \n")
            with open("CAcities.csv", "r") as csv_file:
                csv_reader = csv.reader(csv_file, delimiter=',')
                checking = True
                for column in csv_reader:
                    if searchLocation.lower() == column[0].lower():
                        lat = column[4]
                        long = column[5]
                        la, lo = lat, long
                        checking = False
                        staticLoc.locationtomain = searchLocation

                if checking:
                    print(searchLocation, " is not a city in California")
                    exit(0)

            print("The coordinates of {0} seems to be ({1},{2})\n".format(
                searchLocation, la, lo))
            print("Opening map, please wait...")
            m = folium.Map([la, lo], zoom_start=12, tiles="Stamen Terrain")
            # Creating current marker
            folium.Marker(location=[la, lo],
                          popup="<b>You are here!</b>",
                          tooltip="Estimated Location",
                          icon=folium.Icon(icon="user")).add_to(m)
            folium.Circle(radius=8000,
                          location=[la, lo],
                          popup="Active Area",
                          color="#3186cc",
                          fill=True,
                          fill_color="#3186cc").add_to(m)
            folium.Marker(location=[37.00012445, -122.06221067580847],
                          popup='UC Santa Cruz',
                          icon=folium.Icon(color='red', icon='book')).add_to(m)
            folium.Circle(radius=1000,
                          location=[37.00012445, -122.06221067580847],
                          popup="Active Area",
                          color="#3186cc",
                          fill=True,
                          fill_color="#3186cc").add_to(m)
            m.add_child(folium.LatLngPopup())

            display(m)
            m.save('corona.html')
            webbrowser.open("corona.html", new=2)
        #Detect current location
        elif searchMode == '2':
            #print("U pressed 2")
            enterMode = False

            geolocator = Nominatim(user_agent="CruzHacks2021")
            g = geocoder.ip('Me')
            la = g.latlng[0]
            lo = g.latlng[1]
            lalo2geo = partial(
                geolocator.reverse, language="en"
            )  #Takes in lat/long from ip and translate to address
            print("\nIt seems that your current location is at:")
            print(lalo2geo("{0}, {1}".format(la, lo)))
            print("(Your estimated coordinates:", g.latlng, ")")
            #Takes the address (folium object) and extract the city name
            location = geolocator.reverse(str(la) + ', ' + str(lo),
                                          language="en")
            data = location.raw
            data = data['address']
            address = str(data)
            #print(address)
            l = []
            final = []
            x = address.find("'city': '")

            for k in range(x + 9, len(address)):
                if address[k] != "'":
                    l.append(address[k])
                else:
                    break
            final = [''.join(l)]
            staticLoc.locationtomain = final[
                0]  #put it to staticLoc.locationtomain which will be sent to main.py
            final.clear()  #clearing the list (optional)

            confirmLocation = input(
                "\nIs this ur current location?? (yes/no) ")
            if confirmLocation == "no":
                userInLocation = input("Please type in where you are at \n"
                                       ).lower()  #Custom location
                if userInLocation == "ucsc" or userInLocation == "uc santa cruz":
                    location = geolocator.geocode(
                        "University of California Santa Cruz")
                else:
                    location = geolocator.geocode(userInLocation)

                la = location.latitude
                lo = location.longitude

                print("\nI assume this should be more like it")
                print(location.address)

                #Takes the address (folium object) and extract the city name
                location = geolocator.reverse(str(la) + ', ' + str(lo),
                                              language="en")
                data = location.raw
                data = data['address']
                address = str(data)
                #print(address)
                l = []
                final = []
                x = address.find("'city': '")
                for k in range(x + 9, len(address)):
                    if address[k] != "'":
                        l.append(address[k])
                    else:
                        break
                final = [''.join(l)]
                staticLoc.locationtomain = final[
                    0]  #put it to staticLoc.locationtomain which will be sent to main.py
                final.clear()  #clearing the list (optional)

            print("Opening map, please wait...")

            #Creating the map
            m = folium.Map([la, lo], zoom_start=14, tiles="Stamen Terrain")
            # Creating current marker
            folium.Marker(location=[la, lo],
                          popup="<b>You are here!</b>",
                          tooltip="Current Location",
                          icon=folium.Icon(icon="user")).add_to(m)
            folium.Circle(radius=500,
                          location=[la, lo],
                          popup="Active Area",
                          color="#3186cc",
                          fill=True,
                          fill_color="#3186cc").add_to(m)
            folium.Marker(location=[37.00012445, -122.06221067580847],
                          popup='UC Santa Cruz',
                          icon=folium.Icon(color='red', icon='book')).add_to(m)
            folium.Circle(radius=1000,
                          location=[37.00012445, -122.06221067580847],
                          popup="Active Area",
                          color="#3186cc",
                          fill=True,
                          fill_color="#3186cc").add_to(m)
            m.add_child(folium.LatLngPopup())

            display(m)
            m.save('corona.html')
            webbrowser.open("corona.html", new=2)

        else:
            print("Thats not 1 or 2 dude...")


#staticLoc()
# voice ctrl?? or voice answer
#vaxination places nearby
Esempio n. 53
0
import json, requests
import geocoder
from geopy.geocoders import Nominatim
import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)


g = geocoder.ip('me')
geolocator = Nominatim()

location = geolocator.reverse("{}, {}".format(g.latlng[0],g.latlng[1]))
arr = location.address.split(',')
city = arr[3]

work = "Union+Beach+NJ"
workString = "Union Beach, NJ"
home = "Highland+Park+NJ"
homeString = "Highland Park, NJ"


base_url = "https://maps.googleapis.com/maps/api/directions/json?"
origin = city
destination1 = work
destination2 = home
api_key = "AIzaSyDRp4m_dZVS6eGY1bd2IyNJJ738YOEc3Qc"

url1 = base_url+"origin="+origin+"&"+"destination="+destination1+"&"+"key="+api_key
url2 = base_url+"origin="+origin+"&"+"destination="+destination2+"&"+"key="+api_key
response = requests.get(url1)
response2 = requests.get(url2)
Esempio n. 54
0
     lng = inp[endlat + 1:endlng].replace(',14', '')
 else:
     lng = inp[endlat + 1:endlng].replace(',13', '')
 lng = float(lng)
 print lng
 f = []
 if u'variants' in item.keys():
     print item[u'variants']
     for data in item[u'variants']:
         for ph in data[u'photos']:
             f.append(ph)
 else:
     f = []
 #r = u"Ζάκυνθος, Ιόνια Νησιά"
 geolocator = Nominatim()
 location = geolocator.reverse([lat, lng], timeout=10)
 ad = location.address
 if 'state_district' in location.raw['address'].keys():
     state_district = location.raw['address']['state_district']
 else:
     state_district = ' '
 r = state_district
 time.sleep(2)
 if elegxos_paralias(item[u'name'], lat, lng, ad, item[u'description'],
                     source, f) != 0:
     print 'Inserted to beachplus'
 else:
     sql1 = """INSERT INTO beach (name,lat,lon,region) VALUES (%s,%s,%s,%s)"""
     args1 = (item[u'name'], lat, lng, r)
     cursor.execute(sql1, args1)
     sql3 = """SELECT MAX(id) FROM beach"""
Esempio n. 55
0
  def get_property_pages(self, files):
    # default map size and zoom factor
    sizeMap = "512x512"
    zoomMap = "7"
  
    # test file type
    file = files[0]
    if len(files) != 1: return
    if file.get_uri_scheme() != 'file': return

    # if mimetype corresponds to JPG image, read data and populate tab
    mimetype = file.get_mime_type().split('/')
    if mimetype[0] in ('image'):
    
      # create tab
      self.tab = Gtk.Label('GPS')
      self.tab.show()

      # create grid
      self.grid = Gtk.Grid()
      self.grid.set_margin_start(10)
      self.grid.set_margin_end(10)
      self.grid.set_margin_top(5)
      self.grid.set_margin_bottom(5)
      self.grid.show()

      # create main scrolled window
      self.window = Gtk.ScrolledWindow()
      self.window.add_with_viewport(self.grid)
      self.window.show_all()

      # read metadata from file
      filename = unquote(file.get_uri()[7:])
      self.tags = Metadata()
      self.tags.open_path(filename)

      # get signed GPS position
      latitude  = self.tags.get_gps_latitude()
      longitude = self.tags.get_gps_longitude()
      altitude  = self.tags.get_gps_altitude()

      # if no GPS data, return
      if latitude == 0 and longitude == 0 and altitude == 0: return

      # trunk GPS data to 6 digits
      parts = str(latitude).split(".")
      strLatitude = parts[0] + "." + parts[1][:6]
      parts = str(longitude).split(".")
      strLongitude = parts[0] + "." + parts[1][:6]
      strAltitude = str(altitude)

      # generate GPS position
      strPosition = strLatitude + ',' + strLongitude

      # generate Google Maps links
      urlMaps = 'https://www.google.com/maps/place/' + strPosition + '/@' + strPosition + ',' + zoomMap + 'z/'
      urlJpeg = 'https://maps.googleapis.com/maps/api/staticmap?maptype=hybrid&zoom=' + zoomMap + '&size=' + sizeMap
      urlJpeg += '&center=' + strPosition + '&markers=' + strPosition + '&key=' + apikey
      
      # generate cache folder, and create if needed
      dirHomeCache = os.environ['HOME'] + '/.cache'
      dirGeotagCache = os.getenv('XDG_CACHE_HOME', dirHomeCache) + '/geotag'
      if not os.path.exists(dirGeotagCache): os.makedirs(dirGeotagCache)

      # generate cache file names
      fileMap = dirGeotagCache + '/map_' + strLatitude + '_' + strLongitude + '_' + sizeMap + '_' + zoomMap + '.png'
      fileDesc = dirGeotagCache + '/map_' + strLatitude + '_' + strLongitude + '.txt'

      # if description is not in the cache, retrieve it from Nominatim
      if not os.path.exists(fileDesc):
        # retrieve place description
        geolocator = Nominatim(user_agent="nautilus-exif-geotag")
        location = geolocator.reverse(strPosition)
        strDescription = location.address

        # write description to cache
        file = codecs.open(fileDesc, "w", "utf-8")
        file.write(strDescription)
        file.close()

      # if map is not in the cache, retrieve it from Google Maps
      if not os.path.exists(fileMap): urllib.request.urlretrieve(urlJpeg, fileMap)

      # retrieve description from cache
      file = codecs.open(fileDesc, "r", "utf-8")
      strDescription = file.read()
      file.close()

      # dislay GPS data
      self.DisplayText("<b>latitude</b>", 0, 0, 1, 1, "right")
      self.DisplayText(strLatitude, 1, 0, 1, 1, "left")
      self.DisplayText("<b>longitude</b>", 0, 1, 1, 1, "right")
      self.DisplayText(strLongitude, 1, 1, 1, 1, "left")
      self.DisplayText("<b>altitude</b>", 0, 2, 1, 1, "right")
      self.DisplayText(strAltitude, 1, 2, 1, 1, "left")

      # dislay address
      value = re.compile(',').sub('\n', strDescription)
      self.DisplayText("<b>address</b>", 2, 0, 1, 3, "right")
      self.DisplayText("<a href='" + urlMaps + "'>" + value + "</a>", 3, 0, 1, 5, "left")

      # dislay gmaps image
      self.DisplayImage(fileMap, 0, 5, 4, 1)

      # return label and tab content
      return Nautilus.PropertyPage( name="NautilusPython::geotag_info", label=self.tab, page=self.window ),
Esempio n. 56
0
def get_location_reversed(lat, lon):
    if geolocate_via_rest:
        locator = Nominatim(user_agent='geocoder')
        return locator.reverse(str(lat) + ', ' + str(lon))
    else:
        return 'foo'
Esempio n. 57
0
                        count=1,
                        geocode="40.0,-100.0,6mi")
print 'made request'
for tweet in tweets:
    tweets[count] = tweet
    count += 1
    print tweet.keys()
    sys.exit(0)
    print tweet['coordinates']

    #sleep right before hitting rate limit of twitter API. Sleep for 15 minutes
    if count % 17 == 0:
        print 'sleeping'
        for i in range(0, len(tweets)):
            classifications[i] = a.predict(tweets[i]['text'])
            s = geocoder.reverse(tweets[i]['coordinates'])

            #try to set state given output of reverse geocode. Else set it to None.
            try:
                if re.split(', ', s.encode(
                        "ascii", "ignore"))[3] == "United States of America":
                    geolocations[i] = re.split(', ',
                                               s.encode("ascii", "ignore"))[2]
            except:
                geolocations[i] = None
        for j in range(0, 50):
            json_result[j]["Num_Positive_Tweets"] = count(
                classifications[classifications == 1
                                and geolocations is not None])
            json_result[j]["Num_Negative_Tweets"] = count(
                classifications[classifications == 0
Esempio n. 58
0
def main():
    with open(filename) as bookmarks_file:
        data = bookmarks_file.read()
    
    geolocator = Nominatim()

    kml = simplekml.Kml()

    lst = list()
    
    # Hacky and doesn't work for all of the stars:
    lat_re = re.compile('markers:[^\]]*latlng[^}]*lat:([^,]*)')
    lon_re = re.compile('markers:[^\]]*latlng[^}]*lng:([^}]*)')
    coords_in_url = re.compile('\?q=(-?\d{,3}\.\d*),\s*(-?\d{,3}\.\d*)')
    
    doc = document_fromstring(data)
    for element, attribute, url, pos in doc.body.iterlinks():
        if 'maps.google' in url:
            description = element.text or ''
            print description.encode('UTF8')
            
            if coords_in_url.search(url):
                # Coordinates are in URL itself
                latitude = coords_in_url.search(url).groups()[0]
                longitude = coords_in_url.search(url).groups()[1]
            else:
                # Load map and find coordinates in source of page
                try:
                    sock = urlopen(url.replace(' ', '+').encode('UTF8'))
                except Exception, e:
                    print 'Connection problem:'
                    print repr(e)
                    print 'Waiting 2 minutes and trying again'
                    time.sleep(120)
                    sock = urlopen(url.replace(' ', '+').encode('UTF8'))
                content = sock.read()
                sock.close()
                time.sleep(3) # Don't annoy server
                try:
                    latitude = lat_re.findall(content)[0]
                    longitude = lon_re.findall(content)[0]
                except IndexError:
                    try:
                        lines = content.split('\n')  # --> ['Line 1', 'Line 2', 'Line 3']
                        for line in lines:
                            if re.search('cacheResponse\(', line):
                                splitline = line.split('(')[1].split(')')[0] + '"]'
                                # in the future we can extract the coordinates from here
                                null = None
                                values = eval(splitline)
                                print values[8][0][1]
                                longitude = str(values[0][0][1])
                                latitude = str(values[0][0][2])
                                continue
                    except IndexError:
                        print '[Coordinates not found]'
                        continue
                    print
            
            print latitude, longitude
            try:
    			location = geolocator.reverse(latitude+", "+longitude)
    			print(location.address)
            except ValueError:
                print '[Invalid coordinates]'
            print
            kml.newpoint(name=description, coords=[(float(longitude), float(latitude))])
            lst.append({'latitude': latitude,
                       'longitude': longitude,
                       'name': description,
                       'url': url.encode(encoding='utf-8', errors='replace'),
                       'address': location.address.encode(encoding='utf-8', errors='replace') if location else 'error'})

            # this is here because there's a tendancy for this script to fail part way through...
            # so at least you can get a partial result
            kml.save("GoogleBookmarks.kml")
            with open('GoogleBookmarks.json', mode='w') as listdump:
                listdump.write(json.dumps(lst))
        sys.stdout.flush()
Esempio n. 59
0
import csv
from geopy.geocoders import Nominatim
import pandas as pd
from geopy.point import Point
geolocator = Nominatim(timeout=5)
count = 0
with open('updatedlocation.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        count = count + 1
        print(count)
        lat = row[1]
        long = row[2]
        user_id = row[0]
        str1 = '"' + lat + ',' + long + '"'
        location = geolocator.reverse(Point(lat, long))
        address = location.address
        print(location.raw)
        try:
            city = location.raw['address']['city']
        except:
            city = ''
        df = pd.DataFrame({
            'id': [user_id],
            'lat': [lat],
            'long': [long],
            'address': [address],
            'city': [city]
        })
        df.to_csv('updatedlocation12.csv',
                  index=False,
def startTrip(data, driver):
    status = True
    error_msg = ''

    try:
    # raise error.Error(data)
        start_date = datetime.utcfromtimestamp(int(data['startDateTrip']) / 1000)
        job_id = None
        period = driver.getPeriodForDate(start_date)
        if period is None:
            period = models.Period(driver.getId(), start_date, data['trip']['start_mileage'])
            database.db_session.add(period)

        trip = database.db_session.query(models.Trip).filter(models.Trip.user_id == data['driverId'], models.Trip.start_date==start_date).first()
        if not trip:
            trip = database.db_session.query(models.Trip).filter(models.Trip.user_id == data['driverId'], models.Trip.start_date==(start_date + timedelta(hours=1))).first()
        if not trip:
            trip = database.db_session.query(models.Trip).filter(models.Trip.user_id == data['driverId'], models.Trip.start_date==(start_date - timedelta(hours=1))).first()
        if not trip:
            #TODO: CONFLICT! DISCUSS CASE WHEN ADDING TRIP TO CLOSED PERIOD? MIGHT HAPPEND DO WE ADD THIS TRIP TO DB OR PRINT EXCEPTION TO MOBILE
            # if period.getStatus() != 'opened':
            # 	raise error.Error('Period for this trip is closed')
            estimated_time = data['trip']['estimated_time']

            estimated_time = timedelta(seconds=estimated_time)

            #TODO: DISCUSS IF CHCECKIN START MILEAGE WITH PERIOD AND SURROUNDING TRIPS


            #TODO: CAN GET VALUES FROM data['lng'], data['lat'] below fields are useless
            start_lat = data['trip']['start_lat']
            start_lon = data['trip']['start_lon']
            start_mileage = data['trip']['start_mileage']

            if data['trip'].has_key('reason'):
                reason = data['trip']['reason']
            else:
                reason = 'Business'

            vehicle_reg_number = ''
            if data['trip'].has_key('vehicleRegistrationNumber') or int(data['trip']['jobId']) == -1:
                print data['trip']
                if int(data['trip']['jobId']) == -1:
                    vehicle_reg_number = 'NOJOB'
                else:                    
                    vehicle_reg_number = models.Vehicle.fixed_registration(data['trip']['vehicleRegistrationNumber'])

                v = database.db_session.query(models.Vehicle).filter_by(registration=vehicle_reg_number).first()
                if not v:
                    v = models.Vehicle(vehicle_reg_number, driver.getCompanyId())
                    database.db_session.add(v)
                    v = database.db_session.query(models.Vehicle).filter_by(registration=vehicle_reg_number).first()

            elif data['trip'].has_key('jobId'):
                job_id = data['trip']['jobId']
                vehicle_reg_number = database.db_session.query(models.Job).filter(models.Job.id == job_id).first().serialize['vehicle']['registration']
                v = database.db_session.query(models.Vehicle).filter_by(registration=vehicle_reg_number).first()
                
            address = "-"
            
            try:
                geolocator = Nominatim()
                location = geolocator.reverse(str(start_lat) + "," + str(start_lon))
                if location:
                    address = location.address

            except Exception, e:
                print "Geocoder timeout"

            trip = models.Trip(address, estimated_time, start_date, start_lat, start_lon, start_mileage, reason, 'active', driver.getId(), period.getStart_date(), vehicle_reg_number, period.getId(), v.id, job_id)
            payroll = models.Payroll.create_if_not_exist(driver, start_date.strftime('%Y-%m-%d'), start_date)

            database.db_session.add(trip)
            driver.setOn_trip(True)
    except Exception, e:
        error_msg += traceback.format_exc()
        status = False
        print 'Star trip error'+ e.message