Skip to content

Instantly share code, notes, and snippets.

View tim-salabim's full-sized avatar
💭
not much

tim-salabim

💭
not much
View GitHub Profile
library(leaflet)
library(mapview)
spatial_merge <- gadmCHE
m1 <- leaflet() %>%
addTiles() %>%
addPolygons(data = spatial_merge,
layerId = spatial_merge@data$OBJECTID,
stroke= TRUE, fillOpacity = 0.5, smoothFactor = 0.5, weight=1,
@tim-salabim
tim-salabim / mapview_big
Last active October 25, 2016 11:32
view and query millions of points colored by a certain variable
#devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")
library(mapview)
library(sp)
library(raster)
library(ggplot2)
### blow diamonds up a bit (change number of repeats - 20 - to blow up more or less)
big <- data.frame(diamonds[rep(seq_len(nrow(diamonds)), 20),])
big$cut <- as.character(big$cut)
# devtools::install_github("rstudio/leaflet", force = TRUE)
library(leaflet)
library(sf)
### polygons --------------------------------------------------------------
poldata <- st_as_sf(mapview::gadmCHE)
class(poldata)
class(st_geometry(poldata))
class(st_geometry(poldata)[[1]])
@tim-salabim
tim-salabim / franconia.R
Last active March 30, 2017 05:47
franconia.rda creation
library(sf)
library(mapview)
library(foreign)
tst <- read_sf("software/data/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.shp")
db <- read.dbf("software/data/NUTS_2013_01M_SH/data/NUTS_AT_2013.dbf")
tst_db <- merge(tst, db, by = "NUTS_ID")
tst_db5 <- tst_db[nchar(as.character(tst_db$NUTS_ID)) == 5, ]
@tim-salabim
tim-salabim / st_explode
Last active May 23, 2017 07:51
st_explode
library(sf)
library(mapview)
library(data.table)
lns = st_cast(trails, "LINESTRING")
# lns = lns[rep(seq_len(nrow(lns)), 1000),]
pts = st_cast(st_line_sample(lns, n = 1), "POINT")
st_explode = function(x) {
if (!inherits(st_geometry(x), "sfc_LINESTRING"))
@tim-salabim
tim-salabim / xyView
Last active September 5, 2017 19:14
Produce scatterplots with mapview and sf
library(mapview)
library(sf)
xyGrid = function(x) {
# x = iris_sf
xrange = mapview:::extendLimits(c(st_bbox(x)[["xmin"]], st_bbox(x)[["xmax"]]))
yrange = mapview:::extendLimits(c(st_bbox(x)[["ymin"]], st_bbox(x)[["ymax"]]))
xticks = pretty(xrange)
@tim-salabim
tim-salabim / add minichart to layers control
Created September 8, 2017 05:48
add minichart to layers control
library(mapview)
library(leaflet.minicharts)
library(sf)
val1 = sample.int(10, nrow(franconia), replace = TRUE)
val2 = round(val1 * runif(nrow(franconia), 0.3, 1), 0)
mat = cbind(val1, val2)
pal = c("darkred", "lightgreen")
@tim-salabim
tim-salabim / sf_sf_list_column.R
Last active October 17, 2017 21:21
sf with sf list column
library(mapview)
library(sf)
## create polygon index column
franconia$index = sprintf("%02.f", 1:nrow(franconia))
## join polygons to points
brew_franc = st_join(breweries, franconia[, c("index", "geometry")])
## split on polygon entity
@tim-salabim
tim-salabim / perpPoints.cpp
Last active October 28, 2017 11:10
perp points in the making
#include <Rcpp.h>
#include <math.h>
using namespace Rcpp;
// helper functions to convert degrees to radians and vice versa =============
// [[Rcpp::export]]
double deg(double radians) {
return 180 * radians / M_PI;
}
@tim-salabim
tim-salabim / perp_points.R
Last active October 28, 2017 11:56
perp points R version
## helper functions to convert degrees to radians and vice versa =============
deg_r = function(radians) {
180 * radians / pi
}
rad_r = function(degrees) {
degrees * pi / 180
}