SPB Git

Repository Public

05b_osm_addr.sql 2.2 KB 42 lines sql

1-- =============================================================================2-- SP BOUCHER | SOURCE FILE3-- =============================================================================4-- Project      : KA Maps Engine5-- File         : server/pipeline/geo/05b_osm_addr.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 work_mem = '512MB';16-- Adresses OSM (si l'extraction 05_osm.sh a tourné) : gardées À PART dans osm_addr, jamais fusionnées aux adresses17-- servies. Raison : ODbL (partage à l'identique d'une base dérivée) — voir docs/DATA_LICENSING.md. Elles servent de18-- source indépendante pour mesurer l'erreur de position (tests).19DROP TABLE IF EXISTS osm_addr;20CREATE TABLE osm_addr (street text NOT NULL, num int NOT NULL, suffix text NOT NULL, postcode text, city text,21                       pt geometry(Point, 4326) NOT NULL, mun_id int, ref text);22DO $$23BEGIN24  IF to_regclass('raw_osm') IS NOT NULL THEN25    INSERT INTO osm_addr (street, num, suffix, postcode, city, pt, ref)26    SELECT btrim(tags->>'addr:street'), substring(tags->>'addr:housenumber' from '^\d+')::int,27           upper(coalesce(substring(tags->>'addr:housenumber' from '^\d+\s*([A-Za-z])$'), '')),28           substring(upper(replace(tags->>'addr:postcode', ' ', '')) from '^[A-Z]\d[A-Z]\d[A-Z]\d$'),29           nullif(btrim(tags->>'addr:city'), ''),30           ST_SetSRID(ST_MakePoint(lon, lat), 4326), left(osm_type, 1) || osm_id31    FROM raw_osm32    WHERE tags->>'addr:housenumber' ~ '^\d{1,6}\s*[A-Za-z]?$' AND nullif(btrim(tags->>'addr:street'), '') IS NOT NULL33      AND substring(tags->>'addr:housenumber' from '^\d+')::int > 0;34    UPDATE osm_addr o SET mun_id = m.mun_id FROM mun_sub m WHERE ST_Covers(m.geom, o.pt);35    DELETE FROM osm_addr WHERE mun_id IS NULL;36  END IF;37END $$;3839CREATE INDEX ON osm_addr (mun_id);40ANALYZE osm_addr;41SELECT count(*) AS adresses_osm FROM osm_addr;42
Files main
Menu

Theme