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
# 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! |
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
# 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) |
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(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)) |
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
# 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) |
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
# 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") |
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
# 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) |
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(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, ])) |
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(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...? |
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(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'), |
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
# documenting some file.path quirks on windows | |
# gdal translate is used below to copy a file. its on the system PATH, enabling easy access via system2 | |
infile <- 'C:/Data/test.tif' | |
> setwd('C:/Data/space\\ test') | |
Error in setwd("C:/Data/space\\ test") : cannot change working directory | |
> setwd('"C:/Data/space test"') | |
Error in setwd("\"C:/Data/space test\"") : | |
cannot change working directory | |
> setwd('C:/Data/space test') |