Skip to content

Commit d195589

Browse files
committed
add get_points.R
1 parent 50cb5bc commit d195589

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

analysis/P01_get_points.R

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Read the metadata and get the coordinates of the field observation
2+
# The input file is originally at :
3+
# https://docs.google.com/spreadsheets/d/1Lz-IBQAPd8RykPj57Nf1Tutb_fQlXg4fG461i29BYIM
4+
# Creates one files
5+
# all_points.gpkg : shapefile with coordinates per year and per dataset
6+
7+
# load the needed package and functions
8+
devtools::load_all()
9+
library(terra)
10+
# make sure no one is logged in from Google Account
11+
googlesheets4::gs4_deauth()
12+
13+
# B.1 Load GPS coordinates
14+
url0 <- "https://docs.google.com/spreadsheets/d/1Lz-IBQAPd8RykPj57Nf1Tutb_fQlXg4fG461i29BYIM/"
15+
16+
gis <- googlesheets4::read_sheet(url0, sheet = 2, skip = 2, col_types = "c", )
17+
18+
keepC <- c("Study_ID", "Site", "Year", "X", "Y")
19+
gis <- gis[, keepC]
20+
21+
# correct study_id name
22+
gis$Study_ID <- gsub("SEBIOPAG _BVD", "SEBIOPAG_BVD", gis$Study_ID)
23+
24+
25+
# B.2 Clean the messy coordinates
26+
gis$X <- as.numeric(gsub(",", ".", gis$X))
27+
gis$Y <- as.numeric(gsub(",", ".", gis$Y))
28+
29+
# invert latitude / longitude in projects that wrongly entered coordinates
30+
# table(gis$X>40, gis$Study_ID)
31+
inv_coo <- c(
32+
"SEBIOPAG_VcG",
33+
"OSCAR",
34+
"LepiBats",
35+
"MUESLI",
36+
"SEBIOPAG_Plaine de Dijon",
37+
"SEBIOPAG_BVD",
38+
"DURUM_MIX_GM",
39+
"FRAMEwork_BVD",
40+
"PestiRed"
41+
)
42+
gis$longitude <- ifelse(gis$Study_ID %in% inv_coo, unlist(gis$Y), unlist(gis$X))
43+
gis$latitude <- ifelse(gis$Study_ID %in% inv_coo, gis$X, gis$Y)
44+
45+
# issue some are not in WGS84, but in EPSG 2154 (LAMB93)
46+
proj <- ifelse(gis$longitude > 180, "LAMB93", "WGS84")
47+
48+
# transform LAMB93 to WGS84
49+
lamb93 <- gis[proj %in% "LAMB93", c("longitude", "latitude")]
50+
shp_2154 <- st_as_sf(lamb93, coords = c("longitude", "latitude"), crs = 2154)
51+
shp_4326 <- st_transform(shp_5698, crs = 4326)
52+
coo_4326 <- st_coordinates(shp_4326)
53+
gis[proj %in% "LAMB93", c("longitude", "latitude")] <- coo_4326
54+
# plot(gis[, c("longitude", "latitude")])
55+
56+
# B.3 Add lines per site and year
57+
58+
# clean years
59+
gis$Year <- gsub(" à ", ",", gis$Year)
60+
gis$Year <- gsub("-", ",", gis$Year)
61+
gis$Year <- gsub(" ", "", gis$Year)
62+
63+
# make a new data.frame with lines per year
64+
splityears <- strsplit(gis$Year, ",")
65+
nyears <- sapply(splityears, length)
66+
gis_year <- data.frame(
67+
"Study_ID" = rep(gis$Study_ID, nyears),
68+
"Site" = rep(gis$Site, nyears),
69+
"Year" = unlist(splityears),
70+
"longitude" = rep(gis$longitude, nyears),
71+
"latitude" = rep(gis$latitude, nyears)
72+
)
73+
74+
# B.4 Formating the output
75+
# remove missing coordinates
76+
gis_year <- gis_year[complete.cases(gis_year), ]
77+
78+
# Remove PestiRed for now (CH)
79+
gis_year <- gis_year[gis_year$Study_ID != "PestiRed", ]
80+
81+
# remove duplicates
82+
gis_year <- gis_year[!duplicated(gis_year), ]
83+
84+
# order
85+
gis_year <- gis_year[order(gis_year$Study_ID, gis_year$Site, gis_year$Site), ]
86+
dim(gis_year) # 1560, 5
87+
# head(gis_year)
88+
shp <- st_as_sf(gis_year, coords = c("longitude", "latitude"), crs = 4326)
89+
# check visualization
90+
mapview::mapview(shp, zcol = "Study_ID")
91+
92+
93+
# B5. Export shapefile
94+
st_write(
95+
shp,
96+
here::here("data", "raw-data", "fields_FR.gpkg"),
97+
append = FALSE
98+
)

0 commit comments

Comments
 (0)