Skip to content

Instantly share code, notes, and snippets.

View rCarto's full-sized avatar
🛠️
...

Timothée Giraud rCarto

🛠️
...
View GitHub Profile
#' Aggregate sf objects
#'
#' Geometries and attributes are aggregated.
#'
#' @param x sf object
#' @param by name of the variable of grouping elements
#' @param var name(s) of the variable(s) to aggregate
#' @param fun function(s) to compute the summary statistics
#'
#' @return An sf object is returned
orthomap <- function(lon, lat) {
  g <- as_s2_geography(TRUE)
  co <- s2_data_countries()
  oc <- s2_difference(g, s2_union_agg(co)) # oceans
  co <- s2_difference(co, s2_union_agg(oc)) # land
  
  # visible half
  b <- s2_buffer_cells(as_s2_geography(paste0("POINT(", lon, " ", lat,")")),
                       distance = 9800000)
@rCarto
rCarto / maptiles.md
Last active December 12, 2023 09:42
# Your Stadia API key:
apikey = "xxxxxxxxxxxxxxxxxxxxxxx"

# Define a bounding box
library(sf)
bb <- st_bbox(obj = c(xmin = 24.73592, xmax = 24.75185, 
                      ymin = 59.4340, ymax = 59.4393), 
              crs = "EPSG:4326")
@rCarto
rCarto / mtq.R
Last active August 21, 2023 19:06
library(raster)
library(cartography)
library(sf)
library(SpatialPosition)
mtq <- st_read(system.file("shape/martinique.shp", package="cartography"))
# use WGS84 proj
mtq_latlon <- st_transform(mtq, 4326)
# this call throw an error but seems to work...
getData('SRTM', lon=-61, lat=14)
@rCarto
rCarto / zoom.R
Last active March 3, 2023 22:04
# load libraries
library(sf)
library(cartography)
library(rnaturalearth)
# import world countries and coastlines from Natural Earth
country <- ne_countries(scale = 50, returnclass = "sf",
type="sovereignty")
coastline <- ne_coastline(scale = 50, returnclass = "sf")
library(sf)
library(cartography)
library(osrm)
# set osrm to my own server
options(osrm.server = "http://address.of.my.server/", osrm.profile = "driving")
# destination point (useR2019 conference in Toulouse)
dst <- data.frame(id="dst", x = 1.4344, y = 43.6113)
dst <- st_as_sf(dst, coords = c('x','y'), crs = 4326)
@rCarto
rCarto / cartomix.R
Last active July 13, 2022 02:19
Script to build the cartomix figure
library(cartography)
library(sp)
library(sf)
# Load data
data(nuts2006)
# Save image
sizes <- getFigDim(x = nuts0.spdf, width = 700, mar = c(0,0,0,0), res = 100)
png('./img/map8.png', width = sizes[1], height = sizes[2], res = 100)
library(rnaturalearth)
library(mapsf)
library(fisheye)
ct <- ne_download(scale = 50, returnclass = "sf")
oc <- ne_download(type = "ocean", category = "physical",
scale = 50, returnclass = "sf")
x <- st_transform(ct, "+proj=natearth2")
z <- st_transform(oc, "+proj=natearth2")
library(mapsf)
#> Le chargement a nécessité le package : sf
#> Linking to GEOS 3.7.1, GDAL 3.1.2, PROJ 7.1.0
mtq <- mf_get_mtq()
mf_theme("nevermind")
mf_map(mtq, graticule = st_crs(4326), 
       axes = TRUE, 
       lon = seq(-60,-62, by=-.2), 
 lat = seq(14, 15, by = .2))
library(sf)
library(osrm)
library(maptiles)
# build a bbox for Paris
bb <- st_bbox(c(xmin = 643069, ymin = 6857478,
xmax = 661079, ymax = 6867081),
crs = 2154)
# get map tiles
osm <- get_tiles(x = bb, provider = "CartoDB.PositronNoLabels",
crop = TRUE, zoom = 13)