SPB Git

Repository Public

03-osm-prepare.sh 4.9 KB 104 lines shellscript

1#!/bin/bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : KA Maps Engine8# File             : server/pipeline/03-osm-prepare.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# Prépare le fichier d'import : Québec complet + bande frontalière complète (25 km) des voisins41# + contexte allégé des voisins (grandes routes, lieux, plans d'eau, limites de pays et de provinces).42. "$(dirname "$0")/env.sh"43D=$KM_SRC/osm; W=$D/work; mkdir -p "$W"44BAND_KM=${KM_BAND_KM:-25}45NEIGH_BAND="ontario new-brunswick newfoundland-and-labrador nunavut maine new-hampshire vermont new-york"46NEIGH_CTX="ontario new-brunswick newfoundland-and-labrador nova-scotia prince-edward-island nunavut maine new-hampshire vermont new-york"4748# 1. Polygone du Québec (.poly Geofabrik) → bande tampon en GeoJSON, calculée par PostGIS (géographie).49python3 - "$D/quebec.poly" > "$W/quebec.wkt" <<'PY'50import sys51rings, cur = [], None52for line in open(sys.argv[1]).read().splitlines()[1:]:53    s = line.strip()54    if s == 'END':55        if cur is not None:56            rings.append(cur); cur = None57        continue58    p = s.split()59    if len(p) == 1:60        cur = [] if not s.startswith('!') else None61        if s.startswith('!'):62            cur = None63        continue64    if cur is not None and len(p) >= 2:65        cur.append((float(p[0]), float(p[1])))66polys = []67for r in rings:68    if r[0] != r[-1]:69        r.append(r[0])70    polys.append('((' + ','.join('%f %f' % c for c in r) + '))')71print('MULTIPOLYGON(' + ','.join(polys) + ')')72PY73psql -X -At -v ON_ERROR_STOP=1 "$KM_DB_URL" > "$W/band.geojson" <<SQL74\set wkt \`cat $W/quebec.wkt\`75SELECT json_build_object('type','FeatureCollection','features', json_build_array(json_build_object(76  'type','Feature','properties',json_build_object('name','quebec-band'),77  'geometry', ST_AsGeoJSON(ST_Simplify(ST_Buffer(ST_MakeValid(ST_GeomFromText(:'wkt',4326))::geography, ${BAND_KM}000)::geometry, 0.005), 6)::json)));78SQL79log "bande frontalière de ${BAND_KM} km : $(stat -c %s "$W/band.geojson") octets"8081# 2. Extraits des voisins (en parallèle).82band() { [ -s "$D/$1-latest.osm.pbf" ] || { echo "extrait $1 absent : ignoré"; return 0; }; osmium extract -O -p "$W/band.geojson" -s smart "$D/$1-latest.osm.pbf" -o "$W/band-$1.osm.pbf" 2>&1 | grep -v '^\[' || true; }83ctx() {84  [ -s "$D/$1-latest.osm.pbf" ] || { echo "extrait $1 absent : ignoré"; return 0; }85  # Nunavut : l'extrait compte des millions de petits lacs ; on n'en garde que les grands (relations multipolygones).86  local water="nwr/natural=water w/waterway=riverbank"87  [ "$1" = nunavut ] && water="r/natural=water"88  osmium tags-filter -O "$D/$1-latest.osm.pbf" \89    w/highway=motorway,motorway_link,trunk,trunk_link,primary n/place=city,town,village,hamlet,state,province \90    r/admin_level=2,4 w/route=ferry $water \91    -o "$W/ctx-$1.osm.pbf" 2>&1 | grep -v '^\[' || true92}93export -f band ctx; export D W94printf '%s\n' $NEIGH_BAND | xargs -P 4 -I{} bash -c 'band {}'95printf '%s\n' $NEIGH_CTX | xargs -P 4 -I{} bash -c 'ctx {}'9697# 3. Fusion (les objets présents dans plusieurs extraits ne sont gardés qu'une fois).98osmium merge -O "$D/quebec-latest.osm.pbf" "$W"/band-*.osm.pbf "$W"/ctx-*.osm.pbf -o "$W/import.osm.pbf"99TS=$(osmium fileinfo -g header.option.osmosis_replication_timestamp "$D/quebec-latest.osm.pbf" 2>/dev/null || true)100[ -n "$TS" ] || TS=$(osmium fileinfo -g header.option.timestamp "$D/quebec-latest.osm.pbf")101echo "$TS" > "$W/import.timestamp"102date -u -d "$TS" +%Y%m%d > "$W/import.version"103log "fichier d'import : $(stat -c %s "$W/import.osm.pbf") octets, extrait du $TS, version $(cat "$W/import.version")"104
Files main
Menu

Theme