From: olivier <> Date: Wed, 17 Feb 2010 15:20:36 +0000 (+0000) Subject: searching by a location now takes into account location historicity ; do not list... X-Git-Tag: 1.1~513 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=db25980f2e177841ac93c320ecb01b736630f90f;p=telemeta.git searching by a location now takes into account location historicity ; do not list historical countries in the geo navigator anymore ; improve geocoder, geocoding only current locations (not historical) and searchin by location alias as a fallback --- diff --git a/telemeta/management/commands/telemeta-geocode.py b/telemeta/management/commands/telemeta-geocode.py index 7d7f0603..2205f9ce 100644 --- a/telemeta/management/commands/telemeta-geocode.py +++ b/telemeta/management/commands/telemeta-geocode.py @@ -19,12 +19,15 @@ class Command(BaseCommand): datafile = codecs.open(datafile, 'r', 'utf-8') except IOError: raise CommandError("Unable to open %s" % datafile) - - locations = [l for l in Location.objects.all().current().filter(type=Location.COUNTRY)] + + locations = {} + for l in Location.objects.all().current().filter(type=Location.COUNTRY): + locations[l] = [a.alias for a in l.aliases.all()] i = 0 geocoded = 0 total = len(locations) + found_by_alias = {} for line in datafile: (geonameid, name, asciiname, alternatenames, latitude, longitude, feature_class, feature_code, country_code, cc2, admin1_code, admin2_code, admin3_code, @@ -43,18 +46,30 @@ class Command(BaseCommand): l.save() geocoded += 1 found.append(l) + else: + for a in locations[l]: + if unaccent(a).lower() in names: + found_by_alias[l] = float(latitude), float(longitude) + break + for l in found: - locations.remove(l) + locations.pop(l) i += 1 if i % 200000 == 0: - print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) + print "Geocoded %d (%d by alias) out of %d countries (parsed %d geonames)" % (geocoded, len(found_by_alias), total, i) if total == geocoded: break - print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) + for l in locations: + if found_by_alias.has_key(l): + l.latitude, l.longitude = found_by_alias[l] + l.save() + geocoded += 1 + + print "Done. Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) datafile.close() diff --git a/telemeta/models/location.py b/telemeta/models/location.py index dcf89fa7..4c73f397 100644 --- a/telemeta/models/location.py +++ b/telemeta/models/location.py @@ -78,6 +78,12 @@ class Location(ModelCore): q &= Q(ancestor_relations__is_direct=True) return Location.objects.filter(q) + def apparented(self): + return Location.objects.filter( + Q(pk=self.id) | + Q(ancestor_relations__ancestor_location=self) | + Q(current_location=self.id)).distinct() + def add_child(self, other): LocationRelation.objects.create(location=other, ancestor_location=self, is_direct=True) for location in self.ancestors(): diff --git a/telemeta/models/query.py b/telemeta/models/query.py index b0f84339..f99300ca 100644 --- a/telemeta/models/query.py +++ b/telemeta/models/query.py @@ -83,7 +83,7 @@ class MediaItemQuerySet(CoreQuerySet): def by_location(self, location): "Find items by location" - return self.filter(Q(location=location) | Q(location__in=location.descendants())) + return self.filter(location__in=location.apparented()) @staticmethod def __name_cmp(obj1, obj2): @@ -92,8 +92,9 @@ class MediaItemQuerySet(CoreQuerySet): def locations(self): from telemeta.models import Location, LocationRelation l = self.values('location') + c = self.values('location__current_location') r = LocationRelation.objects.filter(location__in=l).values('ancestor_location') - return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r)) + return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r) | Q(pk__in=c)) def countries(self, group_by_continent=False): countries = [] @@ -101,8 +102,9 @@ class MediaItemQuerySet(CoreQuerySet): for id in self.filter(location__isnull=False).values_list('location', flat=True).distinct(): location = Location.objects.get(pk=id) for l in location.countries(): - if not l in countries: - countries.append(l) + c = l.current_location + if not c in countries: + countries.append(c) if group_by_continent: grouped = {} @@ -225,7 +227,7 @@ class MediaCollectionQuerySet(CoreQuerySet): def by_location(self, location): "Find collections by location" - return self.filter(Q(items__location=location) | Q(items__location__in=location.descendants())).distinct() + return self.filter(items__location__in=location.apparented()).distinct() def by_recording_year(self, from_year, to_year=None): "Find collections by recording year" @@ -351,13 +353,8 @@ class LocationQuerySet(CoreQuerySet): self.__class__.__flatname_map = map return map - def current(self, is_current=True): - if is_current: - where = ["locations.id = locations.current_location_id"] - else: - where = ["locations.id <> locations.current_location_id"] - - return self.extra(where = where); + def current(self): + return self.filter(id__in=self.values_list('current_location_id', flat=True)).distinct() class LocationManager(CoreManager):