Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br_sfb_sicar #787

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ models:
br_rj_isp_estatisticas_seguranca:
+materialized: table
+schema: br_rj_isp_estatisticas_seguranca
br_sfb_sicar:
+materialized: table
+schema: br_sfb_sicar
br_sp_saopaulo_dieese_icv:
+materialized: table
+schema: br_sp_saopaulo_dieese_icv
Expand Down
109 changes: 109 additions & 0 deletions models/br_sfb_sicar/br_sfb_sicar__area_imovel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{{
config(
alias="area_imovel",
schema="br_sfb_sicar",
materialized="incremental",
partition_by={
"field": "data_atualizacao_car",
"data_type": "date",
"granularity": "day",
},
cluster_by=["sigla_uf"],
)
}}

with
municipios_car as (
select distinct
safe_cast(cod_estado as string) sigla_uf,
safe_cast(municipio as string) municipio_nome,

from `basedosdados-staging.br_sfb_sicar_staging.area_imovel` as t
),

muncipios_car_diretorio as (
select sd.*, m.id_municipio as id_municipio
from municipios_car sd
left join
`basedosdados.br_bd_diretorios_brasil.municipio` as m
on lower(
regexp_replace(normalize(sd.municipio_nome, nfd), r"[^a-zA-Z0-9\s]", "")
)
= lower(regexp_replace(normalize(m.nome, nfd), r"[^a-zA-Z0-9\s]", ""))
and sd.sigla_uf = m.sigla_uf
),
correcao_manual_falhas as (
select
sigla_uf,
municipio_nome,
case
when sigla_uf = 'PE' and municipio_nome = 'Iguaracy'
then '2606903'
when sigla_uf = 'RN' and municipio_nome = 'Januario Cicco'
then '2405306'
when sigla_uf = 'RN' and municipio_nome = "Olho d'Agua do Borges"
then '2408409'
when sigla_uf = 'PA' and municipio_nome = "Santa Izabel do Para"
then '1506500'
when sigla_uf = 'SP' and municipio_nome = "Florinea"
then '3516101'
when sigla_uf = 'SP' and municipio_nome = "Sao Luiz do Paraitinga"
then '3550001'
when sigla_uf = 'SP' and municipio_nome = "Biritiba Mirim"
then '3506607'
when sigla_uf = 'MT' and municipio_nome = "Santo Antonio de Leverger"
then '5107800'
when sigla_uf = 'MT' and municipio_nome = "Poxoreu"
then '5107008'
when sigla_uf = 'BA' and municipio_nome = "Muquem do Sao Francisco"
then '2922250'
when sigla_uf = 'MG' and municipio_nome = "Passa Vinte"
then '3147808'
when sigla_uf = 'SE' and municipio_nome = "Amparo do Sao Francisco"
then '2800100'
when sigla_uf = 'BA' and municipio_nome = "Santa Terezinha"
then '2928505'
when sigla_uf = 'TO' and municipio_nome = "Tabocao"
then '1708254'
when sigla_uf = 'MG' and municipio_nome = "Dona Euzebia"
then '3122900'
when sigla_uf = 'MG' and municipio_nome = "Sao Tome das Letras"
then '3165206'
when sigla_uf = 'SC' and municipio_nome = "Grao-Para"
then '4206108'
when sigla_uf = 'CE' and municipio_nome = "Itapaje"
then '2306306'
else id_municipio
end as id_municipio
from muncipios_car_diretorio
),

final_table as (
select
safe_cast(data_extracao as date) data_extracao,
safe_cast(data_atualizacao_car as date) data_atualizacao_car,
safe_cast(cod_estado as string) sigla_uf,
safe_cast(t2.id_municipio as string) id_municipio,
safe_cast(cod_imovel as string) id_imovel,
safe_cast(mod_fiscal as string) modulos_fiscais,
safe_cast(num_area as float64) area,
safe_cast(ind_status as string) status,
safe_cast(ind_tipo as string) tipo,
safe_cast(des_condic as string) condicao,
safe_cast(
safe.st_geogfromtext(geometry, make_valid => true) as geography
) geometria,
from `basedosdados-staging.br_sfb_sicar_staging.area_imovel` as car
left join
correcao_manual_falhas as t2
on car.sigla_uf = t2.sigla_uf
and car.municipio = t2.municipio_nome

)

select *
from final_table

{% if is_incremental() %}
where data_extracao > (select max(data_extracao) from {{ this }})
{% endif %}
16 changes: 16 additions & 0 deletions models/br_sfb_sicar/br_sfb_sicar__dicionario.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{
config(
alias="dicionario",
schema="br_sfb_sicar",
materialized="table",
)
}}


select
safe_cast(id_tabela as string) id_tabela,
safe_cast(nome_coluna as string) nome_coluna,
safe_cast(chave as string) chave,
safe_cast(cobertura_temporal as string) cobertura_temporal,
safe_cast(valor as string) valor,
from `basedosdados-staging.br_sfb_sicar_staging.dicionario` as t
69 changes: 69 additions & 0 deletions models/br_sfb_sicar/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
version: 2
models:
- name: br_sfb_sicar__area_imovel
description: Esta tabela contém o polígono de demarcação da área dos imóveis com
cadastro ambiental rural.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns: [id_imovel]
- not_null_proportion_multiple_columns:
at_least: 0.95
- custom_dictionary_coverage:
columns_covered_by_dictionary: [status, tipo]
dictionary_model: ref('br_sfb_sicar__dicionario')
columns:
- name: data_atualizacao_car
description: Data de atualização dos dados na fonte original. O data de atualização
pode variar a depender da Unidade da Federação.
tests:
- relationships:
to: ref('br_bd_diretorios_data_tempo__data')
field: data.data
- name: sigla_uf
description: Sigla da Unidade da Federação (UF) onde se localiza o cadastro
tests:
- relationships:
to: ref('br_bd_diretorios_brasil__uf')
field: sigla
- name: id_municipio
description: ID Município - IBGE 7 Dígitos onde se localiza o cadastro
tests:
- relationships:
to: ref('br_bd_diretorios_brasil__municipio')
field: id_municipio
- name: id_imovel
description: Código de Inscrição no do Cadastro Ambiental Rural (CAR)
- name: modulos_fiscais
description: Quantidade de módulos fiscais do imóvel
- name: area
description: Área do imóvel
- name: status
description: Status do cadastro do imóvel
- name: tipo
description: Tipo do Imóvel Rural
- name: condicao
description: Condição em que o cadastro se encontra no fluxo de análise pelo
órgão competente
- name: geometria
description: Geometria do imóvel
- name: br_sfb_sicar__dicionario
description: Dicionário para tradução dos códigos das tabelas do conjunto br_sfb_sicar
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- id_tabela
- nome_coluna
- chave
- cobertura_temporal
columns:
- name: id_tabela
description: ID Tabela
- name: nome_coluna
description: Nome da coluna
- name: chave
description: Chave
- name: cobertura_temporal
description: Cobertura Temporal
- name: valor
description: Valor
Loading