def get_shops(lon, lat, km): current_point = geos.fromstr("POINT(%s %s)" % (lon, lat)) distance_from_point = {"km": km} shops = models.Shop.gis.filter( location__distance_lte=(current_point, measure.D(**distance_from_point))) shops = shops.distance(current_point).order_by("distance") return shops.distance(current_point)
def get_shops(longitude, latitude): current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': 10} shops = Shop.gis.filter( location__distance_lte=(current_point, measure.D(**distance_from_point))) shops = shops.distance(current_point).order_by('distance') return shops.distance(current_point)
def get_inmuebles(longitude, latitude): current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': 2} inmuebles = Inmueble.gis.filter( location__distance_lte=(current_point, measure.D(**distance_from_point))) inmuebles = inmuebles.distance(current_point).order_by('distance') return inmuebles.distance(current_point)
def get_queryset(self): latitude = self.request.query_params.get('latitude') longitude = self.request.query_params.get('longitude') current_point = fromstr('POINT(%s %s)' % (longitude, latitude)) vehicles = VehicleLocation.objects.filter( loc__distance_lte=(current_point, measure.D(km=10))).annotate( distance=Distance('loc', current_point)).order_by('distance') return vehicles
def get_events(request,longitude,latitude): current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': 10} events = Event.gis.filter(location__distance_lte=(current_point, measure.D(**distance_from_point))) events = events.distance(current_point).order_by('distance') ##return events.distance(current_point) max_time = Event.get_max_time() context = {"max_time":max_time, "events":events} return render(request, 'events.json', context, content_type='application/json')
def get_restaurants(longitude, latitude): current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude), srid=4326) distance_from_point = {'km': 100} restaurants = Restaurant.objects.filter(coordinates__distance_lte=( current_point, measure.D(**distance_from_point) )).annotate( distance=Distance('coordinates', current_point)).order_by('distance') return restaurants
def get_suppliers_nearby(farmer_location, supplier_queryset=None, radius=2.5): if supplier_queryset is None: supplier_queryset = Supplier.objects.all() if farmer_location is not None: distance_from_point = {'km': radius} supplier_queryset = supplier_queryset.filter( geom__distance_lte=(farmer_location, measure.D(**distance_from_point))) return supplier_queryset else: return []
def public_nearby_events(self, request): search_point = Point(float(request.GET.get('lat')), float(request.GET.get('lng'))) distance_from_point = {'km': 50} result = Event.gis.filter(is_public=True, geo_cords__distance_lte=(search_point, measure.D(**distance_from_point))).distance( search_point).order_by('distance') page = self.paginate_queryset(result) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(result, many=True) return Response(serializer.data)
def get_queryset(self): latitude = self.request.query_params.get('latitude') longitude = self.request.query_params.get('longitude') current_point = fromstr('POINT(%s %s)' % (longitude, latitude)) guide = GuideProfile.objects.filter( loc__distance_lte=(current_point, measure.D(km=10))).annotate( distance=Distance('loc', current_point)).order_by( 'distance')[0] message = "There is an emergency near you.Please contact {}.Location Details: https://www.google.com/maps/@{},{},15z".format( self.request.user.touristprofile.phone_number, latitude, longitude) to = guide.phone_number SENDSMS(to, message) return message
def get_Restaurants(longitude, latitude, radius): current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': radius} print(radius) # Restaurants = models.Restaurant.objects.filter( # location__distance_lte=(current_point, measure.D(**distance_from_point))) # geom = geos.fromstr('POINT(-73 40)') Restaurants = models.Restaurant.objects.filter( location__distance_lte=(current_point, measure.D(m=radius))).distinct() # print(models.Restaurant.objects.annotate(distance=Distance("location", current_point)).order_by("distance")[0:6]) print("ktnaaaaaaaa", Restaurants.count()) # Restaurants = Restaurants[0].location.distance(current_point).order_by('distance') # return Restaurants[0].distance(current_point) # return models.Restaurant.objects.annotate(distance=Distance("location", current_point)).order_by("distance") return Restaurants
def get_changes(request,longitude,latitude,time="1970-01-01T00:00+00:00"): max_time = Event.get_max_time() current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': 10} # first filter by distance events = Event.gis.filter(location__distance_lte=(current_point, measure.D(**distance_from_point))) # then filter by last_changed time events = events.filter(last_changed__gt=time).distinct() #order by distance events = events.distance(current_point).order_by('distance') context = {"max_time":max_time, "events":events} return render(request, 'events.json', context, content_type='application/json')
def index(request): form = AddressForm() peoples = [] if request.POST: form = AddressForm(request.POST) if form.is_valid(): address = form.cleaned_data['address'] try: lng, lat = geocoder(address) except Exception: pass else: location = geos.Point(lng, lat, srid=4326) peoples = People.objects.annotate(distance=Distance('location', location)).filter( location__distance_lte=(location, measure.D(km=50))).order_by('distance') return render(request, 'near/index.html', {'form': form, 'peoples': peoples})
def get_queryset(self, *args, **kwargs): query = Station.objects.all() if self.kwargs: if self.kwargs['current_status']: query = Station.objects.filter( category=self.kwargs['current_status']) if self.kwargs['longitude'] and self.kwargs['latitude']: longitude = self.kwargs['longitude'] latitude = self.kwargs['longitude'] current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) distance_from_point = {'km': 10} query = query.gis.filter( location__distance_lte=(current_point, measure.D(**distance_from_point))) query = query.distance(current_point).order_by('distance') return query
def get_near_organisations(current_point, distance_from_point=None, qs=None, order=True): """ get all centers with the distance from current_point where the distance is less than distance_from_point """ if current_point is None: return Organisation.objects.none() if qs is not None: organisations = qs else: organisations = Organisation.objects.all() if distance_from_point: organisations = organisations.filter(location__distance_lt=(current_point, measure.D(**distance_from_point))) organisations = organisations.annotate(distance=Distance('location', current_point)) if order: return organisations.order_by('distance') else: return organisations
def home(request, pk, unit, near_by): try: user = Account.objects.get(pk=pk) distance_from_point = {unit: near_by} print('Location : ', user.location) near_by_users = Account.gis.filter( location__distance_lte=(user.location, measure.D(**distance_from_point))) except Account.DoesNotExist: near_by_users = None print('NEAR LOCATION : ', near_by_users) context = { 'label': 'Yo!', 'user': user, 'near_by_users': near_by_users, } return render(request, 'accounts/html/index.html', context)
def __get_restaurants(request, longitude, latitude, categories, limit=20): ''' Returns objects at given point that satisfy set of categories, or all of them if categories is empty. input: str longitude str latitude list categories output: list of dicts ''' currentPoint = Point(float(longitude), float( latitude)) # geos.GEOSGeometry('POINT(%s %s)' % (longitude, latitude)) distance_m = {'km': 30} list_of_cats = [] for c in categories: list_of_cats.append(Cuisine.objects.get(name=c)) if list_of_cats: restaurants = Restaurant.gis.filter( approved=True, location__distance_lte=(currentPoint, measure.D(**distance_m)), cuisines__in=list_of_cats, is_closed=False).distance(currentPoint).order_by('distance') else: restaurants = Restaurant.gis.filter( approved=True, location__distance_lte=(currentPoint, measure.D(**distance_m)), is_closed=False).distance(currentPoint).order_by('distance') if hasattr(request.user, 'is_venue_moderator') and request.user.is_venue_moderator(): pass else: restaurants = restaurants.filter(approved=True) # seems that this thing doesn't actually order objects by distance # btw at this step there is no distance property in objects or rows in table # restaurants = restaurants.distance(currentPoint).order_by('distance') restaurants = restaurants[:limit] # String based JSON data = RestaurantSerializer('json', restaurants) # Actual JSON object to be edited data = json.loads(data) # if venue has multiple categories and some of them # are in list_of_cats than venue will appear in data that some times # so we will uniqify venues in data if len(list_of_cats) > 1: data = {v['pk']: v for v in data}.values() for restaurant in data: d = geopy_distance(currentPoint, restaurant['fields']['location']).kilometers restaurant['fields']['distance'] = round(d, 1) # Fancy splitting on POINT(lon, lat) lng = restaurant['fields']['location'].split()[1][1:] lat = restaurant['fields']['location'].split()[2][:-1] del restaurant['fields']['location'] restaurant['fields']['lng'] = lng restaurant['fields']['lat'] = lat # Replace cuisine ids with names cat_names = [] for cat_id in restaurant['fields']['cuisines']: cat = Cuisine.objects.get(id=cat_id) cat_names.append(cat.name) restaurant['fields']['cuisines'] = cat_names return data
def index(request): if request.user.is_authenticated() and not request.user.venueuser.location: return redirect('profile-form', pk=request.user.venueuser.pk) if 'lat' in request.GET and 'lon' in request.GET: return closest(request) form = forms.AddressForm() restaurants = [] latitude = "" longitude = "" address = "" name = "" if request.POST: page = request.POST.get('page') form = forms.AddressForm(request.POST) if form.is_valid(): address = form.cleaned_data['address'] name = form.cleaned_data['name'] if not address: restaurants = Restaurant.objects.filter(approved=True, is_suspended=False, name__icontains=name) try: longitude, latitude = restaurants[0].location except IndexError: pass else: geocoder = GoogleV3(api_key=GOOGLE_KEY) try: location = geocoder.geocode(address) except Exception as e: print e location = "" if location: latitude = location.latitude longitude = location.longitude currentPoint = geos.GEOSGeometry('POINT(%s %s)' % (longitude, latitude)) distance_m = {'km': 30} restaurants = Restaurant.gis.filter(approved=True, location__distance_lte=(currentPoint, measure.D(**distance_m)), name__icontains=name).distance(currentPoint).order_by( 'distance') else: page = request.GET.get('page') restaurants = Restaurant.objects.filter(approved=True, is_suspended=False) # if hasattr(request.user, 'is_venue_moderator') and request.user.is_venue_moderator(): # pass # else: # restaurants = Restaurant.objects.filter(approved=True, is_suspended=False) paginator = Paginator(restaurants, 20) page = request.GET.get("page") try: restaurants = paginator.page(page) except PageNotAnInteger: restaurants = paginator.page(1) except EmptyPage: restaurants = paginator.page(paginator.num_pages) context = {'all_restaurants': restaurants, 'form': form, 'longitude': longitude, 'latitude': latitude, 'address': address, 'name': name} return render(request, 'restaurants/restaurants.html', context)
def near(cls, latitude, longitude, sort='start', include_private=False): point = geos.Point(float(longitude), float(latitude)) return cls.upcoming(sort, include_private).filter( venue__location__distance_lte=(point, measure.D(mi=50)))