CNEFE: mudanças entre as edições

De Documentação
Sem resumo de edição
Linha 169: Linha 169:
-- os casos de não same podem ser resgatados por geometria
-- os casos de não same podem ser resgatados por geometria


select count(*) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same)) where dist<=20;
select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same)) where dist<=20;  
-- 12193
select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same)) where dist<=5;
-- 1848  (não compensa)
 
select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) t2 where is_same) t3
--  49597 | 2.67  (ok distancia pequena e numero expressivo)


select count(*), count(distinct via_name||num_endereco) from (select distinct  cod_setor, via_name, num_endereco, cep
select count(*), count(distinct via_name||num_endereco) from (select distinct  cod_setor, via_name, num_endereco, cep
from vw02_ibge_cnefe2022           
from vw02_ibge_cnefe2022           
) t; -- 288685 | 277160
) t; -- 288685 | 277160

Edição das 09h44min de 31 de maio de 2024

CNEFE - Cadastro Nacional de Endereços para Fins Estatísticos

Ver https://www.ibge.gov.br/estatisticas/sociais/populacao/38734-cadastro-nacional-de-enderecos-para-fins-estatisticos.html?edicao=40122&t=resultados

Faces de Quadra

Pegar em 4314902_faces_de_logradouros_2021.prj

mkdir /tmp/POA_FACES/

cd /tmp/POA_FACES

wget https://dl.digital-guard.org/5d5daa06e048ad8c83c909babee040e26fd8a54a29ed4cef2d2d220a3bd17b84.zip

zip -T 5d5daa06e048ad8c83c909babee040e26fd8a54a29ed4cef2d2d220a3bd17b84.zip

cd '/tmp/5d5daa06e048ad8c83c909babee040e26fd8a54a29ed4cef2d2d220a3bd17b84/rs_faces_de_logradouros_2021/'
cp 4314902* /tmp/POA_FACES

Supor arquivos logradouro em log:

create database poa;
\c poa
create extension postgis;
-----
shp2pgsql  log | psql -q postgres://postgres@localhost/poa

Melhorando os dados:

drop view if exists vw01_log ;
create view vw01_log as select *, regexp_replace( coalesce(nm_tip_log,'') || ' '||  coalesce(nm_tit_log,'') ||' '|| coalesce(nm_log,''), '\s+', ' ', 'g') as via_name from log;

select gid, cd_setor, cd_quadra, cd_face, via_name, tot_res from vw01_log limit 100;

Pontos

Por cidade temos Censo_Demografico_2022/Arquivos_CNEFE/Municipio, direto latLong em srid=4326.

Pelo cabeçalho podemos copiar para uma tabela texto,

CREATE TABLE ibge_cnefe2022 (
COD_UNICO_ENDERECO text,
COD_UF text,
COD_MUNICIPIO text,
COD_DISTRITO text,
COD_SUBDISTRITO text,
COD_SETOR text,
NUM_QUADRA text,
NUM_FACE text,
CEP text,
DSC_LOCALIDADE text,
NOM_TIPO_SEGLOGR text,
NOM_TITULO_SEGLOGR text,
NOM_SEGLOGR text,
NUM_ENDERECO text,
DSC_MODIFICADOR text,
NOM_COMP_ELEM1 text,
VAL_COMP_ELEM1 text,
NOM_COMP_ELEM2 text,
VAL_COMP_ELEM2 text,
NOM_COMP_ELEM3 text,
VAL_COMP_ELEM3 text,
NOM_COMP_ELEM4 text,
VAL_COMP_ELEM4 text,
NOM_COMP_ELEM5 text,
VAL_COMP_ELEM5 text,
LATITUDE real,
LONGITUDE real,
NV_GEO_COORD text,
COD_ESPECIE text,
DSC_ESTABELECIMENTO text,
COD_INDICADOR_ESTAB_ENDERECO text,
COD_INDICADOR_CONST_ENDERECO text,
COD_INDICADOR_FINALIDADE_CONST text,
COD_TIPO_ESPECI text
);
copy ibge_cnefe2022 from '/tmp/POA_FACES/4314902_PORTO_ALEGRE.csv' with  (format CSV, delimiter ';', header);

----
drop view if exists vw02_ibge_cnefe2022;
create view vw02_ibge_cnefe2022 as
 select *, ST_GeoHash(geom,9) as ghs
 from (
  select *, regexp_replace( coalesce(nom_tipo_seglogr,'') ||' '|| coalesce(nom_titulo_seglogr,'') ||' '|| coalesce(nom_seglogr,''), '\s+',' ','g') as via_name,
         ST_SetSRID(ST_MakePoint(longitude::float, latitude::float),4326) as geom
  from ibge_cnefe2022
 ) t
;
--select count(*) from ibge_cnefe2022; -- 762239
-- select count(*) from (select distinct ST_GeoHash(geom,9) as ghs, cod_setor, num_quadra, num_face, via_name, num_endereco from vw02_ibge_cnefe2022 -- same where geom is not null and via_name>'' and num_endereco>'' ) t; -- 379957
-- select 379957.0/762239; -- 49,847% = 50%
  • Unicidade de endereço: DISTINCT cod_setor, cep, via_name, num_endereco
  • Unicidade de posição: DISTINCT ghs
  • Filtrando unicidades: primeiro de endereço, porém incluindo array de geohashes distintos. Se forem vizinhos não possuem mesmo prefixo conferir maior distancia entre eles.
CREATE FUNCTION hcodes_sameparent(g text[]) RETURNS boolean AS $f$
  SELECT  bool_and( CASE
     WHEN l1=l2 THEN substr(g1,1,l1-1)=substr(g2,1,l2-1)
     WHEN l1>l2 THEN g1 ~ ('^'||g2)  -- WHEN l1=l2+1
     ELSE g2 ~ ('^'||g1)  -- WHEN l2=l1+1  ELSE false
     END  )
  FROM ( 
    select g1, g2, length(g1) l1, length(g2) l2
    from unnest(g) WITH ORDINALITY AS a(g1,i1) 
    inner join unnest(g) WITH ORDINALITY AS b(g2,i2) 
      on i1<i2
    ) t1
$f$ LANGUAGE SQL;

CREATE FUNCTION geoms_maxdist(g geometry[], use_spheroid boolean default false) RETURNS float AS $f$
    SELECT MAX( ST_Distance(g1,g2,use_spheroid) )
    FROM unnest(g) WITH ORDINALITY AS a(g1,i1) 
    INNER JOIN unnest(g) WITH ORDINALITY AS b(g2,i2) 
      ON i1<i2
$f$ LANGUAGE SQL;

create table tmp_ibge_cnefe2022_uniq1 AS  -- unicidade básica, ainda com repetição de pontos
   SELECT DISTINCT ghs, via_name, num_endereco
   FROM vw02_ibge_cnefe2022
; -- 379228 de 762239 = select round(100.0*379228.0/762239); = 50%

select avg(n) from (select ghs, count(*) n from ibge_cnefe2022_uniq1 group by 1) t
where n=1 -- n>1
;
-- 325993 não-duplicados mais 2.16*24642 duplicados = 325993+2.16*24642 = 379220 ~ 379228.

create table ibge_cnefe2022_uniq_lixo AS  -- erro
with upt as (
   SELECT ghs FROM vw02_ibge_cnefe2022 group by 1 having count(*)=1
), uend as(
   SELECT cod_setor, cep, via_name, num_endereco
   FROM vw02_ibge_cnefe2022
   group by 1,2,3,4 having count(*)=1
) select i.ghs, i.cod_setor, i.cep, i.via_name, i.num_endereco 
  from upt inner join (vw02_ibge_cnefe2022 i inner join uend
    ON i.cod_setor=uend.cod_setor AND i.cep=uend.cep AND i.via_name=uend.via_name AND i.num_endereco=uend.num_endereco)
    ON upt.ghs=i.ghs
;

Só na redução de pontos para 50% já é bem drástica, provavelmente inclui casos que diferenciam apenas no complemento. Desses, com duplicidade posicional apenas ~7,6% (24642 de 325993), sendo também razoável o seu descarte de tmp_ibge_cnefe2022_uniq1.

drop table if exists ibge_cnefe2022_uniq2; create table ibge_cnefe2022_uniq2 AS -- remove endereços duplicados em locais diferentes

select v.cod_setor, v.num_quadra, v.num_face, v.cep, v.via_name, v.num_endereco,
      array_agg(v.ghs) ghss, array_agg(v.geom) geoms
from(  
  SELECT DISTINCT ON (ghs) ghs, via_name, num_endereco, count(*) OVER (PARTITION BY ghs) AS ghs_count
  FROM tmp_ibge_cnefe2022_uniq1 
) g -- 325993
INNER JOIN vw02_ibge_cnefe2022 v ON 1=g.ghs_count AND g.ghs=v.ghs

group by 1,2,3,4,5,6 order by 1,2,3,4,5,6

-- 257387


select count(*) from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where is_same;-- 49597 select count(*) from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same); -- 22523

-- conclusão: vale a pena manter o same, removendo o último dígito, fornecendo um booleano para usuário decidir se valido ou não. -- os casos de não same podem ser resgatados por geometria

select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same)) where dist<=20; -- 12193 select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) where not(is_same)) where dist<=5; -- 1848 (não compensa)

select count(*), avg(dist) from (select geoms_maxdist(geoms,true) dist from (select cep, via_name, num_endereco, hcodes_sameparent(ghss) as is_same, geoms from ibge_cnefe2022_uniq2 where cardinality(ghss)>1 ) t2 where is_same) t3 -- 49597 | 2.67 (ok distancia pequena e numero expressivo)

select count(*), count(distinct via_name||num_endereco) from (select distinct cod_setor, via_name, num_endereco, cep from vw02_ibge_cnefe2022 ) t; -- 288685 | 277160