-
Notifications
You must be signed in to change notification settings - Fork 200
/
filters.py
185 lines (143 loc) · 6.39 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from math import cos, pi
from django.db.models import Q
from django.core.exceptions import ImproperlyConfigured
from django.contrib.gis.db import models
from django.contrib.gis.geos import Polygon, Point
from django.contrib.gis import forms
from rest_framework.filters import BaseFilterBackend
from rest_framework.exceptions import ParseError
from .tilenames import tile_edges
try:
import django_filters
except ImportError: # pragma: no cover
raise ImproperlyConfigured(
'restframework-gis filters depend on package "django-filter" '
'which is missing. Install with "pip install django-filter".'
)
try: # pragma: no cover
# django >= 1.8
from django.contrib.gis.db.models.lookups import gis_lookups
except ImportError: # pragma: no cover
# django <= 1.7
gis_lookups = models.sql.query.ALL_TERMS
__all__ = [
'InBBoxFilter',
'InBBOXFilter',
'GeometryFilter',
'GeoFilterSet',
'TMSTileFilter',
'DistanceToPointFilter'
]
class InBBoxFilter(BaseFilterBackend):
bbox_param = 'in_bbox' # The URL query parameter which contains the bbox.
def get_filter_bbox(self, request):
bbox_string = request.query_params.get(self.bbox_param, None)
if not bbox_string:
return None
try:
p1x, p1y, p2x, p2y = (float(n) for n in bbox_string.split(','))
except ValueError:
raise ParseError('Invalid bbox string supplied for parameter {0}'.format(self.bbox_param))
x = Polygon.from_bbox((p1x, p1y, p2x, p2y))
return x
def filter_queryset(self, request, queryset, view):
filter_field = getattr(view, 'bbox_filter_field', None)
include_overlapping = getattr(view, 'bbox_filter_include_overlapping', False)
if include_overlapping:
geoDjango_filter = 'bboverlaps'
else:
geoDjango_filter = 'contained'
if not filter_field:
return queryset
bbox = self.get_filter_bbox(request)
if not bbox:
return queryset
return queryset.filter(Q(**{'%s__%s' % (filter_field, geoDjango_filter): bbox}))
# backward compatibility
InBBOXFilter = InBBoxFilter
class GeometryFilter(django_filters.Filter):
field_class = forms.GeometryField
def __init__(self, *args, **kwargs):
kwargs.setdefault('widget', forms.BaseGeometryWidget)
super(GeometryFilter, self).__init__(*args, **kwargs)
class GeoFilterSet(django_filters.FilterSet):
GEOFILTER_FOR_DBFIELD_DEFAULTS = {
models.GeometryField: {
'filter_class': GeometryFilter
},
}
def __new__(cls, *args, **kwargs):
try:
cls._meta.filter_overrides.update(cls.GEOFILTER_FOR_DBFIELD_DEFAULTS)
# maintain compatibility for django-filter < 0.15
except AttributeError: # pragma: nocover
cls.filter_overrides.update(cls.GEOFILTER_FOR_DBFIELD_DEFAULTS)
cls.LOOKUP_TYPES = sorted(gis_lookups)
return super(GeoFilterSet, cls).__new__(cls)
class TMSTileFilter(InBBoxFilter):
tile_param = 'tile' # The URL query paramater which contains the tile address
def get_filter_bbox(self, request):
tile_string = request.query_params.get(self.tile_param, None)
if not tile_string:
return None
try:
z, x, y = (int(n) for n in tile_string.split('/'))
except ValueError:
raise ParseError('Invalid tile string supplied for parameter {0}'.format(self.tile_param))
bbox = Polygon.from_bbox(tile_edges(x, y, z))
return bbox
class DistanceToPointFilter(BaseFilterBackend):
dist_param = 'dist'
point_param = 'point' # The URL query parameter which contains the
def get_filter_point(self, request):
point_string = request.query_params.get(self.point_param, None)
if not point_string:
return None
try:
(x, y) = (float(n) for n in point_string.split(','))
except ValueError:
raise ParseError('Invalid geometry string supplied for parameter {0}'.format(self.point_param))
p = Point(x, y)
return p
def dist_to_deg(self, distance, latitude):
"""
distance = distance in meters
latitude = latitude in degrees
at the equator, the distance of one degree is equal in latitude and longitude.
at higher latitudes, a degree longitude is shorter in length, proportional to cos(latitude)
http://en.wikipedia.org/wiki/Decimal_degrees
This function is part of a distance filter where the database 'distance' is in degrees.
There's no good single-valued answer to this problem.
The distance/ degree is quite constant N/S around the earth (latitude),
but varies over a huge range E/W (longitude).
Split the difference: I'm going to average the the degrees latitude and degrees longitude
corresponding to the given distance. At high latitudes, this will be too short N/S
and too long E/W. It splits the errors between the two axes.
Errors are < 25 percent for latitudes < 60 degrees N/S.
"""
# d * (180 / pi) / earthRadius ==> degrees longitude
# (degrees longitude) / cos(latitude) ==> degrees latitude
lat = latitude if latitude >= 0 else -1 * latitude
rad2deg = 180 / pi
earthRadius = 6378160.0
latitudeCorrection = 0.5 * (1 + cos(lat * pi / 180))
return (distance / (earthRadius * latitudeCorrection) * rad2deg)
def filter_queryset(self, request, queryset, view):
filter_field = getattr(view, 'distance_filter_field', None)
convert_distance_input = getattr(view, 'distance_filter_convert_meters', False)
geoDjango_filter = 'dwithin' # use dwithin for points
if not filter_field:
return queryset
point = self.get_filter_point(request)
if not point:
return queryset
# distance in meters
dist_string = request.query_params.get(self.dist_param, 1000)
try:
dist = float(dist_string)
except ValueError:
raise ParseError('Invalid distance string supplied for parameter {0}'.format(self.dist_param))
if (convert_distance_input):
# Warning: assumes that the point is (lon,lat)
dist = self.dist_to_deg(dist, point[1])
return queryset.filter(Q(**{'%s__%s' % (filter_field, geoDjango_filter): (point, dist)}))