|
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,14 @@ 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("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 | + |
82 | 80 | geom_wkt = feature.geom.wkt |
83 | 81 | geom = GEOSGeometry(geom_wkt, srid=4326) |
84 | | - if geom.geom_type == "Polygon": |
85 | | - geom = MultiPolygon(geom) |
86 | | - |
87 | 82 | centroid = geom.centroid.wkt |
88 | 83 | bbox = geom.envelope.wkt |
89 | 84 | # import all shapes for admin2 |
@@ -117,26 +112,26 @@ def handle(self, *args, **options): |
117 | 112 |
|
118 | 113 | @transaction.atomic |
119 | 114 | 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") |
122 | 118 | admin2 = Admin2() |
123 | 119 | admin2.code = code |
124 | 120 | admin2.name = name |
125 | 121 | admin2.centroid = centroid |
126 | 122 | admin2.bbox = bbox |
127 | | - country_iso2 = options["country_iso2"] |
128 | | - # find district_id based on centroid of admin2 and country. |
129 | 123 | 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 |
131 | 126 | 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}") |
133 | 128 | pass |
134 | 129 |
|
135 | 130 | # save data |
136 | 131 | if admin2.admin1_id is not None and ((import_missing == "all") or (code in import_missing.keys())): |
137 | 132 | try: |
138 | | - admin2.save() |
139 | 133 | print("importing", admin2.name) |
| 134 | + admin2.save() |
140 | 135 | if options["update_geom"]: |
141 | 136 | self.update_geom(admin2, geom) |
142 | 137 | except IntegrityError as e: |
|
0 commit comments