From 08e5165bba7fae2252046638d74e7059879928bd Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Sat, 27 Aug 2011 17:52:00 -0700 Subject: [PATCH] Add GeoSearchManager to use both spatial and trigram similarity queries --- gazetteer/places/models.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index 0a2dda3..dc1a268 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -1,5 +1,5 @@ -from django.db import models -from django.contrib.gis.db import models +from django.db import connection +from django.contrib.gis.db import models, manager # Create your models here. @@ -10,6 +10,16 @@ class AuthorityRecord(models.Model): def __unicode__(self): return self.preferred_name +class GeoSearchManager(manager.GeoManager): + def overlaps(self, bbox, text=None): + bbox = 'POLYGON(((%f %f, %f %f, %f %f, %f %f, %f %f)))' % + (bbox[0], bbox[1], bbox[2], bbox[1], bbox[2], bbox[3], + bbox[0], bbox[3], bbox[0], bbox[1]) + qset = super(GeoSearchManager, self).get_query_set().filter(geometry_bboverlaps=bbox) + if text: + text = text.replace("'", "''") # escape the ' + qset = qset.extra(where="preferred_name %% '%s'" % text) + return qset class Feature(models.Model): authority_record = models.ForeignKey(AuthorityRecord, null=True, blank=True) @@ -23,6 +33,7 @@ class Feature(models.Model): time_frame = models.ForeignKey("TimeFrame", null=True, blank=True) relationships = models.ManyToManyField("Feature", through='Relationship', blank=True) objects = models.GeoManager() + search = models.GeoSearchManager() class Meta: ordering = ['preferred_name']