Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | ACADEMIC RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : UQO Éval10 * File : src/app/api/metre/geocode/route.ts11 * Module : app12 * Repository : https://git.spboucher.ai/uqo-eval13 * Project Website : https://www.uqo-eval.app14 * Created : 2026-10-2815 * Last Modified : 2026-10-2816 *17 * =============================================================================18 * SOFTWARE MAINTAINER & AFFILIATION19 * =============================================================================20 *21 * Maintainer : Simon-Pierre Boucher22 * Position : Professor of Finance23 * Institution : Université du Québec en Outaouais (UQO)24 * Department : Département des sciences administratives25 * Website : https://spboucher.ai26 * Git : https://git.spboucher.ai27 *28 * Research Contact : research@spboucher.ai29 * Direct Contact : simon@spboucher.ai30 * Security/Admin : admin@spboucher.ai31 *32 * The affiliation is descriptive. It is not an institutional endorsement,33 * a license grant or a determination of ownership.34 *35 * =============================================================================36 * COPYRIGHT, AUTHORSHIP & DATA37 * =============================================================================38 *39 * Copyright (c) the author(s) of this work.40 * Ownership is not asserted by this notice; see the repository records.41 *42 * This header identifies the maintainer of the software. Scientific43 * authorship of any associated paper is stated in the paper itself and44 * is not changed by this notice. Datasets and third-party materials45 * remain the property of their respective owners and licensors.46 *47 * This file is governed by the applicable project license, copyright48 * notices, institutional agreements and relevant law.49 *50 * =============================================================================51 * LICENSE & MAINTENANCE52 * =============================================================================53 *54 * License : None specified (no license file in this repository)55 * Classification : Public56 *57 * =============================================================================58 * END OF SP BOUCHER SOURCE HEADER59 * =============================================================================60 */6162// UQO Éval — Métré : géocodage d'une adresse québécoise (Mapbox Geocoding, jeton serveur) ou coordonnées d'une unité MAMH (?mamh=id).63import { NextRequest, NextResponse } from "next/server";64import { HttpError, handle, requireUser } from "@/lib/metre/auth";65import { getUnit } from "@/lib/db";6667export const runtime = "nodejs";6869export async function GET(req: NextRequest) {70 return handle(async () => {71 requireUser(req, { limit: { key: "geocode", max: 60, windowMs: 60_000 } });72 const mamh = req.nextUrl.searchParams.get("mamh");73 if (mamh) {74 const u = getUnit(mamh.replace(/\D/g, "") || mamh);75 if (!u || !u.lat || !u.lng) throw new HttpError(404, "Unité MAMH introuvable ou non géoréférencée.");76 return NextResponse.json({ results: [{ name: [u.adresse, u.municipalite].filter(Boolean).join(", "), lat: u.lat, lng: u.lng, source: "MAMH" }] });77 }78 const q = (req.nextUrl.searchParams.get("q") ?? "").trim().slice(0, 160);79 if (q.length < 3) throw new HttpError(400, "q (3 caractères minimum) ou mamh requis.");80 const { MAPBOX_TOKEN } = await import("@/lib/kamaps");81 const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(q)}.json?country=ca&language=fr&limit=5&bbox=-80,44.9,-57,63&access_token=${MAPBOX_TOKEN}`;82 const r = await fetch(url);83 if (!r.ok) throw new HttpError(502, `Géocodage indisponible (HTTP ${r.status}).`);84 const j = (await r.json()) as { features?: { place_name?: string; center?: [number, number] }[] };85 return NextResponse.json({ results: (j.features ?? []).filter((f) => f.center).map((f) => ({ name: f.place_name ?? q, lat: f.center![1], lng: f.center![0], source: "Mapbox" })) });86 });87}88