Skip to content

Instantly share code, notes, and snippets.

View etiennebr's full-sized avatar

Etienne B. Racine etiennebr

  • Intact Lab
  • Montreal
View GitHub Profile
@etiennebr
etiennebr / as.data.table.r
Last active November 13, 2023 11:24
Transform raster or terra object to data.table
#' Transform raster to data.table
#'
#' @param x Raster* object
#' @param row.names `NULL` or a character vector giving the row names for the data frame. Missing values are not allowed
#' @param optional logical. If `TRUE`, setting row names and converting column names (to syntactic names: see make.names) is optional
#' @param xy logical. If `TRUE`, also return the spatial coordinates
#' @param centroids logical. If TRUE return the centroids instead of all spatial coordinates (only relevant if xy=TRUE)
#' @param sepNA logical. If TRUE the parts of the spatial objects are separated by lines that are NA (only if xy=TRUE and, for polygons, if centroids=FALSE
#' @param ... Additional arguments (none) passed to `raster::as.data.frame`
#'
@etiennebr
etiennebr / say_x
Created April 11, 2014 00:37
How to find the name of a function passed to a function once inside a function
say_x <- function(x) {
deparse(substitute(x))
}
@etiennebr
etiennebr / cast_extent.r
Last active September 14, 2016 19:16
Transfrom raster extent to a data.frame or a geometry
#' Transform extent coordinates to data.frame
#' @param e Raster or Extent object
#' @param loop Should first and last coordinates be the same ? (defautl TRUE, required to create a polygon)
extent_as_data_frame_poly = function (e, loop = TRUE) {
e = extent(e)
e_poly = data.frame(x = c(e@xmin, e@xmax, e@xmax, e@xmin),
y = c(e@ymin, e@ymin, e@ymax, e@ymax))
if (loop) e_poly = rbind(e_poly, e_poly[1, ])
e_poly
}
@etiennebr
etiennebr / ovr_block_size.py
Last active August 29, 2015 14:04
Get overview block size from image
import sys
from osgeo import gdal
if len(sys.argv) > 1:
ds = gdal.Open(sys.argv[1])
print(ds.GetRasterBand(1).GetOverview(0).GetBlockSize())
@etiennebr
etiennebr / rasterize_extra.r
Last active February 8, 2018 20:13
rasterize using the extent of an object and no raster
library("raster")
setMethod("rasterize", signature(x="data.frame", y="missing"),
function(x, field, res = 1, fun = 'last', ...) {
r = raster(res = res, xmn = min(x$x), xmx = max(x$x), ymn = min(x$y), ymx = max(x$y))
rasterize(x, r, field = field, fun = fun, ...)
})
setMethod("rasterize", signature(x="SpatialPoints", y="missing"),
function(x, field, res = 1, fun = 'last', ...) {
@etiennebr
etiennebr / install_packages.r
Created February 1, 2015 19:42
Install required packages mentionned in a project
# list required packages
list_r_files = function() {
list.files(patt = "(?i)\\.(r|rmd)$", full = TRUE, recursive = TRUE)
}
regular_expression <- function(x, p) regmatches(x, regexec(p, x))
last = function(x) x[length(x)]
scan_packages = function(x) {
@etiennebr
etiennebr / rowcol-raster.sql
Last active February 26, 2016 20:46
Build a multiband raster from row-col data
DROP SCHEMA test CASCADE;
CREATE SCHEMA test;
CREATE TABLE test.measure AS
SELECT
r as rowy, c as colx, b as band,
round(random() * 100)::float as v
FROM
generate_series(1, 6) as r
CROSS JOIN LATERAL
@etiennebr
etiennebr / zipack.R
Last active April 1, 2016 16:55
R - Pack multiple files in a zip
#' Pack multiple files in a zip
#'
#' @export
#' @param fz Complete zip filename and path
#' @param fs Named List. Names are files names and content to be passed to FUN
#' @param FUN Function for writing to disc (defautl write.csv). Has to accept content as
#' first argument and file name as second. Other arguments are passed using ...
#' @param temp.dir Temporary directory to write files and zip. Is appened to file names
#' if not NULL.
#' @param flags A character string of flags to be passed to the command. Defaults is \code{-r9Xj}
@etiennebr
etiennebr / calendar.R
Last active April 11, 2016 13:14
Create calendar (can be used to plot calendars with ggplot2)
first_day_in_month = function(x) {
day(x) = 1
hour(x) = 0
minute(x) = 0
second(x) = 0
return(x)
}
first_day_in_year = function(x) {
month(x) = 1
#' Multivariate mutate
#' Mutate multiple columns
#'
#' @param .df A tbl
#' @param ... Name-value pairs of expressions that return one or more columns with 1 or nrow(.df) observations
#' @param .dots A list of formulas used to work around non-standard evaluation.
#' @export
#' @aliases mutatem_
#' @examples
#' df <- tibble(x=1:5, y=5:1)