Skip to content

Commit ec78cf9

Browse files
committed
use admin1 id from the shapefile, expose admin2 in the django admin
1 parent 9ad6461 commit ec78cf9

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

api/admin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ class RegionAdmin(geoadmin.OSMGeoAdmin, CompareVersionAdmin, RegionRestrictedAdm
550550
search_fields = ('name',)
551551
modifiable = True
552552

553+
class Admin2Admin(geoadmin.OSMGeoAdmin, CompareVersionAdmin, RegionRestrictedAdmin):
554+
search_fields = ('name',)
555+
modifiable = True
553556

554557
class UserProfileAdmin(CompareVersionAdmin):
555558
search_fields = ('user__username', 'user__email', 'country__name',)
@@ -795,6 +798,7 @@ def has_add_permission(cls, request, obj=None):
795798
admin.site.register(models.Country, CountryAdmin)
796799
admin.site.register(models.Region, RegionAdmin)
797800
admin.site.register(models.District, DistrictAdmin)
801+
admin.site.register(models.Admin2, Admin2Admin)
798802
admin.site.register(models.Appeal, AppealAdmin)
799803
admin.site.register(models.AppealDocument, AppealDocumentAdmin)
800804
admin.site.register(models.AppealFilter, AppealFilterAdmin)

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

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

1515
class Command(BaseCommand):
16-
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"
16+
help = "import a shapefile of administrative boundary level 2 data to the GO database. To run, python manage.py import-admin2-data input.shp"
1717

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

@@ -43,12 +43,6 @@ def add_arguments(self, parser):
4343
action='store_true',
4444
help='Import all admin2 boundaries in the shapefile, if possible.'
4545
)
46-
parser.add_argument(
47-
'--country-iso2',
48-
type=str,
49-
required=True,
50-
help='Country iso2 code'
51-
)
5246

5347
@transaction.atomic
5448
def handle(self, *args, **options):
@@ -77,13 +71,14 @@ def handle(self, *args, **options):
7771

7872
# loop through each feature in the shapefile
7973
for feature in data[0]:
80-
code = feature.get("code")
81-
name = feature.get("name")
74+
code = feature.get("pcode")
75+
name = feature.get("name") if 'name' in feature.fields else feature.get("shapeName")
76+
admin1_id = feature.get("district_id") if "district_id" in feature.fields else feature.get("admin1_id")
77+
78+
#FIXME: must make sure code and admin1_id are not null before continuing
79+
8280
geom_wkt = feature.geom.wkt
8381
geom = GEOSGeometry(geom_wkt, srid=4326)
84-
if geom.geom_type == "Polygon":
85-
geom = MultiPolygon(geom)
86-
8782
centroid = geom.centroid.wkt
8883
bbox = geom.envelope.wkt
8984
# import all shapes for admin2
@@ -117,26 +112,26 @@ def handle(self, *args, **options):
117112

118113
@transaction.atomic
119114
def add_admin2(self, options, import_missing, feature, geom, centroid, bbox):
120-
code = feature.get("code") or "N.A"
121-
name = feature.get("name")
115+
code = feature.get("pcode")
116+
name = feature.get("name") if 'name' in feature.fields else feature.get("shapeName")
117+
admin1_id = feature.get("district_id") if "district_id" in feature.fields else feature.get("admin1_id")
122118
admin2 = Admin2()
123119
admin2.code = code
124120
admin2.name = name
125121
admin2.centroid = centroid
126122
admin2.bbox = bbox
127-
country_iso2 = options["country_iso2"]
128-
# find district_id based on centroid of admin2 and country.
129123
try:
130-
admin2.admin1_id = self.find_district_id(centroid, country_iso2)
124+
admin1 = District.objects.get(id=admin1_id)
125+
admin2.admin1_id = admin1.id
131126
except ObjectDoesNotExist:
132-
print(f"Country({country_iso2}) or admin 1 does not found for - admin2: {name}")
127+
print(f"admin1 {admin1_id} not found for - admin2: {name}")
133128
pass
134129

135130
# save data
136131
if admin2.admin1_id is not None and ((import_missing == "all") or (code in import_missing.keys())):
137132
try:
138-
admin2.save()
139133
print("importing", admin2.name)
134+
admin2.save()
140135
if options["update_geom"]:
141136
self.update_geom(admin2, geom)
142137
except IntegrityError as e:

0 commit comments

Comments
 (0)