|
13 | 13 | from api.models import Admin2Geoms |
14 | 14 |
|
15 | 15 | 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" |
17 | 17 |
|
18 | 18 | missing_args_message = "Filename is missing. A shapefile with valid admin polygons is required." |
19 | 19 |
|
@@ -43,12 +43,6 @@ def add_arguments(self, parser): |
43 | 43 | action='store_true', |
44 | 44 | help='Import all admin2 boundaries in the shapefile, if possible.' |
45 | 45 | ) |
46 | | - parser.add_argument( |
47 | | - '--country-iso2', |
48 | | - type=str, |
49 | | - required=True, |
50 | | - help='Country iso2 code' |
51 | | - ) |
52 | 46 |
|
53 | 47 | @transaction.atomic |
54 | 48 | def handle(self, *args, **options): |
@@ -77,13 +71,18 @@ def handle(self, *args, **options): |
77 | 71 |
|
78 | 72 | # loop through each feature in the shapefile |
79 | 73 | 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 | + |
82 | 84 | geom_wkt = feature.geom.wkt |
83 | 85 | geom = GEOSGeometry(geom_wkt, srid=4326) |
84 | | - if geom.geom_type == "Polygon": |
85 | | - geom = MultiPolygon(geom) |
86 | | - |
87 | 86 | centroid = geom.centroid.wkt |
88 | 87 | bbox = geom.envelope.wkt |
89 | 88 | # import all shapes for admin2 |
@@ -117,26 +116,35 @@ def handle(self, *args, **options): |
117 | 116 |
|
118 | 117 | @transaction.atomic |
119 | 118 | 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 |
122 | 126 | admin2 = Admin2() |
123 | 127 | admin2.code = code |
124 | 128 | admin2.name = name |
125 | 129 | admin2.centroid = centroid |
126 | 130 | 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 | + |
129 | 136 | 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 |
131 | 139 | 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}") |
133 | 141 | pass |
134 | 142 |
|
135 | 143 | # save data |
136 | 144 | if admin2.admin1_id is not None and ((import_missing == "all") or (code in import_missing.keys())): |
137 | 145 | try: |
138 | | - admin2.save() |
139 | 146 | print("importing", admin2.name) |
| 147 | + admin2.save() |
140 | 148 | if options["update_geom"]: |
141 | 149 | self.update_geom(admin2, geom) |
142 | 150 | except IntegrityError as e: |
|
0 commit comments