geodjango: find all points from a database, bound by a polygon

778 views
Skip to first unread message

nbv4

unread,
May 28, 2009, 5:12:48 PM5/28/09
to Django users
I have this model:
------------------------
from django.contrib.gis.db import models

class Base(models.Model):
identifier = models.CharField(max_length=8,
primary_key=True)
local = models.CharField(max_length=8)
iata = models.CharField(max_length=8)
name = models.CharField(max_length=96)
location = models.PointField()

------------------------

Which is populated by 40,000 entries in my database where the
"location" attribute is a longitude, latitude point on a world map.

In a view I have a set of bounds that correspond to north, east south
and west limits, and I'm trying to figure out how to create a query
that will find all Bases within that bounded box. I can't find this
kind of thing anywhere in the docs. Before I switched to geodjango, I
could just do:

------------------
Base.objects.filter(lat__lte=NBounds).filter(lat__gte=SBounds).filter
(lng__lte=WBounds).filter(lng__gte=EBounds)
------------------

Whats the equivalent with PointFields?

Ariel Nunez

unread,
May 28, 2009, 5:52:44 PM5/28/09
to django...@googlegroups.com
On Thu, May 28, 2009 at 4:12 PM, nbv4 <cp36...@ohio.edu> wrote:

I have this model:
------------------------
from django.contrib.gis.db import models

class Base(models.Model):
       identifier      =       models.CharField(max_length=8,
primary_key=True)
       local           =       models.CharField(max_length=8)
       iata            =       models.CharField(max_length=8)
       name            =       models.CharField(max_length=96)
       location        =       models.PointField()

------------------------

This(or similar) may work:


>>>from django.contrib.gis.gdal.envelope import Envelope

>>>bbox=Envelope((-104.609252, 29.763374, -95.23506, 38.971823))

>>>Base.objects.filter(location__intersects=bbox)

Ariel.

nbv4

unread,
May 28, 2009, 6:26:30 PM5/28/09
to Django users
On May 28, 5:52 pm, Ariel Nunez <ingenieroar...@gmail.com> wrote:
> On Thu, May 28, 2009 at 4:12 PM, nbv4 <cp368...@ohio.edu> wrote:
>
> > I have this model:
> > ------------------------
> > from django.contrib.gis.db import models
>
> > class Base(models.Model):
> >        identifier      =       models.CharField(max_length=8,
> > primary_key=True)
> >        local           =       models.CharField(max_length=8)
> >        iata            =       models.CharField(max_length=8)
> >        name            =       models.CharField(max_length=96)
> >        location        =       models.PointField()
>
> > ------------------------
>
> *This(or similar) may work:*
>
> *
> *
>
> >>>from django.contrib.gis.gdal.envelope import Envelope
> >>>bbox=Envelope((-104.609252, 29.763374, -95.23506, 38.971823))
>
> *>>>Base.objects.filter(location__intersects=bbox)*
>
> Ariel.

doesn't seem to work :(

>>> W = 39.9; N = -82.9; E=40.4; S=-82.2
>>> bounds = Envelope((N, W, S, E, ))
>>> Base.objects.filter(location__intersects=bounds)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/var/lib/python-support/python2.6/django/db/models/
manager.py", line 102, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/var/lib/python-support/python2.6/django/db/models/query.py",
line 489, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/var/lib/python-support/python2.6/django/db/models/query.py",
line 507, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/var/lib/python-support/python2.6/django/db/models/sql/
query.py", line 1258, in add_q
can_reuse=used_aliases)
File "/var/lib/python-support/python2.6/django/db/models/sql/
query.py", line 1119, in add_filter
elif (value == '' and lookup_type == 'exact' and
File "/var/lib/python-support/python2.6/django/contrib/gis/gdal/
envelope.py", line 78, in __eq__
raise OGRException('Equivalence testing only works with other
Envelopes.')
OGRException: Equivalence testing only works with other Envelopes.

Ariel Nunez

unread,
May 28, 2009, 6:44:18 PM5/28/09
to django...@googlegroups.com
>>> from django.contrib.gis.gdal.envelope import Envelope 
>>> W = 39.9; N = -82.9; E=40.4; S=-82.2
>>> bounds = Envelope((N, W, S, E, ))
>>> Base.objects.filter(location__intersects=bounds.wkt)

Then try calingl the wkt methond on the Envelope object to get a proper string repr of the polygon.

Ariel.

nbv4

unread,
May 28, 2009, 7:43:39 PM5/28/09
to Django users
OMG wow that worked. Thanks a ton.

Ariel Nunez

unread,
May 28, 2009, 10:11:16 PM5/28/09
to django...@googlegroups.com
> Then try calingl the wkt methond on the Envelope object to get a proper
> string repr of the polygon.
>
 
What is even more amazing is how many typos are in that sentence. I should make my default font bigger so I notice them better.

Glad it works,

Ariel. 
Reply all
Reply to author
Forward
0 new messages