How rspatial/geodata works
Project Type and Purpose
• This is an R package (full-featured library) that exposes a collection of download‐and‐load functions for spatial and environmental data.
• Its purpose is to let end users call single R functions (e.g., worldclim, soil_world) that under the hood retrieve, cache, and import large geo-datasets.
File Structure
• Top level:
– DESCRIPTION, NAMESPACE, README.md, LICENSE – standard R package metadata.
– R/ – contains function definitions grouped by dataset type (biooracle, soil, crop, etc.).
– man/ – documentation for each function.
– inst/ex/ – example data shipped with the package.
• No separate “frontend” or “backend”: it’s library code invoked in the user’s R session.
• Implicit subsystems:
– HTTP/client subsystem (via httr/curl) for downloads.
– Local file cache I/O subsystem.
– Data ingestion (unzip, readRaster/readOGR).
Extract Insights from the README
• The package’s API surface is a set of ~20 functions, each pointing at an external data source (Bio-Oracle, GADM, SoilGrids, WorldClim, etc.).
• Each function:
1. Resolves target URL/tiles.
2. Downloads data (with caching).
3. Reads files into R spatial objects.
4. Returns the object to the user.
a. User Environment (R session & workspace)
b. geodata Package (library code in R/)
– Submodules by data domain: climate, elevation, soil, crops, occurrence, administrative boundaries, travel time.
– Utility submodule: download manager, path helpers, URI builders.
c. External Data Providers (REST APIs / FTP servers):
– Bio-Oracle, CMIP6, GADM, SoilGrids, GBIF, SEDAC, WorldClim, MapSPAM, etc.
d. Local Cache / File System
e. CRAN/GitHub (installation source)
Relationships and Data Flows
• Start: User calls geodata::<function>() in R.
• The function calls the internal URI helper → constructs URL.
• The download manager checks local cache folder (instanced via geodata_path()).
– If not present or outdated, it issues HTTP(S)/FTP requests to the external service.
– Streams data to disk.
• Once downloaded, the appropriate reader (e.g., raster::raster(), utils::unzip(), sf::st_read()) ingests the files into memory.
• The R function returns a spatial object to the user.
Architectural Patterns & Design Principles
• Single‐responsibility: each R script in R/ implements one dataset source.
• DRY utilities: uri_helpers.R, path.R, download.R manage common tasks.
• Caching layer: avoids repeated downloads, improves performance.
• Extensibility: new data sources can be added by creating a new R/ function that reuses helpers.
– R session & geodata library (component)
– CRAN/GitHub (package source)
– From CRAN/GitHub → local installation → R library folder.
– From R function calls → URI helpers/download manager → external service.
– From external service → local cache → data‐reader → R session.