Fixes and improvements

This commit is contained in:
Matthew Northcott 2023-02-08 14:24:48 +13:00
parent 436075f11c
commit c1796c22a6
12 changed files with 199 additions and 67 deletions

View file

@ -1,7 +1,9 @@
from django.core.management.base import BaseCommand
from django.contrib.gis.utils import LayerMapping
from glob import iglob
from pathlib import Path
from zipfile import ZipFile, is_zipfile
import right_tree.api.data
from right_tree.api.models import SoilLayer, EcologicalDistrictLayer, ChristchurchRegion
@ -32,15 +34,24 @@ christchurchregion_mapping = {
'geom': 'MULTIPOLYGON',
}
# Shapefiles
soillayer_shp = Path(right_tree.api.data.__file__).resolve().parent / 'resources' / 'fundamental_soil_layers' / 'fundamental-soil-layers-new-zealand-soil-classification.shp'
ecologicaldistrictlayer_shp = Path(right_tree.api.data.__file__).resolve().parent / 'resources' / 'ecological_districts' / 'DOC_EcologicalDistricts_2021_08_02.shp'
christchurchregion_shp = Path(right_tree.api.data.__file__).resolve().parent / 'resources' / 'chch_zone' / 'Greater_Christchurch_Area.shp'
resources_path = Path(right_tree.api.data.__file__).resolve().parent / "resources"
soillayer_shp = resources_path / "fundamental-soil-layers-new-zealand-soil-classification.shp"
ecologicaldistrictlayer_shp = resources_path / "Ecological_Districts.shp"
christchurchregion_shp = resources_path / "Greater_Christchurch_Area.shp"
class Command(BaseCommand):
help = 'Ingests the shapefile data for ecological regions and soil layers.'
def handle(self, *args, **options):
query = str(resources_path / "*.zip")
sources = [ZipFile(path) for path in iglob(query) if is_zipfile(path)]
for zf in sources:
zf.extractall(resources_path)
zf.close()
self.stdout.write('Loading soil layers...')
soil_lm = LayerMapping(SoilLayer, soillayer_shp, soillayer_mapping, transform=False)
soil_lm.save(strict=True)