-
Notifications
You must be signed in to change notification settings - Fork 49
/
generate_countries.py
90 lines (82 loc) · 4.38 KB
/
generate_countries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.geos import MultiPolygon, Polygon
from django.core.management.base import BaseCommand
from localities.models import Country
class Command(BaseCommand):
help = 'Import Localities from CSV file'
non_countries = ['Scarborough Reef']
def handle(self, *args, **options):
Country.objects.all().delete()
data_source = DataSource('localities/data/ne_10m_admin_0_countries.shp')
layer = data_source[0]
for feature in layer:
country_name = feature['NAME'].value
country_name = country_name.encode('utf-8')
# -------------------------------------------------
# CORRECTING THE NAME
# -------------------------------------------------
country_name = country_name.replace(' (Petrel Is.)', '')
country_name = country_name.replace('Barb.', 'Barbuda')
country_name = country_name.replace('Br.', 'British')
country_name = country_name.replace(
'Central African Rep.', 'Central African Republic'
)
country_name = country_name.replace('Curaçao', 'Curacao')
country_name = country_name.replace('Côte d\'Ivoire', 'Ivory Coast')
country_name = country_name.replace(
'Cyprus U.N. Buffer Zone', 'United Nations Buffer Zone in Cyprus'
)
country_name = country_name.replace(
'Dem. Rep. Congo', 'Democratic Republic of the Congo'
)
country_name = country_name.replace('Dem. Rep. Korea', 'North Korea')
country_name = country_name.replace('Eq.', 'Equatorial')
country_name = country_name.replace(
'Fr. S. Antarctic Lands', 'French Southern and Antarctic Lands'
)
country_name = country_name.replace('Fr.', 'French')
country_name = country_name.replace('Geo.', 'Georgia')
country_name = country_name.replace('Gren.', 'the Grenadines')
country_name = country_name.replace('Herz.', 'Herzegovina')
country_name = country_name.replace('I.', 'Islands')
country_name = country_name.replace('Is.', 'Islands')
country_name = country_name.replace('N.', 'Northern')
country_name = country_name.replace('Rep.', 'Republic')
country_name = country_name.replace('U.S.', 'United States')
country_name = country_name.replace('S.', 'South')
country_name = country_name.replace('Sandw.', 'Sandwich')
country_name = country_name.replace('São Tomé and Principe', 'Sao Tome and Principe')
country_name = country_name.replace('St-Barthélemy', 'Saint Barthelemy')
country_name = country_name.replace('St.', 'Saint')
country_name = country_name.replace('St-', 'Saint ')
country_name = country_name.replace('Ter.', 'Territory')
country_name = country_name.replace('Vin.', 'Vincent')
country_name = country_name.replace('W.', 'Western')
country_name = country_name.strip()
# -------------------------------------------------
# FINISH
# -------------------------------------------------
geometry = feature.geom
try:
country = Country.objects.get(name=country_name)
if 'MultiPolygon' not in geometry.geojson:
geometry = MultiPolygon(
[Polygon(coords) for coords in
country.geometry.coords[0]] +
[Polygon(geometry.coords[0])]).geojson
else:
geometry = MultiPolygon(
[Polygon(coords) for coords in
country.geometry.coords[0]] +
[Polygon(coords) for coords in geometry.coords[0]]).geojson
country.polygon_geometry = geometry
except Exception:
if 'MultiPolygon' not in geometry.geojson:
geometry = MultiPolygon(Polygon(geometry.coords[0])).geojson
else:
geometry = geometry.geojson
country = Country(name=country_name)
country.polygon_geometry = geometry
if country_name not in self.non_countries:
country.save()