Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: DistanceToPointFilter #125

Closed
yamila-moreno opened this issue Dec 22, 2016 · 11 comments
Closed

Question: DistanceToPointFilter #125

yamila-moreno opened this issue Dec 22, 2016 · 11 comments
Labels

Comments

@yamila-moreno
Copy link
Contributor

Hi!
I'm trying to use the DistanceToPointFilter, but I cannot get it working.

This is my code:

class RouteView(viewsets.ReadOnlyModelViewSet):
    queryset = Route.objects.all()
    serializer_class = RouteSerializer
    filter_backends = (InBBoxFilter, DistanceToPointFilter, DjangoFilterBackend)
    filter_class = RouteFilter
    distance_filter_field = 'start_point'
    bbox_filter_field = 'start_point'
    bbox_filter_include_overlapping = True

I've tried all the combinations with these params:

routes/?dist=4000&point=lon,lat
routes/?dist=4000&point=lat,lon
routes/?dist=4000&point=lon,lat&distance_filter_convert_meters=True

etc... Am I missing something? [The other filters are properly working]
Thanks for your help :D

@nemesifier
Copy link
Member

Should be right, provided lon and lat are two floats.

You may want to take a look at the DistanceToPointFilter code to understand how it works.

@yamila-moreno
Copy link
Contributor Author

Hi, lat and lon are floats, as expected. I checked the code before asking. I'll keep trying.

@nemesifier
Copy link
Member

nemesifier commented Dec 22, 2016

Check also the test:
https://github.com/djangonauts/django-rest-framework-gis/blob/master/tests/django_restframework_gis_tests/test_filters.py#L130

I tried to use this feature a while ago (more than 1 year) and I remember it worked, but since then I have not tried anymore. Since I didn't add the feature myself I can be sure the feature works correctly just as long as the test works too, assuming the test code doesn't contain an hidden flaw.

Let me know what you find.
Thanks.

@tobias47n9e
Copy link

@yamila-moreno This is just a wild guess: If you happen to run the a Spatialite database the filter won't work because Spatialite does not support dwithin.

Source: https://docs.djangoproject.com/en/1.10/ref/contrib/gis/db-api/#spatial-lookup-compatibility

@Gagaro
Copy link

Gagaro commented Feb 13, 2017

I have the same issue. I'm using postgis and I can't make this filter work properly.

This may be related to my DB. The filter done is :

queryset.filter(geometry__dwithin=('POINT(-104.590948 38.319914)', 1))

And it returns a point which is ~10 km away. I'm not sure how to debug further however.

EDIT: The generated SQL is :

SELECT "geodirectory_entry"."id", "geodirectory_entry"."owner_id", "geodirectory_entry"."geometry" FROM "geodirectory_entry" WHERE ("geodirectory_entry"."owner_id" = 1 AND ST_DWithin("geodirectory_entry"."geometry", ST_GeomFromEWKB('\001\001\000\000 \346\020\000\000\315\314\314\314\314,Z\300*\304#\361\362(C@'::bytea), 1.0))

@Gagaro
Copy link

Gagaro commented Feb 20, 2017

You actually need to set distance_filter_convert_meters on the view itself.

class MyViewSet(viewsets.ModelViewSet):
    filter_backends = (
        DistanceToPointFilter
    )
    distance_filter_convert_meters = True

@tobias47n9e
Copy link

@Gagaro I think by default the coordinate system is WGS84 (EPSG: 4326). So the request with '1' should return all points in a circle with roughly 111 km diameter.

@Gagaro
Copy link

Gagaro commented Feb 21, 2017

Yes that was my issue. So the distance_filter_convert_meters parameter should be enough right?

@tobias47n9e
Copy link

@Gagaro At least for me it works if I set distance_filter_convert_meters in the view class. I can look up a point by focusing on a point roughly 1° away and setting the dist to around 100 000 meters.

Testing to look up a point at 0 lon 0 lat: In degrees 0 lon 1 lat needs distance of at least 1°. That means that dwithin probably counts points at the very edge of the circle as inside of the polygon. When using meters the point is filtered at around 111400 meters.

Seems like everything is working as expected.

@Gagaro
Copy link

Gagaro commented Feb 22, 2017

All right, thanks the help 👍 , I'm fairly new to gis development.

@nemesifier
Copy link
Member

Hey people, I prefer this github issue tracker to be used as a bug-tracker. Therefore if any of you think there's a bug, please send a failing test.

For questions and discussions, we have two other instruments that are very useful:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants