示例#1
0
    def save(self, force_insert=False, force_update=False):
        if not self.address:
            raise ValidationError(u'Location object must be initialized with an address.')
            
        location = "%s, %s, %s" % (self.address, 'Somerville', 'MA')

        if not self.geometry:
            try:
                result = geocode_to_point_object(location)
            except AmbiguousResult:
                pass
                
        if result:
            self.title = result[0]
            self.geometry = result[1]
        else:
            raise Exception(u'Failed to geocode address.')
                
        super(Location, self).save()
示例#2
0
文件: views.py 项目: gerlad/greenline
def your_station(request, form_class=StationForm, template_name="stations/your_station.html"):
    ''' Request an address and return a new Location, 
        with directions to nearest Station. '''
        
    context = {'GOOGLE_API_KEY': lazy_key()}
    station = None
    geom = None
    title = None
    
    if request.method == "POST":
        result = None
        data = request.POST.copy()
        address = data.get("address")
        
        location = "%s, %s, %s" % (address, 'Somerville', 'MA')
        
        try:
            result = geocode_to_point_object(location) # result = (str, (Point object))
        except (HTTPError, BadStatusLine):
            raise StationBadRequest(request)
            
        if result:
            title = result[0]
            geom = Point(result[1][0], result[1][1])
            latitude = result[1][1]
            longitude = result[1][0]
            nearest = nearest_station_from_geo(geom)
            
            if not nearest:
                station = None
            else:
                if len(nearest) > 1:
                    station1 = nearest[0]
                    stations = list(Station.objects.filter(name=station1))
                    station2 = nearest[1]
                    stations.append(Station.objects.filter(name=station2))
                    stations = (stations[0], stations[1][0])
                    stations[0].geometry.transform(4326)
                    stations[1].geometry.transform(4326)
                    
                    station = nearer_station(geom, stations)
                else:
                    nearest = nearest[0]
                    station = Station.objects.filter(name=nearest) # a GeoQuerySet
                    station = station[0] # a Station
                    station.geometry.transform(4326)

            if station:
                exact_distance = distance.distance((latitude, longitude), (station.latitude, station.longitude)) # geopy.distance.VincentyDistance
                #log.debug('Location is  %s meters from %s', exact_distance.meters, station.name) 
                exact_distance_in_feet = exact_distance.ft
                exact_distance_in_meters = str(exact_distance.meters)
                exact_distance_in_meters = exact_distance_in_meters[:exact_distance_in_meters.rfind('.')]
                exact_distance_in_feet = str(exact_distance.feet)
                exact_distance_in_feet = exact_distance_in_feet[:exact_distance_in_feet.rfind('.')]
            else:
                raise Exception("Got an error for %s ." % escape(result[0]))
        else:
            raise Exception("Unknown geocoding error. Please try another address.")
    else:
        if request.method == 'GET':
            station_form = StationForm()        
            return render_to_response(template_name, {
                "form": station_form
            }, context_instance=RequestContext(request))
            
    return render_to_response("stations/checkin.html", {
        'name': address,
        'station': station,
        'geom': geom,
        'title': title,
        #'distance_in_meters':exact_distance_in_meters,
        'distance_in_feet':exact_distance_in_feet,
    }, context_instance=RequestContext(request))