Repository Public
1-- =============================================================================2-- SP BOUCHER | SOURCE FILE3-- =============================================================================4-- Project : KA Maps Engine5-- File : server/pipeline/geo/07b_index.sql6-- Author : Simon-Pierre Boucher | https://spboucher.ai7-- Contact : support@spboucher.ai8-- Copyright : (c) 2026 Simon-Pierre Boucher9-- License : None specified (no license file in this repository)10-- =============================================================================11-- END OF SP BOUCHER SOURCE HEADER12-- =============================================================================1314SET client_min_messages = warning;15SET maintenance_work_mem = '2GB';16UPDATE place p SET norm = n.norm FROM name_norm n WHERE n.name = p.name;17DELETE FROM place WHERE norm IS NULL OR norm = '';18-- Localité OSM homonyme de sa municipalité (ou d'un arrondissement/quartier officiel voisin) : doublon, retirée19DELETE FROM place l USING place o20WHERE l.kind = 'locality' AND o.kind IN ('municipality', 'borough', 'neighbourhood') AND o.norm = l.norm21 AND (o.mun_id = l.mun_id OR ST_DWithin(o.pt, l.pt, 8000));22-- Doublons exacts de localités / points d'intérêt (même nom, même catégorie, < 400 m)23DELETE FROM place x USING place y24WHERE x.kind IN ('locality', 'poi') AND x.kind = y.kind AND coalesce(x.sub, '') = coalesce(y.sub, '') AND x.norm = y.norm25 AND x.id > y.id AND ST_DWithin(x.pt, y.pt, 600);26-- Localité dont le nom n'est qu'un point cardinal ou un mot vide : inutilisable comme contexte27DELETE FROM place WHERE kind = 'locality' AND norm IN ('est', 'ouest', 'nord', 'sud', 'centre', 'rue', 'avenue', 'chemin', 'rang', 'route');28UPDATE place p SET norm_ctx = CASE WHEN p.kind IN ('municipality', 'mrc', 'region') OR m.norm IS NULL OR m.norm = p.norm29 THEN p.norm ELSE p.norm || ' ' || m.norm END30FROM (SELECT p2.id, mu.norm FROM place p2 LEFT JOIN mun mu ON mu.id = p2.mun_id) m WHERE m.id = p.id;31CREATE INDEX place_trgm ON place USING gin (norm_ctx gin_trgm_ops);32CREATE INDEX place_norm_prefix ON place (norm text_pattern_ops);33CREATE INDEX place_ctx_prefix ON place (norm_ctx text_pattern_ops);34CREATE INDEX place_pt ON place USING gist (pt);35CREATE INDEX place_ref ON place (ref);36CREATE INDEX amenity_pt ON amenity USING gist (pt);37CREATE INDEX amenity_kind_pt ON amenity USING gist (kind, pt);38ANALYZE place; ANALYZE amenity;39SELECT kind, count(*) FROM place GROUP BY 1 ORDER BY 2 DESC;40SELECT kind, count(*) FROM amenity GROUP BY 1 ORDER BY 2 DESC;41