Mercurial > repos > ecology > srs_pca
annotate functions.r @ 1:21e21363a8b4 draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit 0f331043178bbfbe5cda0e31d887971464b3071c
| author | ecology | 
|---|---|
| date | Fri, 07 Apr 2023 14:38:58 +0000 | 
| parents | 0931c98270c9 | 
| children | 
| rev | line source | 
|---|---|
| 
0
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
1 #Rscript | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
2 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
3 ######################### | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
4 ## Functions ## | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
5 ######################### | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
6 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
7 #####Packages : raster | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
8 # sp | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
9 # ggplot2 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
10 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
11 ####Set paramaters for all tools using BiodivMapR | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
12 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
13 # path for the Mask raster corresponding to image to process | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
14 # expected to be in ENVI HDR format, 1 band, integer 8bits | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
15 # expected values in the raster: 0 = masked, 1 = selected | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
16 # set to FALSE if no mask available | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
17 input_mask_file <- FALSE | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
18 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
19 # relative or absolute path for the Directory where results will be stored | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
20 # For each image processed, a subdirectory will be created after its name | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
21 output_dir <- "RESULTS" | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
22 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
23 # SPATIAL RESOLUTION | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
24 # resolution of spatial units for alpha and beta diversity maps (in pixels), relative to original image | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
25 # if Res.Map = 10 for images with 10 m spatial resolution, then spatial units will be 10 pixels x 10m = 100m x 100m surfaces | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
26 # rule of thumb: spatial units between 0.25 and 4 ha usually match with ground data | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
27 # too small window_size results in low number of pixels per spatial unit, hence limited range of variation of diversity in the image | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
28 window_size <- 10 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
29 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
30 # PCA FILTERING: Set to TRUE if you want second filtering based on PCA outliers to be processed. Slower | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
31 filterpca <- TRUE | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
32 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
33 ################################################################################ | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
34 ## DEFINE PARAMETERS FOR METHOD ## | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
35 ################################################################################ | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
36 nbcpu <- 4 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
37 maxram <- 0.5 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
38 nbclusters <- 50 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
39 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
40 ################################################################################ | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
41 ## PROCESS IMAGE ## | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
42 ################################################################################ | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
43 # 1- Filter data in order to discard non vegetated / shaded / cloudy pixels | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
44 ndvi_thresh <- 0.5 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
45 blue_thresh <- 500 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
46 nir_thresh <- 1500 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
47 continuum_removal <- TRUE | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
48 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
49 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
50 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
51 #### Convert raster to dataframe | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
52 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
53 # Convert raster to SpatialPointsDataFrame | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
54 convert_raster <- function(data_raster) { | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
55 r_pts <- raster::rasterToPoints(data_raster, spatial = TRUE) | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
56 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
57 # reproject sp object | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
58 geo_prj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
59 r_pts <- sp::spTransform(r_pts, sp::CRS(geo_prj)) | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
60 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
61 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
62 # Assign coordinates to @data slot, display first 6 rows of data.frame | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
63 r_pts@data <- data.frame(r_pts@data, longitude = sp::coordinates(r_pts)[, 1], | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
64 latitude = sp::coordinates(r_pts)[, 2]) | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
65 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
66 return(r_pts@data) | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
67 } | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
68 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
69 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
70 #### Potting indices | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
71 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
72 plot_indices <- function(data, titre) { | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
73 graph_indices <- ggplot2::ggplot(data) + | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
74 ggplot2::geom_point(ggplot2::aes_string(x = data[, 2], y = data[, 3], color = data[, titre]), shape = "square", size = 2) + ggplot2::scale_colour_gradient(low = "blue", high = "orange", na.value = "grey50") + | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
75 ggplot2::xlab("Longitude") + ggplot2::ylab("Latitude") + | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
76 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust = 1), plot.title = ggplot2::element_text(color = "black", size = 12, face = "bold")) + ggplot2::ggtitle(titre) | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
77 | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
78 ggplot2::ggsave(paste0(titre, ".png"), graph_indices, width = 12, height = 10, units = "cm") | 
| 
 
0931c98270c9
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
79 } | 
