# 1m elevation raster. Fetches the DEM tiles listed by `elevation_grids_links`,
# downsamples to a coarser averaged grid, then samples per-way ascent/descent.
name: elevation
description: " 1m elevation "
enable: false
class_name: elevationMapper
depends_on: elevation_grids_links
debug:
  endpoint: elevation
data_type: static
source:
  input: none
  mode: multi
  check_metadata:
    enable: true
    keys: ["last_modified"]
  file_path: ""
  fetch: http
  save_local: true
  destination: tmp/elevation_zips/elevation.zip
  response_type: zip
  multi_fetch:
    enable: true
    strategy: explicit_url_list
    urls:
      # Produced by the `elevation_grids_links` datasource (after_filter_hook).
      input: data/grid/elevation_grid_links.json
job:
  executor: process
  trigger:
    type:
      name: run_once
enrichment_operators:
  operators:
    - type: raster_aggregate
      source_table: staging
      raster_col: rast
      target_col: rast
      cell_size: 10          # 1 m DEM -> 10 m averaged cells (tunable)
      algorithm: Average     # ST_Resample: Average | Bilinear | Min | Max | Mode
mapping:
  enable: true
  table_name: elevation_mapping
  strategy:
    type: sql_template
    sql_file: mapping_sql/elevation_raster.sql
  base_table:
    column_name: "elevation_factor"
    column_type: Integer
storage:
  persistent: true
  expires_after: 168h   # match the elevation grid update interval (7 days)
  staging:
    table_name: elevation_staging
    table_class: ElevationStagingTable
  enrichment:
    table_name: elevation_enrichment
    table_class: ElevationEnrichmentTable

# Materialized view for this datasource (replaces mv_configs/mv_elevation.yaml).
# refresh defaults to `concurrently` (mv_defaults), valid here because of the
# unique (way_id, way_link_index) index. The firing/dependency datasource name
# defaults to this datasource; only the extra dependency tables are listed.
materialized_view:
  name: mv_ways_with_elevation
  description: "Per-way elevation stats (ascent, descent, max slope) from the elevation pipeline"
  depends_on:
    tables:
      - { name: ways_base }
      - { name: elevation_mapping }
  definition:
    select_sql: |
      SELECT
          w.way_id AS way_id,
          w.way_link_index,
          COALESCE(m.total_ascent, 0.0)  AS total_ascent,
          COALESCE(m.total_descent, 0.0) AS total_descent,
          COALESCE(m.max_slope, 0.0)     AS max_slope
      FROM {schema}.ways_base w
      LEFT JOIN {schema}.elevation_mapping m
          ON w.id = m.way_id
  indexes:
    - { name: idx_mv_elevation_way_id_link, columns: [way_id, way_link_index], unique: true }
    - { name: idx_mv_elevation_way_id, columns: [way_id] }
