Skip to content

Instantly share code, notes, and snippets.

View edzer's full-sized avatar

Edzer Pebesma edzer

View GitHub Profile
# (C) 2019, Edzer Pebesma, CC-BY-SA
set.seed(1331)
library(stars)
library(colorspace)
tif = system.file("tif/L7_ETMs.tif", package = "stars")
r = read_stars(tif)
nrow = 5
ncol = 8
#m = matrix(runif(nrow * ncol), nrow = nrow, ncol = ncol)
see https://github.com/r-spatial/sf/blob/master/demo/twitter.R
https://twitter.com/allison_horst/status/1071456081308614656
@edzer
edzer / gist:c91541972ccd862b543467e8f59c26c3
Created February 16, 2018 15:08
floating forest with stars & sf
# from: https://github.com/Irosenthal/Floating_Forests/blob/master/scripts/demo_of_merge/demo_merge.R
# see https://twitter.com/jebyrnes/status/963279659868749825
# alt: indicates a stars+sf workflow alternative to sp+raster
###########
# Libraries
###########
library(sp)
library(rgdal)
library(rgeos)
require("sf")
require("dplyr")
require("hexbin")
# Linux libertine font "sf", converted to path with Inkscape,
# added points between existing points 2 times, then turned all segments into straight lines.
# Saved as SVG with absolute coordinates (Preferences > SVG Output > Path Data).
# Loaded coords from SVG source code, remove letters from start and end, and replace " " with ","
coords_f <- c(218.1169,163.46992,215.56952,177.96334,213.51976,189.84421,211.82546,200.33884,210.34442,210.67351,208.24728,226.35176,205.51032,243.54066,201.92029,259.27223,197.26391,270.57846,195.45112,272.90665,193.28288,274.70167,190.97247,275.85687,188.73314,276.26564,187.03291,276.03164,185.79476,275.38887,184.84097,274.42619,183.99382,273.23248,182.45947,271.13533,180.24976,269.10927,177.54243,267.58084,174.51519,266.97658,171.25987,267.58973,169.08867,269.18036,167.87718,271.37526,167.501,273.8012,168.44294,277.0032,171.48203,279.79643,176.93817,281.77214,185.13126,282.52154,191.01986,281.80176,196.83737,279.60686,202.29944,
library(openeo) # make the openEO api functions available to the user
eo = openeo_connect("http://.../api/v0", user = "edzer", passwd = "****") # auth
# R6 object with reference semantics / encapsulated OOP
eo$datasets() # list/describe data sets available:
# Sentinel2A-L1C
# Proba-V
# ...
@edzer
edzer / gist:381dac079ddcd5174be31209675b3822
Created October 21, 2017 09:21
stars -> sf for affine raster
library(stars)
library(sf)
geomatrix = system.file("tif/geomatrix.tif", package = "stars")
x = st_stars(geomatrix)
st_dimensions(x)
## from to offset delta geotransform
## x 1 20 1841002 1.5 1841000, 1.5, -5, 1144000, -5, -1.5
## y 1 20 1144003 -1.5 1841000, 1.5, -5, 1144000, -5, -1.5
## refsys point values
## x +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs TRUE NULL
> system.time(for(i in 1:100000){a <- st_as_sfc("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")})
user system elapsed
11.228 0.000 11.231
> system.time(for(i in 1:100000){a <- wkt_coords("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")})
user system elapsed
33.340 0.000 33.349
st_cast0 <- function(x, to, ...) UseMethod("st_cast0")
# see this figure:
# https://cloud.githubusercontent.com/assets/520851/21387553/5f1edcaa-c778-11e6-92d0-2d735e4c8e40.png
# columns start counting at 0:
which_col = function(cls) {
switch(cls,
POINT = 0,
LINESTRING = 1,