Merge branch 'master' of github.com:mhall119/GetTogether

This commit is contained in:
Michael Hall 2018-03-17 16:05:22 -04:00
commit 03e54bbc3f
3 changed files with 87 additions and 87 deletions

View file

@ -1,31 +1,32 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from events.models.locale import Country, SPR, City from events.models.locale import Country, SPR, City
# Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt # Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt
GEONAMEID=0 GEONAMEID = 0
NAME=1 NAME = 1
ASCIINAME=2 ASCIINAME = 2
ALTERNATENAMES=3 ALTERNATENAMES = 3
LATITUDE=4 LATITUDE = 4
LONGITUDE=5 LONGITUDE = 5
FEATURE_CLASS=6 FEATURE_CLASS = 6
FEATURE_CODE=7 FEATURE_CODE = 7
COUNTRY_CODE=8 COUNTRY_CODE = 8
COUNTRY_CODE_2=9 COUNTRY_CODE_2 = 9
ADMIN1=10 ADMIN1 = 10
ADMIN2=11 ADMIN2 = 11
ADMIN3=12 ADMIN3 = 12
ADMIN4=13 ADMIN4 = 13
POPULATION=14 POPULATION = 14
ELEVATION=15 ELEVATION = 15
DIGITAL_ELEVATION=16 DIGITAL_ELEVATION = 16
TIMEZONE=17 TIMEZONE = 17
MODIFICATION_DATE=18 MODIFICATION_DATE = 18
COUNTRY_CACHE = dict() COUNTRY_CACHE = dict()
SPR_CACHE = dict() SPR_CACHE = dict()
class Command(BaseCommand): class Command(BaseCommand):
help = 'Loads city data from GeoNames database file' help = 'Loads city data from GeoNames database file'
@ -38,22 +39,24 @@ class Command(BaseCommand):
for country in Country.objects.all(): for country in Country.objects.all():
COUNTRY_CACHE[country.code] = country COUNTRY_CACHE[country.code] = country
for spr in SPR.objects.all(): for spr in SPR.objects.all():
SPR_CACHE["%s.%s"%(spr.country.code, spr.code)] = spr SPR_CACHE["%s.%s" % (spr.country.code, spr.code)] = spr
cities_file = open(options['file'], 'r') with open(options['file'], 'r') as cities_file:
for city_line in cities_file.readlines(): for city_line in cities_file.readlines():
city = city_line.split("\t") city = city_line.split("\t")
if len(city) == 19: if len(city) == 19:
if city[FEATURE_CODE].startswith("PPL"): if city[FEATURE_CODE].startswith("PPL"):
country = COUNTRY_CACHE.get(city[COUNTRY_CODE]) country = COUNTRY_CACHE.get(city[COUNTRY_CODE])
spr = SPR_CACHE.get("%s.%s"%(city[COUNTRY_CODE], city[ADMIN1])) spr = SPR_CACHE.get("%s.%s" % (city[COUNTRY_CODE], city[ADMIN1]))
if country is not None and spr is not None: if country is not None and spr is not None:
try: try:
City.objects.update_or_create(name=city[NAME], spr=spr, defaults={'tz':city[TIMEZONE], 'longitude':city[LONGITUDE], 'latitude':city[LATITUDE]}) City.objects.update_or_create(
except: name=city[NAME],
print("Warning: Failed to load city %s for %s" % (city[NAME], spr)) spr=spr,
else: defaults={'tz': city[TIMEZONE], 'longitude': city[LONGITUDE], 'latitude': city[LATITUDE]})
print("Short line (%s): %s" % (len(city), city_line)) except Exception as e:
cities_file.close() print("Warning: Failed to load city %s for %s (%s)" % (city[NAME], spr, e))
else:
print("Short line (%s): %s" % (len(city), city_line))
else: else:
print("No File in options!") print("No File in options!")

View file

@ -1,27 +1,28 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from events.models.locale import Country from events.models.locale import Country
# Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt # Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt
ISO=0 ISO = 0
ISO3=1 ISO3 = 1
ISO_NUMERIC=2 ISO_NUMERIC = 2
FIPS=3 FIPS = 3
COUNTRY=4 COUNTRY = 4
CAPITAL=5 CAPITAL = 5
AREA=6 AREA = 6
POPULATION=7 POPULATION = 7
CONTINENT=8 CONTINENT = 8
TLD=9 TLD = 9
CURRENCYCODE=10 CURRENCYCODE = 10
CURRENCYNAME=11 CURRENCYNAME = 11
PHONE=12 PHONE = 12
POSTAL_CODE_FORMAT=13 POSTAL_CODE_FORMAT = 13
POSTAL_CODE_REGEX=14 POSTAL_CODE_REGEX = 14
LANGUAGES=15 LANGUAGES = 15
GEONAMEID=16 GEONAMEID = 16
NEIGHBOURS=17 NEIGHBOURS = 17
EQUIVALENTFIPSCODE=18 EQUIVALENTFIPSCODE = 18
class Command(BaseCommand): class Command(BaseCommand):
help = 'Loads country data from GeoNames database file' help = 'Loads country data from GeoNames database file'
@ -31,17 +32,15 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
if 'file' in options: if 'file' in options:
countries_file = open(options['file'], 'r') with open(options['file'], 'r') as countries_file:
for country_line in countries_file.readlines(): for country_line in countries_file.readlines():
if country_line.startswith("#"): if country_line.startswith("#"):
continue continue
country = country_line.split("\t") country = country_line.split("\t")
if len(country) == 19: if len(country) == 19:
#print("%s - %s" % (country[ISO], country[COUNTRY])) # print("%s - %s" % (country[ISO], country[COUNTRY]))
Country.objects.get_or_create(name=country[COUNTRY], code=country[ISO]) Country.objects.get_or_create(name=country[COUNTRY], code=country[ISO])
else: else:
print("Short line (%s): %s" % (len(country), country_line)) print("Short line (%s): %s" % (len(country), country_line))
countries_file.close()
else: else:
print("No File in options!") print("No File in options!")

View file

@ -3,10 +3,10 @@ from django.core.management.base import BaseCommand, CommandError
from events.models.locale import Country, SPR, City from events.models.locale import Country, SPR, City
# Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt # Fields from geoname table, from http://download.geonames.org/export/dump/readme.txt
COMBINED_CODE=0 COMBINED_CODE = 0
NAME=1 NAME = 1
ASCIINAME=2 ASCIINAME = 2
GEONAMEID=3 GEONAMEID = 3
COUNTRY_CACHE = dict() COUNTRY_CACHE = dict()
@ -22,20 +22,18 @@ class Command(BaseCommand):
# Preload country cache # Preload country cache
for country in Country.objects.all(): for country in Country.objects.all():
COUNTRY_CACHE[country.code] = country COUNTRY_CACHE[country.code] = country
spr_file = open(options['file'], 'r') with open(options['file'], 'r') as spr_file:
for spr_line in spr_file.readlines(): for spr_line in spr_file.readlines():
if spr_line.startswith("#"): if spr_line.startswith("#"):
continue continue
spr = spr_line.split("\t") spr = spr_line.split("\t")
if len(spr) ==4: if len(spr) ==4:
COUNTRY_CODE, SPR_CODE = spr[COMBINED_CODE].split(".") COUNTRY_CODE, SPR_CODE = spr[COMBINED_CODE].split(".")
country = COUNTRY_CACHE.get(COUNTRY_CODE) country = COUNTRY_CACHE.get(COUNTRY_CODE)
if country is not None: if country is not None:
#print("%s - %s, %s" % (SPR_CODE, spr[NAME], country.name)) #print("%s - %s, %s" % (SPR_CODE, spr[NAME], country.name))
SPR.objects.get_or_create(name=spr[NAME], code=SPR_CODE, country=country) SPR.objects.get_or_create(name=spr[NAME], code=SPR_CODE, country=country)
else: else:
print("Short line (%s): %s" % (len(spr), spr_line)) print("Short line (%s): %s" % (len(spr), spr_line))
spr_file.close()
else: else:
print("No File in options!") print("No File in options!")