Fixed a bug related to inaccurate calc of the visual center of the map

This commit is contained in:
dormantman 2019-12-02 20:50:47 +03:00
parent 3b05a552aa
commit ab2304a6fa

View File

@ -10,14 +10,27 @@ class CustomGeoSpatialFilteringFilterBackend(GeoSpatialFilteringFilterBackend):
"""Automatically adds centering and sorting within bounding box."""
@staticmethod
def calculate_center(a, b):
return (a[0] + b[0]) / 2, (a[1] + b[1]) / 2
def calculate_center(first, second):
if second[1] < 0 <= first[1]:
reverse_first, reverse_second = 180 - abs(first[1]), 180 - abs(second[1])
diff = (reverse_first + reverse_second) / 2
if reverse_first < reverse_second:
result_part = -180 + (180 + second[1] - diff)
else:
result_part = 180 - (180 - first[1] - diff)
else:
result_part = (first[1] + second[1]) / 2
return round((first[0] + second[0]) / 2, 2), round(result_part, 2)
def filter_queryset(self, request, queryset, view):
ret = super().filter_queryset(request, queryset, view)
bb = request.query_params.get('location__geo_bounding_box')
if bb:
center = self.calculate_center(*map(lambda p: list(map(lambda x: float(x),p.split(','))), bb.split('__')))
center = self.calculate_center(*map(lambda point: list(map(float, point.split(','))), bb.split('__')))
request.GET._mutable = True
request.query_params.update({
'ordering': f'location__{center[0]}__{center[1]}__km'