library("rstac")
library("stars")
catalog = stac("https://earth-search.aws.element84.com/v1")
collection = "sentinel-2-l2a"
bbox = c(16, 52, 17, 53) # xmin, ymin, xmax, ymax (WGS84)
datetime = "2023-01-01T00:00:00Z/2023-12-31T00:00:00Z" # RFC 3339
catalog |>
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
| # Original source: https://custom-scripts.sentinel-hub.com/sentinel-2/l2a_optimized/ | |
| enhanceRGB = function(x, maxR = 3, midR = 0.13, sat = 1.2, gamma = 1.8, | |
| cores = 1) { | |
| ## functions ----------------------------------------------------------------- | |
| sAdj = function(a) { | |
| adjGamma(adj(a, midR, 1, maxR)) | |
| } |
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(leaflet) | |
| library(data.table) | |
| ## Morocco buildings (1.21 GB) | |
| url = "https://data.humdata.org/dataset/c6059279-4521-4b39-8b18-d43aedc012c3/resource/6af2ee44-1807-4fb7-a647-a42fc7ffbff0/download/open_buildings_v3_morocco_epicenter.csv.gz" | |
| df = fread(url, nThread = 4, header = TRUE, sep = ",") | |
| df = st_as_sf(df, crs = "EPSG:4326", wkt = "geometry") # convert to spatial object | |
| write_sf(df, "morocco.gpkg") # save as geopackage |
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
| ## This function is faster alternative to `rbind()` in the {sf} package. | |
| ## Basically, it uses `collapse::unlist2d()` to combine rows. | |
| ## See also: https://github.com/r-spatial/sf/issues/798 | |
| fastrbindsf = function(x, check = FALSE) { | |
| if (length(x) == 0) stop("Empty list") | |
| if (isTRUE(check)) { | |
| ref_crs = sf::st_crs(x[[1]]) | |
| ref_colnames = colnames(x[[1]]) | |
| for (i in seq_len(length(x))) { |
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") | |
| file = system.file("shape/nc.shp", package = "sf") | |
| shp = read_sf(file) | |
| ## write zipped shapefile | |
| write_sf(shp, "myshapefile.shp.zip", driver = "ESRI Shapefile") | |
| ## read zipped shapefile | |
| read_sf("myshapefile.shp.zip") |
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("readr") | |
| library("geojsonsf") | |
| ## download all roads for Europe | |
| data = "https://usaminedroads.blob.core.windows.net/road-detections/Europe-Full.zip" | |
| options(timeout = 6000) | |
| download.file(data, "Europe-Full.zip") | |
| unzip("Europe-Full.zip") |
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("tidyr") | |
| library("ggplot2") | |
| library("cranlogs") | |
| packages = c("sf", "stars", "terra", "raster", "exactextractr", | |
| "rnaturalearth", "tidycensus", "s2") | |
| df2021 = cran_downloads(packages, from = "2021-01-01", to = "2021-12-31") | |
| df2022 = cran_downloads(packages, from = "2022-01-01", to = "2022-12-31") | |
| ## remove outliers |
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("stars") | |
| library("gganimate") | |
| library("ggnewscale") | |
| tif = system.file("tif/L7_ETMs.tif", package = "stars") | |
| bands = paste0("B", c(1:5, 7)) | |
| r = read_stars(tif) | |
| r = st_set_dimensions(r, 3, values = bands, names = "band") | |
| r_bbox = st_as_sfc(st_bbox(r)) |
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("geojsonsf") | |
| options(timeout = 600) | |
| url = "https://minedbuildings.z5.web.core.windows.net/global-buildings/dataset-links.csv" | |
| links = read.csv(url) | |
| # select sample data (Bangkok) | |
| bangkok = subset(links, | |
| Location == "Thailand" & | |
| QuadKey %in% c(132203132, 132203133, 132203310, 132203311)) |
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("terra") | |
| n = c(10, 50, 100, 150, 200) | |
| t = matrix(NA, length(n), 3) | |
| for (i in seq_along(n)) { | |
| r = rast(xmin = 0, xmax = n[i], ymin = 0, ymax = n[i], ncol = n[i], nrow = n[i], | |
| crs = "epsg:3857") # plannar CRS |