Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : KA Maps Engine8# File : server/pipeline/geo/01_fetch.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# KA Maps — téléchargement des sources géographiques ouvertes (licences : docs/DATA_LICENSING.md).41# ./01_fetch.sh ne télécharge que ce qui manque42# REFRESH=1 ./01_fetch.sh retélécharge tout ; un fichier n'est remplacé que si le nouveau est complet et d'une43# taille plausible (au moins la moitié de l'ancien) — sinon l'ancien reste et on le signale44set -uo pipefail45SRC=${SRC:-/var/lib/kamaps/src/geo}46REFRESH=${REFRESH:-0}47UA="Mozilla/5.0 (X11; Linux x86_64) kamaps-import"48mkdir -p "$SRC"/{nar,oda,aq,sda,cp,muni,quartiers,osm}49FAILED=050get() {51 local url=$1 dst=$252 if [ "$REFRESH" != 1 ] && [ -s "$dst" ]; then return 0; fi53 if ! curl -sfL --retry 3 --retry-delay 10 -A "$UA" -o "$dst.new" "$url"; then54 echo "ECHEC téléchargement $url" >&2; rm -f "$dst.new"; FAILED=$((FAILED + 1)); return 055 fi56 local new old; new=$(stat -c %s "$dst.new"); old=$(stat -c %s "$dst" 2>/dev/null || echo 0)57 if [ "$new" -lt $((old / 2)) ] || [ "$new" -lt 1000 ]; then58 echo "REFUS $dst : nouveau fichier de $new octets contre $old" >&2; rm -f "$dst.new"; FAILED=$((FAILED + 1)); return 059 fi60 mv "$dst.new" "$dst"61}62DQ=https://www.donneesquebec.ca/recherche/dataset6364# Statistique Canada — Registre national d'adresses : édition la plus récente publiée (AAAAMM), sur 18 mois65cur=$(cat "$SRC/nar/edition.txt" 2>/dev/null || echo "")66latest=""67for i in $(seq 0 18); do68 ym=$(date -u -d "$(date -u +%Y-%m-15) -$i month" +%Y%m)69 if [ "$(curl -s -o /dev/null -w '%{http_code}' -I "https://www150.statcan.gc.ca/n1/pub/46-26-0002/2022001/$ym.zip")" = 200 ]; then latest=$ym; break; fi70done71if [ -n "$latest" ] && [ "$latest" != "$cur" ]; then72 if ( cd "$SRC/nar" && aria2c -q -x8 -s8 -c -o "$latest.zip.part" "https://www150.statcan.gc.ca/n1/pub/46-26-0002/2022001/$latest.zip" \73 && rm -rf Addresses.new && mkdir Addresses.new && unzip -q -j "$latest.zip.part" "Addresses/Address_24*" -d Addresses.new \74 && [ "$(ls Addresses.new | wc -l)" -ge 1 ] ); then75 ( cd "$SRC/nar" && rm -rf Addresses && mv Addresses.new Addresses && rm -f ./*.zip && mv "$latest.zip.part" "$latest.zip" && echo "$latest" > edition.txt )76 echo "RNA : édition $latest"77 else78 echo "ECHEC RNA édition $latest (on garde $cur)" >&2; FAILED=$((FAILED + 1)); rm -rf "$SRC/nar/Addresses.new" "$SRC/nar/$latest.zip.part"79 fi80fi81[ -d "$SRC/nar/Addresses" ] || { echo "RNA absent" >&2; exit 1; }8283# Base de données ouvertes d'adresses (2021, figée)84get https://www150.statcan.gc.ca/n1/pub/46-26-0001/2021001/ODA_QC_v1.zip "$SRC/oda/ODA_QC_v1.zip"85( cd "$SRC/oda" && unzip -q -o ODA_QC_v1.zip ODA_QC_v1.csv )86# Gouvernement du Québec (MRNF) — AQréseau, découpages administratifs (SDA 1/20 000) ; ISQ — CP Territoires87get "https://diffusion.mern.gouv.qc.ca/Diffusion/RGQ/Vectoriel/Carte_Topo/Local/AQReseau/OGC(GPKG)/AQreseau_GPKG.zip" "$SRC/aq/AQreseau_GPKG.zip"88( cd "$SRC/aq" && unzip -q -o AQreseau_GPKG.zip )89get https://diffusion.mern.gouv.qc.ca/diffusion/RGQ/Vectoriel/Theme/Local/SDA_20k/GPKG/SDA.gpkg.zip "$SRC/sda/SDA.gpkg.zip"90( cd "$SRC/sda" && unzip -q -o SDA.gpkg.zip )91get $DQ/1a6267da-82ed-4ea0-b35d-9e9618a58ce7/resource/bbd5521c-120f-494b-b2a3-a6a682d8d458/download/cp_territoires.csv "$SRC/cp/cp_territoires.csv"92# Adresses ponctuelles municipales (CC-BY 4.0)93get https://donnees.montreal.ca/dataset/63317d72-426e-41fa-bfef-b0f28bb06ce1/resource/fed5fd02-5535-458e-b13f-66e7a31a6d78/download/adresses.csv "$SRC/muni/montreal.csv"94get $DQ/2567eac8-a3ce-423a-a43d-6b8e74708c1d/resource/cef2c0b7-8eac-4466-9a0b-6987501edace/download/vdq-adresse.csv "$SRC/muni/quebec.csv"95get $DQ/280c3684-29a2-4f74-babb-7d74117096f5/resource/1222e9d9-eb58-4ae5-8c62-d7164a87e758/download/adresse-ponctuelle-norme.json "$SRC/muni/gatineau.geojson"96get "https://donneesouvertes-sherbrooke.opendata.arcgis.com/api/download/v1/items/d03eeb91762a41259eb8b959635c7437/csv?layers=0" "$SRC/muni/sherbrooke.csv"97get $DQ/59622b04-1132-4e6c-b63e-36182aa810ae/resource/23f7f854-278e-4fd3-838d-fad233713f71/download/sag_adresseponctuelle.csv "$SRC/muni/saguenay.csv"98get $DQ/c7811bb5-ed56-4356-a574-7b32193f990d/resource/8254bc8f-8b0f-4085-8213-3449553e0d27/download/adresse-ponctuelle.csv "$SRC/muni/levis.csv"99# Quartiers (CC-BY 4.0) : Montréal (référence en habitation, sociologiques), Québec, Longueuil, Trois-Rivières, Lévis100get https://donnees.montreal.ca/dataset/f38c91a1-e33f-4475-a112-3b84b1c60c1e/resource/a80e611f-5336-4306-ba2a-fd657f0f00fa/download/quartierreferencehabitation.geojson "$SRC/quartiers/mtl_reference.geojson"101get https://donnees.montreal.ca/fr/dataset/c8f37ad6-16ff-4cdc-9e5a-e47898656fc9/resource/9f68405f-c658-42e1-a786-cb43a29e344e/download/quartiers_sociologiques_2014.geojson "$SRC/quartiers/mtl_sociologiques.geojson"102get $DQ/5b1ae6f2-6719-46df-bd2f-e57a7034c917/resource/436c85aa-88d9-4e57-9095-b72b776a71a0/download/vdq-quartier.geojson "$SRC/quartiers/quebec.geojson"103get $DQ/8d486ac4-022c-4687-8bff-90df0566ef80/resource/f97982f3-021b-4ac5-a6da-6afdf04b57d1/download/quartiers.json "$SRC/quartiers/longueuil.geojson"104get $DQ/a7383b8a-4ebf-4e2d-ac31-2c9fa3050f06/resource/7a144527-1c93-4e01-949b-4b78fe4d4288/download/quartier-nom-populaire-v3r.json "$SRC/quartiers/trois_rivieres.geojson"105get $DQ/b68b2510-5704-48ba-95ca-cb2cf1c25d61/resource/daa9a1ef-23ad-4570-81ec-e613e9c7c37b/download/secteur.json "$SRC/quartiers/levis.geojson"106echo "FETCH_DONE échecs=$FAILED"107# Un téléchargement refusé ou raté n'arrête pas la reconstruction : l'ancien fichier sert, et la validation tranche.108exit 0109