Example #1
0
    def import_city(self):
        uptodate = self.download_once('city')
        if uptodate and not self.force:
            return
        data = self.get_data('city')
        self.build_country_index()
        self.build_region_index()
        cnt = 0
        print('Importing city data ...')

        for items in self.parse(data):
            cnt += 1
            type = items[7]
            if type not in conf['CITY_TYPES']:
                continue

            city = City()
            city.id = int(items[0])  # geoname_id
            city.name = items[1]  # Real name
            city.lat = float(items[4])  # latitude in decimal degrees (wgs84)
            city.lng = float(items[5])  # longitude in decimal degrees (wgs84)
            city.population = items[14]

            # Find country
            try:
                city.country = self.country_index[items[8]]
            except:
                print('Skip city "{0}", no related country found!'
                      .format(city.id))
                continue

            # Find region
            try:
                rc = '{0}.{1}'.format(items[8].upper(), items[10])
                city.region = self.region_index[rc]
            except:
                print('Skip city "{0}", no related region found!'
                      .format(city.id))
                continue

            city.save()
        print('{0} cities imported.'.format(cnt))
Example #2
0
    def import_city(self):
        uptodate = self.download_once('city')
        if uptodate and not self.force:
            return
        data = self.get_data('city')
        self.build_country_index()
        self.build_region_index()
        cnt = 0
        print('Importing city data ...')

        for items in self.parse(data):
            cnt += 1
            type = items[7]
            if type not in conf['CITY_TYPES']:
                continue

            city = City()
            city.id = int(items[0])  # geoname_id
            city.name = items[1]  # Real name
            city.lat = float(items[4])  # latitude in decimal degrees (wgs84)
            city.lng = float(items[5])  # longitude in decimal degrees (wgs84)
            city.population = items[14]

            # Find country
            try:
                city.country = self.country_index[items[8]]
            except:
                print('Skip city "{0}", no related country found!'.format(
                    city.id))
                continue

            # Find region
            try:
                rc = '{0}.{1}'.format(items[8].upper(), items[10])
                city.region = self.region_index[rc]
            except:
                print('Skip city "{0}", no related region found!'.format(
                    city.id))
                continue

            city.save()
        print('{0} cities imported.'.format(cnt))