#!/usr/bin/env bash # rebuild-overview-tiles.sh — Export all overview layers from PostGIS, generate PMTiles, upload to MinIO # Includes: UAT boundaries, administrativ, simplified terenuri (z10-z14), simplified cladiri (z12-z14) # Usage: ./scripts/rebuild-overview-tiles.sh # Dependencies: ogr2ogr (GDAL), tippecanoe, mc (MinIO client) set -euo pipefail # ── Configuration ── DB_HOST="${DB_HOST:-10.10.10.166}" DB_PORT="${DB_PORT:-5432}" DB_NAME="${DB_NAME:-architools_db}" DB_USER="${DB_USER:-architools_user}" DB_PASS="${DB_PASS:-stictMyFon34!_gonY}" MINIO_ALIAS="${MINIO_ALIAS:-myminio}" MINIO_BUCKET="${MINIO_BUCKET:-tiles}" MINIO_ENDPOINT="${MINIO_ENDPOINT:-http://10.10.10.166:9002}" MINIO_ACCESS_KEY="${MINIO_ACCESS_KEY:-admin}" MINIO_SECRET_KEY="${MINIO_SECRET_KEY:-MinioStrongPass123}" TMPDIR="${TMPDIR:-/tmp/tile-rebuild}" OUTPUT_FILE="overview.pmtiles" PG_CONN="PG:host=${DB_HOST} port=${DB_PORT} dbname=${DB_NAME} user=${DB_USER} password=${DB_PASS}" echo "[$(date -Iseconds)] Starting overview tile rebuild..." # ── Setup ── mkdir -p "$TMPDIR" cd "$TMPDIR" # ── Step 1: Export views from PostGIS (parallel) ── echo "[$(date -Iseconds)] Exporting PostGIS views to FlatGeobuf..." # UAT boundaries (4 zoom levels) ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ uats_z0.fgb "$PG_CONN" \ -sql "SELECT name, siruta, geom FROM gis_uats_z0 WHERE geom IS NOT NULL" & ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ uats_z5.fgb "$PG_CONN" \ -sql "SELECT name, siruta, geom FROM gis_uats_z5 WHERE geom IS NOT NULL" & ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ uats_z8.fgb "$PG_CONN" \ -sql "SELECT name, siruta, county, geom FROM gis_uats_z8 WHERE geom IS NOT NULL" & ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ uats_z12.fgb "$PG_CONN" \ -sql "SELECT name, siruta, county, geom FROM gis_uats_z12 WHERE geom IS NOT NULL" & # Administrativ (intravilan, arii speciale) ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ administrativ.fgb "$PG_CONN" \ -sql "SELECT object_id, siruta, layer_id, cadastral_ref, geom FROM gis_administrativ WHERE geom IS NOT NULL" & # Terenuri for overview — let tippecanoe handle simplification ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ terenuri_overview.fgb "$PG_CONN" \ -sql "SELECT object_id, siruta, cadastral_ref, area_value, layer_id, geom FROM gis_terenuri WHERE geom IS NOT NULL" & # Cladiri for overview — let tippecanoe handle simplification ogr2ogr -f FlatGeobuf -s_srs EPSG:3844 -t_srs EPSG:4326 \ cladiri_overview.fgb "$PG_CONN" \ -sql "SELECT object_id, siruta, cadastral_ref, area_value, layer_id, geom FROM gis_cladiri WHERE geom IS NOT NULL" & wait echo "[$(date -Iseconds)] Export complete." # ── Step 2: Generate PMTiles with tippecanoe ── echo "[$(date -Iseconds)] Generating PMTiles..." tippecanoe \ -o "$OUTPUT_FILE" \ --named-layer=gis_uats_z0:uats_z0.fgb \ --named-layer=gis_uats_z5:uats_z5.fgb \ --named-layer=gis_uats_z8:uats_z8.fgb \ --named-layer=gis_uats_z12:uats_z12.fgb \ --named-layer=gis_administrativ:administrativ.fgb \ --named-layer=gis_terenuri:terenuri_overview.fgb \ --named-layer=gis_cladiri:cladiri_overview.fgb \ --minimum-zoom=0 \ --maximum-zoom=14 \ --base-zoom=14 \ --drop-densest-as-needed \ --detect-shared-borders \ --simplification=10 \ --hilbert \ --force echo "[$(date -Iseconds)] PMTiles generated: $(du -h "$OUTPUT_FILE" | cut -f1)" # ── Step 3: Upload to MinIO (atomic swap) ── echo "[$(date -Iseconds)] Uploading to MinIO..." # Configure MinIO client alias (idempotent) mc alias set "$MINIO_ALIAS" "$MINIO_ENDPOINT" "$MINIO_ACCESS_KEY" "$MINIO_SECRET_KEY" --api S3v4 2>/dev/null || true # Ensure bucket exists mc mb --ignore-existing "${MINIO_ALIAS}/${MINIO_BUCKET}" 2>/dev/null || true # Upload as temp file first mc cp "$OUTPUT_FILE" "${MINIO_ALIAS}/${MINIO_BUCKET}/overview_new.pmtiles" # Atomic rename (zero-downtime swap) mc mv "${MINIO_ALIAS}/${MINIO_BUCKET}/overview_new.pmtiles" "${MINIO_ALIAS}/${MINIO_BUCKET}/overview.pmtiles" echo "[$(date -Iseconds)] Upload complete." # ── Step 4: Cleanup ── rm -f uats_z0.fgb uats_z5.fgb uats_z8.fgb uats_z12.fgb administrativ.fgb \ terenuri_overview.fgb cladiri_overview.fgb "$OUTPUT_FILE" echo "[$(date -Iseconds)] Rebuild finished successfully."