示例#1
0
def dados_do_local(endereco):
    """
	Dado o endereco, retorna o endereco processado, a latitude e a longitude do local.
        Exemplo:
         place, (lat, lng) = dados_do_local(endereco)
	"""
    from geopy import geocoders
    if hasattr(settings,
               "EASY_MAPS_GOOGLE_KEY") and settings.EASY_MAPS_GOOGLE_KEY:
        g = geocoders.Google(settings.EASY_MAPS_GOOGLE_KEY)
    else:
        g = geocoders.Google(resource='maps')
    return g.geocode(endereco)
示例#2
0
文件: models.py 项目: rimbalinux/LMD3
 def fill_geocode_data(self):
     if not self.address:
         self.geocode_error = True
         return
     try:
         if hasattr(settings, "EASY_MAPS_GOOGLE_KEY") and settings.EASY_MAPS_GOOGLE_KEY:
             g = geocoders.Google(settings.EASY_MAPS_GOOGLE_KEY)
         else:
             g = geocoders.Google(resource='maps')
         address = smart_str(self.address)
         self.computed_address, (self.latitude, self.longitude,) = g.geocode(address)
         self.geocode_error = False
     except (UnboundLocalError, ValueError,geocoders.google.GQueryError):
         self.geocode_error = True
def encode_origin(origin_address):
    origin_address += 'Philadelphia,PA'
    g = geocoders.Google()
    place, (lat, lng) = g.geocode(origin_address)  #, exactly_one=False)
    print lat, lng, place
    return "{\"origin\":{\"latitude\":" + str(lat) + ",\"longitude\":" + str(
        lng) + "}}"
def show_search(request):
    """ Shows search """
    from geopy import geocoders
    from distance_helper import distance_between_points
    from market_buy.models import Show
    marketplace = request.marketplace

    city = request.POST.get('city', None)
    state = request.POST.get('state')
    zip = request.POST.get('zip')
    country = request.POST.get('country', 'US')

    if (city == u'') or (state == u''):
        request.flash[
            'message'] = "Please, fill at least the city and state fields to complete the search!"
        request.flash['severity'] = "error"
        return HttpResponseRedirect(reverse("buy_show_listing"))

    try:
        g = geocoders.Google(settings.GOOGLE_KEY)
        place = "%s, %s, %s" % (city, state, country)
        place, point1 = g.geocode(place)
    except Exception, e:
        request.flash[
            'message'] = "Could not determine your location. Try again with other input data!"
        request.flash['severity'] = "error"
        return HttpResponseRedirect(reverse("buy_show_listing"))
示例#5
0
def dfh(csvFH):
    baseFolder = "./data/dfh/"
    for folder in os.listdir(baseFolder):
        folder = baseFolder + "/" + folder
        g = geocoders.Google()
        print folder
        for dataFile in os.listdir(folder):
            dataFile = folder + "/" + dataFile
            content = open(dataFile, "r").read()
            data = decomposeDfhData(content)
            price = 0
            lat = 0
            lng = 0
            for line in data:
                key = line[0]
                val = line[1]
                #			print key,"\t:\t",val
                if (key == "address"):
                    try:
                        place, (lat, lng) = g.geocode(val)
                    except:
                        #print "error parsing location: "+val
                        lat = 0
                        lng = 0

                if key == "price":
                    price = val.strip("$").strip(" ")

            if price != 0 and lat != 0 and lng != 0:
                csvLine = str(price)
                csvLine = csvLine.replace(',', "")
                csvLine = csvLine + "," + str(lat) + "," + str(lng)
                print csvLine
                csvFH.write(csvLine + "\n")
    print "done dfh"
def get_page(page_id):
    url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&pageids=%s&rvprop=content&indexpageids&format=json' % page_id
    json = scraperwiki.scrape(url)
    content = simplejson.loads(json)
    id = content['query']['pageids'][0]
    actual_content = content['query']['pages'][id]['revisions'][0]['*']
    actual_content = re.sub("{{cite[^}]+}}", "", actual_content)
    match = re.search('{{Infobox ([^}]*)}}', actual_content)
    g = geocoders.Google(
        'ABQIAAAAD0swA2fCWr4WHfJT0hFApRQFh6EL7AzOjwKglJFzgjH9brlzARTFAkwWGlSkuzv9Tq_kKMoInlVlcA'
    )

    if match != None:
        # there is an info box GOOD

        infobox = match.group(1)
        details = infobox.split('|')
        info_dict = {}
        for pair in details:
            try:
                (key, val) = pair.split('=')
                key = key.strip()
                val = val.replace('[', '').replace(']', '')
                if key == 'Genre' or key == 'Current_members':
                    val = val.split('<br>')
                    if len(val) == 1:
                        val = val[0].split('<br />')
                    if len(val) == 1:
                        val = val[0].split('<br/>')
                    if len(val) == 1:
                        val = val[0].split(',')
                    val = [re.sub(r'<[^>]*?>', '', e).strip() for e in val]

                info_dict[key] = val
            except ValueError:
                pass

        if info_dict.get('Origin', None) != None:
            # skip page without origin
            try:
                place, (lat,
                        lng) = g.geocode(info_dict['Origin'].encode('utf8'))
            except:
                return None
            ret = {
                'pageids': id,
                'Name': info_dict.get('Name', None),
                'Origin': info_dict.get('Origin', None),
            }
            if info_dict.get('Genre', False):
                i = 1
                for genre in info_dict['Genre']:
                    ret['Genre%s' % i] = genre
                    i = i + 1
        else:
            return None
    else:
        #manage where there is not an info box
        return None
    return {'data': ret, 'latlng': (lat, lng)}
示例#7
0
def main():
    g = geocoders.Google(KEY)
    reader = csv.reader(open('BenAndJerry.csv', 'rb'))
    writer = csv.writer(open('BenAndJerryGeo.csv', 'wb'))
    head = reader.next()
    head.append('Address')
    head.append('Geocoded')
    head.append('Latitude')
    head.append('Longitude')
    writer.writerow(head)
    n = 1
    good = bad = 0
    for row in reader:
        n += 1
        addr = lat = lng = place = ''
        if row[13] != 'Yes':
            print 'Skip %d' % n
        else:
            addr = row[5] + ', ' + row[6] + ', ' + row[2] + ' ' + row[7]
            try:
                place, (lat, lng) = g.geocode(addr)
                good += 1
                print 'Good %d: %.5f, %.5f: %s (from %s)' % (n, lat, lng,
                                                             place, addr)
            except ValueError:
                lat = lng = place = '***FAIL***'
                bad += 1
                print 'Bad  %d: (from %s)' % (n, addr)
            time.sleep(2)
        row.append(addr)
        row.append(place)
        row.append(lat)
        row.append(lng)
        writer.writerow(row)
    print 'Good: %d, bad: %d' % (good, bad)
示例#8
0
    def import_stores(self, sheet):
        """
        Import a list of store data from the given sheet
        from an Excel workbook.
        """
        # Get our geocoder
        g = geocoders.Google()

        for n in range(sheet.nrows):
            values = sheet.row_values(n)

            store_key = values[0]
            if isinstance(store_key, (int, long, float)):
                # Create new store instance
                store = Store.from_row(values)

                if self.geocode:
                    try:
                        # Geocode the store location
                        address, pos = g.geocode(store.address_raw)

                        store.address = address.strip()
                        store.latitude = pos[0]
                        store.longitude = pos[1]
                        store.save()
                    except ValueError:
                        print "Multiple addresses returned for store %s!" % store.key

                    # Sleep to prevent hitting the geocoder rate limit
                    time.sleep(.35)

                # Some output
                self.uprint(store)
示例#9
0
def populate_professionals_without_geocodes():
	from geopy import geocoders
	from rolodx.main.models import Professional
	import sys, traceback
	import decimal
	import time
	pros = Professional.objects.filter(address_latitude=None)[100:]
	g = geocoders.Google()
	print 'Populating geocodes for %s professionals' % len(pros)
	for pro in pros:
		if pro.street_address != None:
			try:
				results = g.geocode(pro.street_address, exactly_one=False)
			except:
				traceback.print_exc(file=sys.stdout)
				try:
					print 'failure address: %s' % pro.street_address
				except:
					print 'could not print address for pro.id %s' % pro.id
				time.sleep(0.5)
				continue
			pro.address_latitude = decimal.Decimal(repr(results[0][1][0]))
			pro.address_longitude = decimal.Decimal(repr(results[0][1][1]))
			print pro.street_address
			print pro.address_latitude, pro.address_longitude
			pro.save()
			time.sleep(0.5)
示例#10
0
def oa_location_form(context, nodelist, *args, **kwargs):

    context.push()
    namespace = get_namespace(context)

    request = kwargs.get('request', None)
    obj = kwargs.get('object', None)
    user = kwargs.get('user', None)
    POST = kwargs.get('POST', None)
    start = kwargs.get('start', 0)
    end = kwargs.get('end', 5)

    if POST and POST.get("form_id") == "oa_location_form":
        form = LocationForm(POST)
        #if form is valid frab the lat/long from the geolocation service
        if form.is_valid():
            gn = geocoders.Google()
            try:
                loc = list(
                    gn.geocode(form.cleaned_data['description'],
                               exactly_one=False))
                loc = [(i[0], i[1][0], i[1][1]) for i in loc[start:end]]
                namespace['location'] = loc
            except Exception, e:
                namespace['error'] = str(e)
示例#11
0
def get_latlng_list(location):
    '''   get latitude and longitude    '''
    latlng = []
    gmaps = geocoders.Google(resource='maps')

    try:
        places, (lat, lng) = gmaps.geocode(location)
        time.sleep(0.1)
        latlng.append("%.5f" % lat)
        latlng.append("%.5f" % lng)
    except Exception as ex:
        #    x = "Didn't find exactly one placemark!"
        #    if str(e).find(x) >= 0:
        #      places, (lat, lng) = g.geocode(location, exactly_one=False)
        #      #places = g.geocode(location, exactly_one=False)
        #      time.sleep(0.1)
        #      latlng.append("%.5f" % places[1][0])
        #      latlng.append("%.5f" % places[1][1])
        #      log = open('logfile.txt', 'a')
        #      s = 'location: {0}, Exception: {1}'.format(location, e)
        #      log.write('%s\n' %s)
        #      log.close()
        #    else:
        #      log = open('logfile.txt', 'a')
        #      s = 'location: {0}, Exception: {1}'.format(location, e)
        #      log.write('%s\n' %s)
        #      log.close()
        log = open('logfile.txt', 'a')
        ss_ = 'location: {0}, Exception: {1}'.format(location, ex)
        log.write('%s\n' % ss_)
        log.close()

    return latlng
示例#12
0
 def save(self, *args, **kwargs):
     # Geocode the address and save the lattitude and longitude
     g = geocoders.Google()
     latlong = g.geocode(self.location)[1]
     self.lat = latlong[0]
     self.long = latlong[1]
     # Call the real save
     super(Location, self).save(*args, **kwargs)
def coorlookup(location):
    """Function is called from the 'sourcepagescraper' function to geolocate locations listed on website for each accident"""
    geolocate = geocoders.Google()
    try:
        loc = geolocate.geocode(location, exactly_one=True)
        return loc
    except:
        return ("", ("", ""))
示例#14
0
def run():
    fn = "straydata-9-8-2012-8-15-52-AM.csv"
    csv_file = open("%s/../fixtures/%s" % (os.path.dirname(__file__), fn))
    contents = csv.reader(csv_file, dialect='excel', delimiter=',')
    header = contents.next()
    g = geocoders.Google('AIzaSyAZoNPSlRTETltbmJvgYYqol0SLAVBgKs')
    for index, row in enumerate(contents):
        populate.apply_async(args=[row], countdown=index)
示例#15
0
def geocode_address_google(street, city, zip_code=''):
    address = "%s %s %s" % (street, city, zip_code)
    try:
        google = geocoders.Google()
        geocode = google.geocode(address)
        place, (lat, lng) = geocode
        return (lat, lng)
    except Exception:
        return None
示例#16
0
 def _guess_location(self, address_array):
     if not CAN_GEOCODE:
         return
     g = geocoders.Google()
     address = ", ".join(address_array)
     _, latlng = g.geocode(address)
     self.location = geos.fromstr("POINT({1} {0})".format(*latlng))
     self.save()
     return self.location
示例#17
0
def settings(request):
    """displays the settings for an account"""
    user = request.user
    profile = user.get_profile()
    accounts = SocialAccount.objects.filter(user=user)
    accountlist = []
    for account in accounts:
        accountlist.append(account.provider)
    apps = SocialApp.objects.all()
    context = {}
    if request.method == 'POST':  # If the form has been submitted...
        if request.POST["type"] == "location":
            lform = LocationForm(request.POST)
            mform = UserSettingsForm()
            if lform.is_valid():  # All validation rules pass
                if profile.location == None:
                    location = Location()
                    location.save()
                    profile.location = location
                    profile.save()
                else:
                    location = profile.location
                location.city = lform.cleaned_data['city']
                location.street = lform.cleaned_data['street']
                location.postcode = lform.cleaned_data['postcode']
                profile.displayLocation = lform.cleaned_data['displayLocation']
                g = geocoders.Google()
                if location.city != "" or location.street != "" or location.street != "":
                    try:
                        places = g.geocode(location.street + ", " +
                                           location.postcode + " " +
                                           location.city,
                                           exactly_one=False)
                        location.latitude = places[0][1][0]
                        location.longitude = places[0][1][1]
                        location.save()
                    except geocoders.google.GQueryError, e:
                        messages.add_message(
                            request, messages.ERROR,
                            u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?"
                        )
                else:
                    location.latitude = None
                    location.longitude = None
                    location.save()
                profile.save()
        else:
            lform = LocationForm()
            mform = UserSettingsForm(request.POST)
            mform.user = request.user
            if mform.is_valid():
                if mform.cleaned_data['email'] != user.email:
                    set_mail(user, mform.cleaned_data['email'])
                if mform.cleaned_data['displayname'] != profile.displayname:
                    profile.displayname = mform.cleaned_data['displayname']
                    profile.save()
                user.save()
示例#18
0
def run(search):
    g = geocoders.Google()
    results = g.geocode(search, exactly_one=False)
    for place, (lat, lng) in results:
        print "PLACE", place
        print "LAT", lat
        print "LNG", lng
        print
    return 0
示例#19
0
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 255

        if 'geocoder' in kwargs:
            self.geocoder = kwargs.pop('geocoder')
        elif GOOGLE_KEY:
            self.geocoder = geocoders.Google(GOOGLE_KEY)

        super(GeoCodeField, self).__init__(*args, **kwargs)
示例#20
0
def geocode(address):
    from geopy import geocoders

    g = geocoders.Google()
    try:
        places = list(g.geocode(address, exactly_one=False))
        return _places2points(places)
    except Exception:
        log.exception("geocoding exception")
        return []
示例#21
0
def getll(loc):
    g = geocoders.Google(domain='maps.google.co.uk')
    try:
        for place, (lat, lng) in g.geocode(loc,exactly_one=False):
            ltd=lat
            lngt=lng
    except:
        ltd=0
        lngt=0
    return ltd,lngt
示例#22
0
def geocode_file(filename):
    ''' This is the main function '''
    f = open(filename, 'r+')
    o = open("output.txt", "w")
    for place in f:
        g = geocoders.Google()
        pla, (lat,lng) = list(g.geocode(place, exactly_one = False))[0]
        o.write(pla.split(",")[0]+","+str(lat)+","+str(lng)+"\n")
    f.close()
    o.close()
示例#23
0
def geo_code(address):
    app_id = "ABQIAAAA6JvOBb2RTumkCsWOAMKcmBRy05bz2Ez3szx9GRUEydtISW_OkxRqGIBjuawVqDpmZxzBW-l_IaKwxg"
    g = geocoders.Google(app_id)
    try:
        res = g.geocode(address.address + "," + address.zip.code + " USA",)
        place, (lat, lng) = res
        if lat and lng:
            address.point = Point((lat, lng), srid=4326)
            address.save()
    except ValueError, e:
        print e
示例#24
0
文件: geocode.py 项目: sboisen/F3
 def handle(self, *args, **options):
     g = geocoders.Google()
     for farm in Farm.objects.all():
         if farm.longitude == None and farm.latitude == None:
             try:
                 place, (lat, lng) = g.geocode(farm.address)
             except:
                 pass
             else:
                 farm.address, farm.latitude, farm.longitude = place, lat, lng
                 farm.save()
示例#25
0
 def update_geolocation(self):
     from geopy import geocoders
     try:
         profile = self.owner().get_profile()
         g = geocoders.Google(settings.GOOGLE_KEY)
         place = "%s, %s, %s, %s" % (profile.street_address, profile.city, profile.state, profile.country)
         place, point = g.geocode(place)
         self.location = "%s,%s" % point
         self.save()
         logging.info("shop %s updated" % self)
     except Exception, e:
         logging.info("shop %s location could not be updated because %s" % (self, e))
示例#26
0
 def save(self):
     from geopy import geocoders
     super(Show, self).save()
     try:
         g = geocoders.Google(settings.GOOGLE_KEY)
         place = "%s, %s, %s, %s" % (self.address, self.city, self.state,
                                     self.country)
         place, point = g.geocode(place)
         self.location = "%s,%s" % point
         super(Show, self).save()
     except:
         pass
    def handle(self, *args, **options):
        """Run geocode."""
        appid = getattr(settings, "YAHOO_APP_ID", None)
        if not "google" in options["geocoder"] and appid is not None:
            self.geocoder = geocoders.Yahoo(appid)
            sys.stderr.write("Geocoding with Yahoo world...\n")
        else:
            self.geocoder = geocoders.Google()
            sys.stderr.write("Geocoding with Google maps...\n")

        for repo in models.Repository.objects.all():
            self.geocode_contact(repo)
示例#28
0
def hardwareEdit(request, id=None):
	user = request.user
	profile = user.get_profile()
	if user.email != "" and profile.mail_confirmed:
		if id==None:
			#Create new hardware
			if request.method == 'POST': # If the form has been submitted...
				form = HardwareForm(request.POST) # A form bound to the POST data
				if form.is_valid(): # All validation rules pass
					h = Hardware()
					h.name = form.cleaned_data['name']
					h.description = form.cleaned_data['description']
					h.condition = form.cleaned_data['condition']
					h.category = form.cleaned_data['category']
					h.state = get_object_or_404(State, id=form.cleaned_data['state'])
					h.owner = user
					if form.cleaned_data['ownlocation']:
						location = Location()
						location.city = form.cleaned_data['city']
						location.street = form.cleaned_data['street']
						location.postcode = form.cleaned_data['postcode']
						g = geocoders.Google()
						if location.city!= "" or location.street!="" or location.street!="":
							searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city']
							try:
								places = g.geocode(urllib.quote(searchstring), exactly_one=False)
								location.latitude = places[0][1][0]
								location.longitude = places[0][1][1]
								location.save()
							except geocoders.google.GQueryError, e:
								messages.add_message(request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?")
								context = {'form':form, 'edit':False}
								return render_to_response('hardware/hardwareform.html', context, RequestContext(request))
							except:
								messages.add_message(request, messages.ERROR, u"Es gab einen Fehler beim überprüfen des Ortes. Hast du dich vielleicht vertippt?")
								context = {'form':form, 'edit':False}
								return render_to_response('hardware/hardwareform.html', context, RequestContext(request))
						else:
							location.latitude = None
							location.longitude = None
							location.save()
						h.location = location
					else:
						h.location = profile.location
					if h.state.temporary and form.cleaned_data['lendlength'] != None:
						h.lendlength = form.cleaned_data['lendlength']
						h.lendlengthtype = form.cleaned_data['lendlengthtype']

					h.save()
					messages.add_message(request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.")
					context = {'form':form, 'hardware':h}
					return render_to_response('hardware/imageform.html', context, RequestContext(request))
示例#29
0
    def save(self, *args, **kwargs):
        cur_location = self.get_current_location()

        if cur_location is not None and cur_location != self.location:
            g = geocoders.Google(settings.GOOGLE_API_KEY)
            try:
                for place, point in g.geocode(cur_location, exactly_one=False):
                    self.latitude, self.longitude = point
                    self.location = cur_location
            except:
                pass

        super(CoordinateModel, self).save(*args, **kwargs)
示例#30
0
def geocode(location):
    """
    Uses the Google Maps geocoder to determine a location's coordinates. If the
    geocoder returns multiple results, choose the first one.
    """
    sys.stdout = open(settings.STDOUT_FILE, 'w')
    g = geocoders.Google(resource='maps')
    ends_with_zip = r'(?: \d{5}| \d{5}-\d{4})$'
    if not re.search(ends_with_zip, location):
        location += ', 65201'
    place, (lat, lng) = list(g.geocode(location, exactly_one=False))[0]
    sys.stdout = sys.__stdout__
    return Point(lng, lat)