Add GeoSearchManager to use both spatial and trigram similarity queries

This commit is contained in:
Schuyler Erle 2011-08-27 17:52:00 -07:00
parent 3a5253aaab
commit 08e5165bba

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import connection
from django.contrib.gis.db import models from django.contrib.gis.db import models, manager
# Create your models here. # Create your models here.
@ -10,6 +10,16 @@ class AuthorityRecord(models.Model):
def __unicode__(self): def __unicode__(self):
return self.preferred_name 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): class Feature(models.Model):
authority_record = models.ForeignKey(AuthorityRecord, null=True, blank=True) 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) time_frame = models.ForeignKey("TimeFrame", null=True, blank=True)
relationships = models.ManyToManyField("Feature", through='Relationship', blank=True) relationships = models.ManyToManyField("Feature", through='Relationship', blank=True)
objects = models.GeoManager() objects = models.GeoManager()
search = models.GeoSearchManager()
class Meta: class Meta:
ordering = ['preferred_name'] ordering = ['preferred_name']