-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_CreateMunicipalCatchments.R
85 lines (69 loc) · 3.45 KB
/
01_CreateMunicipalCatchments.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
## ---------------------------
##
## Script name: 01_CreateMunicipalCatchments.R
##
## Purpose: Create source water catchment for municipal water supply in Canada
## Based on location of municipal water licences as they
## connect to HydroRIVERS/RiverATLAS
##
## Author: Dr. François-Nicolas Robinne, Canadian Forest Service
##
## Date Created: 2022-11-04
##
## Email: francois.robinne@nrcan-rncan.gc.ca
##
## ---------------------------
##
## Notes: This script creates two watersheds layers:
## - All water licences
## - Water licences without data from Indigenous Services Canada (ISC)
##
## Beware of file names when running the script with data from the FigShare repo
##
## We strongly recommend reading the data descriptor for best use of the data
## and reuse of this script.
##
## ---------------------------
## General options -------------------------------------------------------------
options(scipen = 6, digits = 4) # non-scientific notation
## Load libraries --------------------------------------------------------------
library(whitebox)
wbt_init(exe_path = 'C:/Users/frobinne/Documents/WhiteboxTools/WBT/whitebox_tools.exe')
library(sf)
library(tidyverse)
## Set working directory -------------------------------------------------------
setwd("C:/Users/frobinne/Documents/Professional/PROJECTS/39_2021_CANADA_F2F_SOURCE2TAP_ACTIVE/02_PROCESSED_DATA")
## Processing ------------------------------------------------------------------
#################################
########## All basins ###########
#################################
# Compute raster basins
outlets <- "HYDROLAB_MCGILL/Intakes_WGS84_RiverAtlas_Segments_With_Licence_V4.shp"
d8_ptr <- "HYDROLAB_MCGILL/hyd_na_ar_dir_merge_15s.tif"
output_basins <- "NRCAN/Unnest_Basins/Municipal_Catchments_V1/All_Licences/Unnest_Basins.tif"
wbt_unnest_basins(d8_pntr = d8_ptr,
pour_pts = outlets,
output = output_basins,
esri_pntr = T,
verbose_mode = T,
compress_rasters = T)
# Convert raster basins to polygons
ws_tif <- list.files("C:/Users/frobinne/Documents/Professional/PROJECTS/39_2021_CANADA_F2F_SOURCE2TAP_ACTIVE/02_PROCESSED_DATA/NRCAN/Unnest_Basins/Municipal_Catchments_V1/All_Licences",
pattern = ".tif$")
for(i in ws_tif){
tif <- i
ws <- paste0(i,".shp")
wbt_raster_to_vector_polygons(input = tif,
output = ws,
wd = "C:/Users/frobinne/Documents/Professional/PROJECTS/39_2021_CANADA_F2F_SOURCE2TAP_ACTIVE/02_PROCESSED_DATA/NRCAN/Unnest_Basins/Municipal_Catchments_V1/All_Licences",
verbose_mode = T)
}
# Merge catchment polygons
file_list <- list.files("C:/Users/frobinne/Documents/Professional/PROJECTS/39_2021_CANADA_F2F_SOURCE2TAP_ACTIVE/02_PROCESSED_DATA/NRCAN/Unnest_Basins/Municipal_Catchments_V1/All_Licences",
pattern = "*shp", full.names = TRUE)
shapefile_list <- lapply(file_list, read_sf)
can_catch <- sf::st_as_sf(data.table::rbindlist(shapefile_list)) %>%
select(-FID)
# Write output
st_write(can_catch,
"C:/Users/frobinne/Documents/Professional/PROJECTS/39_2021_CANADA_F2F_SOURCE2TAP_ACTIVE/02_PROCESSED_DATA/NRCAN/Unnest_Basins/Municipal_Catchments_V1/Can-SWaP_AllLicences_V2.gpkg")