-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added RIPARIAS-specific data and import script
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
dashboard/management/commands/load_wallonia_river_basins_areas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+1.07 MB
source_data/public_areas/wallonia_river_basins/river_basins_wallonia.gpkg
Binary file not shown.