]> git.parisson.com Git - telemeta.git/commitdiff
searching by a location now takes into account location historicity ; do not list...
authorolivier <>
Wed, 17 Feb 2010 15:20:36 +0000 (15:20 +0000)
committerolivier <>
Wed, 17 Feb 2010 15:20:36 +0000 (15:20 +0000)
telemeta/management/commands/telemeta-geocode.py
telemeta/models/location.py
telemeta/models/query.py

index 7d7f0603f843f971b8119897691acc3a637e5932..2205f9cea410f9b2a393a36eca9b0bd32bd3c75d 100644 (file)
@@ -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()                
 
index dcf89fa73af5b9a60b96704d65486af7f93acd70..4c73f39752ee45dc0e4988ca59ebcc3efa972d45 100644 (file)
@@ -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():
index b0f8433910d43dacb98bf911b30215b4a71fd1ec..f99300cacc52bc8313d8710b794118ad6abf3b04 100644 (file)
@@ -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):