Skip to content

Commit

Permalink
injecting countries (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfiejz authored Jun 23, 2023
1 parent e2e36a5 commit 338e06d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 38 deletions.
38 changes: 19 additions & 19 deletions cap_feed/country.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 343,
Expand Down Expand Up @@ -282,7 +282,7 @@
"iso": "AI",
"iso3": "AIA",
"society_url": "",
"region": null,
"region": 1,
"key_priorities": null,
"inform_score": null,
"id": 217,
Expand Down Expand Up @@ -426,7 +426,7 @@
"iso": "AW",
"iso3": "ABW",
"society_url": "",
"region": null,
"region": 1,
"key_priorities": null,
"inform_score": null,
"id": 231,
Expand Down Expand Up @@ -927,7 +927,7 @@
"iso": "BM",
"iso3": "BMU",
"society_url": "",
"region": null,
"region": 1,
"key_priorities": null,
"inform_score": null,
"id": 233,
Expand Down Expand Up @@ -992,7 +992,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 0,
"key_priorities": null,
"inform_score": null,
"id": 346,
Expand Down Expand Up @@ -3054,7 +3054,7 @@
"iso": "GI",
"iso3": "GIB",
"society_url": "",
"region": null,
"region": 3,
"key_priorities": null,
"inform_score": null,
"id": 241,
Expand Down Expand Up @@ -3421,7 +3421,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 0,
"key_priorities": null,
"inform_score": null,
"id": 351,
Expand Down Expand Up @@ -3472,7 +3472,7 @@
"iso": "VA",
"iso3": "VAT",
"society_url": "",
"region": null,
"region": 3,
"key_priorities": null,
"inform_score": null,
"id": 274,
Expand Down Expand Up @@ -3664,7 +3664,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 0,
"key_priorities": null,
"inform_score": null,
"id": 352,
Expand Down Expand Up @@ -4616,7 +4616,7 @@
"iso": "MO",
"iso3": "MAC",
"society_url": "http://www.redcross.org.mo/",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 279,
Expand Down Expand Up @@ -5311,7 +5311,7 @@
"iso": "NR",
"iso3": "NRU",
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 251,
Expand Down Expand Up @@ -5571,7 +5571,7 @@
"iso": "NU",
"iso3": "NIU",
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 253,
Expand Down Expand Up @@ -5752,7 +5752,7 @@
"iso": "OM",
"iso3": "OMN",
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 258,
Expand Down Expand Up @@ -6060,7 +6060,7 @@
"iso": "PN",
"iso3": "PCN",
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 256,
Expand Down Expand Up @@ -6156,7 +6156,7 @@
"iso": "PR",
"iso3": "PRI",
"society_url": "",
"region": null,
"region": 1,
"key_priorities": null,
"inform_score": null,
"id": 257,
Expand Down Expand Up @@ -6365,7 +6365,7 @@
"iso": "SH",
"iso3": "SHN",
"society_url": "",
"region": null,
"region": 0,
"key_priorities": null,
"inform_score": null,
"id": 262,
Expand Down Expand Up @@ -6616,7 +6616,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 358,
Expand Down Expand Up @@ -7148,7 +7148,7 @@
"iso": null,
"iso3": null,
"society_url": "",
"region": null,
"region": 2,
"key_priorities": null,
"inform_score": null,
"id": 360,
Expand Down Expand Up @@ -8230,7 +8230,7 @@
"iso": "EH",
"iso3": "ESH",
"society_url": "",
"region": null,
"region": 0,
"key_priorities": null,
"inform_score": null,
"id": 269,
Expand Down
19 changes: 19 additions & 0 deletions cap_feed/migrations/0002_country_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.2 on 2023-06-23 11:38

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('cap_feed', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='country',
name='region',
field=models.ForeignKey(blank=True, default='', on_delete=django.db.models.deletion.SET_DEFAULT, to='cap_feed.region'),
),
]
9 changes: 5 additions & 4 deletions cap_feed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ class Region(models.Model):
centroid = models.CharField(max_length=255, blank=True, default='')

def __str__(self):
return self.id
return self.name

class Country(models.Model):
id = models.CharField(max_length=255, primary_key=True)
name = models.CharField(max_length=255)
iso = models.CharField(max_length=255, blank = True, default='')
iso3 = models.CharField(max_length=255, blank = True, default='')
iso = models.CharField(max_length=255, blank=True, default='')
iso3 = models.CharField(max_length=255, blank=True, default='')
polygon = models.TextField(max_length=16383, blank=True, default='')
centroid = models.CharField(max_length=255, blank=True, default='')
region = models.ForeignKey(Region, blank=True, on_delete=models.SET_DEFAULT, default='')

def __str__(self):
return self.id
return self.name
56 changes: 41 additions & 15 deletions cap_feed/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import requests
import xml.etree.ElementTree as ET

Expand All @@ -7,10 +8,6 @@
from .models import Alert, Region, Country


import json
with open('cap_feed/region.json') as file:
data = json.load(file)

region_centroids = ["17.458740234362434 -2.677413176352464", "-80.83261851536723 -2.6920536197633442", "117.78896429869648 -3.1783208418475954", "30.64725652750233 45.572165430308736", "21.18749859869599 31.264366696701767"]


Expand All @@ -20,6 +17,7 @@
def index(request):
getAlerts()
saveRegions()
saveCountries()
latest_alert_list = Alert.objects.order_by("-sent")[:10]
template = loader.get_template("cap_feed/index.html")
context = {
Expand All @@ -29,17 +27,45 @@ def index(request):


def saveRegions():
count = 0
for region_entry in data:
region = Region()
region.id = region_entry["id"]
region.name = region_entry["region_name"]
coordinates = region_entry["bbox"]["coordinates"][0]
for coordinate in coordinates:
region.polygon += str(coordinates[0]) + "," + str(coordinates[1]) + " "
region.centroid = region_centroids[count]
count += 1
region.save()
with open('cap_feed/region.json') as file:
region_data = json.load(file)
count = 0
for region_entry in region_data:
region = Region()
region.id = region_entry["id"]
region.name = region_entry["region_name"]
coordinates = region_entry["bbox"]["coordinates"][0]
for coordinate in coordinates:
region.polygon += str(coordinate[0]) + "," + str(coordinate[1]) + " "
region.centroid = region_centroids[count]
count += 1
region.save()


def saveCountries():
with open('cap_feed/country.json') as file:
country_data = json.load(file)
for country_entry in country_data:
country = Country()
country.id = country_entry["id"]
country.name = country_entry["name"]
region_id = country_entry["region"]
if ("Region" in country.name) or ("Cluster" in country.name):
continue
if region_id is not None:
country.region = Region.objects.get(id=country_entry["region"])
if country_entry["iso"] is not None:
country.iso = country_entry["iso"]
if country_entry["iso3"] is not None:
country.iso3 = country_entry["iso3"]
if country_entry["bbox"] is not None:
coordinates = country_entry["bbox"]["coordinates"][0]
for coordinate in coordinates:
country.polygon += str(coordinate[0]) + "," + str(coordinate[1]) + " "
if country_entry["centroid"] is not None:
coordinates = country_entry["centroid"]["coordinates"]
country.centroid = str(coordinates[0]) + "," + str(coordinates[1])
country.save()

# sources = [
# ("https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-atom-france", {'atom': 'http://www.w3.org/2005/Atom', 'cap': 'urn:oasis:names:tc:emergency:cap:1.2'}),
Expand Down

0 comments on commit 338e06d

Please sign in to comment.