Week 6: Spatial Joins and Areal-Weighted Interpolation

PPOL 6805 / DSAN 6750: GIS for Spatial Data Science
Fall 2025

Jeff Jacobs

jj1088@georgetown.edu

Wednesday, October 2, 2024

Doing Things with DE-9IM (Back to Binary Operations)

Almost a Spatial Join

Code
source("../dsan-globals/_globals.r")
library(tidyverse) |> suppressPackageStartupMessages()
library(sf) |> suppressPackageStartupMessages()
library(rnaturalearth) |> suppressPackageStartupMessages()
library(mapview) |> suppressPackageStartupMessages()
africa_sf <- ne_countries(continent = "Africa", scale = 50)
africa_union_sf <- sf::st_union(africa_sf)
africa_map <- mapview(africa_sf, label="geounit", legend=FALSE)
N <- 10
sampled_points_sf <- sf::st_sample(africa_union_sf, N) |> sf::st_sf() |> mutate(temp = runif(N, 0, 100))
sampled_points_map <- mapview(sampled_points_sf, label="Random Point", col.regions=cbPalette[1], legend=FALSE)
countries_points_sf <- africa_sf[sampled_points_sf,]
filtered_map <- mapview(countries_points_sf, label="geounit", legend=FALSE) + sampled_points_map
(africa_map + sampled_points_map) | filtered_map

Spatial Filter \(\neq\) Spatial Join

  • The issue: Data attributes of POINTs are not merged into data attributes of POLYGONs
POINT Attributes
Code
st_geometry(sampled_points_sf) <- c("geom")
sampled_points_sf |> head()
geom temp
POINT (25.13669 -10.26536) 51.880463
POINT (-5.100807 15.54896) 37.143558
POINT (-8.31297 24.72026) 49.780183
POINT (-11.99807 12.42975) 36.045325
POINT (25.63262 -19.65709) 69.893106
POINT (0.1314828 17.26716) 7.535125
POLYGON Attributes
Code
countries_points_sf |> head(4)
featurecla scalerank labelrank sovereignt sov_a3 adm0_dif level type tlc admin adm0_a3 geou_dif geounit gu_a3 su_dif subunit su_a3 brk_diff name name_long brk_a3 brk_name brk_group abbrev postal formal_en formal_fr name_ciawf note_adm0 note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est pop_rank pop_year gdp_md gdp_year economy income_grp fips_10 iso_a2 iso_a2_eh iso_a3 iso_a3_eh iso_n3 iso_n3_eh un_a3 wb_a2 wb_a3 woe_id woe_id_eh woe_note adm0_iso adm0_diff adm0_tlc adm0_a3_us adm0_a3_fr adm0_a3_ru adm0_a3_es adm0_a3_cn adm0_a3_tw adm0_a3_in adm0_a3_np adm0_a3_pk adm0_a3_de adm0_a3_gb adm0_a3_br adm0_a3_il adm0_a3_ps adm0_a3_sa adm0_a3_eg adm0_a3_ma adm0_a3_pt adm0_a3_ar adm0_a3_jp adm0_a3_ko adm0_a3_vn adm0_a3_tr adm0_a3_id adm0_a3_pl adm0_a3_gr adm0_a3_it adm0_a3_nl adm0_a3_se adm0_a3_bd adm0_a3_ua adm0_a3_un adm0_a3_wb continent region_un subregion region_wb name_len long_len abbrev_len tiny homepart min_zoom min_label max_label label_x label_y ne_id wikidataid name_ar name_bn name_de name_en name_es name_fa name_fr name_el name_he name_hi name_hu name_id name_it name_ja name_ko name_nl name_pl name_pt name_ru name_sv name_tr name_uk name_ur name_vi name_zh name_zht fclass_iso tlc_diff fclass_tlc fclass_us fclass_fr fclass_ru fclass_es fclass_cn fclass_tw fclass_in fclass_np fclass_pk fclass_de fclass_gb fclass_br fclass_il fclass_ps fclass_sa fclass_eg fclass_ma fclass_pt fclass_ar fclass_jp fclass_ko fclass_vn fclass_tr fclass_id fclass_pl fclass_gr fclass_it fclass_nl fclass_se fclass_bd fclass_ua geometry
67 Admin-0 country 1 3 Senegal SEN 0 2 Sovereign country 1 Senegal SEN 0 Senegal SEN 0 Senegal SEN 0 Senegal Senegal SEN Senegal NA Sen. SN Republic of Senegal NA Senegal NA NA Senegal NA 2 6 5 5 16296364 14 2019 23578 2019 7. Least developed region 4. Lower middle income SG SN SN SEN SEN 686 686 686 SN SEN 23424943 23424943 Exact WOE match as country SEN NA SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN -99 -99 Africa Africa Western Africa Sub-Saharan Africa 7 7 4 -99 1 0 2.7 8 -14.778586 15.13813 1159321243 Q1041 السنغال সেনেগাল Senegal Senegal Senegal سنگال Sénégal Σενεγάλη סנגל सेनेगल Szenegál Senegal Senegal セネガル 세네갈 Senegal Senegal Senegal Сенегал Senegal Senegal Сенегал سینیگال Sénégal 塞内加尔 塞內加爾 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA MULTIPOLYGON (((-12.28062 1…
112 Admin-0 country 5 3 Mauritania MRT 0 2 Sovereign country 1 Mauritania MRT 0 Mauritania MRT 0 Mauritania MRT 0 Mauritania Mauritania MRT Mauritania NA Mrt. MR Islamic Republic of Mauritania NA Mauritania NA NA Mauritania NA 3 3 2 1 4525696 12 2019 7600 2019 7. Least developed region 5. Low income MR MR MR MRT MRT 478 478 478 MR MRT 23424896 23424896 Exact WOE match as country MRT NA MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT -99 -99 Africa Africa Western Africa Sub-Saharan Africa 10 10 4 -99 1 0 3.0 8 -9.740299 19.58706 1159321075 Q1025 موريتانيا মৌরিতানিয়া Mauretanien Mauritania Mauritania موریتانی Mauritanie Μαυριτανία מאוריטניה मॉरीतानिया Mauritánia Mauritania Mauritania モーリタニア 모리타니 Mauritanië Mauretania Mauritânia Мавритания Mauretanien Moritanya Мавританія موریتانیہ Mauritanie 毛里塔尼亚 茅利塔尼亞 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA MULTIPOLYGON (((-16.37334 1…
114 Admin-0 country 1 3 Mali MLI 0 2 Sovereign country 1 Mali MLI 0 Mali MLI 0 Mali MLI 0 Mali Mali MLI Mali NA Mali ML Republic of Mali NA Mali NA NA Mali NA 1 4 1 7 19658031 14 2019 17279 2019 7. Least developed region 5. Low income ML ML ML MLI MLI 466 466 466 ML MLI 23424891 23424891 Exact WOE match as country MLI NA MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI -99 -99 Africa Africa Western Africa Sub-Saharan Africa 4 4 4 -99 1 0 3.0 7 -2.038455 18.69271 1159321063 Q912 مالي মালি Mali Mali Malí مالی Mali Μάλι מאלי माली Mali Mali Mali マリ共和国 말리 Mali Mali Mali Мали Mali Mali Малі مالی Mali 马里 馬利共和國 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA MULTIPOLYGON (((-11.3894 12…
152 Admin-0 country 1 3 Guinea GIN 0 2 Sovereign country 1 Guinea GIN 0 Guinea GIN 0 Guinea GIN 0 Guinea Guinea GIN Guinea NA Gin. GN Republic of Guinea NA Guinea NA NA Guinea NA 6 3 7 2 12771246 14 2019 12296 2019 7. Least developed region 5. Low income GV GN GN GIN GIN 324 324 324 GN GIN 23424835 23424835 Exact WOE match as country GIN NA GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN -99 -99 Africa Africa Western Africa Sub-Saharan Africa 6 6 4 -99 1 0 3.0 8 -10.016402 10.61852 1159320795 Q1006 غينيا গিনি Guinea Guinea Guinea گینه Guinée Γουινέα גינאה गिनी Guinea Guinea Guinea ギニア 기니 Guinee Gwinea Guiné Гвинея Guinea Gine Гвінея جمہوریہ گنی Guinée 几内亚 幾內亞 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA MULTIPOLYGON (((-10.2832 8….

Our First Real Spatial Join: st_join()

Code
joined_sf <- countries_points_sf |> st_join(sampled_points_sf)
joined_sf |> head()
featurecla scalerank labelrank sovereignt sov_a3 adm0_dif level type tlc admin adm0_a3 geou_dif geounit gu_a3 su_dif subunit su_a3 brk_diff name name_long brk_a3 brk_name brk_group abbrev postal formal_en formal_fr name_ciawf note_adm0 note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est pop_rank pop_year gdp_md gdp_year economy income_grp fips_10 iso_a2 iso_a2_eh iso_a3 iso_a3_eh iso_n3 iso_n3_eh un_a3 wb_a2 wb_a3 woe_id woe_id_eh woe_note adm0_iso adm0_diff adm0_tlc adm0_a3_us adm0_a3_fr adm0_a3_ru adm0_a3_es adm0_a3_cn adm0_a3_tw adm0_a3_in adm0_a3_np adm0_a3_pk adm0_a3_de adm0_a3_gb adm0_a3_br adm0_a3_il adm0_a3_ps adm0_a3_sa adm0_a3_eg adm0_a3_ma adm0_a3_pt adm0_a3_ar adm0_a3_jp adm0_a3_ko adm0_a3_vn adm0_a3_tr adm0_a3_id adm0_a3_pl adm0_a3_gr adm0_a3_it adm0_a3_nl adm0_a3_se adm0_a3_bd adm0_a3_ua adm0_a3_un adm0_a3_wb continent region_un subregion region_wb name_len long_len abbrev_len tiny homepart min_zoom min_label max_label label_x label_y ne_id wikidataid name_ar name_bn name_de name_en name_es name_fa name_fr name_el name_he name_hi name_hu name_id name_it name_ja name_ko name_nl name_pl name_pt name_ru name_sv name_tr name_uk name_ur name_vi name_zh name_zht fclass_iso tlc_diff fclass_tlc fclass_us fclass_fr fclass_ru fclass_es fclass_cn fclass_tw fclass_in fclass_np fclass_pk fclass_de fclass_gb fclass_br fclass_il fclass_ps fclass_sa fclass_eg fclass_ma fclass_pt fclass_ar fclass_jp fclass_ko fclass_vn fclass_tr fclass_id fclass_pl fclass_gr fclass_it fclass_nl fclass_se fclass_bd fclass_ua temp geometry
67 Admin-0 country 1 3 Senegal SEN 0 2 Sovereign country 1 Senegal SEN 0 Senegal SEN 0 Senegal SEN 0 Senegal Senegal SEN Senegal NA Sen. SN Republic of Senegal NA Senegal NA NA Senegal NA 2 6 5 5 16296364 14 2019 23578 2019 7. Least developed region 4. Lower middle income SG SN SN SEN SEN 686 686 686 SN SEN 23424943 23424943 Exact WOE match as country SEN NA SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN SEN -99 -99 Africa Africa Western Africa Sub-Saharan Africa 7 7 4 -99 1 0 2.7 8 -14.778586 15.13813 1159321243 Q1041 السنغال সেনেগাল Senegal Senegal Senegal سنگال Sénégal Σενεγάλη סנגל सेनेगल Szenegál Senegal Senegal セネガル 세네갈 Senegal Senegal Senegal Сенегал Senegal Senegal Сенегал سینیگال Sénégal 塞内加尔 塞內加爾 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 36.045325 MULTIPOLYGON (((-12.28062 1…
112 Admin-0 country 5 3 Mauritania MRT 0 2 Sovereign country 1 Mauritania MRT 0 Mauritania MRT 0 Mauritania MRT 0 Mauritania Mauritania MRT Mauritania NA Mrt. MR Islamic Republic of Mauritania NA Mauritania NA NA Mauritania NA 3 3 2 1 4525696 12 2019 7600 2019 7. Least developed region 5. Low income MR MR MR MRT MRT 478 478 478 MR MRT 23424896 23424896 Exact WOE match as country MRT NA MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT -99 -99 Africa Africa Western Africa Sub-Saharan Africa 10 10 4 -99 1 0 3.0 8 -9.740299 19.58706 1159321075 Q1025 موريتانيا মৌরিতানিয়া Mauretanien Mauritania Mauritania موریتانی Mauritanie Μαυριτανία מאוריטניה मॉरीतानिया Mauritánia Mauritania Mauritania モーリタニア 모리타니 Mauritanië Mauretania Mauritânia Мавритания Mauretanien Moritanya Мавританія موریتانیہ Mauritanie 毛里塔尼亚 茅利塔尼亞 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 49.780183 MULTIPOLYGON (((-16.37334 1…
112.1 Admin-0 country 5 3 Mauritania MRT 0 2 Sovereign country 1 Mauritania MRT 0 Mauritania MRT 0 Mauritania MRT 0 Mauritania Mauritania MRT Mauritania NA Mrt. MR Islamic Republic of Mauritania NA Mauritania NA NA Mauritania NA 3 3 2 1 4525696 12 2019 7600 2019 7. Least developed region 5. Low income MR MR MR MRT MRT 478 478 478 MR MRT 23424896 23424896 Exact WOE match as country MRT NA MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT MRT -99 -99 Africa Africa Western Africa Sub-Saharan Africa 10 10 4 -99 1 0 3.0 8 -9.740299 19.58706 1159321075 Q1025 موريتانيا মৌরিতানিয়া Mauretanien Mauritania Mauritania موریتانی Mauritanie Μαυριτανία מאוריטניה मॉरीतानिया Mauritánia Mauritania Mauritania モーリタニア 모리타니 Mauritanië Mauretania Mauritânia Мавритания Mauretanien Moritanya Мавританія موریتانیہ Mauritanie 毛里塔尼亚 茅利塔尼亞 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 45.477871 MULTIPOLYGON (((-16.37334 1…
114 Admin-0 country 1 3 Mali MLI 0 2 Sovereign country 1 Mali MLI 0 Mali MLI 0 Mali MLI 0 Mali Mali MLI Mali NA Mali ML Republic of Mali NA Mali NA NA Mali NA 1 4 1 7 19658031 14 2019 17279 2019 7. Least developed region 5. Low income ML ML ML MLI MLI 466 466 466 ML MLI 23424891 23424891 Exact WOE match as country MLI NA MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI -99 -99 Africa Africa Western Africa Sub-Saharan Africa 4 4 4 -99 1 0 3.0 7 -2.038455 18.69271 1159321063 Q912 مالي মালি Mali Mali Malí مالی Mali Μάλι מאלי माली Mali Mali Mali マリ共和国 말리 Mali Mali Mali Мали Mali Mali Малі مالی Mali 马里 馬利共和國 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 37.143558 MULTIPOLYGON (((-11.3894 12…
114.1 Admin-0 country 1 3 Mali MLI 0 2 Sovereign country 1 Mali MLI 0 Mali MLI 0 Mali MLI 0 Mali Mali MLI Mali NA Mali ML Republic of Mali NA Mali NA NA Mali NA 1 4 1 7 19658031 14 2019 17279 2019 7. Least developed region 5. Low income ML ML ML MLI MLI 466 466 466 ML MLI 23424891 23424891 Exact WOE match as country MLI NA MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI MLI -99 -99 Africa Africa Western Africa Sub-Saharan Africa 4 4 4 -99 1 0 3.0 7 -2.038455 18.69271 1159321063 Q912 مالي মালি Mali Mali Malí مالی Mali Μάλι מאלי माली Mali Mali Mali マリ共和国 말리 Mali Mali Mali Мали Mali Mali Малі مالی Mali 马里 馬利共和國 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 7.535125 MULTIPOLYGON (((-11.3894 12…
152 Admin-0 country 1 3 Guinea GIN 0 2 Sovereign country 1 Guinea GIN 0 Guinea GIN 0 Guinea GIN 0 Guinea Guinea GIN Guinea NA Gin. GN Republic of Guinea NA Guinea NA NA Guinea NA 6 3 7 2 12771246 14 2019 12296 2019 7. Least developed region 5. Low income GV GN GN GIN GIN 324 324 324 GN GIN 23424835 23424835 Exact WOE match as country GIN NA GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN GIN -99 -99 Africa Africa Western Africa Sub-Saharan Africa 6 6 4 -99 1 0 3.0 8 -10.016402 10.61852 1159320795 Q1006 غينيا গিনি Guinea Guinea Guinea گینه Guinée Γουινέα גינאה गिनी Guinea Guinea Guinea ギニア 기니 Guinee Gwinea Guiné Гвинея Guinea Gine Гвінея جمہوریہ گنی Guinée 几内亚 幾內亞 Admin-0 country NA Admin-0 country NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 60.728323 MULTIPOLYGON (((-10.2832 8….

But… We Were Still in Easy Mode

  • Every point could be matched one-to-one with a country. But what if… 😱
Code
g <- st_make_grid(st_bbox(st_as_sfc("LINESTRING(0 0,1 1)")), n = c(2,2))
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')

Spatially Intensive vs. Spatially Extensive

  • Extensive attributes: associated with a physical size (length, area, volume, counts of items). Ex: population count.
    • Associated with an area \(\implies\) if that area is cut into smaller areas, the population count needs to be split too
    • (At minimum, the sum of the population counts for the smaller areas needs to equal the total for the larger area)
  • Intensive attributes: Not proportional to support: if the area is split, values may vary but on average remain the same. Ex: population density
    • If an area is split into smaller areas, population density is not split similarly!
    • The sum of population densities for the smaller areas is a meaningless measure
    • Instead, the mean will be more useful as ~similar to the density of the total

Handling the Extensive Case

  • 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) \]

Handling the Intensive Case

  • Assume the variable \(Y\) has constant value over a space \(S_i\) (e.g., population density in assumed to be the same across all sub-areas)
  • Then the estimate for a sub-area is the same as the estimate for the total area:

\[ \hat{Y}_{ij} = Y_i(S_i) \]

  • So that we can obtain estimates of \(Y\) for new spatial units \(T_j\) via area-weighted average of the source values:

\[ \hat{Y}_j(T_j) = \sum_{i=1}^{p}\frac{|A_{ij}|}{|T_j|}Y_j(S_i) \]

Let’s Go See It In Action!

 

Week 6 Lab: Interpolating Kurdistan

References