Skip to content

GeoTerm na AddressForAll

Peter edited this page Oct 2, 2020 · 1 revision

O projeto GeoTerm é de uso geral, mas a sua adoção na AddressForAll é sua principal aplicação. A seguir as configurações e estado atual da base terminológica da AddressForAll.

Implementação inicial e reprodução

A implementação inicial foi realizada com sh make.sh na base DL03. O restante pode ser reproduzido, mas seguindo-se o log de transações, e resgatando-se dados originais do digital-preservartion ou inserts.

Namespaces

Temos as seguintes definições padronizadas de namespaces:

nscount nsid label description lang is_base fk_partof kx_regconf jinfo created
1 1 vianameprefix Prefixo de nome de via (tipo de logradouro) pt t portuguese 2020-09-30
2 2 vianame Nome de via sem prefixo e por extenso pt t portuguese 2020-09-30
3 4 vianameprefix-abv Abreviação de prefixo de nome de via pt f portuguese 2020-09-30

Vianame

CREATE TABLE optim.vianame(
   id serial           NOT NULL PRIMARY KEY,
   tprefix_id int      NOT NULL REFERENCES optim.term(id), -- rua, avenida, etc.
   tname_id int        NOT NULL REFERENCES optim.term(id), -- nome mesmo
   is_current boolean  NOT NULL DEFAULT true
   ,UNIQUE(tprefix_id,tname_id)
);
CREATE VIEW optim.vw01_vianame AS
  select v.*, t1.term||' '||t2.term as via_name
  from optim.vianame v
     INNER JOIN tstore.term t1 ON v.tprefix_id=t1.id
     INNER JOIN tstore.term t2 ON v.tname_id=t2.id
;

CREATE or replace FUNCTION optim.vianame_insert(
  -- falta insert-array para conjuntos
  p_name text, -- nome completo da via
  p_source int DEFAULT NULL,
  p_is_current boolean DEFAULT true
) RETURNS int AS $f$
  WITH afixes AS (
    SELECT aa[2] as prefix_id,  CASE 
        WHEN aa[2] is not null THEN COALESCE(
            tstore.upsert( array_to_string(p[aa[1]+1:],' '), 2,null,false,null,false,null,p_source),
            tlib.n2c_check(array_to_string(p[aa[1]+1:],' '),2)
        ) 
        ELSE NULL END as sufix_id
    FROM (
     SELECT p, CASE
       -- not elegant code but supposing compiler optimization (otherwise better is pgPLSQL)
       WHEN tlib.n2c_check(array_to_string(p[1:3],' '),1) is not null THEN array[3, tlib.n2c_check(array_to_string(p[1:3],' '),1) ]
       WHEN tlib.n2c_check(p[1]||' '||p[2],1)  is not null THEN array[2, tlib.n2c_check(p[1]||' '||p[2],1)]
       WHEN tlib.n2c_check(p[1],1)  is not null THEN array[1, tlib.n2c_check(p[1],1)]
       WHEN tlib.n2c_check(p[1],4)  is not null THEN array[1, tlib.n2c_check(p[1],4)]
       ELSE null
       END AS aa
     FROM (SELECT regexp_split_to_array(tlib.normalizeterm(p_name),'\s+')) t1(p) -- or lower(unaccent(trim(p_name)))
    ) t2
  )  -- ,ins1 AS (
     INSERT INTO optim.vianame (tprefix_id, tname_id, is_current) 
        SELECT prefix_id, sufix_id, p_is_current
        FROM afixes
        WHERE prefix_id IS NOT NULL AND sufix_id IS NOT NULL
     ON CONFLICT DO NOTHING
     RETURNING tprefix_id
$f$ language SQL;
-- ex. select optim.vianame_insert(x,2) FROM (SELECT DISTINCT jtags->>'name' AS x from osm_road where jtags?'name') t;
-- ex. select optim.vianame_insert(x,3) FROM (select distinct logradouro as x from lix order by 1) t;

Exemplos de carga

osm_id parent_abbrev name jurisd_local_id
242397 RS Porto Alegre 4314902
297599 PR Pato Branco 4118501
298426 SP Cabreúva 3508405

....

Clone this wiki locally