def coordonate(element):
    '''
    aceasta functie extrage pentru o anumita zona geolocatia
    pentru ca folosim un api gratuit se returneaza o eroare 
    daca extragem prea multe anunturi
    
    '''
    try:
        time.sleep(2)
        zona = Geocoder.geocode(element)
        #dictionar_zone[element]=zona.coordinates
        
        rezultat = zona.coordinates
        for i in range(20):
            if rezultat == None:
                time.sleep(5)
                zona = Geocoder.geocode(element)
                rezultat = zona.coordinates
            else:
                print('a mers ' + str(element) + " " + str(rezultat))
                return rezultat
                break

    except requests.ConnectionError as e: #am primit eroare de conectare asa ca am pus sa incerce inca o data dupa 120 de secunde
        time.sleep(120)
        print(element)
        coordonate(element)
    except GeocoderError:
        time.sleep(10)
        coordonate(element)
def geo(request,city1,city2):
    context = RequestContext(request)

    citydata1=Geocoder.geocode(city1)
    codtuple1=citydata1[0].coordinates
    codtuple1=list(codtuple1)

    citydata2=Geocoder.geocode(city2)
    codtuple2=citydata2[0].coordinates
    codtuple2=list(codtuple2)

    #Calculate Distance
    lat1, lon1 = codtuple1
    lat2, lon2 = codtuple2
    R = 6371 #Earth's Radius in Kms.

    lat_diff_radians = math.radians(lat2-lat1)
    long_diff_radians = math.radians(lon2-lon1)
    #Haversine formula to calculate distance between a pair of cordinates
    a = math.sin(lat_diff_radians/2) * math.sin(long_diff_radians/2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(long_diff_radians/2) * math.sin(long_diff_radians/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    distance = R * c
    
    context_dict = {'cood1':codtuple1,'cood2':codtuple2,'distance':distance}
    

    return render_to_response('coods.html',context_dict,context)
Example #3
0
 def clean_location(self):
     data = self.cleaned_data['location']
     if data is not None:
         data = data.replace("'", "")
         data = data.replace('"', "")
         data = data.replace('\\', "")
     try:
         Geocoder.geocode(data)
     except GeocoderError as e:
         if e.status == "ZERO_RESULTS":
             raise ValidationError(
                 "Invalid Address",
                 code='invalid',
                 params={'value': '42'},
             )
         else:
             time.sleep(2)
             try:
                 Geocoder.geocode(data)
             except GeocoderError as e:
                 if e.status == "ZERO_RESULTS":
                     raise ValidationError(
                         "Invalid Address",
                         code='invalid',
                         params={'value': '42'},
                     )
                 else:
                     print("API Failure")
     return data
Example #4
0
def testCreate(request):
	if request.POST:
		form = suggestTest(request.POST)
		#I think this is where we would set the field for latLon
		#request = request.POST.copy()
		#request.setitem('latLon',latLon)
		if form.is_valid():
			address = form.cleaned_data['street']
			latLon = Geocoder.geocode(address)
			form = form.save(commit=False)
			coords = Geocoder.geocode(address)
			coords = coords[0].coordinates
			lat = coords[0]
			lon = coords[1]
			latLon = str(lat)+","+str(lon)
			form.latLon = latLon
			form.save()
			return HttpResponseRedirect(reverse('playgroundapp_home'))
	else:
		form = suggestTest()
	args = {}
	args.update(csrf(request))

	args['form'] = form
	args['dropdown'] = SchoolDistrict.objects.all()
	return render_to_response('playgroundapp/create_playground.html', args)
def geocode(address):
    try:
        return Geocoder.geocode(address)[0].coordinates
    except:
        rep = re.match(r"((.+-\d+)|(.+\d+号))", ad).group()
        print('%sのgeocodeが見つかりませんでした。調整します。→%s' % (ad, rep))
        return Geocoder.geocode(rep)[0].coordinates
Example #6
0
    def test_google_maps_request(self):
        geo = Geocoder(api_key=GEOCODE_API)

        self.assertEqual(geo.geocode("Dallas, Texas").coordinates, (32.7766642, -96.79698789999999))
        self.assertEqual(geo.geocode("Austin, Texas").coordinates, (30.267153, -97.7430608))
        self.assertEqual(geo.geocode("University of Texas at Austin").coordinates, (30.2849185, -97.7340567))
        self.assertEqual(geo.geocode("UT Austin").coordinates, (30.2849185, -97.7340567))
Example #7
0
    def test_time_google_api(self):
        geo = Geocoder(api_key=GEOCODE_API)

        start=time.time()
        geo.geocode("University of Texas at Austin")
        end = time.time()
        self.assertTrue(1 > end-start, "Single geocoder is Slower than 1s")
Example #8
0
    def test_google_lyft_uber_pipe(self):
        geo = Geocoder(api_key=GEOCODE_API)

        sessionu = Session(server_token='hVj-yE_w4iJQ5-rbp8hmKZSNekOzLV1cvnF_BrNl')
        clientu = UberRidesClient(sessionu)

        auth_flow = ClientCredentialGrant(
            'd-0DVSBkAukU',
            'I-yZZtV1WkY_903WKVqZEfMEls37VTCa',
            'rides.request',
            )
        session = auth_flow.get_session()

        client = LyftRidesClient(session)
        start = time.time()

        dlat,dlong = geo.geocode("UT Austin").coordinates
        alat,along = geo.geocode("Round Rock, TX").coordinates
        response = client.get_cost_estimates(dlat,dlong,alat,along)
        response = clientu.get_price_estimates(
            start_latitude=dlat,
            start_longitude=dlong,
            end_latitude=alat,
            end_longitude=along,
            seat_count=2
        )

        end = time.time()
        self.assertTrue(10 > end-start)
Example #9
0
def geocoder(country, q, decimal_places=3):

    components = "country:" + country
    if settings.GOOGLE_MAPS_GEOCODING_API_KEY:
        geocoder = Geocoder(settings.GOOGLE_MAPS_GEOCODING_API_KEY)
        response = geocoder.geocode(q, components=components)
    else:
        response = Geocoder.geocode(q, components=components)

    results = []

    for entry in response.raw:

        # The Google Geocoder API will return the country used in the components if
        # there is no match. Filter this from our results.
        if "country" in entry['types']:
            continue

        result = {
            "address": entry['formatted_address'],
            "latitude": entry['geometry']['location']['lat'],
            "longitude": entry['geometry']['location']['lng'],
        }

        # round to the require precision
        for key in ['latitude', 'longitude']:
            result[key] = round(result[key], decimal_places)

        # Check that the result has not already been added. This is possible
        # when several different areas evaluate to the same one (eg various
        # instances of South Africa's "Cape Town").
        if result not in results:
            results.append(result)

    return results
Example #10
0
def xwalk_generic(form, advert):
    if not advert:
        advert = models.Advert()

    advert.set_owner(current_user.id)

    if form.category.data:
        advert.set_category(form.category.data)

    advert.set_title(form.title.data)

    if form.description.data:
        advert.set_description(form.description.data)

    if form.condition.data:
        advert.set_condition(form.condition.data)

    if form.price.data:
        advert.set_price(form.price.data)

    if form.location.data:
        advert.set_spot(form.location.data)
        if form.location.data == "home":
            if current_user.location:
                lat, lon = current_user.location
                advert.set_location(lat, lon)
            else:
                raise XWalkException(
                    "We do not have an address for your account. If you wish to use this option, please edit your information under My Account."
                )
        elif form.location.data == "uni":
            mail = current_user.id.split("@")
            domain = mail[-1]
            uni = domain_uni_lookup[domain]["address"]
            results = Geocoder.geocode(uni + ", United Kingdom")
            lat, lng = results[0].coordinates
            advert.set_location(lat, lng)
        elif form.location.data == "postcode":
            results = Geocoder.geocode(form.postcode.data + ", United Kingdom")
            lat, lng = results[0].coordinates
            advert.set_location(lat, lng)

    if form.keywords.data:
        advert.set_keywords(form.keywords.data)

    image = request.files["upload"]
    if image and allowed_file(image.filename):
        image_id = uuid.uuid4().hex
        name = image.filename.split(".")
        extension = name[-1]
        image_name = str(image_id) + "." + extension
        image.save(os.path.join(app.config["IMAGES_FOLDER"], image_name))
        advert.set_image_id(image_name)
    elif image and not allowed_file(image.filename):
        flash("This is not an allowed image type", "error")

    advert.expires_in(app.config.get("ADVERT_TIMEOUT", 604800))
    advert.save()
    advert.refresh()
    return advert
Example #11
0
def geocode(address):
    try:
        return Geocoder.geocode(address)[0].coordinates
    except:
        rep = re.match(r"((.+-\d+)|(.+\d+号))", ad).group()
        print('%sのgeocodeが見つかりませんでした。調整します。→%s' % (ad, rep))
        return Geocoder.geocode(rep)[0].coordinates
Example #12
0
def geoloc(request, entreprise_id=None):
	geocoder=Geocoder()
	try:
		proxy=os.environ['http_proxy']
		geocoder.set_proxy(proxy)
	except KeyError:
		pass
	entreprise = Entreprise.objects.get(pk=entreprise_id)
	adresseComplete = entreprise.adresse_propre+","+entreprise.ville_propre
	#testAdresse = "20 place de la Republique, Montargis"

	try:
		if geocoder.geocode(adresseComplete).valid_address :
			resultat = geocoder.geocode(adresseComplete)
			entreprise.latitude=resultat[0].coordinates[0]
			entreprise.longitude=resultat[0].coordinates[1]
			message = "adresse : "+str(resultat[0].coordinates)
			entreprise.save()
		else:
			message = "adresse non valide"
	except Exception as inst:
		message=inst.args
		
	return render(request, "entreprises/geolocalisation.html", {
		'entreprise': entreprise,
		'afficherAC': adresseComplete,
		'message':message
	})
Example #13
0
 def get_geocode(self):
     ad = self._get_address()
     try:
         return Geocoder.geocode("福岡県" + ad)[0].coordinates
     except:
         rep = re.match(r"((.+-\d+)|(.+\d+号))", ad).group()
         print('%sのgeocodeが見つかりませんでした。調整します。→%s' % (ad, rep))
         return Geocoder.geocode("福岡県" + rep)[0].coordinates
Example #14
0
def dist_address(addr1, addr2):
    ''' given two address, return distance in miles '''
    result1 = Geocoder.geocode(addr1)
    result2 = Geocoder.geocode(addr2)
    if (result1.count==1)&(result2.count==1): ## if only one valid address
        return dist(result1.coordinates, result2.coordinates) ## dist(coordinate1, coordinate2)
    else:
        print 'Error'
        return None
Example #15
0
def find_Path(MAP, model):
    gmaps = GoogleMaps()
    
    home = raw_input("Enter your starting address: ")
    end =  raw_input("Enter your destination address: ")
    results = Geocoder.geocode(home)
    results2 = Geocoder.geocode(end)
    #results = Geocoder.reverse_geocode(41.96786329,-87.71349889)
    #results2 = Geocoder.reverse_geocode(41.763258, -87.61172601)

    print '---Finding Path from ', results, ' to ', results2

    dirs  = gmaps.directions(results, results2)

    time  = dirs['Directions']['Duration']['seconds']
    dist  = dirs['Directions']['Distance']['meters']
    route = dirs['Directions']['Routes'][0]
    
    PATH = []
    EVALUATION = []
    
    for step in route['Steps']:
        position = (step['Point']['coordinates'][1], step['Point']['coordinates'][0])
        if position in MAP:
            for i in range(4): PATH.append('H')
            PATH.append('no')
        else:
            for i in range(4):PATH.append('L')
            PATH.append('yes')
        
        #PREDICT FOR EACH CITY IN THE INTERMEDIATE STEPS
        with open("predict.arff", "a") as myfile:
            for elem in PATH:
                if elem == 'yes' or elem == 'no': myfile.write(elem)
                else: myfile.write(elem  + ',')
            myfile.write('\n')
        PATH = []

    EVALUATION = model.TestClassifier("predict.arff")
    #mark current place as SAFE since you're already at that location anyways
    EVALUATION[0] = 'yes'
    
    #Print Final Results At this Point
    k = 0
    for step in route['Steps']:
        position = (step['Point']['coordinates'][1], step['Point']['coordinates'][0])
        print step['Point']['coordinates'][1], step['Point']['coordinates'][0] ,  '===========================>SAFE? ', EVALUATION[k]
        print step['descriptionHtml']
        k +=1
    
    #UPDATE THE FILES
    delete("predict.arff")
    filename2 = "predict.arff"
    shutil.copy("predict_start.arff", filename2)
    if os.path.isfile (filename2): print "Have a Safe Journey"
    else: print "#####Problem Here: Error 99. Engineeer Will Fix the Bug ASAP"
Example #16
0
	def _geocode(self, address,city,state,zipcode):
		# prepare geocoder
		from pygeocoder import Geocoder
		myg=Geocoder(api_key='AIzaSyAJmxEb1O6GJMxP9QuhCc4-HV2aae2FolA')
		addrstr = "%s %s, %s %s" %(address,city, state, zipcode)
		try:
			loc = myg.geocode(addrstr)
		except Exception, e:
			print "GEOCODER FAIL: %s. Maybe try setting the proxy?" %(e)
			myg.set_proxy('mtaweb.metro.net:8118')
			loc = myg.geocode(addrstr)
Example #17
0
def map(location,destination):
	api_key=open('api_key').read()
	url="https://maps.googleapis.com/maps/api/js?key=%s"%api_key
	geoOrig=Geocoder.geocode(location)
	geoDest=Geocoder.geocode(destination)
	lat1,lng1=geoOrig[0].coordinates
	lat2,lng2=geoDest[0].coordinates
	directionsurl=("https://maps.googleapis.com/maps/api/directions/json?origin=%s&destination=%s&key=%s")%(location, destination, api_key)
	directions=requests.get(directionsurl)
	print directions.content
	return render_template("base.html", url=url, lat1=lat1, lng1=lng1, lat2=lat2,lng2=lng2)
Example #18
0
def determine_estimate():

    ### Gets addresses, converts them to coordinates, requests estimates from API, creates dictionary, saves to a session to use later

    uber_prices = []

    app_start_address = request.args.get("start_address")
    app_end_address = request.args.get("end_address")

    ### This is a fix for the problem that arose from the geocode lbirary, fixes SSL restrictions. See site for more details: https://urllib3.readthedocs.org/en/latest/security.html#pyopenssl

    # try:
    # 	import urllib3.contrib.pyopenssl
    # 	urllib3.contrib.pyopenssl.inject_into_urllib3()
    # except ImportError:
    # 	pass

    ### If the server receives correct input, it will convert to coordinates, make reuqest from Uber API, if not it will display an error message

    # try:
    geo_start_address = Geocoder.geocode(app_start_address)
    geo_end_address = Geocoder.geocode(app_end_address)
    if geo_start_address.valid_address and geo_end_address.valid_address:

        start_coordinates = geo_start_address[0].coordinates
        end_coordinates = geo_end_address[0].coordinates

        start_lat = start_coordinates[0]
        start_long = start_coordinates[1]

        end_lat = end_coordinates[0]
        end_long = end_coordinates[1]

        AUTH = Uber(ENV.CLIENT_ID, ENV.SERVER_TOKEN, ENV.SECRET)

        estimate = AUTH.get_price_estimate(start_lat, start_long, end_lat, end_long)

        services_and_prices = estimate["prices"]

        for i in range(len(services_and_prices)):
            uber_price = {}
            uber_price["service"] = services_and_prices[i]["display_name"]
            uber_price["price"] = services_and_prices[i]["estimate"]
            uber_prices.append(uber_price)

        session["user_travel"] = uber_prices

        return render_template("results.html")

    else:

        message = "Something went wrong. Please make sure you entered valid addresses."

        return render_template("index.html", message=message)
Example #19
0
def coordinates((start_address, end_address)):
    '''
	Calculates coordinates from start and destination addresses
	'''
    try:
        start_location = Geocoder.geocode(start_address)
        end_location = Geocoder.geocode(end_address)
    except:
        start_location = False
        end_location = False

    return start_location, end_location
Example #20
0
    def test_geocode_with_components(self):
        """Test pygeocoder geocode()"""

        addr = 'East London'
        components = 'country:ZA'
        g = Geocoder()

        bare_result = g.geocode(addr)
        self.assertEqual(bare_result.city, 'London')
        self.assertEqual(bare_result.country, 'United Kingdom')

        za_result = g.geocode(addr, components=components)
        self.assertEqual(za_result.city, 'East London')
        self.assertEqual(za_result.country, 'South Africa')
Example #21
0
def find_place(loc,entity_locs):
    location_file=io.open("txt/all_locations.txt",encoding="utf-8")
    locations=[]
    for location in location_file:
        location=location.split("\n")[0]
        locations.append(location.encode('utf-8'))
    try:
        if(loc!="" or entity_locs!=[]):
            if(loc!=""):    #university name is present
                result = Geocoder.geocode(loc)
                context={'lat':result[0].coordinates[0],'lng':result[0].coordinates[1],'place':loc,'type':"university"}
                return context

            elif(entity_locs):
                for a in entity_locs:
                    a=a.lower()
                    if a in locations:
                        result = Geocoder.geocode(a)
                        context={'lat':result[0].coordinates[0],'lng':result[0].coordinates[1],'place':a,'type':"",}
                        return context
                    elif(a[-2:]=="an"):
                        for location in locations:
                            if(a[:-3] in location):
                                length=len(a[:-3])
                                if(a[:-3]==location[:length]):
                                    result = Geocoder.geocode(location)
                                    context={'lat':result[0].coordinates[0],'lng':result[0].coordinates[1],'place':location,'type':""}
                                    return context
                    elif(a[-3:]=="ans"):
                        for location in locations:
                            if(a[:-4] in location):
                                length=len(a[:-4])
                                if(a[:-4]==location[:length]):
                                    result = Geocoder.geocode(location)
                                    context={'lat':result[0].coordinates[0],'lng':result[0].coordinates[1],'place':location,'type':""}
                                    return context
                    elif(a[-3:]=="ese" ):
                        for location in locations:
                            if(a[:-3] in location):
                                length=len(a[:-3])
                                if(a[:-3]==location[:length]):
                                    result = Geocoder.geocode(location)
                                    context={'lat':result[0].coordinates[0],'lng':result[0].coordinates[1],'place':location,'type':""}
                                    return context                           
        return({'lat':None,'lng':None,'place':None,'type':None})
    except Exception as e:
        print "problem ",e    
        return({'lat':None,'lng':None,'place':None,'type':None})
Example #22
0
    def save(self, *args, **kwargs):
        print "Form:DestinationForm_save"
        commit = kwargs.pop('commit', True)
        instance = super(DestinationForm, self).save(*args, commit = False, **kwargs)
        searchlocation = self.cleaned_data['address']
        geoloc = Geocoder.geocode(searchlocation)[0]
        instance.name = str(geoloc).split(',')[0].strip()
        country, created = Country.objects.get_or_create(name=geoloc.country)
        instance.country_id = country.id
        geoloc_state = geoloc.state
        if not geoloc_state:
            geoloc_state = "None"
        state, created = State.objects.get_or_create(name=geoloc_state, country_id=instance.country_id)

        instance.state_id = state.id

        if not instance.time_required:
            instance.time_required = "0"

        instance.latitude = geoloc.coordinates[0]
        instance.longitude = geoloc.coordinates[1]
        #I'm wondering why this is not handled by the the default value tha tis being set. Enough time wasted on this.
        instance.added_on = datetime.utcnow()
        print 'Going to commit data', instance.added_by
        if commit:
            instance.save()
        return instance
Example #23
0
 def on_success(self, data):
     if 'text' in data:
         try:
             (_, tag, name, address) = data['text'].split('\\', 4)
             result = Geocoder.geocode(address)
             (lat, lng) = result[0].coordinates
             formatted_address = result[0].formatted_address
             feed = self.get_feed(name.upper(), lat, lng)
             if feed:
                 # Filter dupes
                 logging.debug('Feed exists: %s' % (feed))
                 if tag.upper() in self.reply_tags.upper():
                     feed.state = 'closed'
                     feed.last_modified = datetime.now()
                     db_session.merge(feed)
                     db_session.commit()
                     logging.debug('Feed updated: %s' % (feed))
             else:
                 f = Feed(name=name.upper(),
                          lat=lat,
                          lng=lng,
                          address=formatted_address)
                 db_session.add(f)
                 db_session.commit()
                 logging.debug('Feed created: %s' % (f))
         except (SQLAlchemyError, DatabaseError) as e:
             logging.error(e)
         except ValueError as e:
             pass
         except (DBAPIError, Exception) as e:
             logging.error(e)
         finally:
             db_session.remove()
Example #24
0
def add_locs(totals):
    zipcodes = pd.read_csv('zip_code_database.csv', dtype={'zip':str})[
        ['zip', 'state', 'latitude', 'longitude']
    ]

    # merge the datasets and add back in the grouped international 
    # PhD almamater too (place it somewhere off Baja)
    totals = pd.merge(totals, zipcodes, on='zip', how='inner').append(
        pd.Series(dict(
            institution='International',
            num_bac_docs=totals.num_bac_docs.iloc[1],
            num_docs=0,
            latitude=29.228359, longitude=-123.072512)), 
        ignore_index=True)

    
    counts = totals.groupby('zip').count().institution
    has_dup_zip = totals.zip.isin(counts[counts > 1].index)
    for i, row in totals[has_dup_zip | (totals.num_docs > 10)].iterrows():
        try:
            # try and do a lookup of the school's geocoded location
            loc = Geocoder.geocode("{} {} {}".format(
                row['institution'], row['state'], row['zip']))
        except: 
            # if the query doesn't work - just use zipcode's lat/lon
            continue

        # if it does, replace the lat/lon
        totals.ix[i, 'latitude'] = loc.latitude
        totals.ix[i, 'longitude'] = loc.longitude
        
    return totals
Example #25
0
def writeToDB(location, source, headline):
    try:
	result = Geocoder.geocode(location)
	(lat, lon) = result[0].coordinates
	cur.execute( "INSERT INTO feeds(lon, lat, source, headline, city) VALUES (%s, %s, %s, %s,  %s)",  (str(lon), str(lat), source, headline, city))
    except GeocoderError: 
	print "The location given by twitter returns error when given to Geocoder"
def create_job():
    title = request.form['title']
    description = request.form['description']
    skills = [
        skill.strip().lower() for skill in request.form['skills'].split(',')
    ]
    location = request.form['location']
    createdOn = datetime.utcnow()
    companyName = request.form["companyName"]
    companyWebSite = request.form["companyWebsite"]
    companyContactEmail = request.form["companyContactEmail"]
    companyContactTelephone = request.form["companyContactTelephone"]
    results = Geocoder.geocode(location)
    lngLat = [results[0].coordinates[1], results[0].coordinates[0]]

    job = {
        "title": title,
        "description": description,
        "skills": skills,
        "location": location,
        "lngLat": lngLat,
        "createdOn": createdOn,
        "company": {
            "name": companyName,
            "website": companyWebSite,
            "contact": {
                "email": companyContactEmail,
                "telephone": companyContactTelephone
            }
        }
    }
    job_id = db.jobs.insert(job, w=1)
    flash(u'Job created with id %s' % job_id)
    return redirect(url_for('create_job'))
Example #27
0
def getLocation( latitude, longitude):
    ''' Get data address data from latitude and longitude '''
    function = 'getLocation'
    address = ''
    city = ''
    postalcode = ''
    country = ''
    g = geocoders.GoogleV3()
    log.debug(function)

    try:
        point = '%.6f, %6f' % (latitude, longitude)

        positions = g.reverse( point )
        if positions:
            data, (latitude, longitude)  = positions[0]
            results = Geocoder.geocode(data)

            address = results[0].route
            if results[0].street_number:
                address = address + ',' + results[0].street_number
            country = results[0].country
            city =  results[0].city
            postalcode = results[0].postal_code
    except:
        pass

    return address, city, postalcode, country
Example #28
0
    def getFromMapsAPI(self, location_string):
        try:
            address = Geocoder.geocode(location_string)
        except GeocoderError:
            return None




        zip = address.postal_code

        if(zip != None):
            try:
                return City.objects.get(zip_code=zip)
            except City.DoesNotExist:

                return None
        formatted_address = address.formatted_address
        if(formatted_address == None):
            return None

        locaiton_array = formatted_address.split(",")
        city_name = locaiton_array[0]
        state = address.state
        if (state == None or city_name == None):
            return None

        possibilities = City.objects.exclude(region=None).filter(name__icontains=city_name, region__name__icontains=state)


        if(possibilities.count() == 0):
            return None

        return possibilities[0]
def get_buildings_info(buildings, update=False):
    """
    Get the information from all the buildings
    """
    filename = 'info.yaml'
    info = load_from(filename)

    if not info:
        info = {
            title: ohmparser.get_building_info(url)
            for title, url in buildings.iteritems()
        }
        update = True

    if update:
        for title, building in info.iteritems():
            if not 'title' in building:
                building['title'] = title
            if not 'Latitude' in building:
                try:
                    results = Geocoder.geocode(building['location'])

                    coordinates = getattr(results[0], 'coordinates')
                    building['Latitude'] = coordinates[0]
                    building['Longitude'] = coordinates[1]
                except (KeyError, GeocoderError):
                    pass

        save_to(info, filename)

    return info
Example #30
0
def get_lat_longs(location):
    """
    :param location:
    :return:
    """
    geocode = Geocoder()
    return geocode.geocode(location)[0].coordinates
Example #31
0
def GetLocation(address):
    geocoder = Geocoder()
    try:
        result = geocoder.geocode(address, language="en")
        return result.coordinates
    except Exception as e:
        return (None, None)
Example #32
0
    def retrieve(self, city, attempt=1):
        """Try to grab json data in multiple ways"""
        r = get_json_or_timeout(self.api_url.format(self.APIKEY,
                                                    '/'.join(self._features),
                                                    self.lang,
                                                    city))

        # when service is unreachable or times out
        if r is None:
            raise WeatherError('Could not reach wunderground.com')
        # if service does not recognize city, it returns possible
        # zmw code, which is possible to search for city with
        elif 'results' in r['response']:
            return self.retrieve('zmw:' + r['response']['results'][0]['zmw'],
                                 attempt + 1)
        # some cities are not recognized and have to have `,country` at the end
        # or be searched based on latitude and longitude
        elif 'error' in r['response']:
            if attempt < 2:
                loc = Geocoder.geocode(city)
                return self.retrieve(','.join([str(loc[0].latitude),
                                               str(loc[0].longitude)]),
                                     attempt + 1)
            elif attempt < 3:
                return self.retrieve(city + ',' + self.country, attempt + 1)
            raise WeatherError(r['response']['error']['description'])
        else:
            return r
Example #33
0
def generate_user_home_work(user):
    user_home=detect_home(user)
    # print user_home
    zip_is_valid = _check_zip_validity(user_home, user)
    logging.debug('starting for %s' % user)
    if Profiles.find({'user_id':user}).count()==0:
        profile_todo={'source':'Shankari','user_id': user,'home':user_home}
        Profiles.insert(profile_todo)
    else:
        #TODO: make detect_home return something better than a N/A string
        Profiles.update({"$and":[{'source':'Shankari'},
                                     {'user_id':user}]},{"$set":{'home':user_home}})
    user_work=detect_work_office(user)
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'work_place':user_work}})
    user_zip=get_userZipcode(user, zip_is_valid)
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip':user_zip}})
    if user_zip!='N/A':
        geoinfo= Geocoder.geocode(user_zip)
        # geocoder returns data in lat,lng format.
        # we convert to lng,lat internally, since that is the format that the
        # rest of our code is in
        zipCen=[geoinfo[0].coordinates[1],geoinfo[0].coordinates[0]]
    else:
        zipCen='N/A'
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip_centroid':zipCen}})


    for day in range(1,6):
        key='work'+str(day)
        Profiles.update({"$and":[{'source':'Shankari'},
                                     {'user_id':user}]},{"$set":{key:detect_daily_work_office(user,day)}})
Example #34
0
def collect_smartly():

    try:
        cur = cnx.cursor()
        data = cur.execute('select * from taxi')
        rows = cur.fetchall()

        cluster = QuadTree({'x': 0, 'y': 0, 'width': 18, 'height': 24}, 0)

        for entries in rows:
            address = Geocoder.geocode(entries[1])
            data = {
                'Address': entries[1],
                'x': address.coordinates[0],
                'y': address.coordinates[1],
                'name': entries[0]
            }
            cluster.insert(data)

        post = []
        post.extend(cluster.getAllobjects())
        cluster.clear()
        #return json.dumps(post)
        return render_template("/admin/cluster/index.html", posts=post)

    except Exception as e:
        print "WHAT THE F**K IS WRONG => {0}".format(e)
        #return {"f**k this shit "}
        return render_template('/admin/cluster/index.html',
                               posts=json.dumps({'error': 'F****d'}))
Example #35
0
 def get_lat_long_gmap(self):
     _logger.info('begin get_lat_long_gmap')
     if self.partner_shipping_id:
         address = self.partner_shipping_id.name
         address += ', '
         address += self.partner_shipping_id.street
         street = ''
         street += self.partner_shipping_id.street
         if self.partner_shipping_id.street2:
             street += ', '
             street += self.partner_shipping_id.street2
         if self.partner_shipping_id.city:
             street += ', '
             street += self.partner_shipping_id.city
         if self.partner_shipping_id.country_id:
             street += ', '
             street += self.partner_shipping_id.country_id.name
         results = Geocoder.geocode(street)
         val = results[0].coordinates
         lat = val[0]
         lng = val[1]
         _logger.info('lat: %s' % lat)
         _logger.info('lat: %s' % lng)
         self.write({'lat': lat, 'lng': lng, 'address': address})
     _logger.info('end get_lat_long_gmap')
     return True
    def retrieve(self, city, attempt=1):
        """Try to grab json data in multiple ways"""
        r = get_json_or_timeout(self.api_url.format(self.APIKEY,
                                                    '/'.join(self._features),
                                                    self.lang,
                                                    city))

        # when service is unreachable or times out
        if r is None:
            raise WeatherError('Could not reach wunderground.com')
        # if service does not recognize city, it returns possible
        # zmw code, which is possible to search for city with
        elif 'results' in r['response']:
            return self.retrieve('zmw:' + r['response']['results'][0]['zmw'],
                                 attempt + 1)
        # some cities are not recognized and have to have `,country` at the end
        # or be searched based on latitude and longitude
        elif 'error' in r['response']:
            if attempt < 2:
                loc = Geocoder.geocode(city)
                return self.retrieve(','.join([str(loc[0].latitude),
                                               str(loc[0].longitude)]),
                                     attempt + 1)
            elif attempt < 3:
                return self.retrieve(city + ',' + self.country, attempt + 1)
            raise WeatherError(r['response']['error']['description'])
        else:
            return r
Example #37
0
def claim_map(request, claim_id):
    claim = Claims.objects.get(pk=claim_id)
    address = claim.address
    try:
        geolocator = Nominatim()
        location = geolocator.geocode(address.encode('utf-8') + ", Умань, Україна")
        lat = location.latitude
        str_lat = str(lat)
        min_lat = str(lat - 0.0008)
        max_lat = str(lat + 0.0008)
        lon = location.longitude
        str_lon = str(lon)
        min_lon = str(lon - 0.00117)
        max_lon = str(lon + 0.00117)
    except:
        results = Geocoder.geocode(address.encode('utf-8') + ", Умань, Україна")
        lat = results[0].coordinates[0]
        str_lat = str(lat)
        min_lat = str(lat - 0.0008)
        max_lat = str(lat + 0.0008)
        lon = results[0].coordinates[1]
        str_lon = str(lon)
        min_lon = str(lon - 0.00117)
        max_lon = str(lon + 0.00117)
    return render(request, 'maps.html', locals())
Example #38
0
def parseLocation(address):
    """
	takes in an address, decodes it with Geocoder,
	and returns a set of coordinates
	"""
    coords = {}
    coords['type'] = "Point"
    log.debug("parseLocation() received: " + address)
    if (address != None):
        latitude = 0
        longitude = 0
        try:
            coordinates = Geocoder.geocode(address)[0]
            latitude = coordinates.latitude
            longitude = coordinates.longitude
        except:
            pass
        coords['coordinates'] = [latitude, longitude]
        global coordCount
        coordCount += 1
        log.debug("coordinates: [" + str(latitude) + ", " + str(longitude) +
                  "%d]")
    else:
        coords['coordinates'] = [0, 0]
        log.debug("coordinates: [0, 0]")
    return coords
Example #39
0
def add_for_destination(request, id):
    print 'In add_point_of_interest_for_destination method. Id:', id
    context = RequestContext(request)
    saved = False
    edit = False
    if request.method == 'POST':
        form = PointOfInterestForm(request.POST)
        if form.is_valid():
            if request.user.is_authenticated():     
                print "User authenticated", request.user.username
                form.instance.added_by = request.user.username
            form.save()
            if 'photo' in request.FILES:
                form.instance.photo = request.FILES['photo'];
                form.save()
                print "Upload file name", request.FILES['photo'].name, form.instance.id
            saved = True;
            print "Got ID", form.instance.id
    else:
        searchlocation = request.GET.get('searchfor','')
        if searchlocation:
            print "Found location from search:", searchlocation
            geoloc = Geocoder.geocode(searchlocation)[0]
            latitude = geoloc.coordinates[0]
            longitude = geoloc.coordinates[1]
            address = utils.generate_address(geoloc)

            poi_instance = PointOfInterest(address = address, latitude = latitude, longitude = longitude, destination_id=id);
            edit = True
        else:
            poi_instance = PointOfInterest(destination_id=id);
        form = PointOfInterestForm(instance=poi_instance)
    print "set id to", form.instance.destination
    print "Saved?", saved    
    return render_to_response('search/add_point_of_interest.html', {'form': form, 'edit': edit, 'saved':saved}, context);
Example #40
0
def _update_account(account, form):
    account.set_name(form.name.data)

    if form.degree.data:
        account.set_degree(form.degree.data)
    elif form.degree.data == "":
        del account.degree

    if form.postcode.data:
        account.set_postcode(form.postcode.data)

        results = Geocoder.geocode(form.postcode.data + ', United Kingdom')
        lat, lng = results[0].coordinates
        account.set_location(lat, lng)
    elif form.postcode.data == "":
        del account.postcode
        account.unset_location()

    if form.phone.data:
        account.set_phone(form.phone.data)
    elif form.phone.data == "":
        del account.phone

    if form.graduation.data:
        account.set_graduation(form.graduation.data)
    elif form.graduation.data == "":
        del account.graduation
Example #41
0
    def geocode(self, fields):
        city = fields[self.schema['properties']['city']].strip()

        state = fields[self.schema['properties']['state']].strip()

        country = fields[self.schema['properties']['country']].strip()

        location = ''
        if city and state and country:
            location = "{0}, {1}, {2}".format(city, state, country)
        elif state and country:
            location = "{0}, {1}".format(state, country)
        elif country:
            location = "{0}".format(country)
        else:
            return None

        if len(location):
            # time.sleep(0.5)
            coordinates = Geocoder.geocode(location)
        else:
            return None

        print "\n\n{0}".format(location)
        print "{0}\n\n".format(coordinates[0].coordinates)

        return [coordinates[0].coordinates[1], coordinates[0].coordinates[0]]
Example #42
0
    def get_geo_information(self, obj):
        if obj.is_valid_address:
            return 0
        address = "%s %s, %s, Deutschland" % (obj.street, obj.number, obj.city)
        address = address.replace(unicode('ä',"utf-8"), "ae")
        address = address.replace(unicode('ö',"utf-8"), "oe")
        address = address.replace(unicode('ü',"utf-8"), "ue")
        address = address.replace(unicode('ß',"utf-8"), "ss")
        try:
            location = Geocoder.geocode(address)
        except Exception:
            obj.is_valid_address = False
            log.info("Google did not return any results for %s" % address )
            return 0
        # chekc if address is valid and add street name from google to get rid of spelling differences
        obj.is_valid_address = location[0].valid_address
        if (obj.is_valid_address):
            obj.street = location[0].route
            obj.city = location[0].locality    
            #self.city = location[0].city
            # add zip code
            obj.zip_code = location[0].postal_code
            (obj.geo_lat, obj.geo_long) = location[0].coordinates
            # in case name is not set. generate it
            if obj.name == '' or obj.name is None:
                obj.name = obj.street + ' ' + str(obj.number);
            log.info("Creating new Kiosk: %s" % obj.name )
#         # custom stuff here
        obj.save()
        return 1
Example #43
0
def geocode(location):
    """Gets geocode results for a location and caches it."""
    try:
        results = Geocoder.geocode(location)
    except GeocoderError:
        return
    return results
Example #44
0
def create_map(profiles):
    options = ("no", "don't", "not", u"❌", "sorry", u"🚫", u"✋", "paypal", "pp",
        "negociable", "negotiable", "negoatiating", "negotiate",
        "offer", "deal", "lower")
    kml = simplekml.Kml()
    coords_map = {}
    g = Geocoder()
    for p in profiles:
        location = p["location"]

        # Check if it's just a message and not a location
        if any(o in location for o in options):
            continue

        # We can't hit the API too hard
        time.sleep(6)
        try:
            coords = coords_map[location]
        except KeyError:
            try:
                results = g.geocode(location)
                lat, lng = results.coordinates
                coords = [(lng, lat)]
                coords_map[location] = coords
                pnt = kml.newpoint()
                pnt.style.labelstyle.scale = 2  # Make the text twice as big
                pnt.name =  p["user_name"]
                pnt.coords = coords

                # Save the KML
                kml.save("poshmark.kml")
            except GeocoderError as e:
                print e
Example #45
0
def writeToDB(location, profile, profilepic, favourites, retweets, body, feedId, feedCreationTimestamp, source):
	try:
		result = Geocoder.geocode(location)
		(lat, lon) = result[0].coordinates
		cur.execute( "INSERT INTO feeds(lon, lat, profile, profilepic, favourites, retweets, body, location, feedID, feedCreationTimestamp, feedInsertionTimestamp, source) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, %s)",  (str(lon), str(lat), profile, profilepic, favourites, retweets, body, location, feedId, feedCreationTimestamp, source))
	except GeocoderError: 
		print "The location given by twitter returns error when given to Geocoder"
Example #46
0
def geocode_property(obj):

    if obj.geocoded is False:

        attempts = 0
        success = False

        while success != True and attempts < 3:
            try:
                result = Geocoder.geocode(obj.address)
                attempts += 1

                if result.valid_address:
                    obj.lat = result[0].coordinates[0]
                    obj.lon = result[0].coordinates[1]
                    obj.geometry = Point(obj.lon, obj.lat)
                    obj.geocoded_address = str(result)
                    obj.geocoded_type = result.raw[0]['geometry']['location_type']
                    obj.geocoded = True

                # no geocoding error
                success = True

            except GeocoderError, e:
                if 'OVER_QUERY_LIMIT' in e:
                    time.sleep(2)
                    # retry
                    continue
                else:
                    # not really true, but stop trying
                    # not sure what happened
                    success = True 
                    break
Example #47
0
def csvGeocoder(data):
    for line in data:
        [
            StreetNumber, Street, City, Province, Country, FullAddress,
            OtherField, OtherField2, Whatever
        ] = line  #Make sure the number of columns matches/aligns with the number of fields listed here.
        if City == "City":  #This skips the header. Don't geocode the header :D
            Latitude = ["Latitude"]
            Longitude = ["Longitude"]
            new_line = line + Latitude + Longitude  #This adds two new columns to your .csv, Latitude and Longitude.
            csv.writer(output_file).writerow(new_line)
            print new_line  # This isn't required. I just like to watch.
        else:
            #I use a column with the Full Address (Street Number, Street, City, Provice/State, Country) But you could concatenate from multiple fields too.
            results = Geocoder.geocode(FullAddress)
            Latitude = [results.coordinates[0]]
            Longitude = [results.coordinates[1]]
            new_line = line + Latitude + Longitude
            csv.writer(output_file).writerow(new_line)
            time.sleep(
                .21
            )  #This throttles your requests. The GoogleAPI doesn't like too many requests per second.
            print new_line  #Printing to the console makes the process a lot longer. Omit for speed.

    del url, City, Address, Ward, Status, ListDate, IntentionDate, ByLaw, PartIVDate, PartVDate, HeritageDistrict, DistrictStatus, HeritageEasement, RegistrationDate, BuildingType, ArchitectBuilder, ConstructionYear, Province, Country, FullAddress, Details, DemoDate, PrimaryAddress, line
    del data

    input_file.close()
    output_file.close()
Example #48
0
def geocode():
    input_type = raw_input(
        'Are you hoping to geocode addresses or coordinates?\nEnter Addresses or Coordinates: '
    )
    results = []
    estab_df = promptFilePath()
    if input_type == 'Addresses':
        print 'Fetching coordinates...'
        for a in estab_df["Address"]:
            time.sleep(0.25)
            results.append(list(geo.geocode(a).coordinates))
        geocoded_estab_df = estab_df.join(
            pd.DataFrame(results, columns=['Latitude', 'Longitude']))
    elif input_type == 'Coordinates':
        print 'Fetching addresses...'
        for i, c in estab_df.iterrows():
            time.sleep(0.25)
            results.append(
                list(
                    geo.reverse_geocode(estab_df.Latitude[i],
                                        estab_df.Longitude[i])))
        geocoded_estab_df = estab_df.join(
            pd.DataFrame(results, columns=['Address']))
    else:
        print 'Please only select Addresses or Coordinates.'
        return
    return geocoded_estab_df
Example #49
0
def planejamento(request):
    pedidos = Pedido.objects.values()
    entregas = []
    addresses = []
    for pedido in pedidos:
        if pedido["entregue"] == False:
            entregas.append(pedido)
    if len(entregas) > 0:
        for entrega in entregas:
            addresses.append(entrega["rua"] + ", " + str(entrega["numero"]) +
                             ", " + entrega["cidade"])
        #Entrega.EntregaController("Rua Apeninos, 990, Sao Paulo").bestRoute(addresses)["addresses"]
        rotas = Entrega.EntregaController(
            "Rua Apeninos, 990, Sao Paulo").bestRoute(addresses)
        planejamento = []
        for address in rotas["addresses"]:
            results = Geocoder.geocode(address)
            latitude, longitude = results[0].coordinates
            planejamento.append((address, latitude, longitude))
        return render_to_response('planejamento.html',
                                  locals(),
                                  context_instance=RequestContext(request))
    else:
        messages.warning(request, 'Nao há entregas para serem feitas')
        return render_to_response('planejamento.html',
                                  locals(),
                                  context_instance=RequestContext(request))
Example #50
0
def create_map(profiles):
    options = ("no", "don't", "not", u"❌", "sorry", u"🚫", u"✋", "paypal", "pp",
               "negociable", "negotiable", "negoatiating", "negotiate",
               "offer", "deal", "lower")
    kml = simplekml.Kml()
    coords_map = {}
    g = Geocoder()
    for p in profiles:
        location = p["location"]

        # Check if it's just a message and not a location
        if any(o in location for o in options):
            continue

        # We can't hit the API too hard
        time.sleep(6)
        try:
            coords = coords_map[location]
        except KeyError:
            try:
                results = g.geocode(location)
                lat, lng = results.coordinates
                coords = [(lng, lat)]
                coords_map[location] = coords
                pnt = kml.newpoint()
                pnt.style.labelstyle.scale = 2  # Make the text twice as big
                pnt.name = p["user_name"]
                pnt.coords = coords

                # Save the KML
                kml.save("poshmark.kml")
            except GeocoderError as e:
                print e
Example #51
0
def air_distance(origin, destination, units='km'):
    ''' Uses great circle distance and an uplift factor of 9% following
    Defra's guidance '''
    latlng_a = Geocoder.geocode(origin)
    latlng_b = Geocoder.geocode(destination)

    dist = great_circle_distance(latlng_a, latlng_b)

    if units == 'km':
        dist = dist / 1000.0
    elif units == 'miles':
        dist = dist / 1000.0 / KM_PER_MILE
    else:
        raise Exception('%s is not a valid unit system. Use "km" or "miles"' % units)

    return dist * (1 + GCD_UPLIFT)
Example #52
0
def restaurant():
    #Get input arguments
    restaurant = request.args.get("restaurant", "")
    miles = request.args.get("miles", '')
    zipcode = request.args.get("zipcode", "")
    try:
        miles = str(int(float(miles)))
        if miles=="0":
            miles = "1"
    except ValueError:
        return json.dumps(["Please enter a number into the miles field"])

    try:
        location = Geocoder.geocode(zipcode)
    except:
        return json.dumps(["I couldn't recognize that address. Could you enter another one?"])

    zipcode = location.formatted_address

    query = restaurant + " " + miles + " " + zipcode
    sql = ('SELECT Result FROM Cached WHERE Query = "' + query + '";')
    db.cursor.execute(sql)
    results = db.cursor.fetchall()
    if len(results)>0:
        result = ast.literal_eval(results[0][0])
    else:
        result = predict_rest.predict_rest(restaurant, miles, location)
        sql = ('INSERT INTO Cached (Query, Result) VALUES ("' + query + '", %s);')
        db.cursor.execute(sql, (str(result),))
        db.commit()

    return json.dumps(result)
    def geo_region():
        for item in map_regions:
            try:
                geo_reg = Geocoder.geocode(item)
            except:
                pass
            else:
                point = (geo_reg[0].coordinates)
                title = item
                joint = ', '.join(map(str, point))
                split_point = joint.split(',', 2)
                lat = split_point[0]
                lon = split_point[1]
                if '(All Digital)' in item:
                    regions_map.addpoint(float(lat), float(lon), "#0101DF", title=title)
                else:
                    regions_map.addpoint(float(lat), float(lon), "#FF0000", title=title)

            # print "Point: "
            # print point
            # print "Joint: "
            # print joint
            # print "split_joint: "
            # print split_point

            sleep(0.1)
Example #54
0
 def get_lat_long_gmap(self):
     _logger.info('begin get_lat_long_gmap')
     if self.partner_shipping_id:
         address = self.partner_shipping_id.name
         address += ', '
         address += self.partner_shipping_id.street
         street = ''
         street += self.partner_shipping_id.street
         if self.partner_shipping_id.street2:
             street += ', '
             street += self.partner_shipping_id.street2
         if self.partner_shipping_id.city:
             street += ', '
             street += self.partner_shipping_id.city
         if self.partner_shipping_id.country_id:
             street += ', '
             street += self.partner_shipping_id.country_id.name
         results = Geocoder.geocode(street)
         val = results[0].coordinates
         lat = val[0]
         lng = val[1]
         _logger.info('lat: %s' % lat)
         _logger.info('lat: %s' % lng)
         self.write({'lat': lat, 'lng': lng, 'address': address})
     _logger.info('end get_lat_long_gmap')
     return True
Example #55
0
 def post(self):
     logger = logging.getLogger('MySite')
     locations = self.request.str_params.getall("eventlocation")
     names = self.request.str_params.getall("eventname")
     dates = self.request.str_params.getall("eventdate")
     times = self.request.str_params.getall("eventtime")
     circle = self.request.get("circle")
     for i in range(len(locations)):
         if len(locations[i])>0:
             result = Geocoder.geocode(str(locations[i]))[0]
             logger.info(circle)
             if result:
                 newEvent = Event()
                 newEvent.creator = self.current_user.id
                 newEvent.name = names[i]
                 newEvent.lat = result.coordinates[0]
                 newEvent.lng = result.coordinates[1]
                 newEvent.address = locations[i]
                 newEvent.circle = circle
                 newEvent.time = times[i]
                 date = dates[i]
                 newEvent.ToD = datetime.date(int(date[6:]),int(date[0:2]),int(date[3:5]))
                 newEvent.put()
         
         
     self.redirect("/main")