Пример #1
0
def parse_csv(csvfile):
    resp_one = []
    country_dict = prepare_countries()
    with open(csvfile, 'r') as fh:
        reader = csv.reader(fh)
        for line in reader:
            local_name, country_code, lat, lng, is_city, data = line
            is_city = (is_city == 'True')
            if not is_city or not local_name:
                continue
            data = simplejson.loads(data)
            name = None
            if 'name:en' in data:
                name = data.get('name:en')
            elif 'name' in data:
                name = data.get('name')
            if not name:
                name = local_name
            if data.get('place') in ('hamlet', 'village'):
                continue
            orig_municipality = data.get('is_in:municipality') or data.get('is_in:city')
            name = force_str(name)
            local_name = force_str(local_name)
            if orig_municipality:
                if ',' in orig_municipality:
                    orig_municipality = orig_municipality.split(',')[0]

                if ';' in orig_municipality:
                    orig_municipality = orig_municipality.split(';')[0]
                orig_municipality = force_str(orig_municipality)
                if (orig_municipality != name and orig_municipality != local_name):
                    continue
            resp_one.append((name, country_code, lat, lng, data, len(data.keys())))

        resp_one.sort(key=lambda k: (-k[-1], k[0]))
        for name, country_code, lat, lng, data, popularity in resp_one:
            if not data:
                data = {}
            city = City(name=force_unicode(name), coordinates=[lat, lng], rating=popularity,
                        additional_data=data,
                        country_id=country_dict[country_code])
            city.save(commit=True)
Пример #2
0
 def nodes(self, nodes):
     for this_entry in nodes:
         osmid, tags, coords = this_entry
         place = tags.get('place')
         name = force_str(tags.pop('name')) if 'name' in tags else ''
         if place in self.town_levels:
             self.entries_main.append({'name': name, 'coords': coords, 'tags': tags})
         if place in self.subtown_levels:
             #if 'is_in' not in tags:
             self.entries_main.append({'name': name, 'coords': coords, 'tags': tags})
             #else:
             #    self.entries_sub.append({'name': name, 'coords': coords, 'tags': tags})
         elif place in self.suburb_levels:
             self.entries_sub.append({'name': name, 'coords': coords, 'tags': tags})
Пример #3
0
 def relations(self, relations):
     coords = None
     for this_entry in relations:
         osmid, tags, orig_ways = this_entry
         ways = filter(lambda o: o[1] == 'way' and o[2] == 'admin_centre', orig_ways)
         if not ways:
             ways = filter(lambda o: o[1] == 'way' and o[2] == 'outer', orig_ways)
         name = force_str(tags.pop('name')) if 'name' in tags else ''
         place = tags.get('place')
         type = None
         if place in self.town_levels:
             type = 'town'
         if place in self.subtown_levels:
             # if 'is_in' not in tags:
             type = 'town'
             #else:
             #    type = 'subtown'
         elif place in self.suburb_levels:
             type = 'subtown'
         if type:
             self._rel_map[osmid] = {
                 'name': name, 'coords': coords, 'tags': tags, 'type': type}
             for way in ways:
                 self._way_map[way[0]] = osmid