Skip to content

Instantly share code, notes, and snippets.

View mdsumner's full-sized avatar

Michael Sumner mdsumner

  • Integrated Digital East Antarctica, Australian Antarctic Division
  • Hobart, Australia
View GitHub Profile
## we only need data.frame setOldClass to get S4 off the case 
 library(sp)
setOldClass( c("tbl_df", "tbl", "data.frame" ) )
 library(dplyr)
 d <- data_frame(x = 1:2, y = 2:3, z = 5:6)
 coordinates(d) <- c("x", "y")
 
 ## yay
library(spbabel) ## devtools::install_github("mdsumner/spbabel")
library(rworldmap)
data(countriesLow)
## summarize, just for plotting by this one grouping attribute
spplot(countriesLow  %>% group_by(continent)  %>% summarize())
## identical to spplot(countriesLow, "continent")

## but we can do more: 
@mdsumner
mdsumner / voyagepopup.r
Created May 3, 2016 03:56
Obtain local map png files to show in mapview:::popupLocalImage along a voyage track
library(mapview)
library(dismo)
library(trip)
library(rgdal)
library(raster)
## get a small data frame with a voyage track of the Aurora Australis
## https://github.com/AustralianAntarcticDivision/raadtools/blob/master/R/raadtools.R
afile <- "testing/aurora.rda"
Creation and read from GeoPackage vector and raster in R.
```R
library(rgdal); library(maptools)
data(wrld_simpl)
writeOGR(wrld_simpl, dsn = "wrld_simpl.gpkg", layer = "wrld_simpl", driver = "GPKG")
library(dplyr)
library(RSQLite)
(db <- src_sqlite("wrld_simpl.gpkg"))

Fix 2016-05-06

svn checkout svn://scm.r-forge.r-project.org/svnroot/rgdal/pkg mv pkg rgdal R CMD build rgdal --no-build-vignettes --no-manual sudo R CMD INSTALL rgdal_1.1-9.tar.gz

R

#' Split a table on attribute groups.
#'
#' This function will return two tables in a list from one, keeping all columns in one table or other. Use d_cols to only keep some of the remaining columns.
#'
#' https://en.wikipedia.org/wiki/Database_normalization
#' @param data
#' @param ... attributes to separate from the rest as "groups"
#'
#' @return
#' @export
library(rgdal)
library(rworldmap)
data(countriesLow)
writeOGR(countriesLow, "mapdata.gpkg", "countriesLow", driver = "GPKG")
library(dplyr)
db <- src_sqlite("mapdata.gpkg")
tab <- tbl(db, "countriesLow")
tab %>% filter(SOVEREIGNT == "Australia") %>% select(geom)

TAS Lidar 5m mosaic obtained from here:

http://www.ga.gov.au/metadata-gateway/metadata/record/gcat_89644

Beware! This is 28Gb of uncompressed ADF joy, it consists of multiple internal .adf files inside the 22be4b55-2465-4320-e053-10a3070a5236_TAS folder.

GDAL can sort this out.

cd C:\data\22be4b55-2465-4320-e053-10a3070a5236_TAS>
@mdsumner
mdsumner / Lidar5.md
Last active July 27, 2016 05:31
GA 5m LIDAR in MGA50 and non-sparse

The GA 5m LIDAR, downloaded for Tassie is in MGA50 (centred on longitude 117, presumably propagated from the western extent).

Here I have two files in a gist, a .md (Markdown doc) that records the shell process to download, uncompress and convert the ADF with GDAL to compressed, tiled and sparse GeoTIFF.

There are two "funny things",

  1. The ADF is stored explicitly and hence massive (~29Gb), but the GeoTIFF is much smaller (reflecting the sparse coastal coverage).
  2. The projection UTM, MGA50 (centred on longitude 117).

(The .r (R) script is just to read the result, crop a section and plot it - otherwise unrelated to this.)

library(sp)
library(raster)
## data from rgeos::createSPComment
p1 <- Polygon(cbind(x=c(0, 0, 10, 10, 0), y=c(0, 10, 10, 0, 0)), hole=FALSE) # I
p2 <- Polygon(cbind(x=c(3, 3, 7, 7, 3), y=c(3, 7, 7, 3, 3)), hole=TRUE) # H
p8 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(1, 2, 2, 1, 1)), hole=TRUE) # H
p9 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(5, 6, 6, 5, 5)), hole=TRUE) # H
p3 <- Polygon(cbind(x=c(20, 20, 30, 30, 20), y=c(20, 30, 30, 20, 20)),
hole=FALSE) # I