Skip to content

Instantly share code, notes, and snippets.

View agila5's full-sized avatar

Andrea Gilardi agila5

View GitHub Profile
@agila5
agila5 / st_covers.md
Last active April 7, 2025 08:26
Covers vs intersects
# Packages
library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE

# Let's work on the unit square
unit_square <- st_as_sfc(st_bbox(c(xmin = 0, xmax = 1, ymin = 0, ymax = 1)))

# Let's divide the unit square into 1600 (regular) cells
cells <- st_as_sf(st_make_grid(unit_square, cellsize = c(0.025, 0.025)))
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE

# Download global NDVI data which average the values from 2024-05-01 to 2024-05-10
options(timeout = 1200)
tmp_nc <- tempfile(fileext = ".nc")
download.file(
suppressPackageStartupMessages({library(spatstat)}) 

vertices <- ppp(
  c(0, 1, 2,  1, 1), 
  c(0, 1, 0, -1, 0),
  window = owin(c(-1, 3), c(-2, 2))
) 

#matrice di adiacenza 
# Sia X1, ..., Xn un campione casuale estratto da X ~ N(0, 1). Dalla teoria
# dell'inferenza statistica, sappiamo che lo stimatore
#
# (n - 1) S^2 / sigma ^ 2= 1 / sigma ^ 2 * sommatoria di (x_i - x_medio) ^ 2
#
# ha distribuzione Chi-quadrato con n - 1 gradi di liberta. Proviamo a
# verificare empiricamente questa affermazione.
m <- 1e4 # numero di simulazioni usate per approssimare la distribuzione
n <- 100 # numero di elementi nel campione
# get data
library(osmextract)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
#> Check the package website, https://docs.ropensci.org/osmextract/, for more details.
munich_multilines <- oe_get(
  place = "Muenches", 
  layer = "multilinestrings", 
  provider = "bbbike", 
 extra_tags = "route"
# packages
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(sfnetworks)
data_old <- tibble::tibble(
  "Location_Northing_OSGR" = c(1, 2, 3), 
  '1st_Road_Number' = c("a", "b", "c")
)
janitor::clean_names(data_old, replace = c("1" = "fir"))
#> # A tibble: 3 x 2
#>   location_northing_osgr first_road_number
#>                    <dbl> <chr>            
#&gt; 1 1 a 
# packages
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(osmextract)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
#> Check the package website, https://docs.ropensci.org/osmextract/, for more details.
# packages
library(spatstat)
#> Loading required package: spatstat.data
#> Loading required package: spatstat.geom
#> spatstat.geom 2.2-2
#> Loading required package: spatstat.core
#> Loading required package: nlme
#> Loading required package: rpart
#> spatstat.core 2.3-0
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter