From 863c49575f3befea4b7b0834f6189cbf58f4edb5 Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Sun, 28 Aug 2011 09:55:00 +0200 Subject: [PATCH] Get .similar_features() working right. --- gazetteer/places/models.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index 23a2731..ae0b47d 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -73,22 +73,23 @@ class Feature(models.Model): cursor = connection.cursor() name = unicode(self).replace("'", "''") # escape ' cursor.execute(""" - SELECT *, %f * similarity / distance AS score FROM ( - SELECT url, preferred_name, feature_type, + SELECT *, %f * similarity / (distance + 1.0) AS score FROM ( + SELECT id, url, preferred_name, feature_type_id, admin1, admin2, is_primary, similarity(preferred_name, '%s') AS similarity, - st_distance_sphere(geometry, '%s') AS distance + st_distance_sphere(geometry, 'SRID=4326;%s') AS distance FROM places_feature WHERE geometry && st_buffer('%s', %f) - AND preferred_name % '%s' + AND preferred_name %%%% '%s' + AND id <> %d ) AS whatever - WHERE %f * similarity / distance >= 1.0 - ORDER BY similarity / distance DESC""" + WHERE %f * similarity / (distance + 1.0) >= 1.0 + ORDER BY similarity / (distance + 1.0) DESC""" % (scale_factor, name, self.geometry.wkt, self.geometry.wkt, - max_distance*0.00001, name, scale_factor) + max_distance*0.00001, name, self.id, scale_factor) ) result_list = [] - fields = ('url', 'preferred_name', 'feature_type', 'admin1', 'admin2', 'is_primary') + fields = ('id', 'url', 'preferred_name', 'feature_type_id', 'admin1', 'admin2', 'is_primary') for row in cursor.fetchall(): vals = dict(zip(fields, row[:len(fields)])) p = type(self)(**vals)