right-tree/backend/right_tree/api/management/commands/loadshapefiles.py

58 lines
2.5 KiB
Python
Raw Normal View History

from django.core.management.base import BaseCommand
from django.contrib.gis.utils import LayerMapping
from pathlib import Path
import right_tree.api.data
from right_tree.api.models import SoilLayer, EcologicalDistrictLayer, ChristchurchZone
# Auto-generated `LayerMapping` dictionary for SoilLayers model
soillayer_mapping = {
'nzsc_class': 'nzsc_class',
'nzsc_group': 'nzsc_group',
'nzsc_order': {'code': 'nzsc_order'},
'shape_leng': 'SHAPE_Leng',
'geom': 'POLYGON',
}
# Auto-generated `LayerMapping` dictionary for ecologicaldistrictlayer model
ecologicaldistrictlayer_mapping = {
'ecological': 'ECOLOGICAL',
'ecologic_1': 'ECOLOGIC_1',
'ecologic_2': {'name': 'ECOLOGIC_2'},
'shape_leng': 'SHAPE_Leng',
'shape_area': 'SHAPE_Area',
'geom': 'POLYGON',
}
# Auto-generated `LayerMapping` dictionary for ChristchurchZone model
christchurchzone_mapping = {
'objectid': 'OBJECTID',
'name': 'NAME',
'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'
christchurchzone_shp = Path(right_tree.api.data.__file__).resolve().parent / 'resources' / 'chch_zone' / 'Greater_Christchurch_Area.shp'
class Command(BaseCommand):
help = 'Ingests the shapefile data for ecological regions and soil layers.'
def handle(self, *args, **options):
self.stdout.write('Loading soil layers...')
soil_lm = LayerMapping(SoilLayer, soillayer_shp, soillayer_mapping, transform=False)
soil_lm.save(strict=True)
self.stdout.write(self.style.SUCCESS('Soil layers loaded succesfully.'))
self.stdout.write('Loading ecological district layers...')
ecologicaldistrictlayer_lm = LayerMapping(EcologicalDistrictLayer, ecologicaldistrictlayer_shp, ecologicaldistrictlayer_mapping, transform=False)
ecologicaldistrictlayer_lm.save(strict=True)
self.stdout.write(self.style.SUCCESS('Ecological district layers loaded succesfully.'))
self.stdout.write('Loading christchurch zone layer...')
christchurchzonelayer_lm = LayerMapping(ChristchurchZone, christchurchzone_shp, christchurchzone_mapping, transform=False)
christchurchzonelayer_lm.save(strict=True)
self.stdout.write(self.style.SUCCESS(' Christchurch zone layer loaded succesfully.'))