示例#1
0
文件: yelp.py 项目: balusarakesh/yelp
def get_city_from_address(address):
    try:
        geolocator = Nominatim()
        location = geolocator.geocode(address)
        if not location:
            for each in address.split(','):
                location = geolocator.geocode(each)
                if location:
                    break
        if location:
            lat = location.raw['lat']
            lon = location.raw['lon']
        else:
            print 'address invalid'
            return
        url = "http://maps.googleapis.com/maps/api/geocode/json?"
        url += "latlng=%s,%s&sensor=false" % (lat, lon)
        v = str(requests.get(url).content)
        j = json.loads(v)
        components = j['results'][0]['address_components']
        town = None
        for c in components:
            if "locality" in c['types']:
                town = c['long_name']
                if town:
                    break
        return town
    except Exception, e:
        print str(e) + 'get_location'
        set_db_status(False)
示例#2
0
    def getLatLon(codigo_pais, latitud, longitud, localizacion):
        result = ""
        # From twitter
        if latitud != "" and longitud !="":
            # OK
            result = [latitud, longitud, "T"]
        else:
            # Nominatim request
            geolocator = Nominatim()
            if(localizacion != ""):
                # Nominatim
                latlong_loc = geolocator.geocode(localizacion, timeout=60)
                if (latlong_loc != None) and (latlong_loc != ""):
                    result = [latlong_loc.latitude, latlong_loc.longitude ,"L"]
                else:
                    # Nominatim
                    latlong_cod = geolocator.geocode(codigo_pais, timeout=60)
                    if (latlong_cod != None) and (latlong_cod != ""):
                        result = [latlong_cod.latitude, latlong_cod.longitude ,"LM"]
            elif(codigo_pais != ""):
                # Nominatim
                latlong_cod = geolocator.geocode(codigo_pais, timeout=60)
                if (latlong_cod != None) and (latlong_cod != ""):
                    result = [latlong_cod.latitude, latlong_cod.longitude ,"C"]

        return result
示例#3
0
    def get_latlon2(self):
        lat=[]
        lon=[]
        missed_point=[]
        geolocator = Nominatim()
        name_seq = self.get_namepop2()['name']
        
        Tokorozawa = geolocator.geocode('Tokorozawa')
        for i, name in enumerate(name_seq):
            #name = str(name)
            geocoded = geolocator.geocode(name)
            if type(geocoded) != type(Tokorozawa):
                lat.append('missed to get data')
                lon.append('missed to get data')
                print i, name, 'missed to get data'
                missed_point.append(i)
                continue
            #lat.append(geolocator.geocode(name).latitude)
            #lon.append(geolocator.geocode(name).longitude)
            lat.append(geocoded.latitude)
            lon.append(geocoded.longitude)

            print i, name, lat[i], lon[i]
        print 'missed points are ', missed_point
        return pd.DataFrame({'lat':lat, 'lon':lon})
示例#4
0
文件: func.py 项目: rmnvncnt/carbone
def get_distance_infos(product, uloc='Paris'):
    ''' get the shortest distance between production location
    and user location (default = Paris)
    out: float
    '''
    # get product origins
    # from the most accurate to the least
    if product['manufacturing_places_tags']:
        places = product['manufacturing_places_tags']
    elif product['origins']:
        places = product['origins']
    else:
        places = product['countries']

    # str to list
    if isinstance(places, str):
        places = [places]

    # get geolocation
    geolocator = Nominatim()
    locations = [geolocator.geocode(p) for p in places if p]
    ucoord = geolocator.geocode(uloc)

    # get distances
    distances = []
    for floc in locations:
        distance = vincenty((ucoord.latitude, ucoord.longitude),
                            (floc.latitude, floc.longitude)).kilometers
        distances.append(distance)

    # return shortest distance
    distance = min(distances)
    return distance, places
    def handle(self, *args, **options):
        geolocator = Nominatim()

        for i in filter(lambda x: x.location.strip(), Event.objects.filter(location__isnull=False)):
            if LocationCache.objects.filter(string=i.location).exists():
                location = LocationCache.objects.get(string=i.location)
            else:
                time.sleep(5)
                try:
                    location = geolocator.geocode(i.location)
                except GeocoderTimedOut:
                    location = None

                if location is None and re.search("\(.*\)", i.location):
                    time.sleep(5)
                    try:
                        location = geolocator.geocode(re.search("(\(.+\))", i.location).group()[1:-1])
                    except GeocoderTimedOut:
                        location = None

                location = LocationCache.objects.create(
                    string=i.location,
                    lat=location.latitude if location else None,
                    lon=location.longitude if location else None,
                )

                if (location.lat, location.lon) == (None, None):
                    continue

            i.lon = location.lon
            i.lat = location.lat
            i.save()
示例#6
0
def get_nearest(user_location, address_list, maps_api_key=None):
    ''' This function returns the nearest address to the
        user location. It compares the user location with an
        existing list of addresses.

        Args:
            user_location (string): can be either the zip code or address.
            maps_api_key (string): optional google maps api key.
            address_list (tuple): list of addresses

        Returns:
            string: the nearest address to the user_location or false if the address list is not a tuple
    '''
    if isinstance(address_list, tuple):
        if len(address_list) > 1:
            if maps_api_key:
                geolocator = GoogleV3(maps_api_key, timeout=10)
            else:
                geolocator = Nominatim(timeout=10)
            user_location = geolocator.geocode(user_location)
            user_latlon = (user_location.latitude, user_location.longitude)
            geo_locations = [geolocator.geocode(address) for address in address_list]
            distance_dict = {vincenty((address.latitude, address.longitude),
                              user_latlon).miles: address for address in geo_locations if address is not None}
            min_distance = min(distance_dict.items(), key=lambda k: k[0])
            return min_distance[1].address
        raise Exception('Tuple must be contain more than one address')
    raise TypeError('Second parameter must be a tuple')
示例#7
0
 def find_coordinates(self):
     """Get and set longitude and Latitude
     
     Scrape individual posting page, if no
     coordinates are found, cascade precision
     (try location, try user_location, or set
     to zero).  Returns an array, first 
     latitude and then longitude.
     """
     self.coordinates = []
     geolocator = Nominatim()
     follow_this = self.url
     follow_page = requests.get(follow_this)
     follow_soup = BeautifulSoup(follow_page.text, "html.parser")
     location = follow_soup.find("div", class_="viewposting")
     if location is not None: # Get from Page
         lat = location['data-latitude']
         lon = location['data-longitude']
     else:
         try: # Get from posted location
             lat = geolocator.geocode(self.location).latitude
             lon = geolocator.geocode(self.location).longitude
         except:
             try: # Get from user locatoin
                 lat = geolocator.geocode(self.user_location).latitude
                 lon = geolocator.geocode(self.user_location).longitude
             except:
                 lat = 0 #38.9047 # This is DC
                 lon = 0 #-77.0164
     self.coordinates.append(lat)
     self.coordinates.append(lon)
示例#8
0
def login():
    login_name = request.json['email']
    service = request.json['service']
    password = request.json['password']
    location = request.json['location']

    geolocator = Nominatim()
    location = geolocator.geocode(location)
    if not location:
        location = geolocator.geocode("Düsseldorf, Germany")
    latitude = location.latitude
    longitude = location.longitude

    try:
        logged_in = pokeapi.login(service, login_name, password,
                                  lat=latitude, lng=longitude, app_simulation=True)
    except AuthException as e:
        return jsonify({'status': 'error',
                        'message': e.__str__()})

    time.sleep(1)
    if not logged_in:
        return jsonify({'status': 'error',
                        'message': 'Failed to login. If the Pokemon GO Servers are online, your credentials may be wrong.'})
    else:
        return jsonify({'status': 'ok'})
示例#9
0
def CalculateDistance(place,city_list,Path_to_Take,Distance,length):
	geolocator = Nominatim();
	main_location = geolocator.geocode(place, exactly_one=True, timeout=None);
	main_coordinates = (main_location.latitude,main_location.longitude);
	list_toSort = [];
	for x in city_list:
		place_location = geolocator.geocode(x, exactly_one=True, timeout=None);
		place_coordinates = (place_location.latitude,place_location.longitude);
		print place_coordinates;
		result = vincenty(main_coordinates,place_coordinates).miles
		print result;
		list_toSort.append({'Name':x,'Distance':result});
	newlist = sorted(list_toSort, key=lambda k: k['Distance']) 
	Path_to_Take.append(newlist[1]);
	Distance += newlist[1]['Distance']
	city_list.remove(city_list[0]);
	if len(Path_to_Take) < length:
		CalculateDistance(newlist[1]['Name'],city_list,Path_to_Take,Distance,length);
	if len(Path_to_Take) == length:
		main_location = geolocator.geocode(Path_to_Take[0]['Name'], exactly_one=True, timeout=None);
		main_coordinates = (main_location.latitude,main_location.longitude);
		place_location = geolocator.geocode(Path_to_Take[length-1]['Name'], exactly_one=True, timeout=None);
		place_coordinates = (place_location.latitude,place_location.longitude);
		result = vincenty(main_coordinates,place_coordinates).miles
		Path_to_Take.append({'Name':Path_to_Take[0]['Name'],'Distance':result});
示例#10
0
def get_start_end(start,end):
	geolocator = Nominatim()
	location = geolocator.geocode(start)
	#put in exceptions if geolocator doesn't work
	start_tuple = (location.latitude, location.longitude)
	location = geolocator.geocode(end)
	end_tuple = (location.latitude, location.longitude)
	return start_tuple, end_tuple
示例#11
0
def loclook(locations):
    geocator = Nominatim()
    coordinates = [ geocator.geocode(x) 
                    for x in locations
                    if geocator.geocode(x) != None ]
    cleancords = [ (x.latitude,x.longitude)
                    for x in coordinates ]
    return cleancords
示例#12
0
文件: app.py 项目: rnagle/cultivate
def real_search():
    query = dict(request.form)
    querystring = get_querystring_from_params(query['term[]'])

    geocoder = Geocoder()
    # loc = geocode(query['city'][0])
    loc = geocoder.geocode(query['city'][0], timeout=10)

    tweets = tweepy.Cursor(
        api.search,
        q=querystring,
        rpp=100,
        result_type="recent"
    ).items(app.config['TWEET_LIMIT'])

    users = {}
    skip = []
    for tweet in tweets:
        user = users.get(tweet.author.screen_name)
        screen_name = tweet.author.screen_name

        if screen_name in skip:
            continue

        if user is None:
            location = tweet.author.location.encode("utf-8")
            user_loc = geocoder.geocode(location, timeout=10)
            print "address: {}".format(location)
            if user_loc and loc:
                if distance(user_loc.longitude, user_loc.latitude, loc.longitude, loc.latitude) < 30:
                    counts = {
                        'statuses': tweet.author.statuses_count,
                        'listed': tweet.author.listed_count,
                        'friends': tweet.author.friends_count,
                        'followers': tweet.author.followers_count,
                        'total_tweets': 0,
                        'total_favorited': 0,
                        'total_retweeted': 0
                    }
                    users[screen_name] = tweet.author._json
                    users[screen_name]['counts'] = counts
                else:
                    skip.append(screen_name)
                    continue
            else:
                skip.append(screen_name)
                continue

        users[screen_name]['counts']['total_tweets'] += 1
        users[screen_name]['counts']['total_favorited'] += tweet.favorite_count
        users[screen_name]['counts']['total_retweeted'] += tweet.retweet_count

    for screen_name in users.keys():
        users[screen_name]['score'] = get_score(users[screen_name], query)

    return Response(json.dumps({'users': users}), mimetype='application/json')
示例#13
0
def returnCountryFromGeoCode(countryParam):
  from geopy.geocoders import Nominatim
  geolocator = Nominatim()
  country_name = 'UNKNOWN'
  countryParam = countryParam.strip()
  loc_dict={
                   'AK':'USA', 'AL':'USA', 'AR':'USA', 'AZ':'USA', 'CA':'USA', 'CO':'USA', 'CT':'USA', 'DC':'USA', 'DE':'USA', 'FL':'USA',
                   'GA':'USA', 'HI':'USA', 'ID':'USA', 'IA':'USA', 'IL':'USA', 'IN':'USA', 'KS':'USA', 'KY':'USA', 'LA':'USA', 'MA':'USA',
                   'MD':'USA', 'ME':'USA', 'MI':'USA', 'MN':'USA', 'MO':'USA', 'MS':'USA', 'MT':'USA', 'NC':'USA', 'ND':'USA', 'NE':'USA',
                   'NH':'USA', 'NJ':'USA', 'NV':'USA', 'NY':'USA', 'OH':'USA', 'OK':'USA', 'OR':'USA', 'PA':'USA', 'RI':'USA', 'NM':'USA',
                   'SC':'USA', 'SD':'USA', 'TN':'USA', 'TX':'USA', 'UT':'USA', 'VA':'USA', 'VT':'USA', 'WA':'USA', 'WI':'USA', 'WV':'USA',
                   'WY':'USA', 'USA':'USA', 'United States':'USA', 'United States of America':'USA', 'London':'UK', 'Bangladesh':'Bangladesh',
                   'Nepal':'Nepal', 'Brazil':'Brazil', 'India':'India', 'Israel':'Israel', 'Canada':'Canada', 'Belgium':'Belgium',
                   'Lebanon':'Lebanon', 'Austria':'Austria', 'Ireland':'Ireland', 'Germany':'Germany', 'Spain':'Spain', 'Colombia': 'Colombia',
                   'China':'China', 'Greece':'Greece', 'Netherlands':'Netherlands', 'Malaysia':'Malaysia', 'Indonesia':'Indonesia',
                   'Italy':'Italy', 'Pakistan':'Pakistan', 'Bulgaria':'Bulgaria', 'New Zealand':'New Zealand', 'New Hampshire':'USA',
                   'Washington':'USA', 'Switzerland':'Switzerland', 'Japan':'Japan', 'Portugal':'Portugal', 'United Kingdom':'UK',
                   'Sri Lanka':'Sri Lanka', 'Illinois':'USA', 'Chile':'Chile', 'Serbia':'Serbia', 'Turkey':'Turkey', 'Romania':'Romania',
                   'Czech Republic':'Czech Republic', 'South Korea':'South Korea', 'Australia':'Australia', 'Croatia':'Croatia',
                   'Europe':'EU', 'Hungary':'Hungary', 'Russia':'Russia', 'South Africa':'South Africa', 'Poland':'Poland', 'UK':'UK',
                   'Vietnam':'Vietnam', 'France':'France', 'Pune':'India', 'Ukraine':'Ukraine', 'Malta':'Malta', 'Republic of Latvia':'Latvia',
                   'Latvia':'Latvia', 'Sweden':'Sweden', 'Slovenia':'Slovenia'
                 }
  if "," in countryParam:
    ctr_str = countryParam.split(",")[-1]
    #print ":-/",ctr_str
    ctr_str = ctr_str.strip()
    if ctr_str in loc_dict:
        #print "accessed customized dict"
        country_name = loc_dict[ctr_str]
    else:
        #print "API ACCESS !"
        location = geolocator.geocode(unicode(ctr_str, 'latin-1'), timeout=3600)
        if location == None:
           country_name = 'UNKNOWN'
        else:
           country_name  = location.raw['display_name'].split(',')[-1]
  else:
    if countryParam in loc_dict:
        #print "accessed customized dict"
        country_name = loc_dict[countryParam]
    else:
        #print "API ACCESS !"
        location = geolocator.geocode(unicode(countryParam, 'latin-1'), timeout=3600)
        if location == None:
            country_name = 'UNKNOWN'
        else:
            country_name  = location.raw['display_name'].split(',')[-1]

  #country_name = unicode(country_name, 'latin-1')
  try:
        country_name = unicode(country_name, 'latin-1')
  except TypeError:
        country_name = 'UNKNOWN'
  return country_name
示例#14
0
def filtering(type,data,value):
    l=value[1:]
    result=[]
    for line in l:
        lines=re.split("\s+",line)
        print lines
        latitude_file=lines[3]
        longitude_file=lines[4]
        time_file=lines[6]
        date_file=lines[5]
        print latitude_file,longitude_file,time_file,date_file
        
        if type=='time':
            comparator=datetime.datetime.strptime(data, "%H:%M:%S")
            if re.findall('[0-9]+:[0-9]+:[0-9][0-9]$',time_file):
                c=datetime.datetime.strptime(time_file, "%H:%M:%S")
                print "C",c,comparator
                if c == comparator:
                    #print "here"
                    result.append(line)

        elif type=='date':
            comparator=datetime.datetime.strptime(data, "%m/%d/%y").date()
            if re.match('[0-9]+/[0-9]+/[0-9]+$',date_file):
                #print value
                date=datetime.datetime.strptime(date_file, "%m/%d/%y").date()
                if comparator == date:
                    result.append(line)

        elif type=='location':
            geolocator = Nominatim()
            location = geolocator.geocode("175 5th Avenue NYC")
            try:
                comparator=data.lower()
                geolocator = Nominatim()
                location = geolocator.geocode(comparator)
                latitude=location.latitude
                longitude=location.longitude
                print latitude,longitude
                if latitude==latitude_file and longitude==longitude_file:
                    print "ayyy"
                    result.append(line)
            except:
                print "lakd"
            for value in lines:
                if comparator==value:
                    result.append(line)

    return result




    #android_test(result)
    del result[:] 
def calculate_distance(add1, add2):
    geolocator = Nominatim()
    
    location1 = geolocator.geocode(add1)
    al1 = (location1.latitude, location1.longitude)
    
    location2 = geolocator.geocode(add2)
    al2 = (location2.latitude, location2.longitude)
    
    distce = vincenty(al1, al2).miles
    return distce
 def closest(cls, city):
     try:
         geolocator = Nominatim()
         location = geolocator.geocode(str(city))
         code1 = (location.latitude, location.longitude)
         schools = {i: geolocator.geocode(i) for i in cls.cities}
         code2 = {i: (schools[i].latitude, schools[i].longitude) for i in schools}
         closest = {i: vincenty(code1, code2[i]).km for i in code2}
         return sorted(closest, key=closest.get)[0]
     except:
         return "Budapest"
示例#17
0
文件: wit.py 项目: vied12/nsfw
def get_air_quality(request):
    geolocator = Nominatim()
    context = request['context']
    entities = request['entities']
    loc = first_entity_value(entities, 'location')

    for context_key in (
        'missingLocation',
        'outOfGermany',
        'subscribed',
        'notSubscribed',
        'location',
        'station',
        'stationId',
        'lastAlertValue',
        'lastAlertDate',
        'kind',
        'clean',
        'notFound',
    ):
        try:
            del context[context_key]
        except KeyError:
            pass
    if not loc:
        context['missingLocation'] = True
    else:
        loc = geolocator.geocode(loc, language='en')
        if not loc:
            loc = geolocator.geocode('{}, Germany'.format(loc), language='en')
        if loc:
            closest_station = get_closest_station(loc.latitude, loc.longitude)
            # is subscribed ?
            if is_subscribed(request['session_id'], closest_station):
                context['subscribed'] = True
            else:
                context['notSubscribed'] = True
            # oldest alert we want
            max_date = datetime.datetime.now() - datetime.timedelta(days=2)
            last_alert = Alert.objects.filter(station=closest_station, report__date__gte=max_date).last()
            context['location'] = loc.address
            context['station'] = closest_station.name
            context['stationId'] = closest_station.pk
            cache.set(request['session_id'], closest_station.pk)
            if last_alert:
                context['lastAlertValue'] = last_alert.value
                context['lastAlertDate'] = last_alert.report.date.strftime('%x')
                context['kind'] = last_alert.report.kind
            else:
                context['clean'] = True
        else:
            context['notFound'] = True
    return context
示例#18
0
def CalculateDistance(generated_array,base_city):
	List_sort = [];
	geolocator = Nominatim();
	main_location = geolocator.geocode(base_city, exactly_one=True, timeout=None);
	main_coordinates = (main_location.latitude,main_location.longitude);
	#print (main_coordinates);
	for x in generated_array:
		other_location = geolocator.geocode(x, exactly_one=True, timeout=None);
		other_coordinates = (other_location.latitude,other_location.longitude);
		result = vincenty(main_coordinates,other_coordinates).miles;
		List_sort.append({'City':x,'Distance_fromBase':result});
	ClosestCity = sorted(List_sort, key=lambda k: k['Distance_fromBase']);
	return ClosestCity[0]['City'];
示例#19
0
def getTrip(fromAddr,toAddr,toDate,toTime):
    geolocator = Nominatim()
    location = geolocator.geocode(fromAddr)
    print "fromAddr",fromAddr,"location",location
    fromCoord=','.join(map(str,(location.latitude,location.longitude)))
    location = geolocator.geocode(toAddr)
    print "toAddr",toAddr,"location",location
    toCoord=','.join(map(str,(location.latitude,location.longitude)))

    #fromCoord='43.83412,11.19555'
    #toCoord='43.77187,11.25790'

    xmlurl = 'http://localhost:8080/otp/routers/default/plan?fromPlace=%s&toPlace=%s&date=%s&time=%s&arriveBy=true&mode=TRANSIT,WALK&maxWalkDistance=750&walkReluctance=20' % (fromCoord, toCoord, toDate[0], toTime[0])
    xmlpath=xmlurl
    html = urllib.urlopen(xmlpath)
    response = json.loads(html.read())

    plan=response['plan']

    reqParams=response['requestParameters']

    arriveBy=datetime.datetime.strptime(reqParams['date']+' '+reqParams['time'], '%m-%d-%Y %H:%M')

    fromName=plan['from']['name']
    toName=plan['to']['name']

    print "From", fromName
    print "To", toName

    itin=[]

    for it in plan['itineraries']:
        newItin={}
        endTime=datetime.datetime.fromtimestamp(int(it['endTime']/1000))
        newItin['endTime']=endTime.strftime('%Y-%m-%d %H:%M:%S')
        startTime=datetime.datetime.fromtimestamp(int(it['startTime']/1000))
        newItin['startTime']=startTime.strftime('%Y-%m-%d %H:%M:%S')
        newItin['transfers']=it['transfers']
        deltaEndTime=(arriveBy - endTime).total_seconds()/60
        newItin['deltaEndTimeMinutes']=deltaEndTime
        newItin['walkDistance']=it['walkDistance']
        points=[]
        for leg in it['legs']:
            points+=PolylineCodec().decode(leg['legGeometry']['points'])
        newItin['points']=points
        itin.append(newItin)
        print "Puoi partire alle %s arrivando con %s minuti di anticipo e facendo %d cambi, camminando per %s metri" % (startTime.strftime('%H:%M'),floor(newItin['deltaEndTimeMinutes']),newItin['transfers'],int(newItin['walkDistance']))

    return itin
示例#20
0
    def current_temp_will(self, message=None, location=None):
        """what's the temperature (in _______): I know how to tell you the temperature!"""
        geolocator = Nominatim()
        if location is None:
            forecast = forecastio.load_forecast(FORECAST_API_KEY, LINCOLN_LAT, LINCOLN_LNG, units="us")
            currently = forecast.currently()
            temp = currently.temperature
            feels_like = currently.apparentTemperature
            combined = "It's currently %sF and feels like %sF here in Lincoln, NE" % (temp, feels_like)

            self.save("local_temp", combined)

            if message:
                return self.say(combined, message=message)
        else:
            geolocation = geolocator.geocode(location)
            lat = geolocation.latitude
            lng = geolocation.longitude

            forecast = forecastio.load_forecast(FORECAST_API_KEY, lat, lng, units="us")
            currently = forecast.currently()
            temp = currently.temperature
            feels_like = currently.apparentTemperature
            full_address = geolocation.address
            combined = "It's currently %s degrees and feels like %s degrees in %s" % (temp, feels_like, full_address)

            self.save(full_address + "_temp", combined)

            if message:
                self.say(combined, message=message)
示例#21
0
def set_location(set_type=None):
    user = User.query.filter_by(id=g.user.id).first()

    if request.method == 'POST':
        input_location = request.form['address']
        geolocator = Nominatim()
        try:
            location = geolocator.geocode(input_location)
            user.loc_input = input_location
            user.loc_latitude = location.latitude
            user.loc_longitude = location.longitude
            db.session.commit()
        except:
            location = None
        return render_template(
            'set_location.html', 
            location=location, 
            type=set_type
        )

    if set_type:
        loc_input = user.loc_input
    else:
        loc_input = None
    return render_template(
        'set_location.html', 
        type=set_type, 
        current_loc=loc_input
    )
示例#22
0
class Geocoder(object):
    NYC = (40.7127, -74.0059)

    def __init__(self, geomodel):
        """
        a geocoder object encapsulates operations to be done on a geo_model (one of our models that extend a GeoDjango
        model and has location fields)
        :param geomodel:
        :return:
        """
        self.geolocator = Nominatim()
        self.geomodel = geomodel

    def geolocate(self):
        address_format = "{addr} {city} {state}"
        address = address_format.format(addr=self.geomodel.address, city=self.geomodel.city, state=self.geomodel.state)

        print "about to geolocate address ", address
        try:
            location = self.geolocator.geocode(address)
        except GeocoderTimedOut:
            location = None

        print "found location ", location
        return location
def geo_tweets():

    geolocator = Nominatim()
    count =1
    tweets_list = db1.view('sentiment-analysis/get_tweet_ids')
    for doc in  db.view('sentiment-analysis/get_alltweets'):
        tw=doc.value
        if  (not_exist(tw['id'])== "true"):
    	    loc_point= tw['geo']
	    #print loc_point
	    try:
                location = geolocator.geocode(loc_point['coordinates'])
	        #print type(location)
	        try:
   	            print count ,location.address
                    modified_place={'geo_address': location.address}
                    place_update = {'place-mod': modified_place}
	            new_tw = tw.copy()
                    new_tw.update(place_update)
                    new_tw.update({'_id': tw['id_str']})
                    try:
                        db1.save(new_tw)
                    except:
                        print ("Tweet " + tw['id_str'] + " already exists !!!! " )
	        except:
                    print ("Returned Location is Empty !!!! " )
            except GeocoderTimedOut as e:
	        print("Error: geocode faied on input %s with message ")
		print loc_point['coordinates']
	else:
    	    print count, 'ALREADY EXIST!!'
        count =count+1
示例#24
0
def api_v1_canvas(request):
    if request.method == 'POST':
        try:
            geolocator = Nominatim()
            location = None
            access_token = request.POST.get('access_token')
            # print "access_token:" + access_token
            text_location = request.POST.get('text_location')
            gps_location = request.POST.get('gps_location').decode('utf8') 

            if text_location:
                geo = geolocator.geocode(text_location)
                if hasattr(geo, 'latitude') and hasattr(geo, 'longitude'):
                    # print geo.latitude
                    # print geo.longitude
                    location = str(geo.latitude) + "," + str(geo.longitude)
                else:
                    pass
            else:
                location = str(gps_location)
                # print "location: " + location
                pass
            filename = parse_places_api(location, access_token)
        except Exception,e:
            print e
        return HttpResponse(json.dumps({'success':True,'filename':filename}), content_type="application/javascript; charset=utf-8") 
    def get(self, height, weight, city, state):
        # combined = pd.read_csv("wonderfullyamazingdata.csv", encoding='ISO-8859-1')
        combined = pd.read_csv("newamazingdata.csv", encoding='ISO-8859-1')
        location =  str(city) + ' ' + str(state)
        geolocator = Nominatim()
        place = geolocator.geocode(location[0])
        latitude = place.latitude
        longitude = place.longitude


        users = [float(height), float(weight), latitude, longitude ]
        players = combined[["height", "weight", "latitude", "longitude"]]

        result = []
        for index in range(0,len(players)):

            result.append(1-distance.cosine(users, players.iloc[index]))

        result = sorted(range(len(result)), key=lambda i: result[i])[-5:]   
        result.reverse()

        ids = []
        for index in result:
            ids.append( combined.ID.iloc[index] )


        ids = str(ids)


        with open('reply.json', 'w') as outfile:
            json_stuff = json.dumps(ids)
            json.dump(json_stuff, outfile)

        return json_stuff
示例#26
0
def geocode_w(txt):
    time.sleep(16+scipy.randn(1))
    geoloc = Nominatim()
    print(txt)
    cded = geoloc.geocode(txt)
    geoloc = 0 
    return cded
示例#27
0
def convertgeo(city):
    geolocator = Nominatim()
    location = geolocator.geocode(city)
    if location:
        return location.latitude, location.longitude
    else:
        return None
    def parse(self,text,place="NYC"):
        dict_addr,addr_type = self.preprocess(text)
        google_key = pickle.load(open("google_api_key.pickle","r"))
        g_coder = GoogleV3(google_key)
        if addr_type == 'complete':
            combined_addr = []
            keys = ["AddressNumber","StreetName","StreetNamePostType","PlaceName","StateName","ZipCode"]
            for key in keys:
                try:
                    combined_addr += [dict_addr[key]]
                except KeyError:
                    continue
                addr = " ".join(combined_addr) 
            n_coder = Nominatim()
            addr = self.pre_formatter(addr,dict_addr)
            lat_long = n_coder.geocode(addr)
            if lat_long: #means the request succeeded
                return lat_long
            else:
                lat_long = g_coder.geocode(addr)
                return lat_long
            #If None, means no address was recovered.
        if addr_type == 'cross streets':
            #handle case where dict_addr is more than 2 nouns long
	    cross_addr = " and ".join(dict_addr) + place 
            try:
                lat_long = g_coder.geocode(cross_addr)
                return lat_long
            except geopy.geocoders.googlev3.GeocoderQueryError:
                return None
示例#29
0
def tags():
    url = request.args["url"] 
    clarifai_api = ClarifaiApi()  # assumes environment variables are set.
    result = clarifai_api.tag_image_urls(url)
    results = result['results'][0]["result"]["tag"]["classes"]#[:3]

    client = foursquare.Foursquare(client_id='JEK02X44TGMNJSE0VC1UBEB4FRNNW3UMFQ4IQOPI4BAR2GXA', \
                                    client_secret='A2Z50VTUHHXEUYJBHCQKB1LXTNVVBYBQR4SDASVTXTWUMWXS') #foursquare shit

    address = request.args["address"] #address is currently a string
    geolocator = Nominatim()
    location = geolocator.geocode(address)
    newLocation = str(location.latitude) + str(", ") + str(location.longitude)
    
    
    # foursquare_dictionary = client.venues.explore(params={'ll': '40.7, -74', 'v': '20160402', 'query': results[0] + ',' + results[1] + ',' + results[2]})

    foursquare_dictionary = client.venues.explore(params={'ll': newLocation, 'v': '20160402', 'query': results[0] + ',' + results[1] + ',' + results[2]})

    #first place to eat
    # status1 = foursquare_dictionary['groups'][0]['items'][0]['venue']['hours']['status']
    address1 = foursquare_dictionary['groups'][0]['items'][0]['venue']['location']['formattedAddress']
    name = foursquare_dictionary['groups'][0]['items'][0]['venue']['name']

    #second place to eat
    address2 = foursquare_dictionary['groups'][0]['items'][1]['venue']['location']['formattedAddress']
   # status2 = foursquare_dictionary['groups'][0]['items'][1]['venue']['hours']['status']
    name1 = foursquare_dictionary['groups'][0]['items'][1]['venue']['name']

    return render_template('tags.html',\
                           newAddress1 = newAddress(address1), name = name,\
                           newAddress2 = newAddress(address2), name1 = name1,\
                           newLocation = newLocation)
def make_LatLonDict(tweet_lat_lon_dict, tweet_location_unique):

    geolocator = Nominatim()

    # Turn the user defined location place name into a geographic (lat,lon) location

    for i, placeName in enumerate(tweet_location_unique):
        if placeName not in tweet_lat_lon_dict:
            try:
                placeGeo  = geolocator.geocode(placeName)
            except Exception as E:
                print("exception happened", type(E), E)

            if i % 20 == 0:
                print(i)
            if placeGeo is not None:
                tweet_lat_lon_dict[placeName] = (placeGeo.latitude, placeGeo.longitude)
            else:
                tweet_lat_lon_dict[placeName] = (None, None)


    # How many location place names were not able to be turned into (lat,lon) locations?
    # Which names were they?
    print("The total (lat,lon) dictionary is %d items long." %(len(tweet_lat_lon_dict)))

    bad_place_names = [k for k,v in tweet_lat_lon_dict.items() if v == (None,None)]

    print("Of these, %d were unable to be converted into (lat,lon) coordinates. These were specifically the following locations: \n" %(len(bad_place_names)))
    print("\n".join(bad_place_names))

    # Write out the dictionary of place names and corresponding latitutes & longitudes to a JSON file
    with open('../data/Twitter_Zika_PlaceName_Geo_info.json', 'w') as twitterGeo_JSONFile:
        json.dump(tweet_lat_lon_dict, twitterGeo_JSONFile, indent=2)
df_latlong.columns = ['PostalCode', 'Latitude', 'Longitude']
df_latlong

# In[67]:

df_new = pd.merge(df_PC_new, df_latlong, on=['PostalCode'], how='inner')
df_new

# # Part 3- Exploring and clustering the neighborhoods in Toronto

# In[68]:

address = 'Toronto, Canada'

geolocator = Nominatim(user_agent="ny_explorer")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print('The geograpical coordinate of Toronto are {}, {}.'.format(
    latitude, longitude))

# #### Creating a map of Toronto

# In[69]:

#!conda install -c conda-forge folium=0.5.0 --yes # uncomment this line if you haven't completed the Foursquare API lab
import folium  # map rendering library

print('Library imported.')

# In[73]:
示例#32
0
from  geopy.geocoders import Nominatim
geolocator = Nominatim()
city ="Marathahalli"
country ="INDIA"
loc = geolocator.geocode(city+','+ country)
print("latitude is :-" ,loc.latitude,"\nlongtitude is:-" ,loc.longitude)
示例#33
0
opção = chamarMenu()

while opção > 0 and opção < 4:
    if opção == 1:
        print(registrar(entrada, "entrada_json.json"))

    elif opção == 2:

        from geopy.geocoders import Nominatim
        geolocator = Nominatim(user_agent="wazeyes")

        endereço = input(
            "Para confirmar digite um endereco com número e cidade. (Exemplo: avenida paulista, 100 São Paulo) : "
        )

        resultado = geolocator.geocode(endereço)
        print("--------------------------------")
        print("\nEndereço completo.: ", resultado)
        print("\nCoordenadas.......: ", resultado.latitude,
              resultado.longitude)

    elif opção == 3:

        latitude = float(input("Digite a latitude...: "))
        longitude = float(input("Digite a longitude.: "))
        valid_address = True
        resultado = geolocator.reverse(f"{latitude}, {longitude}")

        print("Endereço completo.: ", resultado)
示例#34
0
def editpost(id):
    if request.method == "POST":
        photos = request.files.getlist("photos")
        errors = []
        pattern = re.compile("^[\dA-Z]{3}-[\dA-Z]{3}-[\dA-Z]{4}$",
                             re.IGNORECASE)
        loc_string = request.form['address'] + ' ' + request.form['city']
        geolocator = Nominatim(user_agent="dormroom")
        location = geolocator.geocode(loc_string)
        list_photos = request.files.getlist("photos[]")
        if request.form['title'] == "" or request.form[
                'description'] == "" or request.form[
                    'address'] == "" or request.form[
                        'phonenumber'] == "" or request.form[
                            'contactname'] == "" or request.form[
                                'contactemail'] == "" or request.form[
                                    'price'] == "":
            errors.append("Not all fields filled out.")
        if validate_email(request.form['contactemail']
                          ) == False and request.form['contactemail'] != "":
            errors.append("Please use a valid email to advertise your post.")
        if len(list_photos) < 2:
            errors.append("Please upload at least two photos.")
        if pattern.match(request.form['phonenumber']) is None:
            errors.append("Please enter a valid phone number.")
        if request.form['price'].isdigit() == False:
            errors.append("Please enter a numeric price.")
        if errors:
            for error in errors:
                flash(error)
        else:
            listing_to_edit = Listing.query.filter_by(id=id).first()
            listing_to_edit.title = request.form['title']
            listing_to_edit.description = request.form['description']
            listing_to_edit.listing_photo_one = b64encode(photos[0].read())
            listing_to_edit.listing_photo_two = b64encode(photos[1].read())
            listing_to_edit.listing_photo_three = b64encode(
                photos[2].read()) if len(photos) > 2 else ''
            listing_to_edit.listing_photo_four = b64encode(
                photos[3].read()) if len(photos) > 3 else ''
            listing_to_edit.listing_photo_five = b64encode(
                photos[4].read()) if len(photos) > 4 else ''
            listing_to_edit.listing_seller = request.form['contactname']
            listing_to_edit.listing_photo = request.form['phonenumber']
            listing_to_edit.listing_email = request.form['contactemail']
            listing_to_edit.listing_address = request.form['address']
            listing_to_edit.listing_added = datetime.datetime.now()
            listing_to_edit.listing_price = request.form['price']
            listing_to_edit.listing_school = session.get("school")
            listing_to_edit.listing_number = request.form['aptnumber']
            listing_to_edit.listing_lat = location.latitude
            listing_to_edit.listing_long = location.longitude
            listing_to_edit.listing_city = request.form['city']
            listing_to_edit.listing_state = request.form['state']
            db.session.commit()
            flash("Your post has been updated.")
            return redirect(url_for('userlistings'))

    return render_template(
        "editpost.html",
        listing_to_edit=Listing.query.filter_by(listing_id=id).first())
示例#35
0
    def init_ui(self):

        geolocator = Nominatim(user_agent="MirageSmartMirror")
        origin = geolocator.geocode(self.address)

        weather_key = "50f9b96898249aa1a036886103f78788"

        weather_url = "https://api.darksky.net/forecast/" + weather_key
        # 0123456789abcdef9876543210fedcba/42.3601,-71.0589
        weather_request = weather_url + "/%f,%f" % (origin.latitude,
                                                    origin.longitude)

        weather_get = requests.get(weather_request)
        weather_json = weather_get.json()

        datetime = QDateTime.currentDateTime()

        font = QFont('Helvetica', 18)
        font.setWeight(1)

        self.weatherBox = QVBoxLayout()

        icon = icons[weather_json['currently']['icon']]
        image = cv2.imread(icon)
        image = cv2.resize(image, (50, 50), interpolation=cv2.INTER_CUBIC)
        image = QImage(image, image.shape[1], image.shape[0], image.strides[0],
                       QImage.Format_RGB888)

        self.dailySummary = QLabel()
        self.dailySummary.setAlignment(Qt.AlignLeft)
        self.dailySummary.setFont(font)
        self.daily = weather_json['daily']['data'][0]['summary']
        self.dailySummary.setText("<font color='white'>" + self.daily +
                                  "</font")

        self.currently = QLabel()
        self.currently.setAlignment(Qt.AlignLeft)
        self.currently.setFont(font)
        self.curr = "Currently " + weather_json['currently']['summary']
        self.currently.setText("<font color='white'>" + self.curr + "</font")

        self.temp = QLabel()
        self.temp.setAlignment(Qt.AlignLeft)
        self.temp.setFont(font)
        self.fahrenheit = weather_json['currently']['temperature']
        self.temp.setText("<font color='white'> %d" % self.fahrenheit +
                          u'\N{DEGREE SIGN}' + "</font")
        # self.dailySummary.setText("<font color='white'>" + "West Lafayette" + "</font")
        # self.currently.setText("<font color='white'>" + "Cloudy" + "</font")
        # self.temp.setText("<font color='white'>" + "72" + u'\N{DEGREE SIGN}' + "</font")

        self.icon = QLabel()
        self.icon.setAlignment(Qt.AlignLeft)
        self.icon.setPixmap(QPixmap.fromImage(image))

        self.weatherBox.addWidget(self.dailySummary)
        self.weatherBox.addWidget(self.currently)
        self.weatherBox.addWidget(self.icon)
        self.weatherBox.addWidget(self.temp)

        self.weatherBox.setAlignment(Qt.AlignLeft)

        self.setLayout(self.weatherBox)

        self.init_timer()
示例#36
0
    breweries = country_breweries_soup.find_all('ul', class_="brewery-info")

    for brewery in breweries:
        ids = ['name', 'telephone', 'state']
        props = {k: brewery.find(class_=k).text if \
        brewery.find(class_=k) is not None else '' for k in ids }
        props['telephone'] = re.sub('[^0-9+]', "", props['telephone'])
        props['address'] = ''
        address1 = brewery.find(class_='address')
        if address1 is not None:
            props['address'] = address1.text.split('|')[0]
            address2 = address1.find_next_sibling('li')
            if address2 is not None:
                props['address'] = props[
                    'address'] + ' ' + address2.text.split('|')[0]
        props['country'] = country['data-country-id']
        try:
            geolocator = Nominatim(user_agent="brewery-finder")
            location = geolocator.geocode(props['address'])
        except Exception as e:
            location = ''
        location = geolocator.geocode(props['address'])
        if location is not None:
            props['latitude'] = location.latitude
            props['longitude'] = location.longitude
        db_brewery = Brewery(**props)
        db.session.add(db_brewery)
        db.session.commit()
    db.session.commit()
示例#37
0
def main():

    #data_list = sqlite3.connect('/Users/keyang/Desktop/SQL/ListingDatabase.db')
    data_list = sqlite3.connect(
        '/Users/Princess/Desktop/scrape/heihei/database/Listing.db')
    #df = pd.read_sql_query("SELECT * FROM listinginfo", data_list)

    #geolocator = Nominatim(user_agent="*****@*****.**")# enter your email as user_agent
    geolocator = Nominatim(user_agent="*****@*****.**")

    #conn = sqlite3.connect('/Users/keyang/Desktop/SQL/ListingDatabase.db')
    conn = sqlite3.connect(
        '/Users/Princess/Desktop/scrape/heihei/database/Listing.db')
    cur = conn.cursor()

    # csv_prefix = '/Users/Princess/Desktop/scrape/heihei/data/' #shiyao prefix
    # entries = os.listdir('/Users/Princess/Desktop/scrape/heihei/data/')

    delete_items(conn)  #if want to reload all database, uncomment it

    # for csv_file in entries:
    to_db = []
    csv_path = './Listing - Form responses 1.csv'
    # if not csv_file.endswith('.csv'):
    #     print("skipping file {}".format(csv_file))
    #     continue
    with open(csv_path, 'r') as table:
        #with open('/Users/Princess/Desktop/scrape/heihei/data/toronto.csv','r') as table:
        print("reading: ", csv_path)
        dr = csv.DictReader(table, delimiter=',')  # comma is default delimiter
        count = 1
        for i in dr:
            name = i['Listing Name']
            address = i['Street Address']
            city = i['City']
            province = i['Province']
            postal_code = i['Postal Code']
            full_addr = address + ' ' + city
            email = i['Email']
            price = i['Price']
            type_accom = i['Type of Accommodation']
            uploaddate = datetime.datetime.today()
            type_accom = i['Type of Accommodation']
            school_code = select_school_code(i['School Name'])
            price = i['Price']
            start_date = i['Availability ']
            lease_type = int(i['Lease length '])
            ac = bool_check(i['Facilities [Air Conditioning]'])
            furnished = bool_check(i['Facilities [Furnished]'])
            laundry = bool_check(i['Facilities [Laundry]'])
            parking = bool_check(i['Facilities [Parking]'])
            image_url = i['Listing Photo']

            #try:
            #geocoding for address

            loc = geolocator.geocode(full_addr)
            longitude = round(loc.longitude, 6)
            latitude = round(loc.latitude, 6)
            dist = calculate_distance(latitude, longitude, school_code)

            id = school_code + str(count)
            print(id)
            # accomodation type change
            start_date, end_date = validate_date(start_date, lease_type)
            #print(start_date)
            items = (id, school_code, email, name, address, city, province, postal_code, \
                    type_accom, price, start_date,lease_type,ac, furnished,laundry, parking,\
                    longitude, latitude,image_url,dist,uploaddate,end_date)

            to_db.append(items)
            # except:
            #     continue
            count += 1
        cur.executemany(
            "INSERT INTO listinginfo VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
            to_db)
    conn.commit()
示例#38
0
from geopy import Location
from geopy.geocoders import Nominatim, GeoNames
from geopy.distance import Geodesic

geolocator = Nominatim(user_agent="QTestingService")
location: Location = geolocator.geocode("Radioweg 45, Almere")
location2: Location = geolocator.geocode(
    "Vrouwenakker 12a, Vrouwenakker, Nederland")
print(location.address)
print(location.raw)
print(location.latitude, location.longitude)

import requests
import json

send_url = 'http://ipinfo.io/loc'
r = requests.get(send_url)
loc = r.text
print(loc)

location: Location = geolocator.reverse(f"{loc}")
print(location.address)

import geocoder
g = geocoder.ip('me')
print(f"{g.latlng[0]},{g.latlng[1]}")
# print(g.latlng)

location: Location = geolocator.reverse(f"{g.latlng[0]},{g.latlng[1]}")
print(location.address)
示例#39
0
def calculate_distance(request):
    distance = None
    destination = None
    location = None
    form = LocationForm(request.POST or None)
    geolocator = Nominatim(user_agent='location')

    ip = get_ip(request)

    #ip = '191.248.99.252'
    country, city, lat, lon = get_geo(ip)
    location = geolocator.geocode(city)

    # location coords
    l_lat = lat
    l_lon = lon
    point_A = (l_lat, l_lon)

    # initial map
    m = folium.Map(width=800,
                   height=400,
                   location=get_center_coord(l_lat, l_lon),
                   zoom_start=8)

    folium.Marker([l_lat, l_lon],
                  tooltip='Clique aqui ver mais',
                  popup=city['city'],
                  icon=folium.Icon(color='black')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)

        # destination coords
        d_lat = destination.latitude
        d_lon = destination.longitude

        point_B = (d_lat, d_lon)
        distance = round(geodesic(point_A, point_B).km, 2)

        # map modification
        m = folium.Map(width=800,
                       height=400,
                       location=get_center_coord(l_lat, l_lon, d_lat, d_lon),
                       zoom_start=get_zoom(distance))
        # locatio marker
        folium.Marker([l_lat, l_lon],
                      tooltip='Clique aqui ver mais',
                      popup=city['city'],
                      icon=folium.Icon(color='black')).add_to(m)

        # destination marker
        folium.Marker([d_lat, d_lon],
                      tooltip='Clique aqui ver mais',
                      popup=destination,
                      icon=folium.Icon(color='purple', icon="cloud")).add_to(m)

        #line route
        line = folium.PolyLine(locations=[point_A, point_B],
                               weight=2,
                               color='blue')

        m.add_child(line)

        instance.location = location
        instance.distance = distance
        instance.save()
    else:
        form = LocationForm()

    m = m._repr_html_()

    context = {
        'distance': distance,
        'form': form,
        'map': m,
    }
    return render(request, 'location/index.html', context)
示例#40
0
def convertToLatLong(locations):
    geolocator = Nominatim()
    for location in locations:
        locationCoordinates = geolocator.geocode(location, timeout=None)
        latitude.append(locationCoordinates.latitude)
        longitude.append(locationCoordinates.longitude)