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,
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()
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():
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):
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 = []
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 = {}
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"
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):