Skip to content

Instantly share code, notes, and snippets.

View obrl-soil's full-sized avatar

obrl_soil obrl-soil

View GitHub Profile
# faffing about with methods to approximate a "lag vertex polygon" (Poore, 1986)
# or "best-fit regular geometric feature" (Stoddard, 1965) for polygon shape
# metric
# References:
# Poore 1986 - MSc Thesis on soil map quantification at
# https://ir.library.oregonstate.edu/downloads/zk51vk04q
# Stoddart, D. (1965). The shape of atolls. Marine Geology, 3(5), 369–383.
# doi:10.1016/0025-3227(65)90025-3 (its on scihub)
# also apparently Fridland 1972 "The pattern of the soil cover" but that's
# find the nearest vertex on a polygon to a point
# re: https://twitter.com/EmmaVitz/status/1095574327855001600
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# pick a random state:
nc1 <- nc[sample(seq(nrow(nc)), 1), ]
# always project to a plane before using geos funs!
@obrl-soil
obrl-soil / sample_to_the_south.R
Created January 9, 2019 21:41
For sampling in a buffer but only keeping points south of centre
# from https://twitter.com/RallidaeRule/status/1083102769169514497
library(sf)
options(stringsAsFactors = FALSE)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
pts <- st_centroid(nc[1:3, ])
pts <- pts[ , c('NAME')]
pts <- st_transform(pts, 32617)
buff <- st_buffer(pts, 10000)
library(sf)
options(stringsAsFactors = FALSE)
# 94720 points -
# src: http://qldspatial.information.qld.gov.au/catalogue/custom/search.page?q=SALI_SITE
many_points <- st_read('D:/Spatial_data/soils/soil_survey_sites.shp') %>%
st_transform(., crs = 3577) # always dist planar, kids
# divide in 2
many_points$nrow <- as.numeric(1:nrow(many_points))
# 2018-07-03
# An example of doing some slightly fancy stuff with GDAL on the command line
# from R using OSGeo4W GDAL, which is on system PATH already
# Task: rescale all input covariate rasters to the last BM run to [0, 1], see
# if it makes any difference to model outputs (spoiler: no)
library(raster)
library(tidyverse)
options(stringsAsFactors = FALSE)
# method: raster stack to uniquely attributed polygons
# using GRASS 7.4 to avoid rasterToPolygons
library(sp)
library(sf)
library(raster)
library(rgrass7)
library(dsmartr) # just for the demo dataset
# https://github.com/obrl-soil/dsmartr/blob/master/data/heronvale_covariates.rda
data("heronvale_covariates")
# Inset method based on https://gis.stackexchange.com/questions/222799/create-an-inset-map-in-r/222877#222877
# libraries
library(sf)
library(raster)
library(tidyverse)
library(rosm)
library(ggspatial)
library(ggsn)
library(rmapshaper)
library(sf)
library(tidyverse)
setwd('C:/data/prepair_test')
# a file with known problems, also at https://github.com/obrl-soil/bits-n-pieces/tree/master/r2p
tester <- read_sf('C:/data/r2p/cat_v_pretty.gpkg')
# I used the QGIS topology checker tool to find invalid geometries. Row 130 is no good:
plot(st_geometry(tester[130, ]))
@obrl-soil
obrl-soil / ggspatial_projected_demo.R
Last active June 20, 2017 05:57
ggplot with projected coords
library(sp)
library(sf)
library(tidyverse)
library(rosm)
library(ggspatial)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ncsp <- as(nc, 'Spatial')
# only for sf < 0.5 I think...?
library(tidyverse)
setwd('C:/DATA')
options(stringsAsFactors = FALSE)
# fake data
write.csv(tribble( ~col1, ~col2, ~col3,
1, 'a', T,
2, 'b', NA,
3, 'c', F),
file = file.path(getwd(), 'csv1.csv'),