Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: m2m query based on spatial point returns empty dict - but should contain results

Hi Stackoverflow people,

I am confused with m2m queries in Django. I have a model RadioStations which lists radio stations around a continent (simply name and the available country) and has the following declaration:

class Station(models.Model):
    name = models.CharField(_('Station Name'), max_length=255
    reference = models.URLField(_('Link'), blank=True, verify_exists=True)  
    country = models.ManyToManyField(WorldBorder)

The class WorldBorder follows the GeoDjango example here.

Now I would like to search for all stations in the US. If I use:

s = Station.objects.filter(country__name__contains = "United States")

I get all stations in the US. However, if I now search with a user location, e.g.

pnt = fromstr('POINT(-96.876369 29.905320)', srid=4326)
s = Station.objects.filter(country__mpoly__contains = pnt)

the result of the query is empty (even so the point is located in the U.S. Is that related to the way of doing a m2m query? Why would the results of the query being empty? Is there a different way of addressing the m2m relationship?

Thank you for your suggestions!

like image 723
neurix Avatar asked Dec 06 '25 03:12

neurix


1 Answers

I was not able to successfully make any geospatial queries using fromstr when I tried geodjano. To solve my issues I used Point.

from django.contrib.gis.geos import Point

pnt = Point(-96.876369, 29.905320)

Perhaps you could trying using hte point class?

like image 102
dm03514 Avatar answered Dec 09 '25 15:12

dm03514



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!