Distance Spatial Queries with Tastypie
Not sure how to use distance_lte spatial filters with yummy pie. I can use the containing spatial filter, I cannot define the format of the distance_lte filter.
Here's what I've tried:
http://www.domain.com/myapp/api/v1/location/?format=json&coord__distance_lte={"type": "Point", "coordinates": [153.09537, -27.52618]},D(m=5)
What returns {"error": "Invalid resource lookup data provided (mismatched type)."}
source to share
From tastypie source code:
# If we are filtering on a GeometryApiField then we should try
# and convert this to a GEOSGeometry object. The conversion
# will fail if we don't have value JSON, so in that case we'll
# just return ``value`` as normal.
your D (m = 3) is not valid JSON. This is the code that converts the fields:
if isinstance(self.fields[field_name], GeometryApiField):
try:
value = GEOSGeometry(unquote(value))
except ValueError:
pass
return value
Since the following code should work internally:
Location.objects.filter(location__distance_lte=(fromstr('POINT(153.09537 -27.52618)', srid=4326), D(m=5)))
I could imagine this should look a bit like this:
[{"type": "Point", "coordinates": [153.09537, -27.52618]},{"type": "D", "m" : 5}]
I am not running this yet. Hope you have more luck!
EDIT: Since I couldn't get this to work, I implemented it myself using Django Tastypie's Advanced Filtering: How to Perform Complex Search with Q Objects Not a perfect solution, but it works.
source to share
This is because Tastypie mistakenly searches for valid filters using the querysets query.query_terms attributes
It will not contain "distance" and you will get an error as a result.
Also, TastyPie mostly doesn't work with these GIS searches (at least not without adding its own special sauce).
You can do remote work, for example, by overriding build_filters and adding "distance" to the allowed filter set:
def build_filters(self, filters=None):
'''
Add in some filters so spatial queries will work.
'''
self._meta.queryset.query.query_terms.update(set(['distance','distance_lte']))
return super(MyResource, self).build_filters(filters)
After which the documentation starts to get correct as to how you pass WKT and / or GeoJSON as receive parameters.
source to share