This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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` | |
#' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
say_x <- function(x) { | |
deparse(substitute(x)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from osgeo import gdal | |
if len(sys.argv) > 1: | |
ds = gdal.Open(sys.argv[1]) | |
print(ds.GetRasterBand(1).GetOverview(0).GetBlockSize()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', ...) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) |
OlderNewer