Week 3: Vector and Raster Representations

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

Class Sessions
Author
Affiliation

Jeff Jacobs

Published

Wednesday, September 9, 2026

Open slides in new tab →

Logistical Table-Setting

  • HW1 Public Tests will be updated ASAP
  • TA Intros!
  • Coding Workshops!

TA Intros

(In alphabetical order by surname wohoo)

Jackson Howes (DSAN)
jh2787@georgetown.edu

  • BS in Government AND Mathematics (Honors), St. Lawrence, Canton, NY
  • Survived fire and flames of this class 🤯

Hermine Wilhelmsen (McCourt)
hw623@georgetown.edu

  • Recent MSc in Sustainability and Social Innovation, HEC Paris 🥳🧑‍🎓
  • Ombuds-person, UNFCCC alum

Relevant Word of the Day: (Shān) = Mountain

Where We Left Off: Let’s Make Some Dang Maps!

Our First Map: Polygons!

From OpenData DC… Last week .shp, this week .geojson!

Code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.1     ✔ tidyr     1.3.2
── 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
Code
library(rmarkdown)
library(sf)
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
Code
# Load DC tracts data
dc_sf_fpath <- "data/Census_Tracts_-_2020.geojson"
dc_sf <- st_read(dc_sf_fpath);
Reading layer `Census_Tracts_-_2020' from data source 
  `/Users/jpj/gtown-local/ppol6805/w03/data/Census_Tracts_-_2020.geojson' 
  using driver `GeoJSON'
Simple feature collection with 206 features and 315 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -77.11976 ymin: 38.79165 xmax: -76.9094 ymax: 38.99585
Geodetic CRS:  WGS 84
Code
cols_to_keep <- c(
  "OBJECTID", "TRACT", "GEOID", "ALAND", "AWATER",
  "STUSAB", "SUMLEV", "GEOCODE", "STATE", "NAME",
  "POP100", "HU100", "geometry"
)
dc_sf <- dc_sf |> select(cols_to_keep)
Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
ℹ Please use `all_of()` or `any_of()` instead.
  # Was:
  data %>% select(cols_to_keep)

  # Now:
  data %>% select(all_of(cols_to_keep))

See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.

sf Objects

dc_sf is an R object of type sf (short for “simple features”), which extends data.frame by adding a special column named geometry (containing POLYGONs)

Code
class(dc_sf)
[1] "sf"         "data.frame"
Code
dim(dc_sf)
[1] 206  13
Code
rmarkdown::paged_table(dc_sf, options=list(rows.print=6))
OBJECTID TRACT GEOID ALAND AWATER STUSAB SUMLEV GEOCODE STATE NAME POP100 HU100 geometry
1 002002 11001002002 849376 0 DC 140 11001002002 11 Census Tract 20.02 4072 1532 POLYGON ((-77.03642 38.9497…
2 002101 11001002101 600992 0 DC 140 11001002101 11 Census Tract 21.01 5687 2335 POLYGON ((-77.02824 38.9581…
3 002102 11001002102 725975 0 DC 140 11001002102 11 Census Tract 21.02 5099 2221 POLYGON ((-77.01997 38.9581…
4 002201 11001002201 415173 0 DC 140 11001002201 11 Census Tract 22.01 3485 1229 POLYGON ((-77.02743 38.9518…
5 002202 11001002202 698895 566 DC 140 11001002202 11 Census Tract 22.02 3339 1454 POLYGON ((-77.01968 38.9520…
6 000101 11001000101 199776 5261 DC 140 11001000101 11 Census Tract 1.01 1406 999 POLYGON ((-77.05714 38.9105…
7 000102 11001000102 1706484 516665 DC 140 11001000102 11 Census Tract 1.02 3417 2053 POLYGON ((-77.06927 38.9005…
8 000201 11001000201 505004 0 DC 140 11001000201 11 Census Tract 2.01 4108 11 POLYGON ((-77.07927 38.9126…
9 000202 11001000202 776435 439661 DC 140 11001000202 11 Census Tract 2.02 4672 2169 POLYGON ((-77.07991 38.9050…
10 000300 11001000300 1042157 2305 DC 140 11001000300 11 Census Tract 3 6161 2845 POLYGON ((-77.08263 38.9212…
11 000400 11001000400 1541239 69 DC 140 11001000400 11 Census Tract 4 1643 998 POLYGON ((-77.07319 38.9276…
12 000501 11001000501 940249 29503 DC 140 11001000501 11 Census Tract 5.01 3829 2489 POLYGON ((-77.05899 38.9183…
13 000502 11001000502 581507 0 DC 140 11001000502 11 Census Tract 5.02 3418 1881 POLYGON ((-77.0664 38.93097…
14 000600 11001000600 1441865 71 DC 140 11001000600 11 Census Tract 6 4676 2435 POLYGON ((-77.07651 38.9422…
15 000702 11001000702 308967 0 DC 140 11001000702 11 Census Tract 7.02 3452 2472 POLYGON ((-77.08252 38.9217…
16 000703 11001000703 232137 0 DC 140 11001000703 11 Census Tract 7.03 3022 2136 POLYGON ((-77.08165 38.9262…
17 000704 11001000704 332346 0 DC 140 11001000704 11 Census Tract 7.04 2753 1724 POLYGON ((-77.07973 38.9334…
18 000802 11001000802 1688582 669985 DC 140 11001000802 11 Census Tract 8.02 3327 1541 POLYGON ((-77.10086 38.9112…
19 000803 11001000803 308153 0 DC 140 11001000803 11 Census Tract 8.03 3174 1692 POLYGON ((-77.08773 38.9357…
20 000804 11001000804 2566768 167978 DC 140 11001000804 11 Census Tract 8.04 3832 1347 POLYGON ((-77.10517 38.9172…
21 000902 11001000902 1867106 307257 DC 140 11001000902 11 Census Tract 9.02 2317 894 POLYGON ((-77.11975 38.9343…
22 000903 11001000903 928101 0 DC 140 11001000903 11 Census Tract 9.03 4202 484 POLYGON ((-77.10104 38.9351…
23 000904 11001000904 2024710 75151 DC 140 11001000904 11 Census Tract 9.04 3148 1216 POLYGON ((-77.11258 38.9402…
24 001002 11001001002 889722 0 DC 140 11001001002 11 Census Tract 10.02 3643 2544 POLYGON ((-77.08564 38.9382…
25 001003 11001001003 982460 0 DC 140 11001001003 11 Census Tract 10.03 3111 1129 POLYGON ((-77.10063 38.9491…
26 001004 11001001004 1471925 0 DC 140 11001001004 11 Census Tract 10.04 4480 1810 POLYGON ((-77.09292 38.9431…
27 001100 11001001100 1676650 0 DC 140 11001001100 11 Census Tract 11 5300 2414 POLYGON ((-77.08573 38.9607…
28 001200 11001001200 1219204 0 DC 140 11001001200 11 Census Tract 12 5371 2672 POLYGON ((-77.07883 38.9465…
29 001301 11001001301 2855501 26550 DC 140 11001001301 11 Census Tract 13.01 4160 2187 POLYGON ((-77.06943 38.9543…
30 001303 11001001303 889581 5274 DC 140 11001001303 11 Census Tract 13.03 3763 2532 POLYGON ((-77.06568 38.9479…
31 001304 11001001304 707155 9432 DC 140 11001001304 11 Census Tract 13.04 4018 3087 POLYGON ((-77.06157 38.9409…
32 001401 11001001401 788095 0 DC 140 11001001401 11 Census Tract 14.01 3258 1477 POLYGON ((-77.07743 38.9673…
33 001402 11001001402 895209 0 DC 140 11001001402 11 Census Tract 14.02 3514 1791 POLYGON ((-77.07331 38.9610…
34 001500 11001001500 4684714 39895 DC 140 11001001500 11 Census Tract 15 6156 2282 POLYGON ((-77.07165 38.9717…
35 001600 11001001600 2667590 7820 DC 140 11001001600 11 Census Tract 16 4471 1720 POLYGON ((-77.05163 38.9871…
36 001702 11001001702 939295 0 DC 140 11001001702 11 Census Tract 17.02 3356 1737 POLYGON ((-77.02532 38.9700…
37 001803 11001001803 1136289 31909 DC 140 11001001803 11 Census Tract 18.03 4102 1640 POLYGON ((-77.04763 38.9647…
38 001804 11001001804 601755 0 DC 140 11001001804 11 Census Tract 18.04 5500 2225 POLYGON ((-77.03342 38.9622…
39 001901 11001001901 769304 0 DC 140 11001001901 11 Census Tract 19.01 4206 1644 POLYGON ((-77.02803 38.9610…
40 001902 11001001902 877346 0 DC 140 11001001902 11 Census Tract 19.02 2117 883 POLYGON ((-77.0199 38.95938…
41 002001 11001002001 632724 1132 DC 140 11001002001 11 Census Tract 20.01 2926 1130 POLYGON ((-77.04338 38.9614…
42 002301 11001002301 407679 0 DC 140 11001002301 11 Census Tract 23.01 3266 1254 POLYGON ((-77.01996 38.9431…
43 002302 11001002302 2010924 5298 DC 140 11001002302 11 Census Tract 23.02 1762 686 POLYGON ((-77.01923 38.9368…
44 002400 11001002400 556408 0 DC 140 11001002400 11 Census Tract 24 4095 1724 POLYGON ((-77.0266 38.94613…
45 002501 11001002501 543460 0 DC 140 11001002501 11 Census Tract 25.01 2688 1099 POLYGON ((-77.03643 38.9429…
46 002503 11001002503 292105 0 DC 140 11001002503 11 Census Tract 25.03 3031 1523 POLYGON ((-77.02971 38.9368…
47 002504 11001002504 311721 0 DC 140 11001002504 11 Census Tract 25.04 3349 1375 POLYGON ((-77.03645 38.9377…
48 002600 11001002600 2106150 60195 DC 140 11001002600 11 Census Tract 26 2592 1013 POLYGON ((-77.05212 38.938,…
49 002702 11001002702 479022 7175 DC 140 11001002702 11 Census Tract 27.02 5825 2806 POLYGON ((-77.05078 38.9330…
50 002703 11001002703 163863 0 DC 140 11001002703 11 Census Tract 27.03 2614 1289 POLYGON ((-77.04419 38.9330…
51 002704 11001002704 295339 2427 DC 140 11001002704 11 Census Tract 27.04 2993 1689 POLYGON ((-77.05045 38.9332…
52 002801 11001002801 171910 0 DC 140 11001002801 11 Census Tract 28.01 4398 1999 POLYGON ((-77.03646 38.9341…
53 002802 11001002802 229696 0 DC 140 11001002802 11 Census Tract 28.02 4685 2430 POLYGON ((-77.03671 38.9271…
54 002900 11001002900 300632 0 DC 140 11001002900 11 Census Tract 29 4443 1944 POLYGON ((-77.03274 38.9319…
55 003000 11001003000 210636 0 DC 140 11001003000 11 Census Tract 30 3561 1748 POLYGON ((-77.03267 38.9301…
56 003100 11001003100 296819 0 DC 140 11001003100 11 Census Tract 31 3873 1818 POLYGON ((-77.02826 38.9318…
57 003200 11001003200 447929 0 DC 140 11001003200 11 Census Tract 32 5099 2174 POLYGON ((-77.02429 38.9363…
58 003301 11001003301 442120 0 DC 140 11001003301 11 Census Tract 33.01 3824 1847 POLYGON ((-77.01494 38.9206…
59 003302 11001003302 204237 0 DC 140 11001003302 11 Census Tract 33.02 2434 1216 POLYGON ((-77.01428 38.9150…
60 003400 11001003400 911458 159593 DC 140 11001003400 11 Census Tract 34 5120 1572 POLYGON ((-77.02297 38.9272…
61 003500 11001003500 382021 0 DC 140 11001003500 11 Census Tract 35 4801 2587 POLYGON ((-77.02705 38.9267…
62 003600 11001003600 305616 0 DC 140 11001003600 11 Census Tract 36 4775 2609 POLYGON ((-77.03241 38.9265…
63 003701 11001003701 172840 0 DC 140 11001003701 11 Census Tract 37.01 2916 1628 POLYGON ((-77.0365 38.91929…
64 003702 11001003702 119532 0 DC 140 11001003702 11 Census Tract 37.02 2906 1612 POLYGON ((-77.03649 38.9234…
65 003801 11001003801 148928 0 DC 140 11001003801 11 Census Tract 38.01 1800 1211 POLYGON ((-77.04171 38.9196…
66 003802 11001003802 207186 0 DC 140 11001003802 11 Census Tract 38.02 3911 2378 POLYGON ((-77.04269 38.9226…
67 003901 11001003901 120715 0 DC 140 11001003901 11 Census Tract 39.01 2598 1769 POLYGON ((-77.04355 38.9235…
68 003902 11001003902 266513 11205 DC 140 11001003902 11 Census Tract 39.02 2022 1379 POLYGON ((-77.04981 38.9278…
69 004001 11001004001 271037 2414 DC 140 11001004001 11 Census Tract 40.01 3941 2494 POLYGON ((-77.05018 38.9212…
70 004002 11001004002 194755 0 DC 140 11001004002 11 Census Tract 40.02 3291 2251 POLYGON ((-77.0463 38.91632…
71 004100 11001004100 788701 7303 DC 140 11001004100 11 Census Tract 41 2836 1757 POLYGON ((-77.05881 38.9158…
72 004201 11001004201 204529 0 DC 140 11001004201 11 Census Tract 42.01 3548 2367 POLYGON ((-77.04166 38.9141…
73 004202 11001004202 207646 0 DC 140 11001004202 11 Census Tract 42.02 2850 2060 POLYGON ((-77.04599 38.9145…
74 004300 11001004300 236694 0 DC 140 11001004300 11 Census Tract 43 4436 3030 POLYGON ((-77.03651 38.9142…
75 004401 11001004401 246895 0 DC 140 11001004401 11 Census Tract 44.01 3430 2302 POLYGON ((-77.03195 38.9199…
76 004402 11001004402 274746 0 DC 140 11001004402 11 Census Tract 44.02 2755 1629 POLYGON ((-77.03196 38.9155…
77 004600 11001004600 437422 0 DC 140 11001004600 11 Census Tract 46 3543 1844 POLYGON ((-77.01811 38.9146…
78 004702 11001004702 326418 0 DC 140 11001004702 11 Census Tract 47.02 3934 2762 POLYGON ((-77.02192 38.9025…
79 004703 11001004703 124124 0 DC 140 11001004703 11 Census Tract 47.03 3331 2356 POLYGON ((-77.02192 38.9029…
80 004704 11001004704 198509 0 DC 140 11001004704 11 Census Tract 47.04 1945 881 POLYGON ((-77.01482 38.9054…
81 004801 11001004801 317222 0 DC 140 11001004801 11 Census Tract 48.01 2820 1636 POLYGON ((-77.02192 38.9115…
82 004802 11001004802 288793 0 DC 140 11001004802 11 Census Tract 48.02 3634 2070 POLYGON ((-77.02192 38.9031…
83 004901 11001004901 271866 0 DC 140 11001004901 11 Census Tract 49.01 3380 2031 POLYGON ((-77.02704 38.9106…
84 004902 11001004902 263879 0 DC 140 11001004902 11 Census Tract 49.02 3449 2070 POLYGON ((-77.02705 38.9041…
85 005001 11001005001 200969 0 DC 140 11001005001 11 Census Tract 50.01 2012 1273 POLYGON ((-77.03195 38.9108…
86 005003 11001005003 94136 0 DC 140 11001005003 11 Census Tract 50.03 2143 1381 POLYGON ((-77.03195 38.9072…
87 005004 11001005004 143156 0 DC 140 11001005004 11 Census Tract 50.04 3861 2524 POLYGON ((-77.03033 38.9096…
88 005202 11001005202 246292 0 DC 140 11001005202 11 Census Tract 52.02 3408 2309 POLYGON ((-77.03653 38.9083…
89 005203 11001005203 93419 0 DC 140 11001005203 11 Census Tract 52.03 2979 2450 POLYGON ((-77.03457 38.9072…
90 005302 11001005302 117635 0 DC 140 11001005302 11 Census Tract 53.02 2518 1832 POLYGON ((-77.04167 38.9115…
91 005303 11001005303 168769 0 DC 140 11001005303 11 Census Tract 53.03 3215 2484 POLYGON ((-77.04298 38.9101…
92 005501 11001005501 164749 0 DC 140 11001005501 11 Census Tract 55.01 2303 1776 POLYGON ((-77.05142 38.9031…
93 005502 11001005502 251302 0 DC 140 11001005502 11 Census Tract 55.02 2968 2016 POLYGON ((-77.0488 38.90776…
94 005503 11001005503 213338 4682 DC 140 11001005503 11 Census Tract 55.03 2492 1799 POLYGON ((-77.05596 38.9045…
95 005601 11001005601 185309 1275 DC 140 11001005601 11 Census Tract 56.01 2900 2437 POLYGON ((-77.05762 38.9026…
96 005602 11001005602 273491 6261 DC 140 11001005602 11 Census Tract 56.02 4100 2355 POLYGON ((-77.0586 38.90134…
97 005801 11001005801 232951 0 DC 140 11001005801 11 Census Tract 58.01 1672 1312 POLYGON ((-77.02192 38.8935…
98 005802 11001005802 682488 0 DC 140 11001005802 11 Census Tract 58.02 2145 1616 POLYGON ((-77.03365 38.8973…
99 005900 11001005900 641993 0 DC 140 11001005900 11 Census Tract 59 2617 1705 POLYGON ((-77.01993 38.8936…
100 006400 11001006400 691410 243648 DC 140 11001006400 11 Census Tract 64 2481 1956 POLYGON ((-77.01535 38.8621…
101 006500 11001006500 509297 0 DC 140 11001006500 11 Census Tract 65 2609 1630 POLYGON ((-77.00912 38.8819…
102 006600 11001006600 285156 0 DC 140 11001006600 11 Census Tract 66 2164 1297 POLYGON ((-77.00352 38.8897…
103 006700 11001006700 428358 0 DC 140 11001006700 11 Census Tract 67 3927 2062 POLYGON ((-76.99497 38.8861…
104 006801 11001006801 244750 0 DC 140 11001006801 11 Census Tract 68.01 2238 1173 POLYGON ((-76.98365 38.8871…
105 006802 11001006802 280108 0 DC 140 11001006802 11 Census Tract 68.02 2580 1206 POLYGON ((-76.98366 38.8797…
106 006804 11001006804 1542277 477741 DC 140 11001006804 11 Census Tract 68.04 2429 17 POLYGON ((-76.98153 38.8789…
107 006900 11001006900 398513 0 DC 140 11001006900 11 Census Tract 69 2799 1793 POLYGON ((-76.99377 38.8845…
108 007000 11001007000 439473 0 DC 140 11001007000 11 Census Tract 70 2697 1346 POLYGON ((-77.00205 38.8806…
109 007100 11001007100 602223 149901 DC 140 11001007100 11 Census Tract 71 3218 1939 POLYGON ((-76.99154 38.8772…
110 007201 11001007201 781457 312419 DC 140 11001007201 11 Census Tract 72.01 2985 2672 POLYGON ((-77.0091 38.87564…
111 007202 11001007202 183977 0 DC 140 11001007202 11 Census Tract 72.02 4303 3563 POLYGON ((-77.00915 38.8798…
112 007203 11001007203 384178 0 DC 140 11001007203 11 Census Tract 72.03 3748 2246 POLYGON ((-77.00574 38.8808…
113 007301 11001007301 4684643 5139082 DC 140 11001007301 11 Census Tract 73.01 3841 1272 POLYGON ((-77.04801 38.8412…
114 007304 11001007304 1232841 8983 DC 140 11001007304 11 Census Tract 73.04 4410 1765 POLYGON ((-76.99423 38.8437…
115 007401 11001007401 1209113 200980 DC 140 11001007401 11 Census Tract 74.01 1259 644 POLYGON ((-77.0054 38.8688,…
116 007403 11001007403 330904 0 DC 140 11001007403 11 Census Tract 74.03 2613 1056 POLYGON ((-76.98128 38.8466…
117 007404 11001007404 826388 0 DC 140 11001007404 11 Census Tract 74.04 3637 1453 POLYGON ((-76.99228 38.8534…
118 007406 11001007406 360152 0 DC 140 11001007406 11 Census Tract 74.06 3528 1286 POLYGON ((-76.99365 38.8567…
119 007407 11001007407 608700 0 DC 140 11001007407 11 Census Tract 74.07 3491 1570 POLYGON ((-76.99424 38.8608…
120 007408 11001007408 372349 0 DC 140 11001007408 11 Census Tract 74.08 2867 1205 POLYGON ((-76.9759 38.85364…
121 007409 11001007409 404873 5767 DC 140 11001007409 11 Census Tract 74.09 3835 1613 POLYGON ((-76.98059 38.8454…
122 007502 11001007502 648317 0 DC 140 11001007502 11 Census Tract 75.02 4687 1997 POLYGON ((-76.97491 38.8617…
123 007503 11001007503 515179 0 DC 140 11001007503 11 Census Tract 75.03 2528 1152 POLYGON ((-76.99259 38.8649…
124 007504 11001007504 764081 0 DC 140 11001007504 11 Census Tract 75.04 2730 1321 POLYGON ((-76.98798 38.8602…
125 007601 11001007601 1115359 141529 DC 140 11001007601 11 Census Tract 76.01 4693 2350 POLYGON ((-76.98973 38.8716…
126 007603 11001007603 1226616 0 DC 140 11001007603 11 Census Tract 76.03 4185 2374 POLYGON ((-76.9682 38.85891…
127 007604 11001007604 1369580 0 DC 140 11001007604 11 Census Tract 76.04 3803 2056 POLYGON ((-76.97134 38.8739…
128 007605 11001007605 453794 0 DC 140 11001007605 11 Census Tract 76.05 3663 1834 POLYGON ((-76.98435 38.8666…
129 007703 11001007703 1017741 1059 DC 140 11001007703 11 Census Tract 77.03 5227 2418 POLYGON ((-76.95737 38.8838…
130 007707 11001007707 842851 0 DC 140 11001007707 11 Census Tract 77.07 3927 1757 POLYGON ((-76.94487 38.8791…
131 007708 11001007708 745809 129100 DC 140 11001007708 11 Census Tract 77.08 2574 1275 POLYGON ((-76.97146 38.8806…
132 007709 11001007709 828872 82685 DC 140 11001007709 11 Census Tract 77.09 2072 1096 POLYGON ((-76.97803 38.8768…
133 007803 11001007803 986677 3755 DC 140 11001007803 11 Census Tract 78.03 4590 2301 POLYGON ((-76.94941 38.8968…
134 007804 11001007804 838428 5705 DC 140 11001007804 11 Census Tract 78.04 3499 1421 POLYGON ((-76.93919 38.8994…
135 007806 11001007806 671434 0 DC 140 11001007806 11 Census Tract 78.06 3457 1122 POLYGON ((-76.94286 38.9020…
136 007807 11001007807 522749 0 DC 140 11001007807 11 Census Tract 78.07 2114 926 POLYGON ((-76.92627 38.8966…
137 007808 11001007808 936852 7722 DC 140 11001007808 11 Census Tract 78.08 3792 1703 POLYGON ((-76.92797 38.8899…
138 007809 11001007809 535254 0 DC 140 11001007809 11 Census Tract 78.09 3155 1369 POLYGON ((-76.94249 38.9020…
139 007901 11001007901 402041 0 DC 140 11001007901 11 Census Tract 79.01 4148 1895 POLYGON ((-76.98366 38.8977…
140 007903 11001007903 247945 0 DC 140 11001007903 11 Census Tract 79.03 1865 917 POLYGON ((-76.97726 38.8941…
141 008001 11001008001 322795 0 DC 140 11001008001 11 Census Tract 80.01 3103 1448 POLYGON ((-76.99025 38.8973…
142 008002 11001008002 578961 0 DC 140 11001008002 11 Census Tract 80.02 3366 1708 POLYGON ((-76.99154 38.8898…
143 008100 11001008100 290284 0 DC 140 11001008100 11 Census Tract 81 3153 1645 POLYGON ((-76.99497 38.8909…
144 008200 11001008200 338424 0 DC 140 11001008200 11 Census Tract 82 2695 1664 POLYGON ((-77.00593 38.8918…
145 008301 11001008301 303973 0 DC 140 11001008301 11 Census Tract 83.01 3220 1761 POLYGON ((-77.00357 38.9002…
146 008302 11001008302 260891 0 DC 140 11001008302 11 Census Tract 83.02 3026 1560 POLYGON ((-76.99846 38.8941…
147 008402 11001008402 274180 0 DC 140 11001008402 11 Census Tract 84.02 2447 1804 POLYGON ((-76.99497 38.8974…
148 008410 11001008410 219939 0 DC 140 11001008410 11 Census Tract 84.10 1843 898 POLYGON ((-76.99496 38.9013…
149 008701 11001008701 382686 0 DC 140 11001008701 11 Census Tract 87.01 2814 1498 POLYGON ((-77.00903 38.9111…
150 008702 11001008702 486411 0 DC 140 11001008702 11 Census Tract 87.02 3574 2073 POLYGON ((-77.00528 38.9093…
151 008802 11001008802 468578 0 DC 140 11001008802 11 Census Tract 88.02 4360 2240 POLYGON ((-76.99314 38.9040…
152 008803 11001008803 1168294 0 DC 140 11001008803 11 Census Tract 88.03 4036 2026 POLYGON ((-77.00243 38.9083…
153 008804 11001008804 778052 0 DC 140 11001008804 11 Census Tract 88.04 2417 1551 POLYGON ((-76.98883 38.9077…
154 008903 11001008903 305651 0 DC 140 11001008903 11 Census Tract 89.03 3062 1661 POLYGON ((-76.98296 38.9008…
155 008904 11001008904 449343 0 DC 140 11001008904 11 Census Tract 89.04 3137 1806 POLYGON ((-76.98327 38.9003…
156 009000 11001009000 1427114 31550 DC 140 11001009000 11 Census Tract 90 4676 2548 POLYGON ((-76.96516 38.9252…
157 009102 11001009102 1816287 0 DC 140 11001009102 11 Census Tract 91.02 4608 2173 POLYGON ((-77.0015 38.91045…
158 009201 11001009201 629781 0 DC 140 11001009201 11 Census Tract 92.01 3103 1549 POLYGON ((-77.00627 38.9270…
159 009203 11001009203 581087 0 DC 140 11001009203 11 Census Tract 92.03 2775 1352 POLYGON ((-77.009 38.91811,…
160 009204 11001009204 308465 0 DC 140 11001009204 11 Census Tract 92.04 2889 1414 POLYGON ((-77.00337 38.9240…
161 009301 11001009301 1109091 0 DC 140 11001009301 11 Census Tract 93.01 3853 1385 POLYGON ((-76.99484 38.9337…
162 009302 11001009302 391905 0 DC 140 11001009302 11 Census Tract 93.02 1750 878 POLYGON ((-76.99604 38.9213…
163 009400 11001009400 1582882 0 DC 140 11001009400 11 Census Tract 94 4424 1800 POLYGON ((-76.9804 38.94309…
164 009503 11001009503 1423609 0 DC 140 11001009503 11 Census Tract 95.03 3106 1282 POLYGON ((-76.98953 38.9502…
165 009504 11001009504 1033168 0 DC 140 11001009504 11 Census Tract 95.04 3208 1442 POLYGON ((-76.99912 38.9462…
166 009505 11001009505 1033208 0 DC 140 11001009505 11 Census Tract 95.05 3796 1674 POLYGON ((-77.01072 38.9623…
167 009507 11001009507 295290 0 DC 140 11001009507 11 Census Tract 95.07 1525 623 POLYGON ((-77.00224 38.9568…
168 009508 11001009508 962212 0 DC 140 11001009508 11 Census Tract 95.08 4243 2068 POLYGON ((-77.00905 38.9533…
169 009509 11001009509 648848 0 DC 140 11001009509 11 Census Tract 95.09 3194 1377 POLYGON ((-77.00201 38.9510…
170 009510 11001009510 841510 0 DC 140 11001009510 11 Census Tract 95.10 4525 1939 POLYGON ((-77.01086 38.9446…
171 009511 11001009511 719468 0 DC 140 11001009511 11 Census Tract 95.11 2066 10 POLYGON ((-77.00836 38.9413…
172 009601 11001009601 1905262 189666 DC 140 11001009601 11 Census Tract 96.01 2086 819 POLYGON ((-76.95789 38.9062…
173 009602 11001009602 1306709 67073 DC 140 11001009602 11 Census Tract 96.02 4481 2046 POLYGON ((-76.96222 38.8995…
174 009603 11001009603 607400 0 DC 140 11001009603 11 Census Tract 96.03 3971 1892 POLYGON ((-76.95637 38.8902…
175 009604 11001009604 503381 65762 DC 140 11001009604 11 Census Tract 96.04 1996 1005 POLYGON ((-76.96327 38.8903…
176 009700 11001009700 403051 3427 DC 140 11001009700 11 Census Tract 97 3188 1355 POLYGON ((-76.99486 38.8292…
177 009801 11001009801 431384 7884 DC 140 11001009801 11 Census Tract 98.01 1848 898 POLYGON ((-77.00386 38.8309…
178 009802 11001009802 200745 0 DC 140 11001009802 11 Census Tract 98.02 2064 880 POLYGON ((-77.00057 38.8309…
179 009803 11001009803 531060 3756 DC 140 11001009803 11 Census Tract 98.03 2968 1480 POLYGON ((-77.00841 38.8336…
180 009804 11001009804 518363 5609 DC 140 11001009804 11 Census Tract 98.04 2517 1133 POLYGON ((-77.00422 38.8404…
181 009807 11001009807 1037012 0 DC 140 11001009807 11 Census Tract 98.07 3523 1682 POLYGON ((-77.01737 38.8239…
182 009810 11001009810 398419 14058 DC 140 11001009810 11 Census Tract 98.10 2461 1243 POLYGON ((-77.00768 38.8314…
183 009811 11001009811 467511 0 DC 140 11001009811 11 Census Tract 98.11 4438 1939 POLYGON ((-77.00242 38.8274…
184 009901 11001009901 2102150 0 DC 140 11001009901 11 Census Tract 99.01 2364 1066 POLYGON ((-76.96481 38.8764…
185 009902 11001009902 1294556 0 DC 140 11001009902 11 Census Tract 99.02 2774 1289 POLYGON ((-76.95291 38.8664…
186 009903 11001009903 383679 0 DC 140 11001009903 11 Census Tract 99.03 2004 743 POLYGON ((-76.93006 38.8898…
187 009904 11001009904 426198 0 DC 140 11001009904 11 Census Tract 99.04 2886 1276 POLYGON ((-76.93578 38.8859…
188 009905 11001009905 431558 0 DC 140 11001009905 11 Census Tract 99.05 2790 1344 POLYGON ((-76.92932 38.8807…
189 009906 11001009906 252684 0 DC 140 11001009906 11 Census Tract 99.06 1773 896 POLYGON ((-76.94056 38.8865…
190 009907 11001009907 466954 0 DC 140 11001009907 11 Census Tract 99.07 2259 1146 POLYGON ((-76.94577 38.8808…
191 010100 11001010100 579230 0 DC 140 11001010100 11 Census Tract 101 2699 2022 POLYGON ((-77.03655 38.9007…
192 010201 11001010201 174884 0 DC 140 11001010201 11 Census Tract 102.01 3341 2471 POLYGON ((-77.02207 38.8791…
193 010202 11001010202 1069479 136323 DC 140 11001010202 11 Census Tract 102.02 2631 2042 POLYGON ((-77.0327 38.88262…
194 010300 11001010300 1084591 0 DC 140 11001010300 11 Census Tract 103 3604 1483 POLYGON ((-77.03637 38.9731…
195 010400 11001010400 2602393 7006 DC 140 11001010400 11 Census Tract 104 4616 1894 POLYGON ((-77.00919 38.8391…
196 010500 11001010500 749891 0 DC 140 11001010500 11 Census Tract 105 3712 2440 POLYGON ((-77.01756 38.8823…
197 010601 11001010601 224811 0 DC 140 11001010601 11 Census Tract 106.01 2130 1486 POLYGON ((-77.009 38.90576,…
198 010602 11001010602 542509 0 DC 140 11001010602 11 Census Tract 106.02 6594 3730 POLYGON ((-77.00444 38.9025…
199 010603 11001010603 464270 0 DC 140 11001010603 11 Census Tract 106.03 3068 2004 POLYGON ((-77.00907 38.8978…
200 010700 11001010700 891588 0 DC 140 11001010700 11 Census Tract 107 2296 1556 POLYGON ((-77.04973 38.9029…
201 010800 11001010800 661580 0 DC 140 11001010800 11 Census Tract 108 6879 1501 POLYGON ((-77.05014 38.8989…
202 010900 11001010900 2381015 2933566 DC 140 11001010900 11 Census Tract 109 3333 1378 POLYGON ((-77.03919 38.8005…
203 011001 11001011001 152450 0 DC 140 11001011001 11 Census Tract 110.01 2423 1636 POLYGON ((-77.01757 38.8764…
204 011002 11001011002 498122 367023 DC 140 11001011002 11 Census Tract 110.02 1859 1471 POLYGON ((-77.02188 38.8765…
205 011100 11001011100 5718693 366232 DC 140 11001011100 11 Census Tract 111 5903 1952 POLYGON ((-76.97913 38.9277…
206 980000 11001980000 6514228 4996439 DC 140 11001980000 11 Census Tract 9800 176 12 POLYGON ((-77.0647 38.89186…

What Are These POLYGON Objects?

Code
head(dc_sf)
OBJECTID TRACT GEOID ALAND AWATER STUSAB SUMLEV GEOCODE STATE NAME POP100 HU100 geometry
1 002002 11001002002 849376 0 DC 140 11001002002 11 Census Tract 20.02 4072 1532 POLYGON ((-77.03642 38.9497…
2 002101 11001002101 600992 0 DC 140 11001002101 11 Census Tract 21.01 5687 2335 POLYGON ((-77.02824 38.9581…
3 002102 11001002102 725975 0 DC 140 11001002102 11 Census Tract 21.02 5099 2221 POLYGON ((-77.01997 38.9581…
4 002201 11001002201 415173 0 DC 140 11001002201 11 Census Tract 22.01 3485 1229 POLYGON ((-77.02743 38.9518…
5 002202 11001002202 698895 566 DC 140 11001002202 11 Census Tract 22.02 3339 1454 POLYGON ((-77.01968 38.9520…
6 000101 11001000101 199776 5261 DC 140 11001000101 11 Census Tract 1.01 1406 999 POLYGON ((-77.05714 38.9105…

Zoom… Enhance…

Let’s try printing just the first row?

Code
rmarkdown::paged_table(dc_sf[1,])
OBJECTID TRACT GEOID ALAND AWATER STUSAB SUMLEV GEOCODE STATE NAME POP100 HU100 geometry
1 002002 11001002002 849376 0 DC 140 11001002002 11 Census Tract 20.02 4072 1532 POLYGON ((-77.03642 38.9497…

Printing the special geometry column?

Code
rmarkdown::paged_table(dc_sf[1, "geometry"])
geometry
POLYGON ((-77.03642 38.9497…

Nope, we’ll have to open the “black box” using the st_coordinates() function from the sf library →

Code
sf::st_coordinates(dc_sf[1,"geometry"])
               X        Y L1 L2
  [1,] -77.03642 38.94975  1  1
  [2,] -77.03643 38.94827  1  1
  [3,] -77.03628 38.94827  1  1
  [4,] -77.03539 38.94827  1  1
  [5,] -77.03452 38.94827  1  1
  [6,] -77.03442 38.94827  1  1
  [7,] -77.03375 38.94827  1  1
  [8,] -77.03309 38.94827  1  1
  [9,] -77.03287 38.94827  1  1
 [10,] -77.03273 38.94827  1  1
 [11,] -77.03170 38.94824  1  1
 [12,] -77.03163 38.94819  1  1
 [13,] -77.03146 38.94802  1  1
 [14,] -77.03112 38.94769  1  1
 [15,] -77.03109 38.94767  1  1
 [16,] -77.03107 38.94765  1  1
 [17,] -77.03104 38.94762  1  1
 [18,] -77.03098 38.94753  1  1
 [19,] -77.03080 38.94735  1  1
 [20,] -77.03071 38.94726  1  1
 [21,] -77.03069 38.94724  1  1
 [22,] -77.03044 38.94699  1  1
 [23,] -77.03023 38.94679  1  1
 [24,] -77.03005 38.94661  1  1
 [25,] -77.02985 38.94642  1  1
 [26,] -77.02982 38.94640  1  1
 [27,] -77.02965 38.94613  1  1
 [28,] -77.02893 38.94614  1  1
 [29,] -77.02821 38.94613  1  1
 [30,] -77.02780 38.94613  1  1
 [31,] -77.02747 38.94613  1  1
 [32,] -77.02702 38.94613  1  1
 [33,] -77.02660 38.94613  1  1
 [34,] -77.02667 38.94641  1  1
 [35,] -77.02679 38.94720  1  1
 [36,] -77.02681 38.94738  1  1
 [37,] -77.02694 38.94826  1  1
 [38,] -77.02695 38.94834  1  1
 [39,] -77.02695 38.94840  1  1
 [40,] -77.02696 38.94847  1  1
 [41,] -77.02701 38.94877  1  1
 [42,] -77.02702 38.94884  1  1
 [43,] -77.02704 38.94900  1  1
 [44,] -77.02705 38.94909  1  1
 [45,] -77.02708 38.94934  1  1
 [46,] -77.02714 38.94974  1  1
 [47,] -77.02716 38.94993  1  1
 [48,] -77.02721 38.95028  1  1
 [49,] -77.02723 38.95037  1  1
 [50,] -77.02724 38.95046  1  1
 [51,] -77.02725 38.95055  1  1
 [52,] -77.02726 38.95063  1  1
 [53,] -77.02728 38.95072  1  1
 [54,] -77.02729 38.95081  1  1
 [55,] -77.02740 38.95163  1  1
 [56,] -77.02743 38.95188  1  1
 [57,] -77.02756 38.95281  1  1
 [58,] -77.02756 38.95283  1  1
 [59,] -77.02757 38.95291  1  1
 [60,] -77.02773 38.95400  1  1
 [61,] -77.02774 38.95409  1  1
 [62,] -77.02779 38.95451  1  1
 [63,] -77.02783 38.95476  1  1
 [64,] -77.02787 38.95505  1  1
 [65,] -77.02788 38.95516  1  1
 [66,] -77.02789 38.95523  1  1
 [67,] -77.02796 38.95572  1  1
 [68,] -77.02800 38.95602  1  1
 [69,] -77.02802 38.95612  1  1
 [70,] -77.02803 38.95622  1  1
 [71,] -77.02807 38.95653  1  1
 [72,] -77.02812 38.95687  1  1
 [73,] -77.02814 38.95703  1  1
 [74,] -77.02816 38.95718  1  1
 [75,] -77.02818 38.95729  1  1
 [76,] -77.02821 38.95750  1  1
 [77,] -77.02821 38.95758  1  1
 [78,] -77.02822 38.95765  1  1
 [79,] -77.02823 38.95781  1  1
 [80,] -77.02824 38.95797  1  1
 [81,] -77.02824 38.95805  1  1
 [82,] -77.02824 38.95813  1  1
 [83,] -77.02824 38.95829  1  1
 [84,] -77.02824 38.95836  1  1
 [85,] -77.02823 38.95851  1  1
 [86,] -77.02822 38.95872  1  1
 [87,] -77.02817 38.95935  1  1
 [88,] -77.02811 38.96003  1  1
 [89,] -77.02806 38.96074  1  1
 [90,] -77.02803 38.96102  1  1
 [91,] -77.02803 38.96105  1  1
 [92,] -77.02802 38.96120  1  1
 [93,] -77.02802 38.96123  1  1
 [94,] -77.02806 38.96127  1  1
 [95,] -77.02816 38.96130  1  1
 [96,] -77.02852 38.96135  1  1
 [97,] -77.02851 38.96130  1  1
 [98,] -77.02851 38.96128  1  1
 [99,] -77.02851 38.96126  1  1
[100,] -77.02854 38.96121  1  1
[101,] -77.02855 38.96119  1  1
[102,] -77.02859 38.96115  1  1
[103,] -77.02876 38.96100  1  1
[104,] -77.02925 38.96054  1  1
[105,] -77.02948 38.96033  1  1
[106,] -77.02952 38.96030  1  1
[107,] -77.02953 38.96028  1  1
[108,] -77.02958 38.96026  1  1
[109,] -77.02962 38.96024  1  1
[110,] -77.02965 38.96023  1  1
[111,] -77.02967 38.96022  1  1
[112,] -77.02970 38.96022  1  1
[113,] -77.02999 38.96003  1  1
[114,] -77.03003 38.96000  1  1
[115,] -77.03011 38.95994  1  1
[116,] -77.03013 38.95992  1  1
[117,] -77.03016 38.95989  1  1
[118,] -77.03038 38.95966  1  1
[119,] -77.03061 38.95938  1  1
[120,] -77.03073 38.95925  1  1
[121,] -77.03081 38.95915  1  1
[122,] -77.03115 38.95874  1  1
[123,] -77.03138 38.95847  1  1
[124,] -77.03148 38.95836  1  1
[125,] -77.03158 38.95824  1  1
[126,] -77.03172 38.95807  1  1
[127,] -77.03181 38.95796  1  1
[128,] -77.03204 38.95770  1  1
[129,] -77.03224 38.95745  1  1
[130,] -77.03231 38.95737  1  1
[131,] -77.03238 38.95729  1  1
[132,] -77.03252 38.95712  1  1
[133,] -77.03257 38.95706  1  1
[134,] -77.03266 38.95695  1  1
[135,] -77.03271 38.95689  1  1
[136,] -77.03294 38.95662  1  1
[137,] -77.03298 38.95658  1  1
[138,] -77.03318 38.95637  1  1
[139,] -77.03341 38.95620  1  1
[140,] -77.03376 38.95566  1  1
[141,] -77.03379 38.95563  1  1
[142,] -77.03420 38.95514  1  1
[143,] -77.03460 38.95466  1  1
[144,] -77.03504 38.95413  1  1
[145,] -77.03520 38.95394  1  1
[146,] -77.03560 38.95347  1  1
[147,] -77.03588 38.95314  1  1
[148,] -77.03604 38.95295  1  1
[149,] -77.03612 38.95285  1  1
[150,] -77.03625 38.95271  1  1
[151,] -77.03642 38.95254  1  1
[152,] -77.03642 38.95188  1  1
[153,] -77.03642 38.95081  1  1
[154,] -77.03642 38.95053  1  1
[155,] -77.03642 38.94975  1  1

Working With sf Objects

Code
# Select column by name
head(dc_sf$NAME)
[1] "Census Tract 20.02" "Census Tract 21.01" "Census Tract 21.02"
[4] "Census Tract 22.01" "Census Tract 22.02" "Census Tract 1.01" 
Code
# Select column by index
head(dc_sf[,4])
ALAND geometry
849376 POLYGON ((-77.03642 38.9497…
600992 POLYGON ((-77.02824 38.9581…
725975 POLYGON ((-77.01997 38.9581…
415173 POLYGON ((-77.02743 38.9518…
698895 POLYGON ((-77.01968 38.9520…
199776 POLYGON ((-77.05714 38.9105…

And… Actually Displaying the Map!

Code
# We can extract *just* the geometry column with the st_geometry function...
dc_geo <- sf::st_geometry(dc_sf)
# This produces an object of type "sfc"
class(dc_geo)
[1] "sfc_POLYGON" "sfc"        
Code
# Plot the geometry with base R's plot() function
plot(dc_geo)

More “Modern” Plots with ggplot2!

Code
library(ggplot2)
dc_sf |>
  ggplot2::ggplot() +
  ggplot2::geom_sf() +
  ggplot2::theme_classic()

Code
dc_sf |>
  ggplot2::ggplot() +
  ggplot2::geom_sf() +
  ggplot2::theme_classic()

How to Do Things with Geometries

From the sf Cheatsheet

HW1 \(\rightarrow\) HW2

  • Once you finish HW1, you’ll know how to create geometries with sf and terra
  • So now, what can you do with them?
  • For example, we’d like to be able to say things like:
    • “The new lamppost cannot be placed at \((x, y)\), since there is already a building there!”
    • “There are \(N_1\) lampposts in County 1, and \(N_2\) lampposts in County 2”
    • “The average resident in Neighborhood A lives 2 km away from their nearest bus stop

First Things First: Loading and Saving

  • Note how there were no data files in HW1 😱
  • From HW2 onwards (and in your GIS life), we’ll:
    • Download from e.g. city Open Data Portals: geo data files, but also loading on-the-fly (this week)
    • Summarize/aggregate (this week and next week)
    • Visualize findings (“Mapping Libraries” unit)

Vector Formats

Shapefiles (.shp et al.)

A shape“file” is actually (at least) three separate files bundled together:

  • Mandatory .shp: Containing feature geometries
  • Mandatory .shx: Positional indices
  • Mandatory .dbf: Data attributes
  • Optional .prj: Coordinate reference system
  • Optional .xml: Metadata

Shapefiles

Let’s see what’s inside the shapefile we first saw in Week 1, containing data on DC’s Census Tracts: Census Tracts in 2020

DC Census Tracts (with the Georgetown campus tract highlighted!) from OpenData.DC.gov

Shapefile Anatomy

From Rodrigue (2016)

GeoJSON / TopoJSON (.geojson)

  • JavaScript Object Notation: General cross-platform format
  • Useful when data is too complex for e.g. .csv
  • TopoJSON = Memory-efficient GeoJSON
  • Bonus: Inline preview on GitHub!
my_data.geojson
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [30, 20], [45, 40],
            [10, 40], [30, 20]
          ]
        ]
      },
      "properties": {
        "color": "green",
        "area": 3565747
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [15, 5], [40, 10],
            [10, 20], [5, 10], 
            [15, 5]
          ]
        ]
      },
      "properties": {
        "color": "red",
        "area": 3272386
      }
    }
  ]
}

GeoPackage (.gpkg)

Raster Formats

  • GeoTIFF (.tif or .tiff)
    • Based on TIFF format developed at NASA
  • NetCDF (.nc4)
    • Used in earth sciences, as format for data sources measured and distributed multiple times per day over large full-country or full-continent areas.

Coordinate Reference Systems (CRS)

  • EPSG (European Petroleum Survey Group) Registry: Most common way to specify a CRS

    • For example, 4326 is the EPSG code for the WGS84 coordinate system
  • PROJ: Rather than opaque numeric code like EPSG, uses plaintext “proj-strings” containing parameter info: datum, ellipsoid, projection, and units (e.g. meters). Example: PROJ4 code EPSG:4326 is represented as

    +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
  • WKT: Lengthy but human-readable descriptions

Geospatial Operations 1: Unary Operations

Getting the Geometries

Using rnaturalearth with mapview

Code
set.seed(6805)
library(tidyverse)
library(sf)
library(rnaturalearth)
library(mapview)
france_sf <- ne_countries(country = "France", scale = 50)
(france_map <- mapview(france_sf, label = "geounit", legend = FALSE))

Centroid of France

Code
france_cent_sf <- sf::st_centroid(france_sf)
Warning: st_centroid assumes attributes are constant over geometries
Code
france_map + mapview(france_cent_sf, label = "Centroid", legend = FALSE)

One We Already Saw: Union

Computing the union of all geometries in the sf via sf::st_union()

Code
library(leaflet.extras2)
Loading required package: leaflet
Code
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)
africa_union_map <- mapview(africa_union_sf, label="st_union(africa)", legend=FALSE)
africa_map | africa_union_map

Helpful for Rasterizing: BBox

Code
africa_bbox_sf <- sf::st_bbox(africa_sf)
africa_bbox_map <- mapview(africa_bbox_sf, label="st_bbox(africa)", legend=FALSE)
africa_map | africa_bbox_map

Convex Hulls by Country

Code
africa_countries_cvx <- sf::st_convex_hull(africa_sf)
africa_countries_cvx_map <- mapview(africa_countries_cvx, label="geounit", legend=FALSE)
africa_map | africa_countries_cvx_map

Convex Hull of Continent

Use st_union() first:

Code
africa_cvx <- africa_sf |> st_union() |> st_convex_hull()
africa_cvx_map <- mapview(africa_cvx, label="geounit", legend=FALSE)
africa_map | africa_cvx_map

One We Already Saw: Centroids

Computing the centroid of all geometries in the sf via sf::st_centroid()

Code
africa_cents_sf <- sf::st_centroid(africa_sf)
Warning: st_centroid assumes attributes are constant over geometries
Code
africa_cents_map <- mapview(africa_cents_sf, label="geounit", legend=FALSE)
africa_map | africa_cents_map

Geospatial Operations 2: Binary Operations

Spatial Joins

Code
nc <- system.file("shape/nc.shp", package="sf") |>
  read_sf() |>
  st_transform('EPSG:2264')
gr <- st_sf(
         label = apply(expand.grid(1:10, LETTERS[10:1])[,2:1], 1, paste0, collapse = ""),
         geom = st_make_grid(nc))
gr$col <- sf.colors(10, categorical = TRUE, alpha = .3)
# cut, to verify that NA's work out:
gr <- gr[-(1:30),]
suppressWarnings(nc_j <- st_join(nc, gr, largest = TRUE))
par(mfrow = c(2,1), mar = rep(0,4))
plot(st_geometry(nc_j), border = 'grey')
plot(st_geometry(gr), add = TRUE, col = gr$col)
text(st_coordinates(st_centroid(st_geometry(gr))), labels = gr$label, cex = .85)
# the joined dataset:
plot(st_geometry(nc_j), border = 'grey', col = nc_j$col)
text(st_coordinates(st_centroid(st_geometry(nc_j))), labels = nc_j$label, cex = .7)
plot(st_geometry(gr), border = '#88ff88aa', add = TRUE)

Spatial Sampling

Code
# Sample random points
africa_points_list <- sf::st_sample(africa_union_sf, 10)
africa_points_sf <- sf::st_sf(africa_points_list)
africa_points_map <- mapview(africa_points_sf, label="Random Point", col.regions=cb_palette[1], legend=FALSE)
Warning in cbind(`Feature ID` = fid, mat): number of rows of result is not a
multiple of vector length (arg 1)
Code
africa_map + africa_points_map

The “Default” Predicate: st_intersects

Code
countries_w_points <- africa_sf[africa_points_sf,]
mapview(countries_w_points, label="geounit", legend=FALSE) + africa_points_map

Counting with lengths()

Code
country_inter <- sf::st_intersects(africa_sf, africa_points_sf)
# Computes point counts for each polygon
(num_intersections <- lengths(country_inter))
 [1] 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0
[39] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2
Code
africa_sf <- africa_sf |> mutate(
  num_points = num_intersections
) |> arrange(geounit)
africa_sf |> select(geounit, num_points) |> head()
geounit num_points geometry
Algeria 2 MULTIPOLYGON (((8.576563 36…
Angola 2 MULTIPOLYGON (((13.07275 -4…
Benin 0 MULTIPOLYGON (((1.622656 6….
Botswana 0 MULTIPOLYGON (((25.25879 -1…
Burkina Faso 0 MULTIPOLYGON (((0.9004883 1…
Burundi 0 MULTIPOLYGON (((30.55361 -2…

Plotting with mapview

Code
mapview(africa_sf, zcol="num_points")

Plotting with ggplot2

Since we’re starting to get into data attributes rather than geometric features, switching to ggplot2 is recommended!

Code
africa_sf |> ggplot(aes(fill=num_points)) +
  geom_sf() +
  theme_classic()

Getting Fancier…

  • To do fancier geospatial operations, we’ll need to start overthinking the different possible relationships between two or more geometries!
  • To this end: predicates

References

Rodrigue, Jean-Paul. 2016. The Geography of Transport Systems. Taylor & Francis.