|
| 1 | +conflictRules("n2khab", exclude = c("read_schemes", "read_scheme_types")) |
| 2 | +library(dplyr) |
| 3 | +library(tidyr) |
| 4 | +library(stringr) |
| 5 | +library(sf) |
| 6 | +library(n2khab) |
| 7 | +library(n2khabmon) |
| 8 | +library(googledrive) |
| 9 | + |
| 10 | +# Setup for googledrive authentication. Set the appropriate env vars in |
| 11 | +# .Renviron and make sure you ran drive_auth() interactively with these settings |
| 12 | +# for the first run (or to renew an expired Oauth token). |
| 13 | +# See ?gargle::gargle_options for more information. |
| 14 | +if (Sys.getenv("GARGLE_OAUTH_EMAIL") != "") { |
| 15 | + options(gargle_oauth_email = Sys.getenv("GARGLE_OAUTH_EMAIL")) |
| 16 | +} |
| 17 | +if (Sys.getenv("GARGLE_OAUTH_CACHE") != "") { |
| 18 | + options(gargle_oauth_cache = Sys.getenv("GARGLE_OAUTH_CACHE")) |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +# IDs and geometries of the units of aquatic types are defined by below code |
| 23 | +# - for watersurface types: wsh_pol |
| 24 | +# - for type 7220, partim rivulets: habspring_units_aquatic |
| 25 | +# - for type 3260: segm_3260 |
| 26 | +# ============================================================================= |
| 27 | + |
| 28 | +# Manually check data source versions (something to be automated by n2khab |
| 29 | +# package in the future, based on preset versions) |
| 30 | +# - watersurfaces_hab: version watersurfaces_hab_v6 |
| 31 | +# - habitatstreams: version habitatstreams_2023 |
| 32 | +# - habitatsprings: version habitatsprings_2020v2 |
| 33 | +# - flanders: version "flanders_2018-05-16" |
| 34 | +file.path( |
| 35 | + locate_n2khab_data(), |
| 36 | + c( |
| 37 | + "20_processed/watersurfaces_hab", |
| 38 | + "10_raw/habitatsprings", |
| 39 | + "10_raw/habitatstreams", |
| 40 | + "10_raw/flanders" |
| 41 | + ) |
| 42 | +) %>% |
| 43 | + list.files(full.names = TRUE) %>% |
| 44 | + xxh64sum() %>% |
| 45 | + .[sort(names(.))] %>% |
| 46 | + identical(c( |
| 47 | + flanders.dbf = "d21a599325723682", |
| 48 | + flanders.prj = "2f10404ffd869596", |
| 49 | + flanders.shp = "72fff53084b356be", |
| 50 | + flanders.shx = "1880e141bbcdc6ca", |
| 51 | + habitatsprings.geojson = "7268c26f52fcefe4", |
| 52 | + habitatstreams.dbf = "dee7a620e3bcae0a", |
| 53 | + habitatstreams.lyr = "a120f92d80c92a3a", |
| 54 | + habitatstreams.prj = "7e64ff1751a50937", |
| 55 | + habitatstreams.shp = "5a7d7cddcc52c5df", |
| 56 | + habitatstreams.shx = "b2087e6affe744f4", |
| 57 | + habitatstreams.sld = "2f192b84b4df99e9", |
| 58 | + watersurfaces_hab.gpkg = "e2920c4932008387" |
| 59 | + )) %>% |
| 60 | + stopifnot() |
| 61 | + |
| 62 | +# Generating wsh_pol (unit ID defined by polygon_id) |
| 63 | +n2khab_targetpops <- |
| 64 | + read_scheme_types() %>% |
| 65 | + select(scheme, type) |
| 66 | +n2khab_types <- |
| 67 | + n2khab_targetpops %>% |
| 68 | + distinct(type) %>% |
| 69 | + arrange(type) |
| 70 | +wsh <- read_watersurfaces_hab(interpreted = TRUE) |
| 71 | +wsh_occ <- |
| 72 | + wsh$watersurfaces_types %>% |
| 73 | + # in general we restrict types using an expanded type list tailored to the |
| 74 | + # type levels present in data sources, but for the aquatic types expansion and |
| 75 | + # subsequent collapse of types are redundant steps |
| 76 | + semi_join(n2khab_types, join_by(type)) |
| 77 | +wsh_pol <- |
| 78 | + wsh$watersurfaces_polygons %>% |
| 79 | + semi_join(wsh_occ, join_by(polygon_id)) %>% |
| 80 | + select(polygon_id) |
| 81 | +wsh_pol |
| 82 | + |
| 83 | +# Temporary approach to generate segm_3260 (i.e. it will miss a part and some |
| 84 | +# may be false positives) |
| 85 | +# (unit ID defined by unit_id) |
| 86 | +habstream <- read_habitatstreams() |
| 87 | +segm_3260 <- |
| 88 | + read_watercourse_100mseg(element = "lines")[habstream, ] %>% |
| 89 | + unite(unit_id, vhag_code, rank) |
| 90 | +segm_3260 |
| 91 | + |
| 92 | +# Generating habspring_units_aquatic (unit ID defined by unit_id) |
| 93 | +flanders_buffer <- |
| 94 | + read_admin_areas(dsn = "flanders") %>% |
| 95 | + st_buffer(40) |
| 96 | +habspring_units_aquatic <- |
| 97 | + # following function will be adapted to support the latest version of the data |
| 98 | + # source (just released); for now use version habitatsprings_2020v2 |
| 99 | + read_habitatsprings(units_7220 = TRUE) %>% |
| 100 | + .[flanders_buffer, ] %>% |
| 101 | + filter(system_type != "mire") |
| 102 | +habspring_units_aquatic |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +# Define the unit IDs per sample support for aquatic strata in groundwater |
| 107 | +# schemes of the considered MNE modules |
| 108 | +# ============================================================================= |
| 109 | + |
| 110 | +# Download and load a few R objects from the POC (these would currently take too |
| 111 | +# much code to regenerate here) |
| 112 | +path <- file.path(tempdir(), "objects_for_aq_piezometers_panfl_pan5.RData") |
| 113 | +drive_download(as_id("1Z93w-C3XRQ8756W3835JPfxggGEstjKR"), path = path) |
| 114 | +env_extradata <- new.env() |
| 115 | +load(path, envir = env_extradata) |
| 116 | +ls(envir = env_extradata) |
| 117 | + |
| 118 | +# Below object can be used to filter the foregoing geospatial objects, taking |
| 119 | +# into account that: |
| 120 | +# - rows with sample_support_code 'watersurface' relate to the IDs in wsh_pol |
| 121 | +# - rows with sample_support_code 'watercourse_segment' relate to the IDs in |
| 122 | +# segm_3260 |
| 123 | +# - rows with sample_support_code 'spring' relate to the IDs in |
| 124 | +# habspring_units_aquatic |
| 125 | +stratum_units_grts_aquatic_gw_spsamples_spares <- |
| 126 | + # units per stratum: |
| 127 | + get("stratum_units_non_cell_n2khab", envir = env_extradata) %>% |
| 128 | + # joining GRTS address per unit. A few non-unique GRTS addresses exist, hence |
| 129 | + # 'many-to-one'. See further. |
| 130 | + inner_join( |
| 131 | + get("units_non_cell_n2khab_grts", envir = env_extradata), |
| 132 | + join_by(sample_support_code, unit_id), |
| 133 | + relationship = "many-to-one", |
| 134 | + unmatched = "error" |
| 135 | + ) %>% |
| 136 | + filter( |
| 137 | + # other 'non-cell' types exist so these must be dropped: |
| 138 | + sample_support_code %in% c( |
| 139 | + "watersurface", |
| 140 | + "watercourse_segment", |
| 141 | + "spring" |
| 142 | + ), |
| 143 | + # terrestrial spring units must also be excluded: |
| 144 | + unit_id %in% habspring_units_aquatic$unit_id | |
| 145 | + sample_support_code != "spring" |
| 146 | + ) %>% |
| 147 | + rename(grts_address_final = grts_address) %>% |
| 148 | + # join with samples ('sample_status' defines whether location is 'in the |
| 149 | + # sample' or is a 'spare unit' (spare units = a bunch of 'next' GRTS addresses |
| 150 | + # in the available GRTS series for a stratum)) |
| 151 | + inner_join( |
| 152 | + get( |
| 153 | + "scheme_moco_ps_stratum_sppost_spsamples_spares_sf", |
| 154 | + envir = env_extradata |
| 155 | + ) %>% |
| 156 | + st_drop_geometry() %>% |
| 157 | + # only use the samples of groundwater schemes |
| 158 | + filter(str_detect(scheme, "^GW")) %>% |
| 159 | + rename(grts_address_drawn = grts_address) %>% |
| 160 | + # collapse module combos and schemes; hereby select the 'prior' |
| 161 | + # sample_status ("in_sample") across module combos and schemes: |
| 162 | + summarize( |
| 163 | + sample_status = sample_status %>% droplevels() %>% levels() %>% first(), |
| 164 | + .by = c( |
| 165 | + stratum, |
| 166 | + grts_address_drawn, |
| 167 | + grts_address_final |
| 168 | + ) |
| 169 | + ) %>% |
| 170 | + mutate(sample_status = factor(sample_status)), |
| 171 | + join_by(stratum, grts_address_final), |
| 172 | + # A few non-unique GRTS addresses exist, hence 'many-to-one'. We will apply |
| 173 | + # a quick-fix below to meet the requirement of 'one unit sampled per GRTS |
| 174 | + # address', but at least the selection will need further alignment with the |
| 175 | + # (likewise) MHQ solution (to be continued). |
| 176 | + relationship = "many-to-one", |
| 177 | + unmatched = "drop" |
| 178 | + ) %>% |
| 179 | + arrange(sample_support_code, stratum, grts_address_drawn, unit_id) %>% |
| 180 | + # for now, de-duplicate units with the same GRTS address by selecting the 1st |
| 181 | + slice(1, .by = c(stratum, sample_support_code, grts_address_final)) %>% |
| 182 | + select(-grts_address_drawn) |
| 183 | +stratum_units_grts_aquatic_gw_spsamples_spares |
| 184 | + |
| 185 | +# Checking that all retained unit IDs from the samples are represented in the |
| 186 | +# geospatial objects |
| 187 | +stratum_units_grts_aquatic_gw_spsamples_spares %>% |
| 188 | + filter(sample_support_code == "watersurface") %>% |
| 189 | + {all(.$unit_id %in% wsh_pol$polygon_id)} %>% |
| 190 | + stopifnot() |
| 191 | + |
| 192 | +stratum_units_grts_aquatic_gw_spsamples_spares %>% |
| 193 | + filter(sample_support_code == "watercourse_segment") %>% |
| 194 | + {all(.$unit_id %in% segm_3260$unit_id)} %>% |
| 195 | + stopifnot() |
| 196 | + |
| 197 | +stratum_units_grts_aquatic_gw_spsamples_spares %>% |
| 198 | + filter(sample_support_code == "spring") %>% |
| 199 | + {all(.$unit_id %in% habspring_units_aquatic$unit_id)} %>% |
| 200 | + stopifnot() |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | +# Adding some possible inspiration (in Dutch) from a former selection of |
| 206 | +# Watina data for analysis |
| 207 | +# ============================================================================= |
| 208 | + |
| 209 | +# Voor aquatische objecten (excl. 7220) wordt een buffer van 30 meter gebruikt: |
| 210 | +# grondwater in de nabije omgeving wordt er beschouwd in relatie tot het |
| 211 | +# oppervlaktewater. Voor waterplassen wordt daarbij het watervlak zelf niet in |
| 212 | +# rekening gebracht: hier worden dus ringen rond de plas gecreëerd. Voor type |
| 213 | +# 7220 wordt een straal van 40 meter gebruikt omdat de locaties slechts als een |
| 214 | +# punt zijn gedigitaliseerd. |
0 commit comments