This file contains 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
get_namespaces <- function(file, as_imports=FALSE) { | |
require(stringi) | |
require(dplyr) | |
nm <- readLines(file) %>% | |
stringi::stri_extract_all(regex='[\\w_.]+::[\\w_.]+') %>% | |
unlist %>% | |
setdiff(NA) %>% | |
sort | |
This file contains 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
# Color palettes that actually look good from 1 .. 12 colors | |
# Taken from: https://blog.graphiq.com/finding-the-right-color-palettes-for-data-visualizations-fcd4e707a283 | |
graphiq <- function (n, option = 'warm') { | |
if (n < 1 || n > 12) stop('colors only defined for n = 1..12'); | |
if (option == 'warm') { | |
return(switch(n, | |
c("#FDB25F"), | |
c("#FFC96B", "#F47942"), | |
c("#FFC96B", "#F47942", "#AB412C"), | |
c("#FFD773", "#F99851", "#EF5833", "#923E2D"), |
This file contains 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) | |
library(rasterVis) | |
library(grid) | |
r <- raster(system.file("external/test.grd", package="raster")) | |
## Main graphic | |
p1 <- levelplot(r) | |
## Inset graphic | |
p2 <- levelplot(r, | |
margin = FALSE, |
This file contains 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
# http://stackoverflow.com/a/30781090/2725969 | |
sample0110b <- function(size, n) { | |
size <- as.integer(size) | |
n <- as.integer(n) | |
if(size > 25 || size < 3L) stop("Size out of valid range") | |
# Generate integer pool and weights |
This file contains 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
sample0110 <- function(size, n, complete.only=FALSE) { | |
size <- as.integer(size) | |
n <- as.integer(n) | |
if(size > 25 || size < 3L) stop( | |
"Currently size min is 3 and max is 25, though should be possible to allow ", | |
"smaller and larger with some changes" | |
) | |
# Generate integer pool and weights |
This file contains 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
polygonizer <- function(x, outshape=NULL, gdalformat = 'ESRI Shapefile', | |
pypath=NULL, readpoly=TRUE, quietish=TRUE) { | |
# x: an R Raster layer, or the file path to a raster file recognised by GDAL | |
# outshape: the path to the output shapefile (if NULL, a temporary file will be created) | |
# gdalformat: the desired OGR vector format | |
# pypath: the path to gdal_polygonize.py (if NULL, an attempt will be made to determine the location | |
# readpoly: should the polygon shapefile be read back into R, and returned by this function? (logical) | |
# quietish: should (some) messages be suppressed? (logical) | |
if (isTRUE(readpoly)) require(rgdal) | |
if (is.null(pypath)) { |
This file contains 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 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 os | |
from osgeo import gdal, ogr, osr | |
import sys | |
def initFields(in_lyr, out_lyr): | |
#Arbitrarily get the first feature | |
feat = in_lyr[0] | |
#loop over each field |
This file contains 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
# This file contains two functions: | |
# - combine.samples (used by par.trace.samples) | |
# - par.trace.samples | |
# and an example at the bottom. | |
## Author: Mikhail Popov (mikhail [at] mpopov.com) | |
# install.packages("rjags") # JAGS must be installed on system | |
# install.packages("doMC") # Unix only |
This file contains 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
rasterToKML <- function(x, out='Rgrid', png=FALSE, name=out, proj="+proj=longlat") { | |
require(raster) | |
require(maptools) | |
require(rgdal) | |
require(rgeos) | |
x <- raster(x) | |
SG <- as(x, 'SpatialGridDataFrame') | |
proj4string(SG) <- CRS(proj) | |
SG <- GE_SpatialGrid(SG) | |
if(png) png <- sprintf('%s.png', out) else png <- tempfile() |
NewerOlder