Query REST API latitude and longitude -


i want users query 2 slugs fields (latitude, longitude) , 2 slug fields compared find nearest distance within 1.5km radius , display api according nearest safehouses. example: when users add latitude, longitude in query,

         www.example.com/safeplace/?find=-37.8770,145.0442 

this show nearest safeplaces within 1.5km

here function

 def distance(lat1, long1, lat2, long2):     r = 6371 # earth radius in km     dlat = math.radians(lat2 - lat1) # convert degrees 2 radians     dlong = math.radians(long2 - long1)     lat1 = math.radians(lat1)     lat2 = math.radians(lat2)     = math.sin(dlat/2) * math.sin(dlat/2) + math.sin(dlong/2) *      math.sin(dlong/2) * math.cos(lat1) * math.cos(lat2)     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))     d = r * c     return d 

here model

class safeplace(models.model):     establishment = models.charfield(max_length=250)     address = models.charfield(max_length=250)     suburb = models.charfield(max_length=250)     postcode = models.integerfield()     state = models.charfield(max_length=250)     type = models.charfield(max_length=250)     latitude = models.decimalfield(decimal_places=6,max_digits=10)     longtitude =  models.decimalfield(decimal_places=6,max_digits=10) 

is there way run loop in database? working on django sqlite. on views.py, how can implement distance function user input in rest api url find nearest safeplace , display rest api?

what need run comparison loop in views.py. pretty difficult execute try explain step step.

assuming using distance(lat, lng, lat2, lng2) function , trying find distance within 2km example.

in views.py

import pandas pd class someapiview(listapiview):     serializer_class = safeplaceserializer ### creating definition sorts parameters lng , lat ###     def get_queryset(self):         queryset = safeplace.objects.all()         lat = float(self.query_params.get('lag', none)         lng = float(self.query_params.get('lng', none)         ### now, reading api using pandas ###         df = pd.read_json('yourapi') ## yourapi url ur api         obj = []         x in range(0, len(df)):             latx = float(df['latitude'][x])             lngx = float(df['longitude'][x])             ### calculating distance ###             km = distance(lat, lng, latx, lngx)             if km <= 2:                 obj.append(df['id'][x])         ### django auto generate primary key calls id ###         ### going call pk queryset ###         return safeplace.objects.filter(pk__in=obj) 

i used pandas work around, load time might slow if have lots of data. however, think job. geo django provides efficient system deal long , lat, not competent in geo django cannot tell. believe work around.

update :

you can query "www.yourapi.com/safeplace?lat=x&lng=y"

i believe know how set urls


Comments