Skip to content

Commit 0d6e42c

Browse files
committed
update RPG calculation
1 parent 9279b4d commit 0d6e42c

8 files changed

Lines changed: 355 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ quarto::quarto_render("analysis/06_haie.qmd")
4545
quarto::quarto_render("analysis/07_fields_Toulouse.qmd")
4646
```
4747

48+
#### 8. exploration of the Swiss dataset
49+
50+
```r
51+
quarto::quarto_render("analysis/08_Swiss_data.qmd")
52+
```
53+
4854

4955
#### Mise à jour de l'index:
5056
```r

analysis/05_summary.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ indr <- read.csv(here("data", "derived-data", "raster_indicators.csv"))
6767
6868
# rpg and oso classes
6969
ref <- read.csv(here("data", "derived-data", "RPG-OSO_classes.csv"))
70+
ref_rpg <- read.csv(here("data", "raw-data", "RPG", "RPG_Explorer_Annexe2.csv"))
71+
7072
7173
# vector indicators
7274
indv <- read.csv(here("data", "derived-data", "vector_indicators.csv"))

analysis/06_haie.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,14 @@ mapview(
189189
```
190190

191191

192+
### IGN
192193

194+
```{r}
195+
library(happign)
196+
meta <- get_layers_metadata("wfs")
197+
meta <- get_layers_metadata("wmr")
198+
meta$Name[grep("haie", meta$Name)]
199+
```
193200

194201

195202
## Comparaison des indicateurs

analysis/F01_get_plot_CH.R

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Get all plots around the fields coordinates in a buffer
2+
# considering the year of the measurement
3+
4+
# For the Swiss dataset, we use data received from Selma Cadot
5+
# Run in ~1min
6+
7+
library(terra)
8+
library(sf)
9+
library(here)
10+
11+
# Load home made functions
12+
devtools::load_all()
13+
14+
# The data was split per year using S01_prep_data.R
15+
# data are available for the period 2019 - 2023
16+
period <- 2019:2023
17+
nutz_layer <- "nutz_XXXX.gpkg"
18+
labRPG <- c("id_parcel", "code_cultu", "code_group")
19+
colNUTZ <- c("nutzungsidentifikator", "nutzung_fr", "Hauptkategorie_FR")
20+
21+
buffer_fields <- c(500, 1000, 1500) #in m
22+
years <- 5 # for crop rotation
23+
24+
datafolder <- here("data", "raw-data")
25+
outfolder <- here("data", "derived-data")
26+
27+
# get the coordinates from the points
28+
# from shinyFunbiodiv/analysis/03_update_data.R
29+
df <- read.csv(here(datafolder, "coordinates_year_crop.csv"))
30+
pts <- vect(df, geom = c("Long", "Lat"), crs = "EPSG:4326")
31+
# project in EPSG 2056
32+
pts <- project(pts, "EPSG:2056")
33+
34+
# select only observation in Switzerland
35+
keep <- pts$Study_ID %in% "PestiRed" & !is.na(df$Lat)
36+
# table(keep) # 203 points
37+
# table(pts$Year[keep])
38+
39+
# table(nutz$Hauptkategorie_FR, useNA="ifany")
40+
rmCat <- c(
41+
"Forêt",
42+
"Haies, bosquets et berges boisées",
43+
"Surfaces en dehors de la SAU"
44+
)
45+
46+
nutz_out <- c() # save the nutz 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+
nutzi <- vect(
60+
file.path(datafolder, "CH", gsub("XXXX", pti$Year, nutz_layer)),
61+
ext = buffi
62+
)
63+
64+
# remove non agricultural fields
65+
nutzi <- nutzi[!nutzi$Hauptkategorie_FR %in% rmCat, ]
66+
# continue only if some fields in nutz
67+
if (nrow(nutzi) > 0) {
68+
# calculate geometrical characteristics
69+
nutzi$Perim_m <- perim(nutzi)
70+
nutzi$Area_ha <- expanse(nutzi) * 0.0001
71+
72+
##
73+
# ponctual information on the site
74+
cat(".")
75+
rei <- terra::relate(nutzi, pti, "intersects")
76+
77+
if (sum(rei) > 0) {
78+
exi <- nutzi[which(rei)[1], ]
79+
# add funbiodiv ID
80+
exi$Funbiodiv_ID <- pti$ID
81+
# keep in nutz_out
82+
nutz_out <- c(nutz_out, exi)
83+
out_i <- data.frame(
84+
data.frame(pti),
85+
data.frame(exi)[, c(colNUTZ, "Perim_m", "Area_ha")]
86+
)
87+
} else {
88+
out_i <- data.frame(
89+
data.frame(pti),
90+
matrix(NA, ncol = length(colNUTZ) + 2)
91+
)
92+
#fmt: skip
93+
names(out_i)[(ncol(pti)+1):ncol(out_i)] <- c(colNUTZ, "Perim_m", "Area_ha")
94+
}
95+
96+
##
97+
# average field size in buffer
98+
cat(".")
99+
for (f in buffer_fields) {
100+
lab <- paste0("Mean_fieldsize_", f, "m_ha")
101+
buf_pts <- buffer(pti, f)
102+
nutz_buf <- relate(nutzi, buf_pts, "intersects")
103+
out_i[, lab] <- mean(nutzi$Area_ha[nutz_buf], na.rm = TRUE)
104+
}
105+
106+
##
107+
# crop rotation
108+
cat(".")
109+
if (sum(rei) > 0) {
110+
nutz_time <- exi[, colNUTZ]
111+
nutz_time$Year <- pti$Year
112+
} else {
113+
nutz_time <- vect()
114+
}
115+
for (y in 1:years) {
116+
out_i[paste0("N-", y)] <- pti$Year - y
117+
lab <- paste0(colNUTZ, "_N-", y)
118+
out_i[, lab] <- NA
119+
if ((pti$Year - y) %in% period) {
120+
nutzy <- vect(
121+
file.path(datafolder, "CH", gsub("XXXX", pti$Year - y, nutz_layer)),
122+
ext = pti
123+
)
124+
125+
if (nrow(nutzy) > 0) {
126+
rey <- relate(nutzy, pti, "intersects")
127+
if (sum(rey) > 1) {
128+
nutzy <- nutzi[which(rey)[1], ]
129+
# add information in out_i
130+
out_i[, lab] <- data.frame(nutzy)[, colNUTZ]
131+
# keep it for save
132+
nutzy$Year <- pti$Year - y
133+
nutz_time <- rbind(nutz_time, nutzy[c(colNUTZ, "Year")])
134+
}
135+
}
136+
}
137+
}
138+
df_out <- rbind(df_out, out_i)
139+
}
140+
}
141+
142+
# export indicators
143+
write.csv(df_out, file.path(outfolder, "metrics_nutz.csv"), row.names = FALSE)
144+
145+
# save rpg fields
146+
nutz_all <- do.call(rbind, nutz_out)
147+
writeVector(nutz_all, file.path(outfolder, "nutz_fields.gpkg"))

analysis/F01_get_plot_FR.R

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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"))

analysis/P01_get_points.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ st_write(
9898
)
9999

100100
## Get classes of RPG and OSO
101-
rpg <- read.csv2("data/raw-data/RPG/REF_CULTURES_GROUPES_CULTURES_2023.csv", )
101+
# rpg <- read.csv2("data/raw-data/RPG/REF_CULTURES_GROUPES_CULTURES_2023.csv", )
102102

103-
rpg2 <- read.csv2("data/raw-data/RPG/REF_CULTURES_2023.csv")
104-
keep <- which(rpg2$CAMPAGNE_DEBUT > 2015 | rpg2$CAMPAGNE_FIN > 2015)
105-
rpg2$LIBELLE_CULTURE[keep][!rpg2$CODE[keep] %in% rpg$CODE_CULTURE]
106-
rpg$CODE_CULTURE %in% rpg2$CODE[keep]
103+
# rpg2 <- read.csv2("data/raw-data/RPG/REF_CULTURES_2023.csv")
104+
# keep <- which(rpg2$CAMPAGNE_DEBUT > 2015 | rpg2$CAMPAGNE_FIN > 2015)
105+
# rpg2$LIBELLE_CULTURE[keep][!rpg2$CODE[keep] %in% rpg$CODE_CULTURE]
106+
# rpg$CODE_CULTURE %in% rpg2$CODE[keep]

analysis/S01_prep_data.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library(terra)
2+
library(here)
3+
4+
dirfile <- here("data", "raw-data", "CH")
5+
file <- file.path(dirfile, "buffer_4000_lnf_hk_area_distance_nutz_60.gpkg")
6+
7+
# super heavy (2.4Gb)
8+
rpg_ch <- vect(file)
9+
10+
# split the file per year (400Mb)
11+
for (i in 2019:2023) {
12+
chi <- rpg_ch[rpg_ch$year == i, ]
13+
# could also select the relevant column to make the data smaller
14+
writeVector(chi, file.path(dirfile, paste0("nutz_", i, ".gpkg")))
15+
}
16+
17+
zoomin <- ext(c(2508265, 2509047, 1119836, 1121222)) + 2000
18+
v <- vect(file.path(dirfile, "nutz_2019.gpkg"), extent = zoomin)
19+
20+
mapview::mapview(v)

make.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
system.time({
2+
source("analysis/F01_get_plot_FR.R")
3+
})
4+
5+
system.time({
6+
source("analysis/F01_get_plot_CH.R")
7+
})

0 commit comments

Comments
 (0)