|
| 1 | +#' spatial_input_gen |
| 2 | +#' |
| 3 | +#' Generate the spatial inputs for RHESSys and RHESSysPreprocessing |
| 4 | +#' @param Name Name of your project |
| 5 | +#' @param basin The area defining your basin/watershed of interest. Assumes it's a raster |
| 6 | +#' @author Will Burke |
| 7 | +#' @export |
| 8 | + |
| 9 | + |
| 10 | +spatial_input_gen = function(name, |
| 11 | + DEM, |
| 12 | + GRASS_path, |
| 13 | + threshold, |
| 14 | + basin = NULL, |
| 15 | + patch = NULL, |
| 16 | + define_watershed = NULL, |
| 17 | + easting = NULL, |
| 18 | + northing = NULL, |
| 19 | + gisDbase = NULL, |
| 20 | + location = NULL, |
| 21 | + mapset = NULL) { |
| 22 | + |
| 23 | + |
| 24 | + # check and convert file paths for args |
| 25 | + |
| 26 | + |
| 27 | + # check GRASS location and version - requires grass 7 |
| 28 | + |
| 29 | + |
| 30 | + out_maps = NULL |
| 31 | + |
| 32 | + # set initial grass environment |
| 33 | + init = rgrass7::initGRASS(gisBase = GRASS_path, |
| 34 | + home = tempdir(), |
| 35 | + override = TRUE) |
| 36 | + # add basin map to new location |
| 37 | + rgrass7::execGRASS(cmd = "r.in.gdal", input = basin, output = "basin", location = "temp_loc") |
| 38 | + # set grass environment to new (correctly projected) location |
| 39 | + init = rgrass7::initGRASS(gisBase = GRASS_path, |
| 40 | + home = tempdir(), |
| 41 | + location = "temp_loc", |
| 42 | + mapset = "PERMANENT", |
| 43 | + override = TRUE) |
| 44 | + |
| 45 | + # check if same proj - should be quiet but idk |
| 46 | + check = rgrass7::execGRASS(cmd = "r.in.gdal",flags = c("j","quiet"), input = DEM) |
| 47 | + # if projections don't match |
| 48 | + if (check == 1) { |
| 49 | + # add DEM to its own location |
| 50 | + rgrass7::execGRASS(cmd = "r.in.gdal", input = DEM, output = "DEM", location = "dem_loc") |
| 51 | + # project DEM from that location to current loc (temp_loc) |
| 52 | + rgrass7::execGRASS(cmd = "r.proj", location = "dem_loc", mapset = "PERMANENT", input = "DEM") |
| 53 | + } |
| 54 | + |
| 55 | + # --- checking env and stuff --- |
| 56 | + # gmeta() |
| 57 | + # rgrass7::execGRASS(cmd = "g.mapset", flags = "p") |
| 58 | + # rgrass7::execGRASS(cmd = "g.mapset", flags = "l") |
| 59 | + # rgrass7::execGRASS(cmd = "g.list", flags = "p", type = "raster") |
| 60 | + |
| 61 | + # check if basin exists and use it as region if it does, if not use raster |
| 62 | + rgrass7::execGRASS(cmd = "g.region", raster = "basin") |
| 63 | + #rgrass7::execGRASS(cmd = "g.region", flags = "p") # print it |
| 64 | + |
| 65 | + # get slope and aspect maps |
| 66 | + rgrass7::execGRASS(cmd = "r.slope.aspect", elevation = "DEM", slope = "slope", aspect = "aspect") |
| 67 | + out_maps = c(out_maps, "slope", "aspect") |
| 68 | + |
| 69 | + # get horizon maps, in degrees |
| 70 | + rgrass7::execGRASS(cmd = "r.horizon", flags = "d", elevation = "DEM", direction = 0, output = "east") |
| 71 | + rgrass7::execGRASS(cmd = "r.horizon", flags = "d", elevation = "DEM", direction = 180, output = "west") |
| 72 | + out_maps = c(out_maps, "east_000", "west_180") |
| 73 | + |
| 74 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'east_horizon = sin(east_000)') |
| 75 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'west_horizon = sin(west_180)') |
| 76 | + out_maps = c(out_maps, "east_horizon", "west_horizon") |
| 77 | + |
| 78 | + #SET TO BE OPTIONAL |
| 79 | + #rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'ehr.100 = east_horizon*100') |
| 80 | + #rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'whr.100 = west_horizon*100') |
| 81 | + #out_maps = c(out_maps, "ehr.100", "whr.100") |
| 82 | + |
| 83 | + # watershed analysis |
| 84 | + subbasin_name = paste0("subbasin.t",as.character(threshold)) |
| 85 | + stream_name = paste0("stream.t",as.character(threshold)) |
| 86 | + hillslope_name = paste0("hillslope.t",as.character(threshold)) |
| 87 | + |
| 88 | + rgrass7::execGRASS(cmd = "r.watershed", elevation = "DEM", threshold = threshold, accumulation = "accumulation", |
| 89 | + drainage = "drainage", basin = subbasin_name, stream = stream_name, half_basin = hillslope_name) |
| 90 | + out_maps = c(out_maps, subbasin_name, stream_name, hillslope_name) |
| 91 | + |
| 92 | + # ----- patch map ----- |
| 93 | + if (patch == "topo") { |
| 94 | + # Topographical |
| 95 | + hinfo = rgrass7::execGRASS(cmd = "r.info", map = hillslope_name, intern = TRUE) |
| 96 | + minmax_ch = unlist(strsplit(grep("Range of data: \\s+ min = \\d\\s+max = \\d",hinfo, value = TRUE),split = "\\s+")) |
| 97 | + hmax = as.numeric(minmax_ch[which(minmax_ch == "max") + 2]) |
| 98 | + |
| 99 | + #rgrass7::execGRASS(cmd = "r.info", map = "DEM") |
| 100 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = paste0('p.topo = (DEM * ', hmax, ') + ', hillslope_name)) |
| 101 | + rgrass7::execGRASS(cmd = "r.clump", input = "p.topo", output = "p.topo.cl") |
| 102 | + out_maps = c(out_maps, "p.topo.cl") |
| 103 | + |
| 104 | + } else if (patch == "grid") { |
| 105 | + # Grid |
| 106 | + rgrass7::execGRASS(cmd = "r.random.cells", output = "grid", distance = 0) |
| 107 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = paste0('p.grid = (grid * ', hmax, ') + ', hillslope_name)) |
| 108 | + rgrass7::execGRASS(cmd = "r.clump", input = "p.grid", output = "p.grid.cl") |
| 109 | + out_maps = c(out_maps, "p.grid.cl") |
| 110 | + |
| 111 | + } else if (patch == "variable") { |
| 112 | + |
| 113 | + # THIS IS MOSTLY HARDCODED AND PROBABLY SHOULD BE ADDED TO A R-BASED FUNCTION THAT CAN DO IT WITH DYNAMIC RESOLUTION INPUTS |
| 114 | + # Variable resolution |
| 115 | + rgrass7::execGRASS(cmd = "g.region", raster = "DEM", res = 90) |
| 116 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'DEM90m = DEM') |
| 117 | + rgrass7::execGRASS(cmd = "g.region", raster = "DEM") #(reset the region back to 30m) |
| 118 | + rgrass7::execGRASS(cmd = "r.topidx", input = "DEM", output = "lna") |
| 119 | + rgrass7::execGRASS(cmd = "r.fillnulls", input = "lna", output = "lna.fill") |
| 120 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'lna.int = round(lna.fill)') |
| 121 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'riparian = lna.int > 7.5') |
| 122 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = paste0('p.rip30.up90 = (riparian == 1) * (DEM *', hmax,') + (riparian < 1)* (DEM90m * 633) + ', hillslope_name)) |
| 123 | + rgrass7::execGRASS(cmd = "r.clump", input = "p.rip30.up90", output = "p.30_90res.cl") |
| 124 | + out_maps = c(out_maps, "p.30_90res.cl") |
| 125 | + |
| 126 | + } else if (patch == "aspatial_wetness") { |
| 127 | + # ALSO DIDNT TEST THIS ONE |
| 128 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = paste0('p.lna = (lna.int * ', hmax,') + ', hillslope_name)) |
| 129 | + out_maps = c(out_maps, "p.lna") |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + # Roads - zero map |
| 134 | + rgrass7::execGRASS(cmd = "r.mapcalc", expression = 'zero = ("DEM" > 0)*0') |
| 135 | + |
| 136 | + if (define_watershed) { |
| 137 | + # ADDING AN INTERACTIVE GUI MAP HERE WOULD ALLOW FOR PRETTY SIMPLE BASIN DELINEATION |
| 138 | + rgrass7::execGRASS(cmd = "r.water.outlet", input = "drainage", output = "basin",coordinates = c(easting, northing)) |
| 139 | + rgrass7::execGRASS(cmd = "d.rast", "basin") |
| 140 | + rgrass7::execGRASS(cmd = "g.copy", raster = "basin", 'MASK') |
| 141 | + } |
| 142 | + |
| 143 | + # Write rasters |
| 144 | + out_names = paste0(name,"_",out_maps,".tif") |
| 145 | + for (i in 1:length(out_maps)) { |
| 146 | + rgrass7::execGRASS(cmd = "r.out.gdal", input = out_maps[i], output = out_names[i], format = "GTiff", type = "Float64") |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | +} |
| 151 | + |
0 commit comments