def process_regions():
    data = prepare_data(REGIONS)

    print("\n--- Seeding regions: ---")

    Polygon.objects.filter(type='region').delete()

    saved = 0

    for feature in data["features"]:
        json_region_name = feature["properties"]["name"]
        try:
            if json_region_name is None:
                continue
            region = Region.objects.get(name=json_region_name)
            polygon = Polygon(id="%s:%s" % ("region", region.id))
            polygon.type = "region"
            polygon.type_id = region.id
            polygon.polygon = json.dumps(feature["geometry"]["coordinates"])
            polygon.save()
            print(".", end="")
            saved += 1
        except (Region.DoesNotExist, Region.MultipleObjectsReturned):
            pass

    in_file = 4651

    print("""\nRegions in our database: %d,
          regions in file: %d, regions matched: %d"""
          % (len(Region.objects.all()), in_file, saved))
def process_cities():
    data = prepare_data(CITIES)

    print("\n--- Seeding cities: ---")

    Polygon.objects.filter(type='city').delete()

    for feature in data["features"]:
        json_city_name = feature["properties"]["GN_ASCII"]
        try:
            if json_city_name is None:
                continue
            city = City.objects.get(name=json_city_name)
            polygon = Polygon(id="%s:%s" % ("city", city.id))
            polygon.type = "city"
            polygon.type_id = city.id
            polygon.polygon = json.dumps(feature["geometry"]["coordinates"])
            polygon.save()
            print(".", end="")
        except City.DoesNotExist:
            pass

    print("\nFinish.")
def process_countries():
    data = prepare_data(COUNTRIES)

    print("\n--- Seeding countries: ---")

    Polygon.objects.filter(type='country').delete()

    for feature in data["features"]:
        json_country_code = feature["properties"]["ISO_A2"]
        try:
            country = Country.objects.get(pk=json_country_code.lower())
        except Country.DoesNotExist:
            print('Not found: ' + json_country_code, end='')

        polygon = Polygon(id="%s:%s" % ("country", country.id))
        polygon.type = "country"
        polygon.type_id = country.id
        polygon.polygon = json.dumps(feature["geometry"]["coordinates"])
        polygon.save()

        print(".", end="")

    print("\nFinish.")