You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -149,20 +151,19 @@ We will use NLCD data later, now we process hydrography dataset.
149
151
We will download and unzip [National Hydrography Dataset](https://www.usgs.gov/national-hydrography/access-national-hydrography-products) for North Carolina and create a GRASS project, in which we will extract the river and adjacent subwatersheds.
Next, we want to extract the subwatersheds along the river.
222
-
If we simply overlap (with [v.select](https://grass.osgeo.org/grass-devel/manuals/v.select.html)) the river and the subwatersheds, we will miss some of them:
223
+
If we simply overlap (with [v.select](https://grass.osgeo.org/grass-devel/manuals/v.select.html))
224
+
the river and the subwatersheds, we will miss some of them
225
+
because the river data don't always overlap or touch the subwatersheds.
223
226
224
227
```{python}
225
228
tools.v_select(
@@ -263,8 +266,9 @@ The rest of the workflow will be done in a CRS used in North Carolina (EPSG 3358
263
266
Since we want our project to use a different CRS more suitable for our study area (EPSG:3358 for North Carolina), we will create it now:
Deactivate the active raster mask, restoring operations to apply across the full region.
561
+
Deactivate the active raster mask, restoring operations to apply across the full computational region.
571
562
572
563
```{python}
573
564
mask.deactivate()
@@ -591,24 +582,26 @@ we will use the workflow we just ran and create a
591
582
script that uses Python's `multiprocessing` module to parallelize the workflow.
592
583
Each subwatershed is processed independently in its own environment, which allows computations to run concurrently without interference.
593
584
594
-
Each subwatershed needs to set different mask and computational region, however normally, those settings are global, and so for different mask and region for each parallel process, we will use mask and region context managers.
595
-
596
-
* Masking is handled using `MaskManager`, a [context manager for setting and managing raster mask](https://grass.osgeo.org/grass-devel/manuals/libpython/grass.script.html#grass.script.MaskManager),
597
-
making it possible to have custom mask for the current process. This feature is available only since GRASS 8.5.
598
-
599
-
```python
600
-
with gs.MaskManager(mask_name=f"basin_{huc12}"):
601
-
# Run actual computation with active mask.
602
-
gs.run_command(...)
603
-
```
585
+
Each subwatershed needs to set different computational region and mask.
586
+
However those setting are usually global for each mapset.
587
+
So, to use different regions and masks for each parallel process, we will use the region and mask context managers.
604
588
605
589
* Computational region is handled using `RegionManager`, a [context manager for setting and managing computational region](https://grass.osgeo.org/grass-devel/manuals/libpython/grass.script.html#grass.script.RegionManager),
606
590
making it possible to have custom region for the current process. This feature is available only since GRASS 8.5.
607
591
608
592
```python
609
593
with gs.RegionManager(vector=f"basin_{huc12}"):
610
594
# Run actual computation in the specified region.
611
-
gs.run_command(...)
595
+
tools.r_sim_water(...)
596
+
```
597
+
598
+
* Masking is handled using `MaskManager`, a [context manager for setting and managing raster mask](https://grass.osgeo.org/grass-devel/manuals/libpython/grass.script.html#grass.script.MaskManager),
599
+
making it possible to have custom mask for the current process. This feature is available only since GRASS 8.5.
600
+
601
+
```python
602
+
with gs.MaskManager(mask_name=f"basin_{huc12}"):
603
+
# Run actual computation with active mask.
604
+
tools.r_sim_water(...)
612
605
```
613
606
614
607
## Putting it all together
@@ -638,20 +631,31 @@ def compute(huc12):
638
631
flags="t",
639
632
)
640
633
# Set the computational region to match the basin
634
+
# while using the NED raster cell size and alignment
641
635
with gs.RegionManager(vector=f"basin_{huc12}", raster="ned"):
642
636
tools.v_to_rast(
643
637
input=f"basin_{huc12}",
644
638
output=f"basin_{huc12}",
645
639
use="val",
646
640
)
641
+
tools.r_proj(
642
+
project="nlcd",
643
+
mapset="PERMANENT",
644
+
input="nlcd",
645
+
output=f"nlcd_{huc12}",
646
+
)
647
+
tools.r_recode(
648
+
input=f"nlcd_{huc12}",
649
+
output=f"mannings_{huc12}",
650
+
rules="mannings.txt",
651
+
)
652
+
tools.r_recode(
653
+
input=f"nlcd_{huc12}",
654
+
output=f"runoff_{huc12}",
655
+
rules="runoff.txt",
656
+
)
647
657
with gs.MaskManager(mask_name=f"basin_{huc12}"):
648
658
# Run actual computation with active mask.
649
-
tools.r_proj(
650
-
project="nlcd",
651
-
mapset="PERMANENT",
652
-
input="nlcd",
653
-
output=f"nlcd_{huc12}",
654
-
)
655
659
tools.r_recode(
656
660
input=f"nlcd_{huc12}",
657
661
output=f"mannings_{huc12}",
@@ -713,11 +717,11 @@ def compute(huc12):
713
717
if __name__ == "__main__":
714
718
tools = Tools()
715
719
# The entire workflow will run in parallel,
716
-
# so this limits the number of threads the tools can use to 1.
720
+
# so this limits the number of threads each individual tool can use to 1.
Load the subwatershed layers into geopandas for visualization. Join the dataframe with the simulation values using the _huc12_ key and keep only subwatersheds that have computed erosion values.
751
+
Load the subwatershed layers into geopandas for visualization.
752
+
Join the dataframe with the simulation values using the _huc12_ key
753
+
and from all NC subwatersheds filter only those we initially selected
0 commit comments