# r-tmap/tmap architecture

> Architecture diagram of the GitHub repository r-tmap/tmap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/r-tmap/tmap
- Repository: https://github.com/r-tmap/tmap

## Overview

##### Main Components (Core Architecture)

a) Data Processing Layer
- Data handling components (spatial_*.R files)
- Support for different spatial data formats (sf, sp, stars, terra)
- Data transformation pipeline (step*.R files)

b) Rendering Layer
- Split into two main rendering paths:
1. Static Maps (tmapGrid*.R files)
2. Interactive Maps (tmapLeaflet*.R files)

c) Styling and Configuration Layer
- Theme management (tmap_style*.R files)
- Scale handling (tmapScale*.R files)
- Layout configuration (tm_layout.R)

##### Component Relationships

**Input Data → Data Processing → Styling/Configuration → Rendering → Output**

With two parallel paths for static and interactive outputs.

##### Key Design Elements

Use distinct visual elements:

##### Specific Implementation Details

Key architectural features:
- Grammar of Graphics implementation
- Layer-based composition system
- Plugin system for different spatial data formats

##### External Dependencies

Connections to:
- Leaflet.js (for interactive maps)
- Grid graphics system (for static maps)
- Spatial libraries (sf, sp, stars, terra)

##### Component Interaction Flow

- Data flow between components
- Configuration application points
- Rendering pipeline stages

##### The transformation pipeline from raw data to final visualization

Keep the abstraction level focused on the major architectural components while showing the relationship between the grammar of graphics implementation and the actual rendering systems.

This design reflects tmap's nature as a specialized visualization library with a strong focus on thematic mapping, showing both its flexibility in handling different spatial data formats and its dual-mode rendering capability.

Remember to include version information (v4) and note the significant architectural changes from v3, as this appears to be a major version transition based on the README.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Data Processing Layer"
        direction TB
        DataInput[("Input Data")]
        SpatialHandlers["Spatial Data Handlers"]:::data
        DataTransform["Data Transformation Pipeline"]:::data
        
        subgraph "Spatial Handlers"
            direction TB
            SF["SF Handler"]:::data
            SP["SP Handler"]:::data
            Stars["Stars Handler"]:::data
            Terra["Terra Handler"]:::data
        end

        subgraph "Transform Pipeline"
            direction TB
            Step1["Step 1: Rearrange"]:::data
            Step2["Step 2: Data"]:::data
            Step3["Step 3: Transform"]:::data
            Step4["Step 4: Plot"]:::data
        end
    end

    subgraph "Styling and Configuration"
        direction TB
        ThemeManagement["Theme Management"]:::config
        ScaleHandling["Scale Handling"]:::config
        LayoutConfig["Layout Configuration"]:::config
        
        subgraph "Scale Components"
            direction TB
            ScaleCont["Continuous Scale"]:::config
            ScaleDisc["Discrete Scale"]:::config
            ScaleCat["Categorical Scale"]:::config
        end
    end

    subgraph "Core Components"
        direction TB
        ShapeHandler["Shape Handler"]:::core
        LayerMgmt["Layer Management"]:::core
        LegendSystem["Legend System"]:::core
        FacetSystem["Faceting System"]:::core
    end

    subgraph "Rendering Layer"
        direction TB
        subgraph "Static Maps"
            direction TB
            GridInit["Grid Init"]:::render
            GridRun["Grid Run"]:::render
            GridComp["Grid Composition"]:::render
        end

        subgraph "Interactive Maps"
            direction TB
            LeafletInit["Leaflet Init"]:::render
            LeafletRun["Leaflet Run"]:::render
            LeafletLayers["Leaflet Layers"]:::render
        end
    end

    subgraph "Output Management"
        direction TB
        Export["Export"]:::output
        Animation["Animation"]:::output
        Save["Save"]:::output
        ModeManager["Mode Manager"]:::output
    end

    DataInput --> SpatialHandlers
    SpatialHandlers --> DataTransform
    DataTransform --> ShapeHandler
    ShapeHandler --> LayerMgmt
    LayerMgmt --> |Static| GridInit
    LayerMgmt --> |Interactive| LeafletInit
    ThemeManagement --> LayerMgmt
    ScaleHandling --> LayerMgmt
    LayoutConfig --> LayerMgmt
    LegendSystem --> GridComp
    LegendSystem --> LeafletLayers
    FacetSystem --> GridComp
    FacetSystem --> LeafletLayers
    GridComp --> Export
    LeafletLayers --> Export
    Export --> Save
    Animation --> Save
    ModeManager --> Export

    %% Click events for component mapping
    click SF "https://github.com/r-tmap/tmap/blob/master/R/spatial_sf.R"
    click SP "https://github.com/r-tmap/tmap/blob/master/R/spatial_sp.R"
    click Stars "https://github.com/r-tmap/tmap/blob/master/R/spatial_stars.R"
    click Terra "https://github.com/r-tmap/tmap/blob/master/R/spatial_terra.R"
    click Step1 "https://github.com/r-tmap/tmap/blob/master/R/step1_rearrange.R"
    click Step2 "https://github.com/r-tmap/tmap/blob/master/R/step2_data.R"
    click Step3 "https://github.com/r-tmap/tmap/blob/master/R/step3_trans.R"
    click Step4 "https://github.com/r-tmap/tmap/blob/master/R/step4_plot.R"
    click GridInit "https://github.com/r-tmap/tmap/blob/master/R/tmapGridInit.R"
    click GridRun "https://github.com/r-tmap/tmap/blob/master/R/tmapGridRun.R"
    click GridComp "https://github.com/r-tmap/tmap/blob/master/R/tmapGridComp.R"
    click LeafletInit "https://github.com/r-tmap/tmap/blob/master/R/tmapLeafletInit.R"
    click LeafletRun "https://github.com/r-tmap/tmap/blob/master/R/tmapLeafletRun.R"
    click LeafletLayers "https://github.com/r-tmap/tmap/blob/master/R/tmapLeaflet_layers.R"
    click ThemeManagement "https://github.com/r-tmap/tmap/blob/master/R/tmap_style.R"
    click ScaleCont "https://github.com/r-tmap/tmap/blob/master/R/tmapScaleContinuous.R"
    click ScaleDisc "https://github.com/r-tmap/tmap/blob/master/R/tmapScaleDiscrete.R"
    click ScaleCat "https://github.com/r-tmap/tmap/blob/master/R/tmapScaleCategorical.R"
    click LayoutConfig "https://github.com/r-tmap/tmap/blob/master/R/tm_layout.R"
    click ShapeHandler "https://github.com/r-tmap/tmap/blob/master/R/tm_shape.R"
    click LayerMgmt "https://github.com/r-tmap/tmap/blob/master/R/tm_layers_polygons.R"
    click LegendSystem "https://github.com/r-tmap/tmap/blob/master/R/tm_legend.R"
    click FacetSystem "https://github.com/r-tmap/tmap/blob/master/R/tm_facets.R"
    click Export "https://github.com/r-tmap/tmap/blob/master/R/tmap_export.R"
    click Animation "https://github.com/r-tmap/tmap/blob/master/R/tmap_animation.R"
    click Save "https://github.com/r-tmap/tmap/blob/master/R/tmap_save.R"
    click ModeManager "https://github.com/r-tmap/tmap/blob/master/R/tmap_mode.R"

    %% Styling
    classDef data fill:#a8d1f0,stroke:#333,stroke-width:1px
    classDef config fill:#fff3b0,stroke:#333,stroke-width:1px
    classDef core fill:#b5e6b5,stroke:#333,stroke-width:1px
    classDef render fill:#d4a8f0,stroke:#333,stroke-width:1px
    classDef output fill:#f0a8a8,stroke:#333,stroke-width:1px
```
