Skip to content

Commit

Permalink
Added RIPARIAS-specific data and import script
Browse files Browse the repository at this point in the history
  • Loading branch information
niconoe committed Nov 6, 2023
1 parent 982f4be commit 4bc19c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions dashboard/management/commands/load_wallonia_river_basins_areas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Custom script to import the river basins areas (Wallonia) for the LIFE RIPARIAS project.
The usual load_area.py script cannot be used because the specifities of the data make it unsuitable for the usual
LayerMapping helper.
See task description at: https://github.com/riparias/early-alert-webapp/issues/10
"""

import os

from django.contrib.gis.gdal import DataSource, SpatialReference, CoordTransform
from django.core.management import BaseCommand

from dashboard.management.commands.helpers import get_multipolygon_from_feature
from dashboard.models import Area, DATA_SRID

THIS_FILE_DIR = os.path.dirname(__file__)
SOURCE_DATA_STRID = SpatialReference("EPSG:31370")

ct = CoordTransform(SOURCE_DATA_STRID, SpatialReference(f"EPSG:{DATA_SRID}"))

SOURCE_DATA: dict[str, dict] = {
"Basins": {
"path": f"{THIS_FILE_DIR}/../../../source_data/public_areas/wallonia_river_basins/river_basins_wallonia.gpkg",
"name_field": "river_basin",
"tags": ["Wallonia", "river basin"],
},
}


class Command(BaseCommand):
help = "Import river areas from Wallonia in the database."

def handle(self, *args, **options) -> None:
self.stdout.write("Importing Flemish river areas")
for layer_name, layer_config in SOURCE_DATA.items():
self.stdout.write(f"Importing layer {layer_name}")

ds = DataSource(layer_config["path"])
layer = ds[0]
self.stdout.write(f"Found {layer.num_feat} features") # type: ignore
for feature in layer:
area_name = feature[layer_config["name_field"]] # type: ignore
tags = layer_config["tags"].copy()
try:
tags.append(feature[layer_config["dynamic_tag_field"]].value) # type: ignore
except KeyError:
pass
self.stdout.write(f"Creating area {area_name}, with tags {tags}")

multipolygon = get_multipolygon_from_feature(feature)
reprojected_multipolygon = multipolygon.transform(ct, clone=True)

area = Area.objects.create(mpoly=reprojected_multipolygon.wkt, name=area_name) # type: ignore
area.tags.add(*tags)
Binary file not shown.

0 comments on commit 4bc19c5

Please sign in to comment.