Skip to content

Instantly share code, notes, and snippets.

View defuneste's full-sized avatar
🌳

Olivier Leroy defuneste

🌳
View GitHub Profile

https://stat.ethz.ch/pipermail/r-sig-geo/2025-May/029541.html

Below we calculate individual distances along polygon edges, and sum up. This works here only because this is a single-ring polygon.

The numbers in play are

129.248935: the planar distance in the crs

129.285384: the "true" distance, to some insanely accurate level

@vgXhc
vgXhc / birth_marriage_counties.R
Created January 30, 2025 22:19
Map to compare percentage of married population and percent of people who gave birth in past 12 mo to US average
library(tidycensus)
library(tidyverse)
library(sf)
library(tmap)
birth <- get_acs(geography = "county", year = 2023, table = "B13002", geometry = TRUE,
summary_var = "B13002_001")
marriage <- get_acs(geography = "county", year = 2023, table = "B12001",
summary_var = "B12001_001")

List files and stream (non-spatial) table from within a tarball on CRAN.

(we need dev gdalraster for the full dir/vsitar listing capability, but reading from remote files or archives is available in many GDAL versions and existing supported GDAL packages on CRAN)

cransrc <- "https://cran.r-project.org/src/contrib" 
library(gdalraster)  ## for listing dirs recursively we need gh:USDAForestService/gdalraster for now
#> GDAL 3.10.0dev-449d5f09b7, released 2024/08/26, GEOS 3.12.2, PROJ 9.4.1

## list all R packages .tar.gz
@elipousson
elipousson / tidyverse-sf-notes.R
Last active December 28, 2024 20:18
Notes on sf compatibility with dplyr (and eventually other tidyverse) functions. Updated 2024-10-30.
#' Documentation of sf and tidyverse compatibility
#'
#' date-created: 2023-12-15
#' date-modified: 2024-10-30
# Notes to add to https://geocompr.r-universe.dev/articles/geocompkg/tidyverse-pitfalls.html
library(sf)
library(tidyverse)
nc = st_read(system.file("shape/nc.shp", package="sf"))

Here is some code to explore the variety available in calculating area across various packages.

x <- rnaturalearth::ne_countries(country = c("Greenland", "Spain"), returnclass = "sf")
as.numeric(sf::st_area(x))/1e6

## Greenland 2.166e6 km²  -41, 72
## Spain     505990 km²   -2, 40
@kadyb
kadyb / zipped_shapefile.R
Last active May 16, 2025 01:54
Directly load and save zipped shapefiles in {sf}
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")
@ThomasG77
ThomasG77 / base-pop-historiques-1876-2019.csv
Last active November 22, 2022 19:32
Base historique population INSEE rapportée aux géographies 2022
We can't make this file beautiful and searchable because it's too large.
CODE_2022,PMUN19,PMUN18,PMUN17,PMUN16,PMUN15,PMUN14,PMUN13,PMUN12,PMUN11,PMUN10,PMUN09,PMUN08,PMUN07,PMUN06,PSDC99,PSDC90,PSDC82,PSDC75,PSDC68,PSDC62,PTOT54,PTOT36,PTOT1931,PTOT1926,PTOT1921,PTOT1911,PTOT1906,PTOT1901,PTOT1896,PTOT1891,PTOT1886,PTOT1881,PTOT1876
01001,779,771,776,767,767,767,767,777,780,784,787,791,804,811,728,579,477,368,347,407,424,506,540,543,566,560,629,637,572,594,622,605,604
01002,256,253,248,243,241,239,236,235,234,221,207,194,195,198,168,159,136,106,88,103,157,203,219,253,256,291,323,357,391,421,486,490,484
01004,14134,14204,14035,14081,14127,14022,14359,14233,13839,13835,13350,12792,12696,12709,11436,10455,9737,9550,8949,7748,7159,6846,6794,5705,4796,4334,4100,4023,3548,3635,3618,3396,3427
01005,1751,1720,1689,1671,1619,1627,1635,1642,1600,1616,1592,1567,1544,1436,1408,1156,848,756,627,588,570,593,618,690,698,805,805,835,852,862,882,879,848
01006,112,112,111,110,109,109,108,110,112,116,120,123,125,120,86,76,65,82,108,107,111,141,135,161,150,159,193,200,206,200,200,213,191
01007,2800,