Skip to content

Instantly share code, notes, and snippets.

@lbusett
lbusett / lb_tricks.R
Last active August 3, 2017 13:23
Various "R" tricks
# String manipulation
### Print an integer number with trailing zeroes
number = 1
sprintf("%03i", number)
### use "easier" patterns with wildcards for regexp
glob2rx('*.tif')
From:
https://stackoverflow.com/questions/34416892/how-to-add-a-scale-bar-in-ggplot-map
library(maps)
library(maptools)
library(ggplot2)
library(grid)
#
@lbusett
lbusett / lint_exclude
Created November 28, 2017 18:01
exclude one lintr
lint_package(".",
linters = with_defaults(
object_camel_case_linter = NULL,
object_snake_case_linter
)
)
@lbusett
lbusett / points_in_distance_parallel.R
Last active September 23, 2020 15:51
function for finding all points within X meters of a point
points_in_distance_parallel <- function(in_pts,
maxdist,
ncuts = 10) {
require(doParallel)
require(foreach)
require(data.table)
require(sf)
# convert points to data.table and create a unique identifier
pts <- data.table(in_pts)
@lbusett
lbusett / bibtex_2academic.R
Created March 13, 2018 18:56
script for importing publications from a "bibtex" file to a hugo-academic website
#' @title bibtex_2academic
#' @description import publications from a bibtex file to a hugo-academic website
#' @author Lorenzo Busetto, phD (2017) <lbusett@gmail.com>
bibtex_2academic <- function(bibfile,
outfold,
abstract = FALSE,
overwrite = FALSE) {
require(RefManageR)
ref_dir <- system.file("extdata/example_files/out_ref", package = "sen2r")
testthat::test_that(
"Tests on custom mask on TOA with smoothing and buffering, with save binary mask", {
outdir_6 <- file.path(tempdir(), "out_test6")
dir.create(dirname(outdir_6), showWarnings = FALSE)
exp_outpath_6 <- file.path(outdir_6, "TOA", "S2A1C_20170703_022_Barbellino_TOA_10.tif")
exp_outpath_msk <- file.path(outdir_6, "MSK", "S2A1C_20170703_022_Barbellino_MSK_10.tif")
unlink(exp_outpath_6)
@lbusett
lbusett / scrip_convert_kml
Created February 19, 2020 13:57
scrip_convert_kml
#install.packages("gdalUtilities")
library(gdalUtilities)
in_folder <- "/home/lb/nr_data/fire_cci/reference/Africa/Landsat8/Luini/178066/kml_lorenzo/"
out_folder <- file.path(in_folder, "shape_conversion")
dir.create(out_folder)
kml_files <- list.files(in_folder, pattern = "kml$", full.names = TRUE)
for(ff in kml_files) {
print(ff)
out_file <- file.path(out_folder,
# funzioni necessarie
find_UTM_zone <- function(longitude, latitude) {
# Special zones for Svalbard and Norway
if (latitude >= 72.0 && latitude < 84.0 )
if (longitude >= 0.0 && longitude < 9.0)
return(31);
if (longitude >= 9.0 && longitude < 21.0)
return(33)
@lbusett
lbusett / fix_kml.R
Last active July 15, 2020 16:54
Fix a kml exported from GEE so that holes are respected
fix_kml <- function(infile, outfile) {
intext <- readLines(con <- file(infile))
out <- list()
ll = 1
indout <- 1
# cycle on lines of input
while(ll <= length(intext)){
curline <- intext[ll]
# If a "hole block" is starting, check if there is only