|
| 1 | +# Get all plots around the fields coordinates in a buffer |
| 2 | +# considering the year of the measurement |
| 3 | + |
| 4 | +# For the French dataset, we will download the interesting part from the API directly |
| 5 | +# using happign package: https://paul-carteron.github.io/happign |
| 6 | +library(happign) |
| 7 | + |
| 8 | +# Run in ~2h |
| 9 | + |
| 10 | +library(terra) |
| 11 | +library(sf) |
| 12 | +library(here) |
| 13 | + |
| 14 | +# Load home made functions |
| 15 | +devtools::load_all() |
| 16 | + |
| 17 | +# check out which RPG layer is available |
| 18 | +# meta_vect <- get_layers_metadata("wfs") # all layers for altimetrie wms |
| 19 | +# look_up <- "RPG" |
| 20 | +# #fmt: skip |
| 21 | +# found <- grepl(tolower(look_up), tolower(meta_vect$Name)) | grepl(tolower(look_up), tolower(meta_vect$Name)) |
| 22 | +# meta_vect$Name[found] |
| 23 | +period <- 2015:2024 |
| 24 | +rpg_layer <- "RPG.XXXX:parcelles_graphiques" |
| 25 | +colRPG <- c("id_parcel", "code_cultu", "code_group") |
| 26 | + |
| 27 | +buffer_fields <- c(500, 1000, 1500) #in m |
| 28 | +years <- 5 # for crop rotation |
| 29 | + |
| 30 | +datafolder <- here("data", "raw-data") |
| 31 | +outfolder <- here("data", "derived-data") |
| 32 | + |
| 33 | +# remove jachere? J5M, J6P, J6S, JAC, JNO |
| 34 | +rmCat <- c("BFP", "BFS", "BOR", "BTA", "SNA") |
| 35 | + |
| 36 | +# get the coordinates from the points |
| 37 | +# from shinyFunbiodiv/analysis/03_update_data.R |
| 38 | +df <- read.csv(here(datafolder, "coordinates_year_crop.csv")) |
| 39 | +pts <- vect(df, geom = c("Long", "Lat"), crs = "EPSG:4326") |
| 40 | +# table(pts$Year) |
| 41 | + |
| 42 | +# select only observation in France between 2015 and 2024 |
| 43 | +keep <- pts$Year %in% period & !pts$Study_ID %in% "PestiRed" |
| 44 | +# table(keep) # 1692 points |
| 45 | + |
| 46 | +rpg_out <- c() # save the rpg information |
| 47 | +df_out <- c() # save the parameters |
| 48 | +# for testing: sample(which(keep), 10) |
| 49 | +for (i in which(keep)) { |
| 50 | + cat(i) |
| 51 | + |
| 52 | + # select point i |
| 53 | + pti <- pts[i] |
| 54 | + |
| 55 | + # create maximum buffer |
| 56 | + buffi <- buffer(pti, max(buffer_fields)) |
| 57 | + |
| 58 | + # get rpg around buffer |
| 59 | + rpgi <- get_wfs( |
| 60 | + x = st_as_sf(buffi), |
| 61 | + layer = gsub("XXXX", pti$Year, rpg_layer) |
| 62 | + ) |
| 63 | + # remove non agricultural fields |
| 64 | + rpgi <- rpgi[!rpgi$code_cultu %in% rmCat, ] |
| 65 | + |
| 66 | + # continue only if some fields in rpg |
| 67 | + if (nrow(rpgi) > 0) { |
| 68 | + # transform as terra SpatVect object |
| 69 | + rpgi <- vect(rpgi) |
| 70 | + |
| 71 | + # save (in case api doesn't work anymore) |
| 72 | + outi <- file.path(datafolder, "happign", paste0("RPGbuf_", pti$ID, ".gpkg")) |
| 73 | + writeVector(rpgi, outi, overwrite = TRUE) |
| 74 | + |
| 75 | + # calculate geometrical characteristics |
| 76 | + rpgi$Perim_m <- perim(rpgi) |
| 77 | + rpgi$Area_ha <- expanse(rpgi) * 0.0001 |
| 78 | + |
| 79 | + ## |
| 80 | + # ponctual information on the site |
| 81 | + cat(".") |
| 82 | + rei <- relate(rpgi, pti, "intersects") |
| 83 | + if (sum(rei) == 1) { |
| 84 | + exi <- rpgi[rei] |
| 85 | + # keep in rpg_out |
| 86 | + rpg_out <- c(rpg_out, exi) |
| 87 | + out_i <- data.frame( |
| 88 | + data.frame(pti), |
| 89 | + data.frame(exi)[, c(colRPG, "Perim_m", "Area_ha")] |
| 90 | + ) |
| 91 | + } else { |
| 92 | + out_i <- data.frame( |
| 93 | + data.frame(pti), |
| 94 | + matrix(NA, ncol = length(colRPG) + 2) |
| 95 | + ) |
| 96 | + #fmt: skip |
| 97 | + names(out_i)[(ncol(pti)+1):ncol(out_i)] <- c(colRPG, "Perim_m", "Area_ha") |
| 98 | + } |
| 99 | + |
| 100 | + ## |
| 101 | + # average field size in buffer |
| 102 | + cat(".") |
| 103 | + for (f in buffer_fields) { |
| 104 | + lab <- paste0("Mean_fieldsize_", f, "m_ha") |
| 105 | + buf_pts <- buffer(pti, f) |
| 106 | + rpg_buf <- relate(rpgi, buf_pts, "intersects") |
| 107 | + out_i[, lab] <- mean(rpgi$Area_ha[rpg_buf], na.rm = TRUE) |
| 108 | + } |
| 109 | + |
| 110 | + ## |
| 111 | + # crop rotation |
| 112 | + cat(".") |
| 113 | + if (sum(rei) == 1) { |
| 114 | + rpg_time <- exi[, colRPG] |
| 115 | + rpg_time$Year <- pti$Year |
| 116 | + } else { |
| 117 | + rpg_time <- vect() |
| 118 | + } |
| 119 | + for (y in 1:years) { |
| 120 | + out_i[paste0("N-", y)] <- pti$Year - y |
| 121 | + lab <- paste0(colRPG, "_N-", y) |
| 122 | + out_i[, lab] <- NA |
| 123 | + if ((pti$Year - y) %in% period) { |
| 124 | + rpgy <- get_wfs( |
| 125 | + x = st_as_sf(pti), |
| 126 | + layer = gsub("XXXX", pti$Year - y, rpg_layer) |
| 127 | + ) |
| 128 | + |
| 129 | + if (nrow(rpgy) > 0) { |
| 130 | + rpgy <- vect(rpgy) |
| 131 | + rey <- relate(rpgy, pti, "intersects") |
| 132 | + if (sum(rey) == 1) { |
| 133 | + rpgy <- rpgy[rey] |
| 134 | + # add information in out_i |
| 135 | + out_i[, lab] <- data.frame(rpgy)[, colRPG] |
| 136 | + # keep it for save |
| 137 | + rpgy$Year <- pti$Year - y |
| 138 | + rpg_time <- rbind(rpg_time, rpgy[c(colRPG, "Year")]) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + if (nrow(rpg_time) > 1) { |
| 144 | + outi <- file.path( |
| 145 | + datafolder, |
| 146 | + "happign", |
| 147 | + paste0("RPGrot_", pti$ID, ".gpkg") |
| 148 | + ) |
| 149 | + writeVector(rpg_time, outi, overwrite = TRUE) |
| 150 | + } |
| 151 | + |
| 152 | + df_out <- rbind(df_out, out_i) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +# export indicators |
| 157 | +write.csv(df_out, file.path(outfolder, "metrics_rpg.csv"), row.names = FALSE) |
| 158 | + |
| 159 | +# save rpg fields |
| 160 | +rpg_all <- do.call(rbind, rpg_out) |
| 161 | +writeVector(rpg_all, file.path(outfolder, "rpg_fields.gpkg")) |
0 commit comments