Ejemplo n.º 1
0
    def add_country(self, feature, geom, centroid, bbox, iso, fields):
        # new country object
        country = Country()

        name = feature.get('NAME_ICRC')
        iso3 = feature.get('ISO3')

        region = feature.get('REGION_IFR')
        # get region from db
        region_id = Region.objects.get(name=self.region_enum[region])

        if ('INDEPENDEN' in fields):
            independent = feature.get('INDEPENDEN')
            if independent == 'TRUE':
                country.independent = True
            elif independent == 'FALSE':
                country.independent = False
            else:
                country.independent = None

        if ('NATIONAL_S' in fields):
            country.society_name = feature.get('NATIONAL_S')

        country.name = name
        country.record_type = 1
        country.iso = iso
        country.iso3 = iso3
        country.region = region_id
        country.geom = geom.wkt
        country.centroid = centroid
        country.bbox = bbox

        # save
        country.save()
Ejemplo n.º 2
0
        def insert_countries():
            print("Deleting countries...")
            Country.objects.all().delete()

            print("Inserting countries...")
            c = Country()
            c.name = "España"
            c.save()
Ejemplo n.º 3
0
        def insert_countries():
            print("Inserting countries...")
            with open('data/countries.json', 'r') as file:
                countries_str = file.read()
                countries_json = json.loads(countries_str)

            for country_json in countries_json:
                country = Country()
                country.pk = country_json.get('pk')
                country.name = country_json.get('name')
                country.save()
Ejemplo n.º 4
0
def updateCountries(request):
    """
    Updating the countries available to the system.
    """

    if not request.user.is_staff:
        return JsonResponse({"status":"error","messages":"permission denied"},status=status.HTTP_403_FORBIDDEN)

    class CountryDataUpload(pydantic.BaseModel):
        small: CountryFeatureCollection 
        large: CountryFeatureCollection
    try:
        uploaded = CountryDataUpload(**json.loads(request.body.decode()))
        getCountries = lambda fc: {c.properties.CNTRY_NAME for c in fc.features}
        assert len(uploaded.small.features) == len(uploaded.large.features)
        assert getCountries(uploaded.small) == getCountries(uploaded.large)
    except Exception as e:
        return JsonResponse({"status":"error","messages":str(e)},status=status.HTTP_400_BAD_REQUEST)

    saved = 0
    updated = 0

    for small,large in zip(uploaded.small.features,uploaded.large.features):
        try:
            c = Country.objects.get(gwno = small.properties.GWCODE)
        except Country.DoesNotExist:
            c = Country(
                    gwno = small.properties.GWCODE,
                    name = small.properties.CNTRY_NAME,
                    iso2c = small.properties.ISO1AL2,
                    shape = dict(large.geometry),
                    simpleshape = dict(small.geometry)
                )
            saved += 1
        else:
            c.iso2c = small.properties.ISO1AL2
            c.name = small.properties.CNTRY_NAME
            c.shape = dict(large.geometry)
            c.simpleshape = dict(small.geometry)
            updated += 1
        c.save()

    return JsonResponse({"status":"success","saved":saved,"updated":updated})
Ejemplo n.º 5
0
 def handle(self, *args, **options):
     c = Country()
     c.name = "España"
     c.save()