Code
source("../dsan-globals/_globals.r")
PPOL 6805 / DSAN 6750: GIS for Spatial Data Science
Fall 2024
source("../dsan-globals/_globals.r")
library(rnaturalearth)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(mapview)
library(leaflet.extras2)
Loading required package: leaflet
<- ne_countries(continent = "Africa", scale = 50) |> select(iso_a3, geounit)
africa_sf <- mapview(africa_sf, label="geounit", legend=FALSE)
africa_map <- 10
N <- sf::st_union(africa_sf)
africa_union_sf <- sf::st_sample(africa_union_sf, N) |> sf::st_sf() |> mutate(temp = runif(N, 0, 100))
sampled_points_sf <- mapview(sampled_points_sf, label="Random Point", col.regions=cbPalette[1], legend=FALSE)
sampled_points_map <- africa_sf[sampled_points_sf,]
countries_points_sf <- mapview(countries_points_sf, label="geounit", legend=FALSE) + sampled_points_map
filtered_map + sampled_points_map) | filtered_map (africa_map
POINT
s are not merged into data attributes of POLYGON
sPOINT
Attributes
st_geometry(sampled_points_sf) <- c("geom")
|> head() sampled_points_sf
geom | temp |
---|---|
POINT (-14.78459 21.8602) | 9.566285 |
POINT (19.22184 -7.860099) | 76.046094 |
POINT (17.56247 19.14144) | 70.000416 |
POINT (3.869355 27.05118) | 11.345776 |
POINT (34.59731 7.619234) | 7.186874 |
POINT (23.00621 8.547459) | 69.730564 |
POLYGON
Attributes
|> head(4) countries_points_sf
iso_a3 | geounit | geometry | |
---|---|---|---|
92 | NER | Niger | MULTIPOLYGON (((13.60635 13… |
103 | MOZ | Mozambique | MULTIPOLYGON (((31.28789 -2… |
104 | MAR | Morocco | MULTIPOLYGON (((-2.219629 3… |
112 | MRT | Mauritania | MULTIPOLYGON (((-16.37334 1… |
st_join()
<- countries_points_sf |> st_join(sampled_points_sf)
joined_sf |> head() joined_sf
iso_a3 | geounit | temp | geometry | |
---|---|---|---|---|
92 | NER | Niger | 79.456693 | MULTIPOLYGON (((13.60635 13… |
103 | MOZ | Mozambique | 52.658794 | MULTIPOLYGON (((31.28789 -2… |
104 | MAR | Morocco | 9.566285 | MULTIPOLYGON (((-2.219629 3… |
112 | MRT | Mauritania | 43.462255 | MULTIPOLYGON (((-16.37334 1… |
172 | ETH | Ethiopia | 7.186874 | MULTIPOLYGON (((35.26836 5…. |
192 | COD | Democratic Republic of the Congo | 76.046094 | MULTIPOLYGON (((30.75117 -8… |
<- st_make_grid(st_bbox(st_as_sfc("LINESTRING(0 0,1 1)")), n = c(2,2))
g par(mar = rep(0,4))
plot(g)
plot(g[1] * diag(c(3/4, 1)) + c(0.25, 0.125), add = TRUE, lty = 2)
text(c(.2, .8, .2, .8), c(.2, .2, .8, .8), c(1,2,4,8), col = 'red')
Assume the extensive attribute \(Y\) is uniformly distributed over a space \(S_i\) (e.g., for population counts we assume everyone is evenly-spaced across the region)
We first compute \(Y_{ij}\), derived from \(Y_i\) for a sub-area of \(S_i\), \(A_{ij} = S_i \cap T_j\):
\[ \hat{Y}_{ij}(A_{ij}) = \frac{|A_{ij}|}{|S_i|}Y_i(S_i) \]
where \(|\cdot|\) denotes area.
Then we can compute \(Y_j(T_j)\) by summing all the elements over area \(T_j\):
\[ \hat{Y}_j(T_j) = \sum_{i=1}^{p}\frac{|A_{ij}|}{|S_i|}Y_i(S_i) \]
\[ \hat{Y}_{ij} = Y_i(S_i) \]
\[ \hat{Y}_j(T_j) = \sum_{i=1}^{p}\frac{|A_{ij}|}{|T_j|}Y_j(S_i) \]
Introducing the spdep
library!