sdplyr

The goal of sdplyr is to provide a dplyr interface to Apache SedonaDB. It lets you use familiar dplyr verbs and the spatial functions documented by sedonafns with SedonaDB-backed lazy data frames.

Installation

sdplyr can be installed from R-multiverse:

install.packages(
  "sdplyr",
  repos = c("https://community.r-multiverse.org", "https://cloud.r-project.org")
)

You can install the development version of sdplyr from GitHub with:

pak::pkg_install("apache/sedona-db/r/sdplyr")

Installing a development version of sdplyr requires a Rust compiler and a GEOS system dependency (e.g., brew install geos or apt-get install libgeos-dev). Install instructions for these dependencies on other platforms can be found on the sf package homepage.

Example

Read GeoParquet files into lazy SedonaDB data frames and use dplyr verbs to perform a spatial join:

library(sdplyr)
#> ── Attaching sdplyr packages ───────────────────────────────────────────────────────────────────────── 0.4.0.9000 ──
#> ✔ sedonadb  0.4.0.9000
#> ✔ sedonafns 0.4.0.9000
#> ✔ dplyr     1.2.0

cities_url <- "https://raw.githubusercontent.com/geoarrow/geoarrow-data/v0.2.0/natural-earth/files/natural-earth_cities.parquet"
countries_url <- "https://raw.githubusercontent.com/geoarrow/geoarrow-data/v0.2.0/natural-earth/files/natural-earth_countries.parquet"

cities <- sd_read_parquet(cities_url)
countries <- sd_read_parquet(countries_url)

cities |>
  inner_join(
    countries,
    by = sd_join_intersects()
  ) |>
  filter(continent != "North America") |>
  select(
    city = name.x,
    country = name.y,
    continent
  ) |>
  arrange(country) |>
  head(10)
#> <sedonab_dataframe: ?? x 3>
#> ┌──────────────┬─────────────┬───────────────┐
#> │     city     ┆   country   ┆   continent   │
#> │     utf8     ┆     utf8    ┆      utf8     │
#> ╞══════════════╪═════════════╪═══════════════╡
#> │ Kabul        ┆ Afghanistan ┆ Asia          │
#> ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Tirana       ┆ Albania     ┆ Europe        │
#> ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Algiers      ┆ Algeria     ┆ Africa        │
#> ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Luanda       ┆ Angola      ┆ Africa        │
#> ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Buenos Aires ┆ Argentina   ┆ South America │
#> ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Yerevan      ┆ Armenia     ┆ Asia          │
#> └──────────────┴─────────────┴───────────────┘
#> Preview of up to 6 row(s)

Spatial predicates can also compare a geometry column with an R spatial object. For example, this query finds cities inside a longitude-latitude bounding box:

cities |>
  filter(
    sd_intersects(
      geometry,
      wk::rct(-80, 40, -60, 60, wk::wk_crs_longlat())
    )
  )
#> <sedonab_dataframe: ?? x 2>
#> ┌──────────┬─────────────────────────────────────────────┐
#> │   name   ┆                   geometry                  │
#> │   utf8   ┆                   geometry                  │
#> ╞══════════╪═════════════════════════════════════════════╡
#> │ Ottawa   ┆ POINT(-75.7019612 45.4186427)               │
#> ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ Toronto  ┆ POINT(-79.38945855491194 43.66464454743429) │
#> ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
#> │ New York ┆ POINT(-73.99571754361698 40.72156174972766) │
#> └──────────┴─────────────────────────────────────────────┘
#> Preview of up to 6 row(s)