Skip to content

Commit 7fc4e00

Browse files
Merge pull request #1581 from IFRCGo/develop
Hotfix for DREF MDR Code validation fix
2 parents e8732da + bee9071 commit 7fc4e00

31 files changed

+563
-67
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## Unreleased
88

9+
## 1.1.460
10+
11+
### Added
12+
- Fix for MDR code validation
13+
- Admin2 enhancements
14+
915
## 1.1.459
1016

1117
### Added
@@ -2084,7 +2090,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
20842090

20852091
## 0.1.20
20862092

2087-
[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.459...HEAD
2093+
[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.460...HEAD
2094+
[1.1.460]: https://github.com/IFRCGo/go-api/compare/1.1.459...1.1.460
20882095
[1.1.459]: https://github.com/IFRCGo/go-api/compare/1.1.458...1.1.459
20892096
[1.1.458]: https://github.com/IFRCGo/go-api/compare/1.1.457...1.1.458
20902097
[1.1.457]: https://github.com/IFRCGo/go-api/compare/1.1.456...1.1.457

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ The above command will generate a list of missing districts in the database base
215215
* `--import-missing missing-districts.txt` -- this will import districts for the iso2 mentioned in `missing-districts.txt` to the database. The file is the same format as generated by the default command.
216216
* `--import-all` -- this option is used to import all districts in the shapefile, if they don't have a code we can match against in the database.
217217

218+
## import-admin2-data
219+
This management command is used for updating and importing admin2 shapefile. To run:
220+
* `python manage.py import-admin2-data <filename.shp>`
221+
222+
The shapefile should have the following mandatory fields:
223+
* name or shapeName
224+
* code or pcode
225+
* admin1_id (this is the ID of the GO district this admin2 belongs to)
226+
227+
See [this ticket](https://github.com/IFRCGo/go-api/issues/1492#issuecomment-1284120696) for a full workflow of preparing the admin2 shapefiles.
228+
The above command will generate a list of missing admin2s in the database based on the code (we use pcodes) to a file called `missing-admin2.txt`
229+
230+
### Options available for the command
231+
* `--update-geom` -- updates the geometry for all admin2 matched in the shapefile.
232+
* `--import-missing missing-admin2.txt` -- this will import admin2 listed in `missing-admin2.txt` to the database. The file is the same format as generated by the default command.
233+
* `--import-all` -- this option is used to import all admin2 in the shapefile.
234+
235+
218236
## Update bbox for regions
219237
Run `python manage.py update-region-bbox` to update the bbox for each region in the database.
220238

@@ -227,5 +245,13 @@ Run ` python manage.py update-sovereign-and-disputed new_fields.csv` to update t
227245
## Update Mapbox Tilesets
228246
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.
229247

248+
### Options available for the command
249+
* `--production` — update production tilesets. If this flag is not set, by default the script will only update staging tiles
250+
* `--update-countries` — update tileset for countries, including labels
251+
* `--update-districts` — update tileset for districts, including labels
252+
* `--update-all` — update all countries and districts tilesets
253+
* `--create-and-update-admin2 <ISO3>` — if a new admin2 tileset should be created, use this argument. It will create a new source on Mapbox and then register a tileset. Ensure that a recipe is create in `mapbox/admin2/` directory. For example, see `mapbox/admin2/COL.json`. To run `python manage.py update-mapbox-tilesets --create-and-update-admin2 COL`
254+
* `--update-admin2 <ISO3>` — use this to update an existing admin2 tileset. For example, `python manage.py update-mapbox-tilesets --update-admin2 COL`
255+
230256
## Import GEC codes
231257
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/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: 27 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,18 @@ 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("code") if 'code' in feature.fields else 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+
local_name = feature.get("local_name") if "local_name" in feature.fields else None
78+
local_name_code = feature.get("local_name_code") if "local_name_code" in feature.fields else None
79+
alternate_name = feature.get("alternate_name") if "alternate_name" in feature.fields else None
80+
alternate_name_code = feature.get("alternate_name_code") if "alternate_name_code" in feature.fields else None
81+
82+
#FIXME: must make sure code and admin1_id are not null before continuing
83+
8284
geom_wkt = feature.geom.wkt
8385
geom = GEOSGeometry(geom_wkt, srid=4326)
84-
if geom.geom_type == "Polygon":
85-
geom = MultiPolygon(geom)
86-
8786
centroid = geom.centroid.wkt
8887
bbox = geom.envelope.wkt
8988
# import all shapes for admin2
@@ -117,26 +116,35 @@ def handle(self, *args, **options):
117116

118117
@transaction.atomic
119118
def add_admin2(self, options, import_missing, feature, geom, centroid, bbox):
120-
code = feature.get("code") or "N.A"
121-
name = feature.get("name")
119+
code = feature.get("code") if 'code' in feature.fields else feature.get("pcode")
120+
name = feature.get("name") if 'name' in feature.fields else feature.get("shapeName")
121+
admin1_id = feature.get("district_id") if "district_id" in feature.fields else feature.get("admin1_id")
122+
local_name = feature.get("local_name") if "local_name" in feature.fields else None
123+
local_name_code = feature.get("local_name_code") if "local_name_code" in feature.fields else None
124+
alternate_name = feature.get("alternate_name") if "alternate_name" in feature.fields else None
125+
alternate_name_code = feature.get("alternate_name_code") if "alternate_name_code" in feature.fields else None
122126
admin2 = Admin2()
123127
admin2.code = code
124128
admin2.name = name
125129
admin2.centroid = centroid
126130
admin2.bbox = bbox
127-
country_iso2 = options["country_iso2"]
128-
# find district_id based on centroid of admin2 and country.
131+
admin2.local_name = local_name
132+
admin2.local_name_code = local_name_code
133+
admin2.alternate_name = alternate_name
134+
admin2.alternate_name_code = alternate_name_code
135+
129136
try:
130-
admin2.admin1_id = self.find_district_id(centroid, country_iso2)
137+
admin1 = District.objects.get(id=admin1_id)
138+
admin2.admin1_id = admin1.id
131139
except ObjectDoesNotExist:
132-
print(f"Country({country_iso2}) or admin 1 does not found for - admin2: {name}")
140+
print(f"admin1 {admin1_id} not found for - admin2: {name}")
133141
pass
134142

135143
# save data
136144
if admin2.admin1_id is not None and ((import_missing == "all") or (code in import_missing.keys())):
137145
try:
138-
admin2.save()
139146
print("importing", admin2.name)
147+
admin2.save()
140148
if options["update_geom"]:
141149
self.update_geom(admin2, geom)
142150
except IntegrityError as e:

0 commit comments

Comments
 (0)