Re: geodjango

3 views
Skip to first unread message

Jeremy Dunck

unread,
Feb 27, 2007, 11:22:01 AM2/27/07
to django-d...@googlegroups.com, travis...@gmail.com
On 2/26/07, Travis Pinney <travis...@gmail.com> wrote:
> Hi Jeremy,
>
> I have geometric queries working now.
>
> Right now I have it set up as
>
> bbox = "POLYGON((-95.36819458007812
> 30.2184318620219,-95.36819458007812
> 30.084840018486364,-95.55702209472656
> 30.084840018486364,-95.55702209472656
> 30.2184318620219,-95.36819458007812 30.2184318620219))"
>
> schs = School.objects.filter(geom__overlaps=bbox)

Is that done by adding lookup types to django.db.models.QUERY_TERMS ?
If so, we need to avoid that in order to package as a contrib app.

> I am not sure if this is the best way to go about it. You probably
> want the string of bbox to be some type of geos object instead of a
> string, where its __str__ is a WKT.

I didn't realize until just now that postgis could handle the WKT
without the helping GeomFromText(wkt) wrapper. Nice. Perhaps that
means we don't need GeoTypes after all. I'll test that.

> I saw from the wiki that you had geo_near, geo_with, which I guess you
> create a geometry class which inherits from the model class . If you
> have this, where will the geometry be stored for that object?

Actually, no. objects.geo_near and .geo_within, etc, are methods of a
custom manager and queryset, not model. I mistakenly left out the
column from the methods. :-/

http://www.djangoproject.com/documentation/model_api/#managers

In a model, someone might do this:

from django.db import models
from django.contrib.gis import db as gis_db

class School(meta.Model):
name = ...
objects = gis_db.Manager()

Assume a location column has already been added to the DB, e.g.
select addgeometrycolumn('','schools_school','location',-1, 'POINT', 2);

Then this would work:
from django.contrib.gis.geometry import Point
x = Point(-95.36819458007812, 30.2184318620219)
School.objects.geo_near('location', point=x, radius=1.0)

This could construct a LinearRing approximating the circle around x
and issue something like:

poly = "POLYGON (...points from curve approximation...)"
_where.append("""
where schools_school.location && %s and
within(schools_school.location, %s)
""")
_params.append(poly, poly)


> I would
> like to go over some of the pros and cons for doing it either way.

Sure. IRC?

> I am working on the GeometryField right now.

If you think you can do that in a reasonable time, OK, but I hadn't
planned on supporting it right away. In-app GIS modification wasn't
something I had an immediate need for, so I was hoping to get the
query functionality done sooner. It was my impression that doing a
custom Field would be fairly involved.

Reply all
Reply to author
Forward
0 new messages