def get_weather_loc(self):
        # gbrd : solar radiation / prcp : precipication
        weather = pd.read_csv("./data/sudeste.csv", usecols=['wsnm','lat','lon','city','prov','yr','mo','da','hr',
                                                             'temp','dewp','hmdy','gbrd','prcp'])
        weather = weather[weather.yr == 2016]
        weather = weather[((weather.mo == 4) | (weather.mo == 5) | (weather.mo == 6))]
        weather.to_csv("./data/sudeste_relevant.csv")
        weather_locs = weather.drop_duplicates('wsnm')
        weather_locs[['wsnm','city','prov','lat','lon']].to_csv('./data/weather_checker_loc.csv', index=False)

        # get location of hospitals
        loc_dict = {'location':[], 'latitude':[], 'longitude':[]}
        weather = pd.read_csv("./data/no_show.csv")
        weather = weather.drop_duplicates('Neighbourhood')
        hospital_locs = weather.Neighbourhood
        geolocator = Nominatim(timeout=10)
        geolocator.urlopen = uo
        for locs in hospital_locs :
            loc_dict['location'].append(locs)
            if locs == 'BELO HOR. (PAMPULHA)':
                loc = geolocator.geocode('PAMPULHA')
            else :
                loc = geolocator.geocode(locs)
            if loc == None :
                print(locs, 'None')
                loc_dict['latitude'].append(np.nan)
                loc_dict['longitude'].append(np.nan)
            else :
                print(locs, loc.latitude, loc.longitude)
                loc_dict['latitude'].append(loc.latitude)
                loc_dict['longitude'].append(loc.longitude)
        loc_df = pd.DataFrame(loc_dict)
        loc_df = loc_df.fillna(loc_df.mean())
        loc_df.to_csv('./data/hospital_loc.csv', index=False)
Esempio n. 2
0
def get_geolocator():
    geolocator = Nominatim()
    requester = geolocator.urlopen
    def requester_hack(req, **kwargs):
        req = Request(url=req, headers=geolocator.headers)
        return requester(req, **kwargs)
    geolocator.urlopen = requester_hack
    return geolocator
Esempio n. 3
0
def prueba():
	#Verificar la existencia del archivo
	import os
	namepath = '/home/mb.csv' # se descarga el archivo en la ruta namepath
	if os.path.exists(namepath):
	  os.remove(namepath) # se actualiza el archivo, es decir, si existe se reemplaza por otro 

	# Descargar el archivo del metrobus
	import wget
	url = 'https://datos.cdmx.gob.mx/explore/dataset/prueba_fetchdata_metrobus/download/?format=csv&timezone=America/Mexico_City&lang=es&use_labels_for_header=true&csv_separator=%2C' #link del archivo
	wget.download(url,namepath) #sentencia para descargar el archivo

	# Leer el archivo
	import pandas as pd
	df = pd.read_csv(namepath) # se lee el archivo en u ndataframe
	
	
	# indica en geographic_poiint  la columna que tiene las coordenadas para cada instancia
	coords = df["geographic_point"]

	# Identificar a partir de la posicion a que alcaldia pertenece
	from geopy.geocoders import Nominatim # se hace uso de una ibreria llamada geopy
	import certifi
	from six.moves import urllib
	def uo(args, **kwargs):
		return urllib.request.urlopen(args, cafile=certifi.where(), **kwargs)
	geolocator = Nominatim()
	geolocator.urlopen = uo # indica po medio de una solicitud http la posicion en el mapa de esas coordenadas dando una serie de datos como pais, estado, ciudad, alcaldia, colonia, calle, codigo postal  

	alcaldia = []
	for i in coords: # se genera una lista de las alcaldias de cada instancia
		try:
			location = geolocator.reverse(i, language='en')
			li = list(location.address.split(","))
			alcaldia.append(li[len(li)-4]) # se alamcena la alcaldia
			#print(k)
			
		except:
			alcaldia.append("") # en caso contrario no se considera alaldia alguna.
	df['alcaldia'] = alcaldia


	# se almacena en Mongodb
	from flask import Flask
	from flask_graphql import GraphQLView
	from pymongo import MongoClient
	import graphene

	client =  MongoClient("mongodb+srv://toto:[email protected]/toto?retryWrites=true&w=majority") # dirección de mi base de datos
	db = client['toto']
	collection = db['toto']
	df.reset_index(inplace=True)
	data_dict = df.to_dict("records")
	collection.insert_many(data_dict)

	#se repite el proceso
	return True
	time.sleep(3600) # una hora
Esempio n. 4
0
def get_geolocator():
    geolocator = Nominatim()
    requester = geolocator.urlopen

    def requester_hack(req, **kwargs):
        req = Request(url=req, headers=geolocator.headers)
        return requester(req, **kwargs)

    geolocator.urlopen = requester_hack
    return geolocator
Esempio n. 5
0
def index():
    form = InputForm(request.form)
    if request.method == 'GET':
        return render_template('index.html',
                               lat=40.703312,
                               lng=-73.97968,
                               form=form)

    if request.method == 'POST':
        #  Handling the start and destination address
        try:
            start_address = request.form['start_address']
            destination = request.form['destination_address']
            geolocator = Nominatim()
            geolocator.urlopen = uo
            start_location = geolocator.geocode(start_address)
            start_lat = start_location.latitude
            start_lng = start_location.longitude

            dest_location = geolocator.geocode(destination)
            destination_lat = dest_location.latitude
            destination_lng = dest_location.longitude

            return render_template('index.html',
                                   lat=start_lat,
                                   lng=start_lng,
                                   start_lat=start_lat,
                                   start_lng=start_lng,
                                   destination_lat=destination_lat,
                                   destination_lng=destination_lng,
                                   form=form)
        except:
            errors = "At least one of the addresses you entered is invalid. Please try again."
            return render_template('index.html',
                                   lat=40.703312,
                                   lng=-73.97968,
                                   form=form,
                                   errors=errors)
Esempio n. 6
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. 7
0
import urllib
import pandas as pd
import certifi
from geopy.geocoders import Nominatim
from urllib.request import urlopen


# install geopy to return location, https://pypi.python.org/pypi/geopy
# Prepare to return location
def uo(args, **kwargs):
    return urllib.request.urlopen(args, cafile=certifi.where(), **kwargs)


geolocator = Nominatim()
geolocator.urlopen = uo

# read in csv
security_alert_df = pd.read_csv('security.csv')
security_alert_df = security_alert_df.dropna()

# change data type
security_alert_df["Address"] = security_alert_df["Address"].astype(str)
security_alert_df["Date"] = security_alert_df["Date"].astype(int)
security_alert_df["Time"] = security_alert_df["Time"].astype(int)
security_alert_df["PrimaryType"] = "ROBBERY"

# get latitude and longitude, and store in list
Latitude_list = []
Longitude_list = []
for address in security_alert_df["Address"]:
Esempio n. 8
0
    def fare_estimate(self, cust_id, device_id, token):
        url = "https://auth.rapido.bike/om/api/orders/v2/rideAmount"
        geolocator = Nominatim()
        geolocator.urlopen = uo
        print('Fare Estimate - ')
        pickup_location = ''
        drop_location = ''
        drop = ''
        try:
            while 1:
                pickup = str(input('Enter Pickup: '))
                drop = str(input('Enter Drop: '))
                pickup_location = geolocator.geocode(pickup)
                drop_location = geolocator.geocode(drop)
                #print(pickup_location)
                print(drop_location)
                break
        except:
            print('Exception - ')
            raise

        payload = "{\"pickupLocation\":{\"addressType\":\"\",\"address\":\"" + pickup_location.address.split(',')[
            0] + "\",\"lat\":" + str(pickup_location.latitude) + ",\"lng\":" + str(
            pickup_location.longitude) + ",\"name\":\"\"},\"dropLocation\":{\"addressType\":\"\",\"address\":\"" + \
                  drop_location.address.split(',')[0] + "\",\"lat\":" + str(drop_location.latitude) + ",\"lng\":" + str(
            drop_location.longitude) + ",\"name\":\"" + drop + "\"},\"serviceType\":\"57370b61a6855d70057417d1\",\"customer\":\"" + cust_id + "\",\"couponCode\":\"\",\"paymentType\":\"paytm\"}"

        headers = {
            'deviceid': device_id,
            'latitude': self.latitude,
            'longitude': self.longitude,
            'appid': "2",
            'currentdatetime': self.current_datetime,
            'internet': "0",
            'appversion': "73",
            'Authorization': "Bearer " + token,
            'Content-Type': "application/json; charset=UTF-8",
            'Content-Length': "501",
            'Host': self.host,
            'Connection': "Keep-Alive",
            'Accept-Encoding': "gzip",
            'User-Agent': "okhttp/3.6.0",
            'Cache-Control': "no-cache"
        }

        response = requests.post(url, data=payload, headers=headers).json()

        print(response['info']['message'])
        request_id = response['data']['requestId']
        time_in_min = response['data']['timeInMts']
        quotes = response['data']['quotes']
        service_id = []
        amount_lst = []
        print('Rider is in ' + str(time_in_min) + ' mins.')
        for value in quotes:
            service_idd = value['serviceId']
            service_id.append(service_idd)
            amount = value['amount']
            amount_lst.append(amount)
            print('Service ID : ' + service_idd)
            print('Fare estimated - ' + str(amount))

        print('service id index - 0 or 1 n so on')
        print(service_id)
        print(amount_lst)

        service_user_selection = int(input('Select Service : '))
        final_service_id = service_id[service_user_selection]
        wallet_type = ['rapido', 'paytm', 'cash']
        print('Select your wallet type as per INDEX of list')
        for wall in range(0, len(wallet_type)):
            print('%i - %s' % (wall, wallet_type[wall]))
        wallet_type_index = int(input('Select from above: '))
        wallet_selected = wallet_type[wallet_type_index]
        print(response)
        while 1:
            resp = self.book_ride(device_id, token, final_service_id, pickup_location, drop_location, cust_id,
                                  request_id, wallet_selected)
            print(resp)
            if resp['info']['status'] == 'success':
                print(resp)
                callback_url = resp['callback_url']
                order_id = resp['data']['_id']
                self.get_details(callback_url, device_id, token)

                yes_or_no = input('Do you want to cancel ride? 1 - Yes | 0 - No ')
                if yes_or_no == 1:
                    self.cancel_booking(order_id, cust_id, token, device_id, pickup_location)
                break
Esempio n. 9
0
	# Leer el archivo
	import pandas as pd
	df = pd.read_csv(namepath) # se lee el archivo en u ndataframe

	# indica en geographic_poiint  la columna que tiene las coordenadas para cada instancia
	coords = df["geographic_point"]

	# Identificar a partir de la posicion a que alcaldia pertenece
	from geopy.geocoders import Nominatim # se hace uso de una ibreria llamada geopy
	import certifi
	from six.moves import urllib
	def uo(args, **kwargs):
		return urllib.request.urlopen(args, cafile=certifi.where(), **kwargs)
	geolocator = Nominatim()
	geolocator.urlopen = uo # indica po medio de una solicitud http la posicion en el mapa de esas coordenadas dando una serie de datos como pais, estado, ciudad, alcaldia, colonia, calle, codigo postal  

	alcaldia = []
	for i in coords: # se genera una lista de las alcaldias de cada instancia
		try:
			location = geolocator.reverse(i, language='en')
			li = list(location.address.split(","))
			alcaldia.append(li[len(li)-4]) # se alamcena la alcaldia
			#print(k)
			
		except:
			alcaldia.append("") # en caso contrario no se considera alaldia alguna.
	df['alcaldia'] = alcaldia


	# se almacena en Mongodb
# of SSL certificates while verifying the identity of TLS hosts
import urllib  #urllib is a package that collects several modules for working with URLs
import ssl  #This module provides some more Pythonic support for SSL


#here, we pass the certifi certificates into the urlopen method to avoid errors of no certifications
def link(args, **kwargs):
    # We create an ssl context to fetch the certificate provided by the local server without performing validation of
    # the certificate
    ssl_context = ssl.create_default_context(cafile=certifi.where())
    return urllib.request.urlopen(args, context=ssl_context)


geolocator = Nominatim(user_agent="Golf Caddie App")
#print(link)
geolocator.urlopen = link

location1 = geolocator.geocode(
    input("Enter your current location address (Where is the ball?):"))
print("Point A: ", location1.address)
print("latitude is :-", location1.latitude, "\nlongtitude is:-",
      location1.longitude, "\n")
location1_coord = location1.latitude, location1.longitude

location2 = geolocator.geocode(
    input("Enter your destination address (Where is the hole?): "))
print("Point B:", location2.address)
print("latitude is :-", location2.latitude, "\nlongtitude is:-",
      location2.longitude, "\n")
location2_coord = location2.latitude, location2.longitude