SPB Git

Repository Public

09-ext.sh 6.7 KB 121 lines shellscript

1#!/bin/bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : KA Maps Engine8# File             : server/pipeline/09-ext.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# Sources ouvertes complémentaires → schéma `ext` (statique, indépendant des versions OSM) :41#   ext.nrcan_building  RNCan « Automatically Extracted Buildings » (LiDAR, hauteurs mesurées) — OGL-Canada42#   ext.odb_building    Statistique Canada, Base de données ouvertes sur les immeubles v3 (Québec) — OGL-Canada43#   ext.parcel          Ville de Montréal, unités d'évaluation foncière (couche `parcel`) — CC BY 4.044# Rejouable ; à relancer quand une source publie une nouvelle édition. Les fusions avec OSM (hauteurs,45# empreintes manquantes) sont refaites à chaque import par sql/enrich.sql.46. "$(dirname "$0")/env.sh"47E=$KM_SRC/ext; mkdir -p "$E"; cd "$E"48OGR="PG:$(echo "$KM_DB_URL" | sed -E 's#postgres://([^:]+):([^@]+)@([^:]+):([0-9]+)/(.+)#host=\3 port=\4 dbname=\5 user=\1 password=\2#')"49UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36"50WHAT=${1:-all}51psqlq -c "CREATE SCHEMA IF NOT EXISTS ext AUTHORIZATION kamaps; GRANT USAGE ON SCHEMA ext TO kamaps_ro, km_geo;"5253if [ "$WHAT" = all ] || [ "$WHAT" = nrcan ]; then54  F=auto_building_opti_2.parquet55  [ -s $F ] || curl -fsSL -o $F https://ftp.maps.canada.ca/pub/nrcan_rncan/extraction/auto_building/auto_building_opti_2/$F56  [ -x /opt/kamaps/venv/bin/python ] || { sudo python3 -m venv /opt/kamaps/venv && sudo /opt/kamaps/venv/bin/pip -q install pyarrow psycopg2-binary; }57  log "RNCan : chargement des bâtiments de l'emprise du Québec"58  /opt/kamaps/venv/bin/python "$KM_PIPE/lib/load_nrcan.py" "$E/$F"59fi6061if [ "$WHAT" = all ] || [ "$WHAT" = odb ]; then62  for n in 1 2; do63    [ -s ODB_v3_QC_$n.zip ] || curl -fsSL -A "$UA" -o ODB_v3_QC_$n.zip https://www150.statcan.gc.ca/pub/34-26-0001/2018001/zip/ODB_v3_QC_$n.zip64    [ -s odb/ODB_v3_QC_$n.gpkg ] || unzip -qo ODB_v3_QC_$n.zip -d odb65  done66  log "ODB v3 : chargement"67  psqlq -c "DROP TABLE IF EXISTS ext.odb_raw"68  ogr2ogr -f PostgreSQL "$OGR" odb/ODB_v3_QC_1.gpkg -nln ext.odb_raw -lco GEOMETRY_NAME=geom -lco FID=gid -lco SPATIAL_INDEX=NONE \69    -dim XY -nlt MULTIPOLYGON -t_srs EPSG:3857 -sql "SELECT id, source, dataset, csdname, type, floors, height, geom FROM ODB_v3_QC_1" \70    --config PG_USE_COPY YES 2>&1 | grep -v 'event trigger' || true71  ogr2ogr -f PostgreSQL "$OGR" odb/ODB_v3_QC_2.gpkg -nln ext.odb_raw -append \72    -dim XY -nlt MULTIPOLYGON -t_srs EPSG:3857 -sql "SELECT id, source, dataset, csdname, type, floors, height, geom FROM ODB_v3_QC_2" \73    --config PG_USE_COPY YES 2>&1 | grep -v 'event trigger' || true74  [ "$(psql -X -At "$KM_DB_URL" -c "SELECT count(*) > 3000000 FROM ext.odb_raw")" = "t" ] || { log "ERREUR : ODB incomplet"; exit 1; }75  psqlq <<'SQL'76DROP TABLE IF EXISTS ext.odb_building;77CREATE TABLE ext.odb_building AS78  SELECT gid, id AS odb_id, source, dataset, csdname,79         nullif(type, '..') AS type,80         CASE WHEN floors ~ '^[0-9]+(\.[0-9]+)?$' THEN floors::real END AS floors,81         ST_MakeValid(geom)::geometry AS geom, ST_PointOnSurface(ST_MakeValid(geom))::geometry(Point,3857) AS pt,82         ST_Area(geom)::real AS area83    FROM ext.odb_raw WHERE geom IS NOT NULL AND NOT ST_IsEmpty(geom);84DELETE FROM ext.odb_building WHERE GeometryType(geom) NOT IN ('POLYGON','MULTIPOLYGON') OR area < 4;85ALTER TABLE ext.odb_building ALTER COLUMN geom TYPE geometry(MultiPolygon,3857) USING ST_Multi(geom);86CREATE INDEX ON ext.odb_building USING gist (geom);87DROP TABLE ext.odb_raw;88ANALYZE ext.odb_building;89SQL90fi9192if [ "$WHAT" = all ] || [ "$WHAT" = parcel ]; then93  Z=mtl-uef.geojson.zip94  [ -s $Z ] || curl -fsSL -A "$UA" -o $Z https://donnees.montreal.ca/dataset/4ad6baea-4d2c-460f-a8bf-5d000db498f7/resource/866a3dbc-8b59-48ff-866d-f2f9d3bbee9d/download/uniteevaluationfonciere.geojson.zip95  [ -s uef/uniteevaluationfonciere.geojson ] || unzip -qo $Z -d uef96  log "Montréal, unités d'évaluation foncière : chargement"97  psqlq -c "DROP TABLE IF EXISTS ext.mtl_uef_raw"98  ogr2ogr -f PostgreSQL "$OGR" uef/uniteevaluationfonciere.geojson -nln ext.mtl_uef_raw -lco GEOMETRY_NAME=geom -lco FID=gid \99    -lco SPATIAL_INDEX=NONE -nlt MULTIPOLYGON -t_srs EPSG:3857 -select ID_UEV,MATRICULE83,SUPERFICIE_TERRAIN,CATEGORIE_UEF,ETAGE_HORS_SOL \100    --config PG_USE_COPY YES 2>&1 | grep -v 'event trigger' || true101  psqlq <<'SQL'102-- Les copropriétés répètent le même contour pour chaque unité : un seul polygone par contour distinct.103-- `id` = plus petit ID_UEV du contour ; `area` = superficie du terrain déclarée (m²), sinon aire géodésique.104DROP TABLE IF EXISTS ext.parcel;105CREATE TABLE ext.parcel AS106  SELECT min(id_uev) AS id, count(*)::int AS units,107         coalesce(nullif(max(superficie_terrain), 0), round(ST_Area(ST_Transform(geom, 4326)::geography)))::int AS area,108         max(etage_hors_sol)::real AS floors_max,109         geom::geometry(MultiPolygon,3857) AS geom110    FROM (SELECT id_uev, superficie_terrain, etage_hors_sol, ST_MakeValid(geom) AS geom, ST_AsEWKB(ST_SnapToGrid(geom, 0.05)) AS k111            FROM ext.mtl_uef_raw WHERE geom IS NOT NULL) s112   WHERE GeometryType(geom) = 'MULTIPOLYGON'113   GROUP BY k, geom;114CREATE INDEX ON ext.parcel USING gist (geom);115DROP TABLE ext.mtl_uef_raw;116ANALYZE ext.parcel;117SQL118fi119psqlq -c "GRANT SELECT ON ALL TABLES IN SCHEMA ext TO kamaps_ro, km_geo;"120log "sources complémentaires chargées"121
Files main
Menu

Theme