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()

        self.logger.info("Importing city data")
        for item in data:
            if not self.call_hook('city_pre', item): continue
            
            if item['featureCode'] not in city_types: continue

            city = City()
            try:
                city.id = int(item['geonameid'])
            except:
                continue
            city.name = item['name']
            city.kind = item['featureCode']
            city.name_std = item['asciiName']
            city.slug = slugify(city.name_std)
            city.location = Point(float(item['longitude']), float(item['latitude']))
            city.population = int(item['population'])
            city.timezone = item['timezone']
            try:
                city.elevation = int(item['elevation'])
            except:
                pass

            country_code = item['countryCode']
            try: 
                country = self.country_index[country_code]
                city.country = country
            except:
                self.logger.warning("{0}: {1}: Cannot find country: {2} -- skipping".format("CITY", city.name, country_code))
                continue

            region_code = item['admin1Code']
            try: 
                region = self.region_index[country_code + "." + region_code]
                city.region = region
            except:
                self.logger.warning("{0}: {1}: Cannot find region: {2} -- skipping".format(country_code, city.name, region_code))
                continue
            
            subregion_code = item['admin2Code']
            try: 
                subregion = self.region_index[country_code + "." + region_code + "." + subregion_code]
                city.subregion = subregion
            except:
                if subregion_code:
                    self.logger.warning("{0}: {1}: Cannot find subregion: {2} -- skipping".format(country_code, city.name, subregion_code))
                pass
            
            if not self.call_hook('city_post', city, item): continue
            city.save()
            self.logger.debug("Added city: {0}".format(city))
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()

        self.logger.info("Importing city data")
        for item in data:
            if not self.call_hook('city_pre', item): continue

            if item['featureCode'] not in city_types: continue

            city = City()
            try:
                city.id = int(item['geonameid'])
            except:
                continue
            city.name = item['name']
            city.kind = item['featureCode']
            city.name_std = item['asciiName']
            city.slug = slugify(city.name_std)
            city.location = Point(float(item['longitude']),
                                  float(item['latitude']))
            city.population = int(item['population'])
            city.timezone = item['timezone']
            try:
                city.elevation = int(item['elevation'])
            except:
                pass

            country_code = item['countryCode']
            try:
                country = self.country_index[country_code]
                city.country = country
            except:
                self.logger.warning(
                    "{0}: {1}: Cannot find country: {2} -- skipping".format(
                        "CITY", city.name, country_code))
                continue

            region_code = item['admin1Code']
            try:
                region = self.region_index[country_code + "." + region_code]
                city.region = region
            except:
                self.logger.warning(
                    "{0}: {1}: Cannot find region: {2} -- skipping".format(
                        country_code, city.name, region_code))
                continue

            subregion_code = item['admin2Code']
            try:
                subregion = self.region_index[country_code + "." +
                                              region_code + "." +
                                              subregion_code]
                city.subregion = subregion
            except:
                if subregion_code:
                    self.logger.warning(
                        "{0}: {1}: Cannot find subregion: {2} -- skipping".
                        format(country_code, city.name, subregion_code))
                pass

            if not self.call_hook('city_post', city, item): continue
            city.save()
            self.logger.debug("Added city: {0}".format(city))