def get_queryset(self): queryset = Location.objects.all() latval = self.request.query_params.get('lat', None) longval = self.request.query_params.get('long', None) radius = self.request.query_params.get('radius', 500) distance = self.request.query_params.get('distance', True) lat1 = self.request.query_params.get('lat1', None) lat2 = self.request.query_params.get('lat2', None) long1 = self.request.query_params.get('long1', None) long2 = self.request.query_params.get('long2', None) name = self.request.query_params.get('name', "") offernow = self.request.query_params.get('offernow', None) if radius is not None and latval is not None and longval is not None: radiusfilter = True else: radiusfilter = False if lat1 is not None and lat2 is not None and long1 is not None and long2 is not None: rangefilter = True else: rangefilter = False if (not radiusfilter) and (not rangefilter): distance = False if radiusfilter: radius = float(radius) latval = float(latval) longval = float(longval) latval = gc.NormalizeLat(latval) longval = gc.NormalizeLat(longval) dlong = gc.AngleAlongParallel(latval, longval, radius) dlat = gc.AngleAlongMeridian(latval, longval, radius) long1 = longval - dlong long2 = longval + dlong lat2 = latval + dlat lat1 = latval - dlat queryset = QueryFilterRegion(queryset, lat1, long1, lat2, long2) if not radiusfilter and rangefilter: lat1 = float(lat1) lat2 = float(lat2) long1 = float(long1) long2 = float(long2) latval, longval, radius = gc.RangeToCenterRadius( lat1, lat2, long1, long2) queryset = QueryFilterRegion(queryset, lat1, long1, lat2, long2) if name: queryset = queryset.filter(name__regex=r'(?i)^.*(%s).*$' % name) if offernow == 'yes' or offernow == 'no' or offernow == 'anytime': for obj in queryset: time = on.TimeAtLocation(obj) obj.offernow = on.LocationOfferNow(obj, time) if offernow == 'yes': queryset = filter(lambda x: x.offernow == True, queryset) elif offernow == 'no': queryset = filter(lambda x: x.offernow == False, queryset) elif offernow == 'anytime': queryset = filter( lambda x: x.offernow == False or x.offernow == True, queryset) dists = [] for obj in queryset: if distance: distance = gc.DistanceTwoPoints(latval, longval, obj.latitude, obj.longitude) dists.append(distance) obj.distance = distance else: obj.distance = None if distance: dists = np.sort(np.array(dists)) queryset = sorted(queryset, key=lambda x: x.distance) queryset = queryset[0:sum(dists <= radius)] return queryset
def get(self, request, format=None): lat1 = self.request.query_params.get('lat1', None) lat2 = self.request.query_params.get('lat2', None) long1 = self.request.query_params.get('long1', None) long2 = self.request.query_params.get('long2', None) latbins = int(self.request.query_params.get('latbins', 20)) longbins = int(self.request.query_params.get('longbins', 20)) offernow = self.request.query_params.get('offernow', None) queryset = Location.objects.all() if lat1 is not None and lat2 is not None and long1 is not None and long2 is not None: rangefilter = True else: rangefilter = False if rangefilter: lat1 = float(lat1) lat2 = float(lat2) long1 = float(long1) long2 = float(long2) queryset = QueryFilterRegion(queryset, lat1, long1, lat2, long2) else: lat1 = -90 lat2 = 90 long1 = -180 long2 = 180 queryset = QueryFilterRegion(queryset, lat1, long1, lat2, long2) if offernow == 'yes' or offernow == 'no' or offernow == 'anytime': for i, obj in enumerate(queryset): time = on.TimeAtLocation(obj) obj.offernow = on.LocationOfferNow(obj, time) if offernow == 'yes': queryset = filter(lambda x: x.offernow == True, queryset) elif offernow == 'no': queryset = filter(lambda x: x.offernow == False, queryset) elif offernow == 'anytime': queryset = filter( lambda x: x.offernow == False or x.offernow == True, queryset) longlist = np.array([obj.longitude for obj in queryset]) latlist = np.array([obj.latitude for obj in queryset]) latedges = np.linspace(lat1, lat2, latbins + 1) if long1 > long2: longlist[longlist < long2] = longlist[longlist < long2] + 360 long2 = long2 + 360 longedges = np.linspace(long1, long2, longbins + 1) counthistall = scipy.stats.binned_statistic_2d( latlist, longlist, latlist, statistic='count', bins=[latedges, longedges]) counthist = counthistall[0] counthistbinnumbers = counthistall[3] meanlathist = scipy.stats.binned_statistic_2d( latlist, longlist, latlist, statistic='mean', bins=[latedges, longedges])[0] meanlonghist = scipy.stats.binned_statistic_2d( latlist, longlist, longlist, statistic='mean', bins=[latedges, longedges])[0] distributionpoints = [] count = 0 totalcount = 0 for lati in range(latbins): for longi in range(longbins): if counthist[lati, longi] > 0: distributionpoint = { 'count': counthist[lati, longi], 'latitude': meanlathist[lati, longi], 'longitude': meanlonghist[lati, longi] } if counthist[lati, longi] == 1: binnum = (longi + 1) + (lati + 1) * (longbins + 2) ind = np.where(binnum == counthistbinnumbers)[0][0] distributionpoint['name'] = queryset[ind].name distributionpoint['offernow'] = queryset[ind].offernow distributionpoint["id"] = queryset[ind].id distributionpoints.append(distributionpoint) count += 1 totalcount += counthist[lati, longi] return Response({ 'count': count, 'totalcount': totalcount, 'results': distributionpoints })
def CalculateOfferNow(self, obj): time = on.TimeAtLocation(obj.location) return on.OfferNow(obj, time)