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

improve cdbg exports #1364

Merged
merged 6 commits into from
Jan 6, 2025
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 products/cdbg/bash/export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mkdir -p output && (

echo "export cdbg_tracts.csv ..."
csv_export cdbg_tracts cdbg_tracts

echo "export cdbg_boroughs.csv ..."
csv_export cdbg_boroughs cdbg_boroughs
)

zip -r output/output.zip output
1 change: 1 addition & 0 deletions products/cdbg/models/intermediate/int__block_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ block_group_details AS (
SELECT
block_groups_floor_area.geoid,
block_groups_demographics.borough_name,
block_groups_demographics.borough_code,
block_groups_demographics.tract,
block_groups_demographics.block_group,
block_groups_floor_area.total_floor_area,
Expand Down
5 changes: 4 additions & 1 deletion products/cdbg/models/intermediate/int__boros.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ WITH tracts AS (
boros AS (
SELECT
borough_name,
borough_code,
sum(total_floor_area) AS total_floor_area,
sum(residential_floor_area) AS residential_floor_area,
sum(total_population) AS total_population,
sum(low_mod_income_population) AS low_mod_income_population
FROM tracts
GROUP BY borough_name
GROUP BY
borough_name,
borough_code
),

boro_calculation AS (
Expand Down
8 changes: 6 additions & 2 deletions products/cdbg/models/intermediate/int__tracts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ WITH block_groups AS (
tracts AS (
SELECT
tract_id AS geoid,
max(borough_name) AS borough_name,
borough_name,
borough_code,
sum(total_floor_area) AS total_floor_area,
sum(residential_floor_area) AS residential_floor_area,
sum(total_population) AS total_population,
sum(low_mod_income_population) AS low_mod_income_population
FROM block_groups
GROUP BY tract_id
GROUP BY
tract_id,
borough_name,
borough_code
),

tracts_calculation AS (
Expand Down
80 changes: 38 additions & 42 deletions products/cdbg/models/product/_product_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ models:
description: Census block groups and their Community Development Block Grant (CDBG) eligibility details
config:
contract:
enforced: false
enforced: true

columns:
- name: geoid
Expand All @@ -18,25 +18,17 @@ models:
data_type: string
tests: [not_null]

# - name: borough_code
# data_type: integer
# tests: [not_null]
#
# - name: borough_tract
# data_type: string
# tests: [not_null]
#
# - name: tract
# data_type: string
# tests: [not_null]
#
# - name: borough_tract_block_group
# data_type: string
# tests: [not_null]
#
# - name: block_group
# data_type: string
# tests: [not_null]
- name: borough_code
data_type: integer
tests: [not_null]

- name: tract
data_type: string
tests: [not_null]

- name: block_group
data_type: string
tests: [not_null]

- name: total_floor_area
data_type: integer
Expand All @@ -47,15 +39,15 @@ models:
tests: [not_null]

- name: residential_floor_area_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: low_mod_income_population
data_type: integer
tests: [not_null]

- name: low_mod_income_population_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: eligibility_flag
Expand All @@ -66,7 +58,7 @@ models:
data_type: string
tests: [not_null]

- name: cdbg_borough
- name: cdbg_boroughs
description: Borough and city-wide Community Development Block Grant (CDBG) details
config:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just drop the config field entirely?

Copy link
Member Author

@damonmcc damonmcc Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was gonna try enforcing them next, but in another PR

edit: gonna do the contracts here

contract:
Expand All @@ -75,7 +67,11 @@ models:
columns:
- name: borough_name
data_type: string
tests: [not_null]
tests: [not_null, unique]

- name: borough_code
data_type: integer
tests: [not_null, unique]

- name: total_floor_area
data_type: integer
Expand All @@ -86,22 +82,30 @@ models:
tests: [not_null]

- name: residential_floor_area_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: low_mod_income_population
data_type: integer
data_type: bigint
tests: [not_null]

- name: low_mod_income_population_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: eligibility_flag
data_type: boolean
tests: [not_null]

- name: eligibility
data_type: string
tests: [not_null]

- name: cdbg_tracts
description: Census tracts and their Community Development Block Grant (CDBG) eligibility details
config:
contract:
enforced: false
enforced: true

columns:
- name: geoid
Expand All @@ -114,17 +118,9 @@ models:
data_type: string
tests: [not_null]

# - name: borough_code
# data_type: integer
# tests: [not_null]
#
# - name: borough_tract
# data_type: string
# tests: [not_null]
#
# - name: tract
# data_type: string
# tests: [not_null]
- name: borough_code
data_type: integer
tests: [not_null]

- name: total_floor_area
data_type: integer
Expand All @@ -135,15 +131,15 @@ models:
tests: [not_null]

- name: residential_floor_area_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: low_mod_income_population
data_type: integer
data_type: bigint
tests: [not_null]

- name: low_mod_income_population_percentage
data_type: float
data_type: decimal
tests: [not_null]

- name: eligibility_flag
Expand Down
11 changes: 10 additions & 1 deletion products/cdbg/models/product/cdbg_block_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ WITH block_groups AS (

eligibility_calculation AS (
SELECT
*,
geoid,
borough_name,
borough_code,
tract,
block_group,
round(total_floor_area::numeric)::integer AS total_floor_area,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the data type before the cast to numeric?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess double precision - I forgot you can't just round that.

I do think you can just cast a double precision to integer to round though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha. I wasn't sure if casting to integer would round correcting, always round in one direction, or truncate

round(residential_floor_area::numeric)::integer AS residential_floor_area,
round(residential_floor_area_percentage::numeric, 2) AS residential_floor_area_percentage,
low_mod_income_population,
round(low_mod_income_population_percentage::numeric, 2) AS low_mod_income_population_percentage,
low_mod_income_population_percentage > 51 AND residential_floor_area_percentage > 50 AS eligibility_flag
FROM block_groups
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ WITH boros AS (

eligibility_calculation AS (
SELECT
*,
borough_name,
borough_code,
round(total_floor_area::numeric)::integer AS total_floor_area,
round(residential_floor_area::numeric)::integer AS residential_floor_area,
round(residential_floor_area_percentage::numeric, 2) AS residential_floor_area_percentage,
low_mod_income_population::bigint,
round(low_mod_income_population_percentage::numeric, 2) AS low_mod_income_population_percentage,
low_mod_income_population_percentage > 51 AND residential_floor_area_percentage > 50 AS eligibility_flag
FROM boros
),
Expand Down
9 changes: 8 additions & 1 deletion products/cdbg/models/product/cdbg_tracts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ WITH tracts AS (

eligibility_calculation AS (
SELECT
*,
geoid,
borough_name,
borough_code,
round(total_floor_area::numeric)::integer AS total_floor_area,
round(residential_floor_area::numeric)::integer AS residential_floor_area,
round(residential_floor_area_percentage::numeric, 2) AS residential_floor_area_percentage,
low_mod_income_population,
round(low_mod_income_population_percentage::numeric, 2) AS low_mod_income_population_percentage,
low_mod_income_population_percentage > 51 AND residential_floor_area_percentage > 50 AS eligibility_flag
FROM tracts
),
Expand Down
2 changes: 1 addition & 1 deletion products/cdbg/models/staging/stg__census_data_blocks.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SELECT
geoid20 AS bctcb2020,
borocode AS borough_code,
borocode::integer AS borough_code,
geogname AS borough_name,
"pop1.1"::numeric AS total_population -- noqa: RF01
FROM {{ source("recipe_sources", "dcp_censusdata_blocks") }}
Expand Down
8 changes: 4 additions & 4 deletions products/cdbg/models/staging/stg__low_mod_by_block_group.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
SELECT
"GEOID"::text AS geoid, -- TODO: coerce to text in ingest
"BORO" AS boro,
"TRACT" AS tract,
"BLKGRP" AS block_group,
REPLACE("LOWMODUNIV", ',', '')::numeric AS potential_lowmod_population,
REPLACE("LOWMOD", ',', '')::numeric AS low_mod_income_population,
"TRACT"::text AS tract,
"BLKGRP"::text AS block_group,
REPLACE("LOWMODUNIV", ',', '')::integer AS potential_lowmod_population,
REPLACE("LOWMOD", ',', '')::integer AS low_mod_income_population,
RTRIM("LOWMOD_PCT", '%')::numeric AS low_mod_income_population_percentage
FROM {{ source("recipe_sources", "hud_lowmodincomebyblockgroup") }}
2 changes: 2 additions & 0 deletions products/cdbg/recipe.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Community Development Block Grant
product: db-cdbg
version_strategy:
pin_to_source_dataset: hud_lowmodincomebyblockgroup
inputs:
missing_versions_strategy: find_latest
datasets:
Expand Down
Loading