Skip to content

Commit

Permalink
Be less clever with jinja, explicitly list column names
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-r-rose committed Sep 5, 2023
1 parent 609d710 commit d982dd2
Showing 1 changed file with 20 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/* Columns selected and renamed from footprints, blocks, and places
are reused in several places. To avoid repeating them with the possibility
for errors, we set them in lists/dictionaries where appropriate. */
{%- set footprint_cols = ['"release"', '"capture_dates_range"'] -%}

{%- set block_cols = {
'"COUNTYFP20"': '"county_fips"',
'"TRACTCE20"': '"tract"',
'"BLOCKCE20"': '"block"',
'"GEOID20"': '"block_geoid"',
}
-%}

{%- set place_cols = {
'"PLACEFP"': '"place_fips"',
'"PLACENS"': '"place_ns"',
'"GEOID"': '"place_geoid"',
'"NAME"': '"place_name"',
'"CLASSFP"': '"class_fips_code"',
}
-%}

with footprints as (
select * from {{ source('building_footprints', 'california_building_footprints') }}
select
"release",
"capture_dates_range",
"geometry"
from {{ source('building_footprints', 'california_building_footprints') }}
),

blocks_source as (
select * from {{ source('tiger_2022', 'blocks') }}
select *
from {{ source('tiger_2022', 'blocks') }}
),

places_source as (
Expand All @@ -34,18 +17,21 @@ places_source as (

blocks as (
select
{% for k, v in block_cols.items() -%}
{{ k }} as {{ v }},
{% endfor -%}
"COUNTYFP20" as "county_fips",
"TRACTCE20" as "tract",
"BLOCKCE20" as "block",
"GEOID20" as "block_geoid",
"geometry"
from blocks_source
),

places as (
select
{% for k, v in place_cols.items() -%}
{{ k }} as {{ v }},
{% endfor -%}
"PLACEFP" as "place_fips",
"PLACENS" as "place_ns",
"GEOID" as "place_geoid",
"NAME" as "place_name",
"CLASSFP" as "class_fips_code",
{{ map_class_fips("CLASSFP") }} as "class_fips",
"geometry"
from places_source
Expand All @@ -55,8 +41,8 @@ footprints_with_blocks as (
{{ spatial_join_with_deduplication(
"footprints",
"blocks",
footprint_cols,
block_cols.values(),
['"release"', '"capture_dates_range"'],
['"county_fips"', '"tract"', '"block"', '"block_geoid"'],
left_geom='"geometry"',
right_geom='"geometry"',
kind="inner",
Expand All @@ -68,8 +54,8 @@ footprints_with_blocks_and_places as (
{{ spatial_join_with_deduplication(
"footprints_with_blocks",
"places",
footprint_cols + block_cols.values() | list,
place_cols.values() | list + ['"class_fips"'],
['"release"', '"capture_dates_range"', '"county_fips"', '"tract"', '"block"', '"block_geoid"'],
['"place_fips"', '"place_ns"', '"place_geoid"', '"place_name"', '"class_fips_code"', '"class_fips"'],
left_geom='"geometry"',
right_geom='"geometry"',
kind="left",
Expand Down

0 comments on commit d982dd2

Please sign in to comment.