How to measure distance with tastypia and geodjango?

Using Tastypie and GeoDjango I am trying to return the results of buildings located within 1 mile of a point.

The TastyPie documentation states that distance search is not supported yet, but I find examples of people who work it, like this discussion and this discussion on StackOverflow, but there are no working code examples that can be applied.

The idea I'm trying to work with is to add a GET command to the end of the url, then the adjacent locations are returned, e.g .:

http://website.com/api/?format=json&building_point__distance_lte=[{"type": "Point", "coordinates": [153.09537, -27.52618]},{"type": "D", "m" : 1}]

      

But when I try to do this, all I get is this:

{"error": "Invalid resource lookup data provided (mismatched type)."}

      

I've been pestering the Tastypie doc for a few days now and just can't figure out how to implement this.

I will give more examples, but I know they will be terrible. All advice is appreciated, thanks!

+3


source to share


1 answer


Got this action, here's an example for posterity:

In api.py, create a resource that looks like this:

from django.contrib.gis.geos import *

class LocationResource(ModelResource):
    class Meta:
        queryset = Building.objects.all()
        resource_name = 'location'

    def apply_sorting(self, objects, options=None):
        if options and "longitude" in options and "latitude" in options:
            pnt = fromstr("POINT(" + options['latitude'] + " " + options['longitude'] + ")", srid=4326)
            return objects.filter(building_point__distance_lte=(pnt, 500))

        return super(LocationResource, self).apply_sorting(objects, options)

      

The "building" field is defined as a PointField in models.py.



Then in your resource url add the following, for example:

&latitude=-88.1905699999999939&longitude=40.0913469999999990

      

This will return all objects within 500 meters.

+2


source







All Articles