Skip to content

Instantly share code, notes, and snippets.

View johnbaums's full-sized avatar

John johnbaums

  • University of Melbourne
  • Melbourne
View GitHub Profile
@hadley
hadley / gist:837414
Created February 21, 2011 17:50
which-encoding.r
all <- iconvlist()
names(all) <- all
tries <- llply(all[330:350], function(cur) iconv(song$artist, cur, "UTF-8"))
vapply(tries, function(x) sum(is.na(x)), integer(1))
@andrewharvey
andrewharvey / README.md
Created September 29, 2012 02:34
Geoscience Australia - Australian Bathymetry and Topography Grid

About

This package contains a set of tools, utilities and stylesheets for working with the Geoscience Australia Australian Bathymetry and Topography Grid.

Here is a sample rendering of this Geoscience Australia Australian Bathymetry and Topography Grid using the process documented in this repository.

Australian Bathymetry and Topography Grid

License

@wkmor1
wkmor1 / rasterToKML.r
Last active December 24, 2015 09:08
convert raster objects or raster readable files to .kml overlay files
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()
@bearloga
bearloga / Parallel RJags with Progress Monitoring.R
Last active December 27, 2015 04:58
par.trace.samples() runs independent MCMC chains in parallel on a multicore/multiCPU system. It breaks up a single long run into several smaller chunks and that allows it to report the simulation's progress. At the end of the run, all the chunks are combined into a single mcmc object. The mcmc objects from the chains are then combined into a sin…
# 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
@lptorres
lptorres / multi2single.py
Created December 4, 2013 01:55
A simple script that converts shapefiles with multipart polygons into singlepart polygons, and copies the fields of the source shapefile. This script is based on a script by Paolo Corti, found here: https://gist.github.com/anonymous/735330 The original script only converts multipolygons by creating a new feature for each part of a multipolygon. …
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
@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`
#'
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)) {
@brodieG
brodieG / sample0110.R
Last active August 29, 2015 14:23
Code for SO Q: Random sample of character vector, without elements prefixing one another
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
@brodieG
brodieG / sample0110b.R
Last active April 14, 2017 15:04
blind sample implementation variation
# 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
@oscarperpinan
oscarperpinan / inset.R
Created August 30, 2016 07:24
Inset graphics with rasterVis and grid
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,