# rspatial/geodata architecture

> Architecture diagram of the GitHub repository rspatial/geodata, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rspatial/geodata
- Repository: https://github.com/rspatial/geodata

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Installation Source
    CRAN["CRAN / GitHub"]:::source
    Metadata["DESCRIPTION & NAMESPACE"]:::metadata
    Install["Installed geodata Package"]:::pkg
    CRAN -->|"install"| Metadata --> Install

    %% R Library Structure
    Install --> Rdir["R/ (Core Library)"]:::pkg
    Rdir --> insthook["Package Init (zzz.R)"]:::util
    Rdir --> dlmgr["Download Manager\n(R/download.R)"]:::util
    Rdir --> path["Path Helpers\n(R/path.R)"]:::util
    Rdir --> uri["URI Builders\n(R/uri_helpers.R)"]:::util

    %% Domain Modules
    subgraph "Dataset Domains"
      direction TB
      worldclim["WorldClim"]:::climate
      biooracle["Bio-Oracle"]:::climate
      elevation["Elevation"]:::elev
      soilgrids["SoilGrids"]:::soil
      afsis["AFSIS"]:::soil
      isda["ISDA"]:::soil
      cropspam["MapSPAM"]:::crop
      sacks["Sacks"]:::crop
      monfreda["Monfreda"]:::crop
      gadm["GADM"]:::admin
      countries["Countries"]:::admin
      occurrence["GBIF Occurrence"]:::occ
      osm["OSM"]:::occ
      traveltime["Travel Time"]:::trav
      pop["Population"]:::pop
      landuse["Land Use"]:::land
    end
    Rdir --> worldclim
    Rdir --> biooracle
    Rdir --> elevation
    Rdir --> soilgrids
    Rdir --> afsis
    Rdir --> isda
    Rdir --> cropspam
    Rdir --> sacks
    Rdir --> monfreda
    Rdir --> gadm
    Rdir --> countries
    Rdir --> occurrence
    Rdir --> osm
    Rdir --> traveltime
    Rdir --> pop
    Rdir --> landuse

    %% User Session
    rsess["R Session\n(User)"]:::session
    rsess -->|calls function| worldclim
    rsess -->|calls function| biooracle

    %% Utilities Flow
    worldclim -->|"build URL"| uri
    biooracle --> uri
    worldclim -->|"cache check"| path
    biooracle --> path
    worldclim -->|"download"| dlmgr
    biooracle --> dlmgr

    %% External Services and Cache
    dlmgr -->|"HTTP GET"| externals
    subgraph "External Data Providers"
      direction TB
      externals["APIs/FTP\n(Bio-Oracle, WorldClim, GADM, SoilGrids, GBIF, etc.)"]:::cloud
    end
    externals -->|"data stream"| cache["Local Cache\n(File System)"]:::cache

    %% Data Ingestion
    cache --> reader["Data Ingestion\n(raster, unzip, sf)"]:::util
    reader -->|return spatial object| rsess

    %% Example Data Flow
    instex["Example Data\n(inst/ex/)"]:::cache
    instex --> reader

    %% Click Events
    click Rdir "https://github.com/rspatial/geodata/tree/master/R/"
    click insthook "https://github.com/rspatial/geodata/blob/master/R/zzz.R"
    click dlmgr "https://github.com/rspatial/geodata/blob/master/R/download.R"
    click path "https://github.com/rspatial/geodata/blob/master/R/path.R"
    click uri "https://github.com/rspatial/geodata/blob/master/R/uri_helpers.R"
    click worldclim "https://github.com/rspatial/geodata/blob/master/R/worldclim.R"
    click biooracle "https://github.com/rspatial/geodata/blob/master/R/biooracle.R"
    click elevation "https://github.com/rspatial/geodata/blob/master/R/elevation.R"
    click soilgrids "https://github.com/rspatial/geodata/blob/master/R/soil_grids.R"
    click afsis "https://github.com/rspatial/geodata/blob/master/R/soil_afsis.R"
    click isda "https://github.com/rspatial/geodata/blob/master/R/soil_isda.R"
    click cropspam "https://github.com/rspatial/geodata/blob/master/R/crop_spam.R"
    click sacks "https://github.com/rspatial/geodata/blob/master/R/crop_sacks.R"
    click monfreda "https://github.com/rspatial/geodata/blob/master/R/crops_monfreda.R"
    click gadm "https://github.com/rspatial/geodata/blob/master/R/gadm.R"
    click countries "https://github.com/rspatial/geodata/blob/master/R/countries.R"
    click occurrence "https://github.com/rspatial/geodata/blob/master/R/occurrence.R"
    click osm "https://github.com/rspatial/geodata/blob/master/R/osm.R"
    click traveltime "https://github.com/rspatial/geodata/blob/master/R/travel_time.R"
    click pop "https://github.com/rspatial/geodata/blob/master/R/pop.R"
    click landuse "https://github.com/rspatial/geodata/blob/master/R/landuse.R"
    click instex "https://github.com/rspatial/geodata/tree/master/inst/ex/"

    %% Styles
    classDef source fill:#f9f,stroke:#333,stroke-width:1px
    classDef metadata fill:#ff9,stroke:#333,stroke-width:1px
    classDef pkg fill:#9cf,stroke:#333,stroke-width:1px
    classDef util fill:#cfc,stroke:#333,stroke-width:1px
    classDef climate fill:#fdd,stroke:#333,stroke-width:1px
    classDef elev fill:#def,stroke:#333,stroke-width:1px
    classDef soil fill:#fed,stroke:#333,stroke-width:1px
    classDef crop fill:#dfd,stroke:#333,stroke-width:1px
    classDef admin fill:#ddf,stroke:#333,stroke-width:1px
    classDef occ fill:#fde,stroke:#333,stroke-width:1px
    classDef trav fill:#cdf,stroke:#333,stroke-width:1px
    classDef pop fill:#fdc,stroke:#333,stroke-width:1px
    classDef land fill:#cfd,stroke:#333,stroke-width:1px
    classDef session fill:#fcf,stroke:#333,stroke-width:1px
    classDef cache fill:#fc9,stroke:#333,stroke-width:1px
    classDef cloud fill:#cff,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
