# onthegomap/maplibre-contour architecture

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

- Interactive diagram: https://gitdiagram.com/onthegomap/maplibre-contour
- Repository: https://github.com/onthegomap/maplibre-contour

## Overview

##### Project Type Analysis
This is a specialized browser-based plugin for MapLibre GL JS that generates contour lines from elevation data in real-time. It's a client-side processing library with worker thread capabilities.

##### Key Components

a. Main Components:
- Client Browser Environment
- Main Thread Components
- Web Worker Thread
- MapLibre GL JS Integration
- External DEM Tile Server

b. Core Processing Pipeline:
- DEM Source Management
- Tile Processing
- Contour Generation
- Vector Tile Encoding

##### Specific Components

Main Thread:
- DemSource (Entry point)
- RemoteDemManager
- Protocol Handler
- Cache System

Worker Thread:
- LocalDemManager
- Image Decoder
- HeightTile Processor
- Isoline Generator
- Vector Tile Encoder

##### Key Processes to Highlight

a. Input Flow:
- MapLibre tile request
- DEM tile fetching
- Neighbor tile fetching

b. Processing Flow:
- Image decoding
- Height data processing
- Contour generation
- Vector tile encoding

c. Caching System:
- DEM tile cache
- Processed data cache

##### Additional Elements

Annotations for:
- Configuration options
- Thread communication
- Error handling
- Timeout mechanisms
- Cache size limits

##### Technical Details to Note

- Worker thread communication via Actor pattern
- Tile processing pipeline
- MapLibre protocol integration
- Caching mechanisms
- Error handling and timeouts

Remember to include the key transformations:
DEM Tiles → Height Data → Isolines → Vector Tiles → Rendered Contours

This will provide a clear understanding of both the system's architecture and its data processing pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Browser
        MLGL[MapLibre GL JS]
        MainThread
        WorkerThread
    end

    subgraph External
        DEM["DEM Tile Server"]
    end

    subgraph MainThread
        DS["DemSource"]:::entryPoint
        RDM["RemoteDemManager"]:::remoteManager
        Cache["Cache System"]:::cache
        PH["Protocol Handler"]
        Actor["Actor System"]:::actor
    end

    subgraph WorkerThread
        WD["Worker Dispatch"]:::workerDispatch
        LDM["LocalDemManager"]:::localManager
        ID["Image Decoder"]:::imageDecoder
        HTP["HeightTile Processor"]:::heightTile
        IG["Isoline Generator"]:::isolineGen
        VTE["Vector Tile Encoder"]:::vectorTile
        Perf["Performance Monitor"]:::performance
    end

    %% External Connections
    DEM -->|DEM Tiles| RDM
    MLGL -->|Tile Requests| DS

    %% Main Thread Flow
    DS -->|Manages| RDM
    RDM -->|Caches| Cache
    DS -->|Communicates| Actor
    Actor -->|Dispatches| WD

    %% Worker Thread Flow
    WD -->|Manages| LDM
    LDM -->|Processes| ID
    ID -->|Feeds| HTP
    HTP -->|Generates| IG
    IG -->|Encodes| VTE
    Perf -.->|Monitors| WD

    %% Return Flow
    VTE -->|Vector Tiles| Actor
    Actor -->|Returns| DS
    DS -->|Renders| MLGL

    %% Click Events
    click DS "https://github.com/onthegomap/maplibre-contour/blob/main/src/dem-source.ts"
    click RDM "https://github.com/onthegomap/maplibre-contour/blob/main/src/remote-dem-manager.ts"
    click Cache "https://github.com/onthegomap/maplibre-contour/blob/main/src/cache.ts"
    click Actor "https://github.com/onthegomap/maplibre-contour/blob/main/src/actor.ts"
    click LDM "https://github.com/onthegomap/maplibre-contour/blob/main/src/local-dem-manager.ts"
    click ID "https://github.com/onthegomap/maplibre-contour/blob/main/src/decode-image.ts"
    click HTP "https://github.com/onthegomap/maplibre-contour/blob/main/src/height-tile.ts"
    click IG "https://github.com/onthegomap/maplibre-contour/blob/main/src/isolines.ts"
    click VTE "https://github.com/onthegomap/maplibre-contour/blob/main/src/vtpbf.ts"
    click WD "https://github.com/onthegomap/maplibre-contour/blob/main/src/worker-dispatch.ts"
    click Perf "https://github.com/onthegomap/maplibre-contour/blob/main/src/performance.ts"

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px
    classDef external fill:#a8d1f0,stroke:#333
    classDef entryPoint fill:#90EE90,stroke:#333
    classDef remoteManager fill:#98FB98,stroke:#333
    classDef cache fill:#FFD700,stroke:#333
    classDef actor fill:#DDA0DD,stroke:#333
    classDef workerDispatch fill:#FFA07A,stroke:#333
    classDef localManager fill:#F0E68C,stroke:#333
    classDef imageDecoder fill:#20B2AA,stroke:#333
    classDef heightTile fill:#87CEEB,stroke:#333
    classDef isolineGen fill:#98FB98,stroke:#333
    classDef vectorTile fill:#DDA0DD,stroke:#333
    classDef performance fill:#FFB6C1,stroke:#333
```
