示例#1
0
    def nearby(self,
               latitude,
               longitude,
               distance_range=5,
               ids=None,
               exclude_ids=None):
        if latitude is None or longitude is None:
            return []
        queryset = self.get_queryset().filter(is_registered=True,
                                              is_visible=True)
        if ids is not None:
            queryset = queryset.filter(id__in=ids)
        if exclude_ids is not None:
            queryset = queryset.exclude(id__in=exclude_ids)
        rough_distance = units.degrees(arcminutes=units.nautical(
            kilometers=distance_range)) * 2
        latitude, longitude = float(latitude), float(longitude)
        queryset = queryset.filter(
            clatitude__range=(latitude - rough_distance,
                              latitude + rough_distance),
            clongitude__range=(longitude - rough_distance,
                               longitude + rough_distance))

        result = []
        for customer in queryset:
            exact_distance = distance.distance(
                (latitude, longitude),
                (customer.clatitude, customer.clongitude)).m
            result.append((customer, exact_distance))

        result.sort(key=lambda x: x[1])
        return result
示例#2
0
文件: geo.py 项目: dalou/django-cargo
    def near(self, latitude=None, longitude=None, distance_range=None, skip_box_approximation=False):
        """
        @param float latitude
        @param float longitude
        @param float distance_range in kilometers | Distance instance
        """
        query_set = super(BaseLocationManager, self).get_query_set()

        if not (latitude and longitude and distance_range):
            return query_set.none()

        if not skip_box_approximation:
            lat, lon, dis = self._format_params(latitude, longitude, distance_range)

            # approximate the area where to search for places
            rough_distance = units.degrees(arcminutes=units.nautical(kilometers=dis)) * 2
            query_set = query_set.filter(
                latitude__range=(
                    lat - rough_distance,
                    lat + rough_distance
                ),
                longitude__range=(
                    lon - rough_distance,
                    lon + rough_distance
                )
            )
        return query_set
示例#3
0
    def near(self,
             latitude=None,
             longitude=None,
             distance_range=None,
             skip_box_approximation=False):
        """
        @param float latitude
        @param float longitude
        @param float distance_range in kilometers | Distance instance
        """
        query_set = super(BaseLocationManager, self).get_query_set()

        if not (latitude and longitude and distance_range):
            return query_set.none()

        if not skip_box_approximation:
            lat, lon, dis = self._format_params(latitude, longitude,
                                                distance_range)

            # approximate the area where to search for places
            rough_distance = units.degrees(arcminutes=units.nautical(
                kilometers=dis)) * 2
            query_set = query_set.filter(
                latitude__range=(lat - rough_distance, lat + rough_distance),
                longitude__range=(lon - rough_distance, lon + rough_distance))
        return query_set
示例#4
0
    def near(self, latitude=None, longitude=None, distance_range=10):
        queryset = super(LocationManager, self).get_queryset()
        if not (latitude and longitude and distance_range):
            return queryset.none()

        latitude = float(latitude)
        longitude = float(longitude)
        distance_range = float(distance_range)

        rough_distance = units.degrees(arcminutes=units.nautical(kilometers=distance_range)) * 2

        queryset = queryset.filter(
            latitude__range=(
                latitude - rough_distance,
                latitude + rough_distance
            ),
            longitude__range=(
                longitude - rough_distance,
                longitude + rough_distance
            )
        )

        locations = []
        for location in queryset:
            if location.latitude and location.longitude:
                exact_distance = distance.distance(
                    (latitude, longitude),
                    (location.latitude, location.longitude)
                ).kilometers

                if exact_distance <= distance_range:
                    locations.append(location)
        queryset = queryset.filter(id__in=[l.id for l in locations])
        return queryset
示例#5
0
def get_nearby_scouts(latitude, longitude, distance_range=50, queryset=None):
    from scouts.models import Scout
    if queryset is None:
        queryset = Scout.objects.all()
    rough_distance = units.degrees(arcminutes=units.nautical(kilometers=distance_range)) * 2
    latitude, longitude = float(latitude), float(longitude)
    queryset = queryset.filter(work_address__latitude__range=(latitude - rough_distance, latitude + rough_distance),
                               work_address__longitude__range=(longitude - rough_distance, longitude + rough_distance))
    return queryset
示例#6
0
 def nearby(latitude, longitude, distance_range=5):
     queryset = House.objects.filter(visible=True)
     rough_distance = units.degrees(arcminutes=units.nautical(
         kilometers=distance_range)) * 2
     latitude, longitude = float(latitude), float(longitude)
     queryset = queryset.filter(
         address__latitude__range=(latitude - rough_distance,
                                   latitude + rough_distance),
         address__longitude__range=(longitude - rough_distance,
                                    longitude + rough_distance))
     return queryset
示例#7
0
    def near(self, location, distance=None):
        """
        Returns a list of items in the :class:`QuerySet` which are
        within the given distance of the given location. Does NOT return
        a :class:`QuerySet`.

        Accepts either a :class:`Place` instance or a (lat, lon) tuple
        for location. Also accepts a Place instance with a
        ``nearby_distance`` attribute added (as returned from
        :func:`loci.utils.geolocate_request`); in this case, distance
        need not be explicitly passed.
        
        """
        
        # figure out if we received an object or tuple and get the location
        try:
            (latitude, longitude) = location.location
        except AttributeError:
            (latitude, longitude) = location

        # make sure we have a valid location
        if not (latitude and longitude):
            return []
        
        # get the passed distance or attached to Place
        if distance == None:
            try:
                distance = location.nearby_distance
            except AttributeError:
                raise ValueError('Distance must be attached or passed explicitly.')

        # prune down the set of places before checking precisely
        #deg_lat = Decimal(str(degrees(arcminutes=nautical(miles=distance))))
        deg_lat = degrees(arcminutes=nautical(miles=distance))
        lat_range = (latitude - deg_lat, latitude + deg_lat)
        long_range = (longitude - deg_lat * 2, longitude + deg_lat * 2)
        queryset = self.filter(
            latitude__range=lat_range,
            longitude__range=long_range
        )
        
        locations = []
        for location in queryset:
            if location.latitude and location.longitude:
                location.exact_distance = geopy.distance.distance(
                    (latitude, longitude),
                    (location.latitude, location.longitude)
                )
                if location.exact_distance.miles <= distance:
                    locations.append(location)
        return locations
示例#8
0
def refreshtrips(request):
    latitude = request.POST.get('latitude')
    longitude = request.POST.get('longitude')

    request.session['latitude'] = latitude
    request.session['longitude'] = longitude

    lat = float(latitude)
    long = float(longitude)
    distance_range = float(ALLOWABLE_DIST)

    rough_distance = units.degrees(arcminutes=units.nautical(
        kilometers=distance_range)) * 2

    all_trips = Trip.objects.filter(
        date__date=timezone.now() + timedelta(hours=9),
        status='ACT',
        meetup_pt__latitude__range=(lat - rough_distance,
                                    lat + rough_distance),
        meetup_pt__longitude__range=(
            long - rough_distance, long + rough_distance)).annotate(
                avail_passengers=MAX_PASSENGERS - Count('passenger')).filter(
                    avail_passengers__lte=4,
                    avail_passengers__gt=0).order_by('avail_passengers')

    nearby = []
    for trip in all_trips:
        if trip.meetup_pt.latitude and trip.meetup_pt.longitude:
            exact_distance = distance.distance(
                (lat, long),
                (trip.meetup_pt.latitude, trip.meetup_pt.longitude)).kilometers

            if exact_distance <= distance_range:
                trip.distance = exact_distance
                nearby.append(trip)

    sorted(nearby, key=lambda m: m.distance)

    trip_cards = loader.render_to_string('discover/tripcards.html',
                                         {'trips': nearby})
    output_data = {'trip_cards': trip_cards}
    return JsonResponse(output_data)
示例#9
0
 def near_by_lat_lng(self, latitude, longitude, proximity):
     """
     Find service provider within latitude and longitude with distance
     :param latitude: latitude
     :param longitude: longitude
     :param proximity: distance
     :return: service-provider queryset
     """
     rough_distance = units.degrees(arcminutes=units.nautical(
         kilometers=proximity)) * 2
     queryset = self.get_queryset() \
         .exclude(latitude=None) \
         .exclude(longitude=None) \
         .filter(
         latitude__range=(latitude - rough_distance, latitude + rough_distance),
         longitude__range=(longitude - rough_distance, longitude + rough_distance)
     )
     return queryset.filter(id__in=[
         obj.id for obj in queryset if proximity >= distance.distance((
             latitude, longitude), (obj.latitude, obj.longitude)).kilometers
     ])
示例#10
0
def organize(request):
    if request.POST.get('latitude') is not None and request.POST.get(
            'longitude') is not None:
        latitude = request.POST.get('latitude')
        longitude = request.POST.get('longitude')

        request.session['latitude'] = latitude
        request.session['longitude'] = longitude

        lat = float(latitude)
        long = float(longitude)
        distance_range = float(ALLOWABLE_DIST)

        rough_distance = units.degrees(arcminutes=units.nautical(
            kilometers=distance_range)) * 2

        all_meetup_pts = Meetup.objects.filter(
            latitude__range=(lat - rough_distance, lat + rough_distance),
            longitude__range=(long - rough_distance, long + rough_distance))

        nearby = []
        for meetup_pt in all_meetup_pts:
            if meetup_pt.latitude and meetup_pt.longitude:
                exact_distance = distance.distance(
                    (lat, long),
                    (meetup_pt.latitude, meetup_pt.longitude)).kilometers

                if exact_distance <= distance_range:
                    meetup_pt.distance = exact_distance
                    nearby.append(meetup_pt)

        sorted(nearby, key=lambda m: m.distance)

        context = {'nearby_meetups': nearby}
        return render(request, 'discover/organize.html', context)
    else:
        return HttpResponseRedirect(reverse('discover:index'))
示例#11
0
def distance_to_degrees(d):
    #Just an approximation, but speeds up clustering by a huge amount and doesnt introduce much error
    #over small distances
    return degrees(arcminutes=nautical(meters=d))
示例#12
0
 def nautical(self):
     return units.nautical(kilometers=self.kilometers)
示例#13
0
 def nautical(self):
     return units.nautical(kilometers=self.kilometers)
示例#14
0
 def nautical(self):  # pylint: disable=C0111
     return units.nautical(kilometers=self.kilometers)
示例#15
0
def get_bounding_box(latitude, longitude, distancekm):
    rough_distance = units.degrees(arcminutes=units.nautical(
        kilometers=distancekm)) * 2
    return latitude - rough_distance, latitude + rough_distance, longitude - rough_distance, longitude + rough_distance
示例#16
0
def get_offset(distance_range=10):
    rough_distance = units.degrees(arcminutes=units.nautical(kilometers=distance_range)) * 2
    return rough_distance
示例#17
0
def createTrip(request):
    organizer = request.POST.get('organizer')
    selected_meetup = request.POST.get('choice')
    organizer = organizer.strip(' ')

    latitude = request.session['latitude']
    longitude = request.session['longitude']

    lat = float(latitude)
    long = float(longitude)

    if len(organizer) == 0:
        distance_range = float(ALLOWABLE_DIST)

        rough_distance = units.degrees(arcminutes=units.nautical(
            kilometers=distance_range)) * 2

        all_meetup_pts = Meetup.objects.filter(
            latitude__range=(lat - rough_distance, lat + rough_distance),
            longitude__range=(long - rough_distance, long + rough_distance))

        nearby = []
        for meetup_pt in all_meetup_pts:
            if meetup_pt.latitude and meetup_pt.longitude:
                exact_distance = distance.distance(
                    (lat, long),
                    (meetup_pt.latitude, meetup_pt.longitude)).kilometers

                if exact_distance <= distance_range:
                    meetup_pt.distance = exact_distance
                    nearby.append(meetup_pt)

        sorted(nearby, key=lambda m: m.distance)

        context = {
            'nearby_meetups':
            nearby,
            'error_message':
            "You did not enter a name. Please type your name or nickname."
        }

        return render(request, 'discover/organize.html', context)
    else:
        new_meetup = get_object_or_404(Meetup, pk=selected_meetup)

        new_trip = Trip(meetup_pt=new_meetup,
                        organizer=organizer,
                        date=timezone.now() + timedelta(hours=9))
        new_trip.save()

        ip = request.session['host_ip']
        new_passenger = Passenger(trip=new_trip,
                                  name=organizer,
                                  ip=ip,
                                  latitude=lat,
                                  longitude=long)
        new_passenger.save()

        request.session['joined_trip'] = new_trip.id
        request.session['passenger'] = organizer
        request.session['passengerID'] = new_passenger.id

        return HttpResponseRedirect(
            reverse('discover:detail', args=(new_trip.id, )))
示例#18
0
文件: format.py 项目: nickzam/geopy
    arcseconds = units.arcseconds(arcminutes=arcminutes - int(arcminutes))
    format_dict = dict(
        symbols,
        degrees=degrees,
        minutes=abs(arcminutes),
        seconds=abs(arcseconds)
    )
    return fmt % format_dict

DISTANCE_FORMAT = "%(magnitude)s%(unit)s"
DISTANCE_UNITS = {
    'km': lambda d: d,
    'm': lambda d: units.meters(kilometers=d),
    'mi': lambda d: units.miles(kilometers=d),
    'ft': lambda d: units.feet(kilometers=d),
    'nm': lambda d: units.nautical(kilometers=d),
    'nmi': lambda d: units.nautical(kilometers=d)
}
def format_distance(kilometers, fmt=DISTANCE_FORMAT, unit='km'):
    """
    TODO docs.
    """
    magnitude = DISTANCE_UNITS[unit](kilometers)
    return fmt % {'magnitude': magnitude, 'unit': unit}

_DIRECTIONS = [
    ('north', 'N'),
    ('north by east', 'NbE'),
    ('north-northeast', 'NNE'),
    ('northeast by north', 'NEbN'),
    ('northeast', 'NE'),
示例#19
0
 def nautical(self): # pylint: disable=C0111
     return units.nautical(kilometers=self.kilometers)
示例#20
0
def search(request):
    context = RequestContext(request)

    if request.method == 'POST':
        search_text = request.POST['search_text']
        search_type = request.POST['type']
        granted=Permission.objects.filter(permitted__exact=request.user)
        granted=[i.owner for i in granted]

        #Keyword Search Option
        if search_type == 'all':

            q1 = Bulletin.objects.filter(
    Q(title__icontains=search_text) |
    Q(text_description__icontains=search_text))


            query = q1.order_by('date_created', 'title')

        # Title Search Option
        if search_type == 'title':
            # if text is contained within title
            q1 = Bulletin.objects.filter(title__icontains=search_text)
            # order by publication date, then headline
            query =q1.order_by('date_created', 'title')

        #Author Search Option
        if search_type == 'author':
            # gets author id based on search of username
            author = User.objects.get(username__exact=search_text)

            id = author.id
            # query db to find all bulletins with given id
            q1 = Bulletin.objects.filter(author_id__exact=id)
            # order by publication date, then headline
            query =q1.order_by('date_created', 'title')

        if search_type == 'date':
            # if text is contained within title
            q1 = Bulletin.objects.filter(date_created=search_text)
            # order by publication date, then headline
            query =q1.order_by('date_created', 'title')

        if search_type =='location':
            lat,long=location_lookup(search_text)
            rough_distance = units.degrees(arcminutes=units.nautical(miles=50))
            q1=Bulletin.objects.filter(Q(lat__range=(lat-rough_distance,lat+rough_distance))|Q(long__range=(long-rough_distance,long+rough_distance)))
            query=q1.order_by('date_created','title')


        bulletins=[]
        for b in query:
           # if b.author in granted:
                bulletins.append(b)
       # # print string
       #  print "bulletins"
       #  print bulletins
        return render_to_response('search.html', {'bulletins':bulletins}, context)

    else:
        #The request is not a POST so it's probably a GET request
        return render_to_response('search.html', {}, context)