En este documento se ejemplifica el uso de lal función thin de la paquetería spThin
#Cargando archivos y librerías
library(pacman)
p_load(raster, spthin, sf, spThin, mapview)
setwd('D:\\GR\\spThin')#directorio raíz
po <- read_sf("D:\\GR\\spThin\\random_po3.shp")
## Simple feature collection with 6 features and 6 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 703503.6 ymin: 2075312 xmax: 789469.7 ymax: 2101615
## Projected CRS: WGS 84 / UTM zone 15N
## # A tibble: 6 × 7
## id X Y spp LON LAT geometry
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <POINT [m]>
## 1 0 754661. 2075312. P. onca -90.6 18.8 (754661.5 2075312)
## 2 5 748320. 2100262. P. onca -90.6 19.0 (748320.2 2100262)
## 3 7 770136. 2087654. P. onca -90.4 18.9 (770136.2 2087654)
## 4 8 789470. 2080819. P. onca -90.3 18.8 (789469.7 2080819)
## 5 9 703504. 2101615. P. onca -91.1 19.0 (703503.6 2101615)
## 6 10 755701. 2083226. P. onca -90.6 18.8 (755701.1 2083226)
mapview(po)
po_df <- as.data.frame(po)
head(po_df) #observar qe ya no existe la columna "geometry"
## id X Y spp LON LAT geometry
## 1 0 754661.5 2075312 P. onca -90.58451 18.75379 POINT (754661.5 2075312)
## 2 5 748320.2 2100262 P. onca -90.64147 18.97986 POINT (748320.2 2100262)
## 3 7 770136.2 2087654 P. onca -90.43615 18.86328 POINT (770136.2 2087654)
## 4 8 789469.7 2080819 P. onca -90.25381 18.79896 POINT (789469.7 2080819)
## 5 9 703503.6 2101615 P. onca -91.06680 18.99701 POINT (703503.6 2101615)
## 6 10 755701.1 2083226 P. onca -90.57364 18.82512 POINT (755701.1 2083226)
thin_po <-
thin( po_df, #base de datos a procesar
lat.col = "LAT", long.col = "LON",
spec.col = "spp", #columnas de la base de datos (se puede discretizar por especie)
thin.par = 10, reps = 100, #thin.par: distancia mínima entre puntos en km. reps = repeticiones del proceso de adelgazamiento.
locs.thinned.list.return = F, #si está en TRUE se genera una lista de cada repetición realizada.
write.files = TRUE, #en TRUE genera archivos con extensión csv.
max.files = 5, #número máximo de archivos generados por cada iteración.
out.dir = getwd(), out.base = "Ponca_thinned") #directorio dónde se guardará el archivo de salida, out.base = nombre base de los archivos.
## **********************************************
## Beginning Spatial Thinning.
## Script Started at: Thu Apr 4 15:45:17 2024
## lat.long.thin.count
## 30 31 32 33 34
## 4 13 50 28 5
## [1] "Maximum number of records after thinning: 34"
## [1] "Number of data.frames with max records: 5"
## [1] "Writing new *.csv files"
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", :
## Writing new *.csv files to output directory: D:/GR/spThin
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin1_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin1_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin1_new_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin1_new_new_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin1_new_new_new_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## [1] "Writing file: D:/GR/spThin/Ponca_thinned_thin1_new_new_new_new_new.csv"
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin2_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin2_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## [1] "Writing file: D:/GR/spThin/Ponca_thinned_thin2_new_new.csv"
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin3_new.csv ' already exists. Renaming file
## to avoid overwriting.
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin3_new_new.csv ' already exists. Renaming file
## to avoid overwriting.
## [1] "Writing file: D:/GR/spThin/Ponca_thinned_thin3_new_new.csv"
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin4_new.csv ' already exists. Renaming file
## to avoid overwriting.
## [1] "Writing file: D:/GR/spThin/Ponca_thinned_thin4_new.csv"
## Warning in thin(po_df, lat.col = "LAT", long.col = "LON", spec.col = "spp", : D:/GR/spThin/Ponca_thinned_thin5_new.csv ' already exists. Renaming file
## to avoid overwriting.
## [1] "Writing file: D:/GR/spThin/Ponca_thinned_thin5_new.csv"
#write.log.file = TRUE, #escribir archivo del proceso iterativo
#log.file = "Ponca_full_log_file.txt" )#nombre del archivo resumen de las iteraciones
#1.Leer el archivo .csv de salida obtenido en el chunck anterior.
po.csv <- read.csv("D:\\GR\\spThin\\Ponca_thinned_thin1.csv")
head(po.csv)
## spp LON LAT
## 1 P. onca -90.74277 18.80947
## 2 P. onca -90.32411 18.94404
## 3 P. onca -90.32230 18.75813
## 4 P. onca -90.89619 18.93556
## 5 P. onca -90.34176 19.04191
## 6 P. onca -90.60102 18.99923
po.spatial <- st_as_sf(po.csv, coords= c('LON', 'LAT'), crs = 4326) #hay que usar las columnas con las coordenadas que ya existen en la base de datos, en este caso se llaman "LON" y "LAT", el número 4326 es el código EPSG del sistema de referencia de coordenadas de WGS84 como datum y como sistema de referencia de coordenadas. Usar este crs también al momento de aplicar la función thin().
`
mapview(po.spatial)