Working with spatial and geometric operations in {sf}

Session 5

2023-09-27

1 Getting started

library(tidyverse)
library(sf)
library(tigris)
options(tigris_use_cache = TRUE)
options("tigris_use_cache" = TRUE)
us_states <- states()

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
us_highways <- primary_roads()

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
maryland <- filter(us_states, NAME == "Maryland")
lower48 <- filter(
  us_states,
  !(NAME %in%
    c(
      "United States Virgin Islands",
      "Commonwealth of the Northern Mariana Islands",
      "Guam",
      "Alaska",
      "American Samoa",
      "Puerto Rico",
      "Hawaii"
    ))
)

2 Converting objects between types

sfg <- st_point(c(-76.712838, 39.253383))

sfg
st_as_sfc(list(sfg))
Geometry set for 1 feature 
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -76.71284 ymin: 39.25338 xmax: -76.71284 ymax: 39.25338
CRS:           NA
sfc <- st_as_sfc(list(sfg), crs = 4326)

sfc
Geometry set for 1 feature 
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -76.71284 ymin: 39.25338 xmax: -76.71284 ymax: 39.25338
Geodetic CRS:  WGS 84
st_as_sf(sfc)
Simple feature collection with 1 feature and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -76.71284 ymin: 39.25338 xmax: -76.71284 ymax: 39.25338
Geodetic CRS:  WGS 84
                           x
1 POINT (-76.71284 39.25338)
sf_df <- data.frame(
  name = "Example data frame",
  lon = -76.712838,
  lat = 39.253383
)

sf <- st_as_sf(
  sf_df,
  coords = c("lon", "lat"),
  crs = 4326
)

sf
Simple feature collection with 1 feature and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -76.71284 ymin: 39.25338 xmax: -76.71284 ymax: 39.25338
Geodetic CRS:  WGS 84
                name                   geometry
1 Example data frame POINT (-76.71284 39.25338)

3 Types of spatial operations

  • Spatial filtering and topological relations
  • Spatial joins and non-overlapping joins
  • Spatial aggregation

Spatial filtering and topological relations

st_filter() defaults to intersecting geometries:

st_filter(
  us_states,
  maryland
)
Simple feature collection with 6 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -83.67539 ymin: 36.54085 xmax: -74.68956 ymax: 42.51607
Geodetic CRS:  NAD83
  REGION DIVISION STATEFP  STATENS GEOID     GEOIDFQ STUSPS
1      3        5      54 01779805    54 0400000US54     WV
2      3        5      24 01714934    24 0400000US24     MD
3      3        5      10 01779781    10 0400000US10     DE
4      1        2      42 01779798    42 0400000US42     PA
5      3        5      51 01779803    51 0400000US51     VA
6      3        5      11 01702382    11 0400000US11     DC
                  NAME LSAD MTFCC FUNCSTAT        ALAND     AWATER    INTPTLAT
1        West Virginia   00 G4000        A  62266513826  488918898 +38.6472854
2             Maryland   00 G4000        A  25151223822 6979843236 +38.9466584
3             Delaware   00 G4000        A   5046692239 1399219008 +38.9985661
4         Pennsylvania   00 G4000        A 115881476238 3397613881 +40.9046042
5             Virginia   00 G4000        A 102256342555 8529908321 +37.5222512
6 District of Columbia   00 G4000        A    158316181   18709785 +38.9042474
      INTPTLON                       geometry
1 -080.6183274 MULTIPOLYGON (((-77.75438 3...
2 -076.6744939 MULTIPOLYGON (((-75.756 39....
3 -075.4416440 MULTIPOLYGON (((-75.50949 3...
4 -077.8275233 MULTIPOLYGON (((-75.5931 39...
5 -078.6681938 MULTIPOLYGON (((-76.9159 36...
6 -077.0165167 MULTIPOLYGON (((-77.11975 3...

st_filter() can also support alternate topological relations:

st_filter(
  us_states,
  maryland,
  .predicate = st_disjoint
)
Simple feature collection with 50 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.2311 ymin: -14.60181 xmax: 179.8597 ymax: 71.43979
Geodetic CRS:  NAD83
First 10 features:
   REGION DIVISION STATEFP  STATENS GEOID     GEOIDFQ STUSPS           NAME
1       3        5      12 00294478    12 0400000US12     FL        Florida
2       2        3      17 01779784    17 0400000US17     IL       Illinois
3       2        4      27 00662849    27 0400000US27     MN      Minnesota
4       1        1      44 01219835    44 0400000US44     RI   Rhode Island
5       4        8      16 01779783    16 0400000US16     ID          Idaho
6       1        1      33 01779794    33 0400000US33     NH  New Hampshire
7       3        5      37 01027616    37 0400000US37     NC North Carolina
8       1        1      50 01779802    50 0400000US50     VT        Vermont
9       1        1      09 01779780    09 0400000US09     CT    Connecticut
10      4        8      35 00897535    35 0400000US35     NM     New Mexico
   LSAD MTFCC FUNCSTAT        ALAND      AWATER    INTPTLAT     INTPTLON
1    00 G4000        A 138965379385 45968913048 +28.3989775 -082.5143005
2    00 G4000        A 143778206717  6216848695 +40.1028754 -089.1526108
3    00 G4000        A 206244791203 18937236061 +46.3159573 -094.1996043
4    00 G4000        A   2677768885  1323681453 +41.5964850 -071.5264901
5    00 G4000        A 214050504522  2390996667 +44.3484222 -114.5588538
6    00 G4000        A  23190211616  1025871482 +43.6727945 -071.5841886
7    00 G4000        A 125935965771 13453455061 +35.5397100 -079.1308636
8    00 G4000        A  23872594714  1030642813 +44.0589536 -072.6710173
9    00 G4000        A  12541999507  1816115183 +41.5798637 -072.7466572
10   00 G4000        A 314198519809   726531289 +34.4346843 -106.1316181
                         geometry
1  MULTIPOLYGON (((-83.10874 2...
2  MULTIPOLYGON (((-87.89243 3...
3  MULTIPOLYGON (((-95.31991 4...
4  MULTIPOLYGON (((-71.67881 4...
5  MULTIPOLYGON (((-116.3584 4...
6  MULTIPOLYGON (((-70.83887 4...
7  MULTIPOLYGON (((-77.89977 3...
8  MULTIPOLYGON (((-72.04187 4...
9  MULTIPOLYGON (((-72.5279 41...
10 MULTIPOLYGON (((-103.0647 3...

Spatial joins

st_join(maryland, us_highways)
Simple feature collection with 466 features and 19 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -79.48765 ymin: 37.88661 xmax: -74.98628 ymax: 39.72304
Geodetic CRS:  NAD83
First 10 features:
    REGION DIVISION STATEFP  STATENS GEOID     GEOIDFQ STUSPS     NAME LSAD
1        3        5      24 01714934    24 0400000US24     MD Maryland   00
1.1      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.2      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.3      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.4      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.5      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.6      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.7      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.8      3        5      24 01714934    24 0400000US24     MD Maryland   00
1.9      3        5      24 01714934    24 0400000US24     MD Maryland   00
    MTFCC.x FUNCSTAT       ALAND     AWATER    INTPTLAT     INTPTLON
1     G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.1   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.2   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.3   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.4   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.5   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.6   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.7   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.8   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
1.9   G4000        A 25151223822 6979843236 +38.9466584 -076.6744939
         LINEARID      FULLNAME RTTYP MTFCC.y                       geometry
1   1104258795090 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.1 1103075320064 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.2 1104258795072 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.3 1104258845568 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.4  110191599447 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.5  110191599448 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.6 1102149806067 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.7 1102149813008 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.8 1102149806121 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....
1.9 1102166635559 Salisbury Byp     M   S1100 MULTIPOLYGON (((-75.756 39....

Non-overlapping joins

st_is_within_distance() is an example of a non-overlapping join:

st_is_within_distance(us_highways, maryland, dist = units::set_units(100, "mi"))
Sparse geometry binary predicate list of length 17522, where the
predicate was `is_within_distance'
first 10 elements:
 1: (empty)
 2: (empty)
 3: (empty)
 4: (empty)
 5: (empty)
 6: (empty)
 7: (empty)
 8: (empty)
 9: (empty)
 10: (empty)

Filtering with spatial joins

You can use these joins for spatial filters using the base R syntax:

is_hwy_within_dist <- st_is_within_distance(
  us_highways,
  maryland,
  dist = units::set_units(100, "mi"),
  sparse = FALSE
)
us_highways[is_hwy_within_dist, ]
Simple feature collection with 2258 features and 4 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -81.72573 ymin: 36.57507 xmax: -74.04969 ymax: 41.34626
Geodetic CRS:  NAD83
First 10 features:
          LINEARID      FULLNAME RTTYP MTFCC                       geometry
17  11013853809147 State Rte 611     S S1100 LINESTRING (-75.1366 40.350...
18  11013853809270 State Rte 611     S S1100 LINESTRING (-75.12634 40.28...
19  11013853809430 State Rte 611     S S1100 LINESTRING (-75.12603 40.28...
75  11013853864377  State Rte 29     S S1100 LINESTRING (-74.76432 40.20...
226  1105598256809  State Rte 26     S S1100 LINESTRING (-77.73622 40.89...
228  1108296536943  State Rte 26     S S1100 LINESTRING (-74.76814 40.20...
229  1108296536969  State Rte 26     S S1100 LINESTRING (-74.71174 40.27...
231  1104486135566  State Rte 26     S S1100 LINESTRING (-77.73356 40.89...
246  1108296538090 State Rte 129     S S1100 LINESTRING (-74.72239 40.18...
247  1108296538091 State Rte 129     S S1100 LINESTRING (-74.7224 40.183...

Filtering with spatial joins

Or using filter() if you use the name of the geomtery column (and convert the matrix output into a logical vector):

filter(
  us_highways,
  as.logical(st_is_within_distance(
    geometry,
    maryland,
    dist = units::set_units(100, "mi"),
    sparse = FALSE
  ))
)
Simple feature collection with 2258 features and 4 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -81.72573 ymin: 36.57507 xmax: -74.04969 ymax: 41.34626
Geodetic CRS:  NAD83
First 10 features:
         LINEARID      FULLNAME RTTYP MTFCC                       geometry
1  11013853809147 State Rte 611     S S1100 LINESTRING (-75.1366 40.350...
2  11013853809270 State Rte 611     S S1100 LINESTRING (-75.12634 40.28...
3  11013853809430 State Rte 611     S S1100 LINESTRING (-75.12603 40.28...
4  11013853864377  State Rte 29     S S1100 LINESTRING (-74.76432 40.20...
5   1105598256809  State Rte 26     S S1100 LINESTRING (-77.73622 40.89...
6   1108296536943  State Rte 26     S S1100 LINESTRING (-74.76814 40.20...
7   1108296536969  State Rte 26     S S1100 LINESTRING (-74.71174 40.27...
8   1104486135566  State Rte 26     S S1100 LINESTRING (-77.73356 40.89...
9   1108296538090 State Rte 129     S S1100 LINESTRING (-74.72239 40.18...
10  1108296538091 State Rte 129     S S1100 LINESTRING (-74.7224 40.183...

Spatial aggregation

us_states_combine <- us_states |>
  group_by(DIVISION) |>
  summarise(
    geometry = st_combine(geometry)
  )

plot(us_states_combine[, 1])

4 Types of geometric operations

  • Simplification
  • Centroids
  • Buffers
  • Clipping (with or without subsetting)
  • Unions
  • Type transformations

Simplification

st_simplify(lower48, dTolerance = 100000)[, 1]
Simple feature collection with 49 features and 1 field (with 5 geometries empty)
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -124.7989 ymin: 25.40562 xmax: -66.90256 ymax: 49.00013
Geodetic CRS:  NAD83
First 10 features:
   REGION                       geometry
1       3 POLYGON ((-78.36567 39.3604...
2       3 POLYGON ((-81.34136 25.4056...
3       2 POLYGON ((-89.45747 37.1908...
4       2 POLYGON ((-89.96571 47.2914...
5       3 POLYGON ((-75.78628 39.6205...
6       1       GEOMETRYCOLLECTION EMPTY
7       4 POLYGON ((-115.9605 47.8981...
8       1 POLYGON ((-70.9832 43.39448...
9       3 POLYGON ((-75.71132 36.2501...
10      1 POLYGON ((-71.58543 44.5129...

Centroids

plot(st_centroid(lower48)[, 1])

Buffers

plot(st_buffer(lower48, dist = 100)[, 1])

plot(st_buffer(lower48, dist = units::set_units(100, "mi"))[, 1])

Clipping (with or without subsetting)

plot(st_intersection(us_highways, maryland)[, 1])

Unions

us_highways_union <- us_highways |>
  group_by(RTTYP) |>
  summarise(
    geometry = st_union(geometry)
  )

plot(us_highways_union[, 1])

Type transformations

plot(st_cast(us_highways[1, ], to = "POINT"))