Skip to content

Commit a8a5d26

Browse files
author
Rub21
committed
Add country-iso2 argument to import admin2
1 parent cea2360 commit a8a5d26

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

api/management/commands/import-admin2-data.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from api.models import Admin2Geoms
1515

1616
class Command(BaseCommand):
17-
help = "import a shapefile of administrative boundary level 2 data to the GO database. To run, python manage.py import-admin2-data input.shp"
17+
help = "import a shapefile of administrative boundary level 2 data to the GO database. To run, python manage.py import-admin2-data input.shp --country-iso2=af"
1818

1919
missing_args_message = "Filename is missing. A shapefile with valid admin polygons is required."
2020

@@ -44,11 +44,16 @@ def add_arguments(self, parser):
4444
action='store_true',
4545
help='Import all admin2 boundaries in the shapefile, if possible.'
4646
)
47+
parser.add_argument(
48+
'--country-iso2',
49+
type=str,
50+
required=True,
51+
help='Country iso2 code'
52+
)
4753

4854
@transaction.atomic
4955
def handle(self, *args, **options):
5056
filename = options['filename'][0]
51-
5257
# a dict to hold all the admin2 that needs to be manually imported
5358
import_missing = {}
5459
if options['import_missing']:
@@ -85,13 +90,10 @@ def handle(self, *args, **options):
8590

8691
centroid = geom.centroid.wkt
8792
bbox = geom.envelope.wkt
88-
# if there is no code, but import all is flagged then import this admin2
89-
if code is None or code == '':
90-
if options['import_all']:
93+
# import all shapes for admin2
94+
if options['import_all']:
9195
self.add_admin2(options, 'all', feature, geom, centroid, bbox)
92-
93-
# for features that has a code and not NA
94-
if code and code != 'N.A':
96+
else:
9597
admin2_objects = Admin2.objects.filter(code=code)
9698
if len(admin2_objects) == 0:
9799
if options['import_missing']:
@@ -132,48 +134,49 @@ def handle(self, *args, **options):
132134
d.bbox = bbox
133135
d.save()
134136

135-
if code == 'N.A':
136-
admin2 = Admin2.objects.filter(name__icontains=name)
137-
if len(admin2):
138-
d = admin2[0]
139-
if options['update_geom']:
140-
self.update_geom(d, geom)
141-
if options['update_centroid']:
142-
d.centroid = centroid
143-
if options['update_bbox']:
144-
d.bbox = bbox
145-
d.save()
146-
else:
147-
if options['import_missing']:
148-
self.add_admin2(options, import_missing, feature, geom, centroid, bbox)
149-
else:
150-
missing_file.writerow({'code': code, 'name': name})
151-
152-
if code == 'f':
153-
admin2 = Admin2.objects.filter(name__icontains=name)
154-
if not len(admin2):
155-
if options['import_missing']:
156-
self.add_admin2(options, import_missing, feature, geom, centroid, bbox)
157-
else:
158-
missing_file.writerow({'code': code, 'name': name})
159-
137+
# if code == 'N.A':
138+
# admin2 = Admin2.objects.filter(name__icontains=name)
139+
# if len(admin2):
140+
# d = admin2[0]
141+
# if options['update_geom']:
142+
# self.update_geom(d, geom)
143+
# if options['update_centroid']:
144+
# d.centroid = centroid
145+
# if options['update_bbox']:
146+
# d.bbox = bbox
147+
# d.save()
148+
# else:
149+
# if options['import_missing']:
150+
# self.add_admin2(options, import_missing, feature, geom, centroid, bbox)
151+
# else:
152+
# missing_file.writerow({'code': code, 'name': name})
153+
154+
# if code == 'f':
155+
# admin2 = Admin2.objects.filter(name__icontains=name)
156+
# if not len(admin2):
157+
# if options['import_missing']:
158+
# self.add_admin2(options, import_missing, feature, geom, centroid, bbox)
159+
# else:
160+
# missing_file.writerow({'code': code, 'name': name})
160161
print('done!')
161162

162163

163164
def add_admin2(self, options, import_missing, feature, geom, centroid, bbox):
164165
code = feature.get('code') or 'N.A'
165166
name = feature.get('name')
166-
admin2 =Admin2()
167+
admin2 = Admin2()
167168
admin2.code = code
168169
admin2.name = name
169170
admin2.centroid = centroid
170171
admin2.bbox = bbox
171172
try:
172173
# find district_id based on centroid of admin2 and country.
173-
admin1_id = self.find_district_id(centroid)
174+
admin1_id = self.find_district_id(centroid, options['country_iso2'])
174175
if admin1_id is None:
176+
print('country does not exist', "af")
175177
pass
176-
admin2.admin1_id = admin1_id
178+
else:
179+
admin2.admin1_id = admin1_id
177180
except ObjectDoesNotExist:
178181
print('country does not exist', "af")
179182
pass
@@ -195,13 +198,14 @@ def update_geom(self, admin2, geom):
195198
Admin2Geom.geom = geom
196199
Admin2Geom.save()
197200

198-
def find_district_id(self, centroid):
201+
def find_district_id(self, centroid, country_iso2):
199202
"""Find district_id for admin2, according to the point with in the district polygon.
200203
Args:
201204
centroid (str): Admin2 centroid
205+
country_iso2 (str): Country iso2
202206
"""
203207
admin1_id = None
204-
country_id = Country.objects.get(iso="af")
208+
country_id = Country.objects.get(iso=country_iso2)
205209
districts = District.objects.filter(country_id=country_id)
206210
districts_ids = [d.id for d in districts]
207211
districts_geoms = DistrictGeoms.objects.filter(district_id__in=districts_ids)

0 commit comments

Comments
 (0)