SPB Git

Repository Public

04-osm-import.sh 4.9 KB 90 lines shellscript

1#!/bin/bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : KA Maps Engine8# File             : server/pipeline/04-osm-import.sh9# Module           : server10# Repository       : https://git.spboucher.ai/ka-maps-engine11# Created          : 2026-12-0112# Last Modified    : 2026-12-0113#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: support@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin   : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License          : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# Importe le fichier préparé dans le schéma `osm_next` (osm2pgsql flex, schéma Lua ka1), puis construit41# les tables généralisées. La version en service (`osm`) n'est pas touchée.42. "$(dirname "$0")/env.sh"43W=$KM_SRC/osm/work44VERSION=${1:-$(cat "$W/import.version")}45TS=$(cat "$W/import.timestamp")46log "import de la version $VERSION dans osm_next"47psqlq -f "$KM_PIPE/sql/meta.sql"48psqlq -c "DROP SCHEMA IF EXISTS osm_next CASCADE; CREATE SCHEMA osm_next AUTHORIZATION kamaps;"49psqlq -c "INSERT INTO meta.version (version, status, osm_timestamp) VALUES ('$VERSION', 'building', '$TS')50          ON CONFLICT (version) DO UPDATE SET status = 'building', osm_timestamp = EXCLUDED.osm_timestamp, created_at = now()51          WHERE meta.version.status <> 'active';"52if [ "$(psql -X -At "$KM_DB_URL" -c "SELECT status FROM meta.version WHERE version = '$VERSION'")" = "active" ]; then53  log "la version $VERSION est déjà active : rien à faire"; exit 354fi55# Sans --slim : tout en mémoire (64 Go), pas de tables intermédiaires ; l'import complet est rejoué chaque semaine.56KM_SCHEMA=osm_next osm2pgsql --create --output=flex --style "$KM_PIPE/lua/ka1.lua" \57  -d "$KM_DB_URL" --number-processes "$KM_THREADS" --log-progress=false "$W/import.osm.pbf"58log "généralisation"59PGOPTIONS="-c search_path=osm_next,public" psql -X -q -v ON_ERROR_STOP=1 "$KM_DB_URL" -f "$KM_PIPE/sql/generalize.sql"60log "couvert du sol bas zoom (généralisation par trame)"61"$KM_PIPE/04b-landcover-lowzoom.sh" osm_next62# Sources ouvertes complémentaires (schéma ext, chargé par 09-ext.sh) : empreintes manquantes puis hauteurs LiDAR.63has() { [ "$(psql -X -At "$KM_DB_URL" -c "SELECT to_regclass('$1') IS NOT NULL")" = "t" ]; }64if has ext.odb_building; then65  log "enrichissement : empreintes ODB absentes d'OSM"66  PGOPTIONS="-c search_path=osm_next,ext,public" psql -X -q -v ON_ERROR_STOP=1 "$KM_DB_URL" -f "$KM_PIPE/sql/enrich-odb.sql"67fi68if has ext.nrcan_building; then69  log "enrichissement : hauteurs LiDAR RNCan"70  PGOPTIONS="-c search_path=osm_next,ext,public" psql -X -q -v ON_ERROR_STOP=1 "$KM_DB_URL" -f "$KM_PIPE/sql/enrich-heights.sql"71  psqlq -c "VACUUM (ANALYZE) osm_next.building"72  PGOPTIONS="-c search_path=osm_next,public" psql -X -q "$KM_DB_URL" -f "$KM_PIPE/sql/report-heights.sql" > "$KM_LOG/heights-$VERSION.txt" 2>&1 || true73  log "rapport des hauteurs : $KM_LOG/heights-$VERSION.txt"74fi75if [ "$(psql -X -At "$KM_DB_URL" -c "SELECT to_regclass('geo.address') IS NOT NULL AND has_table_privilege('geo.address', 'SELECT')" 2>/dev/null)" = "t" ]; then76  log "enrichissement : numéros civiques de la base d'adresses (geo.address)"77  PGOPTIONS="-c search_path=osm_next,public" psql -X -q -v ON_ERROR_STOP=1 "$KM_DB_URL" -f "$KM_PIPE/sql/enrich-housenum.sql"78else79  log "geo.address absente ou illisible : numéros civiques OSM seuls"80fi81psqlq -c "GRANT USAGE ON SCHEMA osm_next TO kamaps_ro, km_geo; GRANT SELECT ON ALL TABLES IN SCHEMA osm_next TO kamaps_ro, km_geo;"82psqlq <<SQL83UPDATE meta.version SET counts = (84  SELECT jsonb_object_agg(c.relname, c.n) FROM (85    SELECT relname, (xpath('/row/c/text()', query_to_xml(format('SELECT count(*) AS c FROM osm_next.%I', relname), false, true, '')))[1]::text::bigint AS n86      FROM pg_class JOIN pg_namespace ns ON ns.oid = relnamespace WHERE ns.nspname = 'osm_next' AND relkind = 'r') c)87 WHERE version = '$VERSION';88SQL89log "import terminé : $(psql -X -At "$KM_DB_URL" -c "SELECT counts FROM meta.version WHERE version = '$VERSION'")"90
Files main
Menu

Theme