Skip to content

Commit 384ac9d

Browse files
committed
improve the mvt management command to update district and country separately and better error handling
1 parent 360040a commit 384ac9d

File tree

2 files changed

+107
-28
lines changed

2 files changed

+107
-28
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,10 @@ Run ` python manage.py update-sovereign-and-disputed new_fields.csv` to update t
247247
## Update Mapbox Tilesets
248248
To update GO countries and districts Mapbox tilesets, run the management command `python manage.py update-mapbox-tilesets`. This will export all country and district geometries to a GeoJSON file, and then upload them to Mapbox. The tilesets will take a while to process. The updated status can be viewed on the Mapbox Studio under tilesets. To run this management command, MAPBOX_ACCESS_TOKEN should be set in the environment.
249249

250+
### Option available for the command
251+
* `--update-countries` — update tileset for countries, including labels
252+
* `--update-districts` — update tileset for districts, including labels
253+
* `--update-all` — update all tilesets
254+
250255
## Import GEC codes
251256
To import GEC codes along with country ids, run `python manage.py import-gec-code appeal_ingest_match.csv`. The CSV should have the columns `'GST_code', 'GST_name', 'GO ID', 'ISO'`

api/management/commands/update-mapbox-tilesets.py

Lines changed: 102 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22
from django.core.management.base import BaseCommand, CommandError
33
from django.conf import settings
44
import time
5+
import os
56

67
class Command(BaseCommand):
78
help = "This command produces a countries.geojson and districts.geojson, and uploads them to Mapbox. It is the source for all GO Maps."
89

10+
missing_args_message = "Argument missing. Specify --update-countries, --update-districts or --update-all."
11+
12+
def add_arguments(self, parser):
13+
parser.add_argument(
14+
'--update-countries',
15+
action='store_true',
16+
help='Update tileset for countries'
17+
)
18+
parser.add_argument(
19+
'--update-districts',
20+
action='store_true',
21+
help='Update tileset for districts'
22+
)
23+
parser.add_argument(
24+
'--update-all',
25+
action='store_true',
26+
help='Update tileset for countries and districts'
27+
)
28+
929
def handle(self, *args, **options):
1030
try:
1131
db = settings.DATABASES['default']
@@ -16,61 +36,115 @@ def handle(self, *args, **options):
1636
DB_PORT = 5432
1737
connection_string = 'PG:host={} dbname={} user={} password={} port={}'.format(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD, DB_PORT)
1838

39+
if (os.getenv("MAPBOX_ACCESS_TOKEN") is None):
40+
raise Exception('MAPBOX_ACCESS_TOKEN must be set')
41+
42+
if options['update_countries'] or options['update_all']:
43+
self.update_countries(connection_string)
44+
45+
if options['update_districts'] or options['update_all']:
46+
self.update_districts(connection_string)
47+
48+
except BaseException as e:
49+
raise CommandError('Could not update tilesets: ' + str(e))
50+
51+
def update_countries(self, connection_string):
52+
try:
1953
print('Exporting countries...')
2054
subprocess.check_call(['touch', '/tmp/countries.geojson'])
2155
subprocess.check_call(['rm', '/tmp/countries.geojson'])
2256
subprocess.check_call(['ogr2ogr', '-f', 'GeoJSON', '/tmp/countries.geojson', connection_string, '-sql', 'select cd.country_id, cd.geom, c.name, c.name_es, c.name_fr, c.name_ar, c.iso, c.region_id, c.iso3, c.independent, c.is_deprecated, c.disputed, c.fdrs, c.record_type from api_countrygeoms cd, api_country c where cd.country_id = c.id and c.record_type=1' ])
2357
print('Countries written to /tmp/countries.geojson')
58+
except Exception as e:
59+
print('Failed to export countries', e)
60+
raise
2461

25-
print('Exporting districts...')
26-
subprocess.check_call(['touch', '/tmp/districts.geojson'])
27-
subprocess.check_call(['rm', '/tmp/districts.geojson'])
28-
# FIXME eventually should be name_en, name_es etc.
29-
subprocess.check_call(['ogr2ogr', '-lco', 'COORDINATE_PRECISION=5', '-f', 'GeoJSON', '/tmp/districts.geojson', connection_string, '-sql', 'select cd.district_id, cd.geom, c.name, c.code, c.country_id, c.is_enclave, c.is_deprecated, country.iso as country_iso, country.iso3 as country_iso3, country.name as country_name, country.name_es as country_name_es, country.name_fr as country_name_fr, country.name_ar as country_name_ar from api_districtgeoms cd, api_district c, api_country country where cd.district_id = c.id and cd.geom is not null and country.id=c.country_id' ])
30-
print('Districts written to /tmp/districts.geojson')
31-
62+
try:
3263
print('Exporting country centroids...')
3364
subprocess.check_call(['touch', '/tmp/country-centroids.geojson'])
3465
subprocess.check_call(['rm', '/tmp/country-centroids.geojson'])
3566
subprocess.check_call(['ogr2ogr', '-lco', 'COORDINATE_PRECISION=4', '-f', 'GeoJSON', '/tmp/country-centroids.geojson', connection_string, '-sql', 'select id as country_id, name_en as name, name_ar, name_es, name_fr, independent, disputed, is_deprecated, iso, iso3, record_type, fdrs, region_id, centroid from api_country where centroid is not null'])
67+
except Exception as e:
68+
print('Failed to export country centroids', e)
69+
raise
3670

37-
print('Exporting district centroids...')
38-
subprocess.check_call(['touch', '/tmp/district-centroids.geojson'])
39-
subprocess.check_call(['rm', '/tmp/district-centroids.geojson'])
40-
# FIXME eventually should be name_en, name_es etc.
41-
subprocess.check_call(['ogr2ogr', '-lco', 'COORDINATE_PRECISION=4', '-f', 'GeoJSON', '/tmp/district-centroids.geojson', connection_string, '-sql', 'select d.id as district_id, d.country_id as country_id, d.name, d.code, d.is_deprecated, d.is_enclave, c.iso as country_iso, c.iso3 as country_iso3, c.name as country_name, c.name_es as country_name_es, c.name_fr as country_name_fr, c.name_ar as country_name_ar, d.centroid from api_district d join api_country c on d.country_id=c.id where d.centroid is not null'])
42-
43-
71+
try:
4472
print('Update Mapbox tileset source for countries...')
4573
subprocess.check_call(['tilesets', 'upload-source', '--replace', 'go-ifrc', 'go-countries-src', '/tmp/countries.geojson'])
74+
except Exception as e:
75+
print('Failed to update tileset source for countries', e)
76+
raise
4677

78+
try:
4779
print('Update Mapbox tileset for countries... and sleeping a minute')
4880
subprocess.check_call(['tilesets', 'publish', 'go-ifrc.go-countries'])
4981
time.sleep(60)
82+
except Exception as e:
83+
print('Failed to update tileset for countries', e)
84+
raise
5085

51-
52-
print('Update Mapbox tileset source for districts...')
53-
subprocess.check_call(['tilesets', 'upload-source', '--replace', 'go-ifrc', 'go-districts-src-1', '/tmp/districts.geojson'])
54-
55-
print('Update Mapbox tileset for districts... and sleeping a minute')
56-
subprocess.check_call(['tilesets', 'publish', 'go-ifrc.go-districts-1'])
57-
time.sleep(60)
58-
59-
86+
try:
6087
print('Update Mapbox tileset source for country centroids...')
6188
subprocess.check_call(['tilesets', 'upload-source', '--replace', 'go-ifrc', 'go-country-centroids', '/tmp/country-centroids.geojson'])
89+
except Exception as e:
90+
print('Failed to update tileset source for country centroids')
91+
raise
6292

93+
try:
6394
print('Update Mapbox tileset for country centroids... and sleeping a minute')
6495
subprocess.check_call(['tilesets', 'publish', 'go-ifrc.go-country-centroids'])
6596
time.sleep(60)
97+
except Exception as e:
98+
print('Failed to update tileset for country centroids')
99+
raise
100+
66101

102+
def update_districts(self, connection_string):
103+
try:
104+
print('Exporting districts...')
105+
subprocess.check_call(['touch', '/tmp/districts.geojson'])
106+
subprocess.check_call(['rm', '/tmp/districts.geojson'])
107+
# FIXME eventually should be name_en, name_es etc.
108+
subprocess.check_call(['ogr2ogr', '-lco', 'COORDINATE_PRECISION=5', '-f', 'GeoJSON', '/tmp/districts.geojson', connection_string, '-sql', 'select cd.district_id, cd.geom, c.name, c.code, c.country_id, c.is_enclave, c.is_deprecated, country.iso as country_iso, country.iso3 as country_iso3, country.name as country_name, country.name_es as country_name_es, country.name_fr as country_name_fr, country.name_ar as country_name_ar from api_districtgeoms cd, api_district c, api_country country where cd.district_id = c.id and cd.geom is not null and country.id=c.country_id' ])
109+
print('Districts written to /tmp/districts.geojson')
110+
except Exception as e:
111+
print('Failed to export districts', e)
112+
raise
113+
114+
try:
115+
print('Exporting district centroids...')
116+
subprocess.check_call(['touch', '/tmp/district-centroids.geojson'])
117+
subprocess.check_call(['rm', '/tmp/district-centroids.geojson'])
118+
# FIXME eventually should be name_en, name_es etc.
119+
subprocess.check_call(['ogr2ogr', '-lco', 'COORDINATE_PRECISION=4', '-f', 'GeoJSON', '/tmp/district-centroids.geojson', connection_string, '-sql', 'select d.id as district_id, d.country_id as country_id, d.name, d.code, d.is_deprecated, d.is_enclave, c.iso as country_iso, c.iso3 as country_iso3, c.name as country_name, c.name_es as country_name_es, c.name_fr as country_name_fr, c.name_ar as country_name_ar, d.centroid from api_district d join api_country c on d.country_id=c.id where d.centroid is not null'])
120+
except Exception as e:
121+
print('Failed to export district centroids', e)
122+
raise
67123

124+
try:
125+
print('Update Mapbox tileset source for districts...')
126+
subprocess.check_call(['tilesets', 'upload-source', '--replace', 'go-ifrc', 'go-districts-src-1', '/tmp/districts.geojson'])
127+
except Exception as e:
128+
print('Failed to update tileset source for districts', e)
129+
raise
130+
131+
try:
132+
print('Update Mapbox tileset for districts... and sleeping a minute')
133+
subprocess.check_call(['tilesets', 'publish', 'go-ifrc.go-districts-1'])
134+
time.sleep(60)
135+
except Exception as e:
136+
print('Failed to update tileset for districts')
137+
raise
138+
139+
try:
68140
print('Update Mapbox tileset source for district centroids...')
69141
subprocess.check_call(['tilesets', 'upload-source', '--replace', 'go-ifrc', 'go-district-centroids', '/tmp/district-centroids.geojson'])
70-
142+
except Exception as e:
143+
print('Failed to update tileset source for district centroid')
144+
raise
145+
try:
71146
print('Update Mapbox tileset for district centroids... [no sleep]')
72147
subprocess.check_call(['tilesets', 'publish', 'go-ifrc.go-district-centroids'])
73-
74-
75-
except BaseException as e:
76-
raise CommandError('Could not update tilesets: ' + str(e))
148+
except Exception as e:
149+
print('Failed to update tileset for distrct centroids')
150+
raise

0 commit comments

Comments
 (0)