Changeset 582
- Timestamp:
- 02/17/10 16:20:36 (5 months ago)
- Location:
- trunk/telemeta
- Files:
-
- 3 modified
-
management/commands/telemeta-geocode.py (modified) (2 diffs)
-
models/location.py (modified) (1 diff)
-
models/query.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/telemeta/management/commands/telemeta-geocode.py
r580 r582 20 20 except IOError: 21 21 raise CommandError("Unable to open %s" % datafile) 22 23 locations = [l for l in Location.objects.all().current().filter(type=Location.COUNTRY)] 22 23 locations = {} 24 for l in Location.objects.all().current().filter(type=Location.COUNTRY): 25 locations[l] = [a.alias for a in l.aliases.all()] 24 26 25 27 i = 0 26 28 geocoded = 0 27 29 total = len(locations) 30 found_by_alias = {} 28 31 for line in datafile: 29 32 (geonameid, name, asciiname, alternatenames, latitude, longitude, feature_class, … … 44 47 geocoded += 1 45 48 found.append(l) 49 else: 50 for a in locations[l]: 51 if unaccent(a).lower() in names: 52 found_by_alias[l] = float(latitude), float(longitude) 53 break 54 46 55 47 56 for l in found: 48 locations. remove(l)57 locations.pop(l) 49 58 50 59 i += 1 51 60 52 61 if i % 200000 == 0: 53 print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i)62 print "Geocoded %d (%d by alias) out of %d countries (parsed %d geonames)" % (geocoded, len(found_by_alias), total, i) 54 63 55 64 if total == geocoded: 56 65 break 57 66 58 print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) 67 for l in locations: 68 if found_by_alias.has_key(l): 69 l.latitude, l.longitude = found_by_alias[l] 70 l.save() 71 geocoded += 1 72 73 print "Done. Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) 59 74 datafile.close() 60 75 -
trunk/telemeta/models/location.py
r552 r582 78 78 q &= Q(ancestor_relations__is_direct=True) 79 79 return Location.objects.filter(q) 80 81 def apparented(self): 82 return Location.objects.filter( 83 Q(pk=self.id) | 84 Q(ancestor_relations__ancestor_location=self) | 85 Q(current_location=self.id)).distinct() 80 86 81 87 def add_child(self, other): -
trunk/telemeta/models/query.py
r580 r582 84 84 def by_location(self, location): 85 85 "Find items by location" 86 return self.filter( Q(location=location) | Q(location__in=location.descendants()))86 return self.filter(location__in=location.apparented()) 87 87 88 88 @staticmethod … … 93 93 from telemeta.models import Location, LocationRelation 94 94 l = self.values('location') 95 c = self.values('location__current_location') 95 96 r = LocationRelation.objects.filter(location__in=l).values('ancestor_location') 96 return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r) )97 return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r) | Q(pk__in=c)) 97 98 98 99 def countries(self, group_by_continent=False): … … 102 103 location = Location.objects.get(pk=id) 103 104 for l in location.countries(): 104 if not l in countries: 105 countries.append(l) 105 c = l.current_location 106 if not c in countries: 107 countries.append(c) 106 108 107 109 if group_by_continent: … … 226 228 def by_location(self, location): 227 229 "Find collections by location" 228 return self.filter( Q(items__location=location) | Q(items__location__in=location.descendants())).distinct()230 return self.filter(items__location__in=location.apparented()).distinct() 229 231 230 232 def by_recording_year(self, from_year, to_year=None): … … 352 354 return map 353 355 354 def current(self, is_current=True): 355 if is_current: 356 where = ["locations.id = locations.current_location_id"] 357 else: 358 where = ["locations.id <> locations.current_location_id"] 359 360 return self.extra(where = where); 356 def current(self): 357 return self.filter(id__in=self.values_list('current_location_id', flat=True)).distinct() 361 358 362 359 class LocationManager(CoreManager):
