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

Convert cities dataset into Postgres database #144

Merged
merged 147 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
efe6776
updated readme
rfl-urbaniak Jan 18, 2024
fc50736
adding mailing list blurb from polis.basis.ai
Jan 18, 2024
f77b557
Merge pull request #114 from BasisResearch/ru-update-readme-in-main
emackev Jan 18, 2024
26865db
add code for loading raw & processed parcel data into db
jfeser Aug 2, 2024
9492302
add code to load raw zip code data
jfeser Aug 2, 2024
e4d7eb2
merge code for loading raw shapes and raw zip codes
jfeser Aug 5, 2024
d1cae37
use correct srid when creating joined parcel table
jfeser Aug 5, 2024
7cae0db
make comments visible in db
jfeser Aug 5, 2024
936ce9f
enable logging
jfeser Aug 5, 2024
ad5dff5
remove table prefixes from field names
jfeser Aug 5, 2024
e90d833
load zip data into shared table
jfeser Aug 5, 2024
c2c3aa6
actually load zip data
jfeser Aug 5, 2024
2d8e43c
switch from dates to validity ranges
jfeser Aug 5, 2024
ccb3b62
create parcel to zip mapping
jfeser Aug 5, 2024
57e7609
track why a parcel was assigned to a zip code
jfeser Aug 6, 2024
cf4b4a0
reformat sql
jfeser Aug 6, 2024
4fb0199
process raw census data
jfeser Aug 6, 2024
70a1a34
add census schema and mapping from parcels to block groups
jfeser Aug 6, 2024
648f22c
fix parcel validities
jfeser Aug 6, 2024
56a0b98
add code to process permits and match them to parcels
jfeser Aug 6, 2024
4f9de18
create a property values view
jfeser Aug 7, 2024
dba7a29
add code to create real_estate_transactions table
jfeser Aug 7, 2024
6c2d43d
add code to load acs demographic data
jfeser Aug 7, 2024
fef6573
remove commented code
jfeser Aug 7, 2024
6a05387
add code to load fair market rents
jfeser Aug 8, 2024
dc6acb7
add code to load usps migration data
jfeser Aug 8, 2024
f868a53
remove comment
jfeser Aug 8, 2024
04b83cd
rename schema.sql to parcel_schema.sql
jfeser Aug 8, 2024
d3fdee8
clean up load_parcels.py
jfeser Aug 8, 2024
14a297b
reformat
jfeser Aug 8, 2024
520efed
add segregation index
jfeser Aug 9, 2024
12c5289
add dbt version of transformations
jfeser Aug 13, 2024
6b43cc1
remove unused seed
jfeser Aug 14, 2024
0eaf473
add segregation index model
jfeser Aug 14, 2024
bcd8415
use 2010 census tract/bg data for all years before 2013
jfeser Aug 14, 2024
f7e78b7
split zip codes into three models and aggregate regions to avoid dupl…
jfeser Aug 14, 2024
43675a2
switch to package syntax
jfeser Aug 14, 2024
3151293
improve performance of region tagging by adding indexes
jfeser Aug 14, 2024
50fc6f2
add data tests
jfeser Aug 14, 2024
ef8024b
fix tests
jfeser Aug 14, 2024
46de3ff
add sources when available
jfeser Aug 14, 2024
ed11162
add remaining sources referencing old data loads
jfeser Aug 14, 2024
9d68634
create model for parking data
jfeser Aug 14, 2024
b76e6b7
fix source
jfeser Aug 14, 2024
c450b7d
add parking to parcel mapping
jfeser Aug 14, 2024
516c56a
add indexes to improve perf
jfeser Aug 14, 2024
310a6da
fix source
jfeser Aug 14, 2024
e0f9760
more perf tuning
jfeser Aug 14, 2024
3ae366e
add postgrest config and example schema
jfeser Aug 14, 2024
2ae4724
import new sources and begin converting
jfeser Aug 16, 2024
a718450
more conversion
jfeser Aug 16, 2024
15827e3
more conversion
jfeser Aug 16, 2024
8b10c8f
Merge branch 'db-parcels' into db-api
jfeser Aug 16, 2024
367ceb2
add acs tract wide table
jfeser Aug 16, 2024
9ea31f2
adding file to load from bucket to db
naiimic Aug 16, 2024
889d8ce
filter tracts in wide table using city boundary
jfeser Aug 16, 2024
ed6c7e6
convert all geometry to standardized srid
jfeser Aug 19, 2024
b26754c
include all zip codes that intersect the city boundary
jfeser Aug 19, 2024
8159ba1
include all zip codes that intersect the city boundary
jfeser Aug 19, 2024
8c96c52
convert all geometry to standardized srid
jfeser Aug 19, 2024
98437a0
add downtown, university, and transit lines
jfeser Aug 19, 2024
bc1217d
add missing primary keys to allow for qgis viz
jfeser Aug 19, 2024
b177443
add high frequency transit stops model
jfeser Aug 19, 2024
778a445
Merge branch 'db-parcels' of github.com:BasisResearch/cities into db-…
jfeser Aug 19, 2024
190cbeb
remove version 1
jfeser Aug 19, 2024
028368f
add dependencies to setup.py
jfeser Aug 20, 2024
2f517dc
Merge branch 'db-api' into db-parcels
jfeser Aug 20, 2024
8e7ebf8
format
jfeser Aug 20, 2024
d2cfa81
ensure postgis is loaded
jfeser Aug 20, 2024
6870b93
unconditionally load all data
jfeser Aug 20, 2024
a72f6f0
null out missing data in residential_permits
jfeser Aug 20, 2024
d33588e
ensure table drop succeeds
jfeser Aug 20, 2024
6c3b575
start adding tables to be used by the model
jfeser Aug 20, 2024
a820807
fix misnamed field
jfeser Aug 20, 2024
9590725
remove last references to old raw schema
jfeser Aug 21, 2024
42847e5
add new loader for acs data
jfeser Aug 21, 2024
686ee09
correctly refer to seed
jfeser Aug 21, 2024
30323c4
more work on tables for model
jfeser Aug 21, 2024
5800b5d
more work on tables for model
jfeser Aug 21, 2024
13952dc
cleanup
jfeser Aug 21, 2024
92f6285
finish adding columns to census_tracts_wide
jfeser Aug 21, 2024
2526c31
remove unused line in clean.sh
jfeser Aug 21, 2024
8da0487
switch api to use public schema
jfeser Aug 22, 2024
9319009
distance to transit should include lines and stops
jfeser Aug 22, 2024
d353167
correct year range for parcels
jfeser Aug 22, 2024
24af31a
add census_tract field
jfeser Aug 22, 2024
5a00c7f
add acs fields to census_tract_wide
jfeser Aug 22, 2024
3ab57e7
readd field name
jfeser Aug 23, 2024
353bcfe
add whiteness and income demographic variables to census_tracts_wide
jfeser Aug 23, 2024
a5b311f
correctly handle the downtown parking limit elimination
jfeser Aug 23, 2024
ea007ac
add mean_limit to the wide table
jfeser Aug 23, 2024
95973f6
correctly load hispanic_or_latino acs data
jfeser Aug 26, 2024
8605652
filter census tracts to city boundary when computing segregation indexes
jfeser Aug 26, 2024
f69a868
unify approach to year replacement for demographic data
jfeser Aug 26, 2024
b776310
fix up acs_tract_wide
jfeser Aug 27, 2024
465bbfe
add type casts
jfeser Aug 27, 2024
be166d2
extract demographics table
jfeser Aug 27, 2024
4cd4b36
switch api over to demographics wide table
jfeser Aug 27, 2024
5ca1ba8
add census_tracts api endpoint
jfeser Aug 27, 2024
ef1fb5b
use filtered census tracts in api endpoint
jfeser Aug 28, 2024
2912c19
add dbt to dev depends
jfeser Aug 28, 2024
027085e
add numeric encoding of census tracts
jfeser Aug 28, 2024
8a00c91
use original column names in census_tracts_wide
jfeser Aug 28, 2024
0ede914
add standardize functions for categorical variables
jfeser Aug 28, 2024
ca3f8e7
census tracts need to be in 4269 for the api
jfeser Aug 29, 2024
eccbd84
reorganize dbt models to support census tract model
jfeser Aug 29, 2024
1fdbf2b
refactor
jfeser Aug 29, 2024
cfa9713
move tract filters to shared view
jfeser Aug 29, 2024
d602f9c
filter out data before 2011, because we don't have demographics
jfeser Aug 29, 2024
08cc805
reorganize api and add high frequency transit
jfeser Aug 30, 2024
68de90b
replace 2020 census tract geometry with 2019
jfeser Aug 30, 2024
9da6843
fix srid for api
jfeser Aug 30, 2024
2de2b05
type conversion
jfeser Sep 3, 2024
affab56
simplify acs models
jfeser Sep 3, 2024
cea3777
simplify zip_codes
jfeser Sep 3, 2024
80e1fb2
document acs data
jfeser Sep 3, 2024
1b48785
document and reorganize zip code models
jfeser Sep 3, 2024
489cbaa
document and reorganize residential permit model
jfeser Sep 3, 2024
d263aae
reorganize and document commercial permits
jfeser Sep 3, 2024
f5ae90c
reorganize fair market rents
jfeser Sep 3, 2024
9a84d87
document high frequency transit lines
jfeser Sep 3, 2024
ab2eb43
reorganize parcels
jfeser Sep 3, 2024
0bda2c7
finish reorganization
jfeser Sep 3, 2024
fd89ca4
reorganize fair market rents data
jfeser Sep 4, 2024
4d3cf74
add mapping table for translating from zip codes to zctas
jfeser Sep 4, 2024
6639fa0
add new api code
jfeser Sep 4, 2024
7b818b9
continue refactoring zip codes to zctas
jfeser Sep 4, 2024
f3c2fc1
refactor load_server to no longer require the password as an env var
jfeser Sep 4, 2024
76fa4a7
handle one to many mapping problem
jfeser Sep 4, 2024
85d8325
add more documentation
jfeser Sep 4, 2024
e52b637
materialize for performance
jfeser Sep 4, 2024
744d8b6
add .env
jfeser Sep 4, 2024
266d24f
add mean and median parcel areas per tract
jfeser Sep 5, 2024
f4a5af4
forgot to standardize new columns
jfeser Sep 5, 2024
1babc51
add temp high frequency transit lines endpoint
jfeser Sep 5, 2024
801b7a7
add more documentation to tracts model tables
jfeser Sep 6, 2024
85eac69
remove spurious points from transit lines data
jfeser Sep 6, 2024
f512ec0
fix dropped model
jfeser Sep 6, 2024
3d5ccd1
remove unused config
jfeser Sep 6, 2024
ec72e2c
drop api from pr
jfeser Sep 6, 2024
8a76e97
select correct geometries
jfeser Sep 6, 2024
d37849a
update .gitignore from ru-tracts-model
rfl-urbaniak Sep 9, 2024
c958a71
Merge branch 'staging-zoning' of https://github.com/BasisResearch/cit…
rfl-urbaniak Sep 9, 2024
6e2a361
new format lint
rfl-urbaniak Sep 9, 2024
5cf1ca5
pin pyro to pass inference test
rfl-urbaniak Sep 9, 2024
e79617c
getting inference test to work
rfl-urbaniak Sep 9, 2024
348bc00
add seaborn to setup to pass notebook tests
rfl-urbaniak Sep 9, 2024
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
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GOOGLE_CLOUD_PROJECT=cities-429602
GOOGLE_CLOUD_BUCKET=minneapolis-basis
SCHEMA=minneapolis
HOST=34.123.100.76
DATABASE=cities
USERNAME=postgres
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ tests/.coverage
.vscode/launch.json
data/sql/counties_database.db
data/sql/msa_database.db
.Rproj.user
**/*.RData
**/*.Rhistory

# data
data/minneapolis/processed/values_long.csv
data/minneapolis/processed/values_with_parking.csv
data/minneapolis/sourced/demographic/**
data/minneapolis/preds/**
data/minneapolis/sourced/parcel_to_census_tract_mappings/**
data/minneapolis/sourced/parcel_to_parking_info_mappings/**

data/minneapolis/.pgpass
64 changes: 55 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,52 @@
</p>


## Evaluating Policy Transfer via Similarity Analysis and Causal Inference
# Evaluating Policy Transfer via Similarity Analysis and Causal Inference


## Getting started


Welcome to the repository for [polis](http://polis.basis.ai/), developed by [Basis Research Institute](https://www.basis.ai/) for [The Opportunity Project (TOP)](https://opportunity.census.gov/) 2023 in collaboration with the U.S. Department of Commerce. The primary goal of this project is to enhance access to data for local policymakers, facilitating more informed decision-making.

This is the backend repository for more advanced users. For a more pleasant frontend experience and more information, please use the [app](http://polis.basis.ai/).


Installation
------------

**Basic Setup:**

```sh

git clone [email protected]:BasisResearch/cities.git
cd cities
git checkout main
pip install .
```
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .
cd tests && python -m pytest

The above will install the minimal version that's ported to [polis.basis.ai](http://polis.basis.ai)

**Dev Setup:**

To install dev dependencies, needed to run models, train models and run all the tests, run the following command:

```sh
pip install -e .[dev]
```

Details of which packages are available in which see `setup.py`.

Welcome to the repository for [polis](http://polis.basis.ai/), developed by the [Basis Research Institute](https://www.basis.ai/) for [The Opportunity Project (TOP)](https://opportunity.census.gov/) 2023 in collaboration with the U.S. Department of Commerce. The primary goal of this project is to enhance access to data for local policymakers, facilitating more informed decision-making.

This is the backend repository for more advanced users. For a more pleasant frontend experience and more information, please use the [app](http://polis.basis.ai/).
** Contributing: **

Before submitting a pull request, please autoformat code and ensure that unit tests pass locally

```sh
make lint # linting
make format # runs black and isort, including on notebooks in the docs/ folder
make tests # linting, unit and notebook tests
```


### The repository is structured as follows:
Expand All @@ -36,11 +69,24 @@ This is the backend repository for more advanced users. For a more pleasant fron
└── tests
```

**WARNING: during the beta testing, the most recent version lives on the `staging-county-data` git branch, and so do the most recent versions of the notebooks. Please switch to this branch before inspecting the notebooks.

If you're interested in downloading the data or exploring advanced features beyond the frontend, check out the `guides` folder in the `docs` directory. There, you'll find:
- `data_sources.ipynb` for information on data sources,
- `similarity-conceptual.ipynb` for a conceptual account of how similarity comparison works.
- `counterfactual-explained.ipynb` contains a rough explanation of how our causal model works.
- `similarity_demo.ipynb` demonstrating the use of the `DataGrabber` class for easy data acces, and of our `FipsQuery` class, which is the key tool in the similarity-focused part of the project,
- `causal_insights_demo.ipynb` for an overview of how the `CausalInsight` class can be used to explore the influence of a range of intervention variables thanks to causal inference tools we employed. [WIP]

Feel free to dive into these resources to gain deeper insights into the capabilities of the Polis project, or to reach out if you have any comments or suggestions.
## Interested? We'd love to hear from you.

[polis](http://polis.basis.ai/) is a research tool under very active development, and we are eager to hear feedback from users in the policymaking and public administration spaces to accelerate its benefit.

If you have feature requests, recommendations for new data sources, tips for how to resolve missing data issues, find bugs in the tool (they certainly exist!), or anything else, please do not hesitate to contact us at [email protected].

To stay up to date on our latest features, you can subscribe to our [mailing list](https://dashboard.mailerlite.com/forms/102625/110535550672308121/share). In the near-term, we will send out a notice about our upcoming batch of improvements (including performance speedups, support for mobile, and more comprehensive tutorials), as well as an interest form for users who would like to work closely with us on case studies to make the tool most useful in their work.

Lastly, we emphasize that this website is still in beta testing, and hence all predictions should be taken with a grain of salt.

Acknowledgments: polis was built by Basis, a non-profit AI research organization dedicated to creating automated reasoning technology that helps solve society's most intractable problems. To learn more about us, visit https://basis.ai.

2 changes: 1 addition & 1 deletion cities/modeling/model_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from typing import Optional

import dill
import pyro
import pyro.distributions as dist
import torch

import pyro
from cities.modeling.modeling_utils import (
prep_wide_data_for_inference,
train_interactions_model,
Expand Down
2 changes: 1 addition & 1 deletion cities/modeling/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import matplotlib.pyplot as plt
import pandas as pd
import pyro
import torch
from pyro.infer import SVI, Trace_ELBO
from pyro.infer.autoguide import AutoNormal
from pyro.optim import Adam # type: ignore
from scipy.stats import spearmanr

import pyro
from cities.utils.data_grabber import (
DataGrabber,
list_available_features,
Expand Down
5 changes: 3 additions & 2 deletions cities/queries/causal_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import pyro
import torch
from sklearn.preprocessing import StandardScaler

import pyro
from cities.modeling.model_interactions import model_cities_interaction
from cities.modeling.modeling_utils import prep_wide_data_for_inference
from cities.utils.cleaning_utils import (
Expand Down Expand Up @@ -576,7 +576,8 @@ def estimate_ATE(self):
label=f"mean = {tau_samples.mean():.3f}",
)
plt.title(
f"ATE for {self.intervention_dataset} and {self.outcome_dataset} with forward shift = {self.forward_shift}"
f"ATE for {self.intervention_dataset} and {self.outcome_dataset} "
f"with forward shift = {self.forward_shift}"
)
plt.ylabel("counts")
plt.xlabel("ATE")
Expand Down
3 changes: 3 additions & 0 deletions dbt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
dbt_packages/
logs/
15 changes: 15 additions & 0 deletions dbt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Welcome to your new dbt project!

### Using the starter project

Try running the following commands:
- dbt run
- dbt test


### Resources:
- Learn more about dbt [in the docs](https://docs.getdbt.com/docs/introduction)
- Check out [Discourse](https://discourse.getdbt.com/) for commonly asked questions and answers
- Join the [chat](https://community.getdbt.com/) on Slack for live discussions and support
- Find [dbt events](https://events.getdbt.com) near you
- Check out [the blog](https://blog.getdbt.com/) for the latest news on dbt's development and best practices
Empty file added dbt/analyses/.gitkeep
Empty file.
29 changes: 29 additions & 0 deletions dbt/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'cities'
version: '1.0.0'

# This setting configures which "profile" dbt uses for this project.
profile: 'cities'

# These configurations specify where dbt should look for different types of files.
# The `model-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"


vars:
srid: 26915 # use UTM zone 15N for all geometric data. note, this must have meters as the unit of measure
# years for which we have census tract/block group data
census_years: [2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023]
Empty file added dbt/macros/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions dbt/macros/median.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro median(attr) %}
(percentile_cont(0.5) within group (order by {{ attr }}))
{% endmacro %}
3 changes: 3 additions & 0 deletions dbt/macros/safe_divide.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro safe_divide(num, dem) %}
(case when {{ dem }} = 0 then 0 else {{ num }} / {{ dem }} end)
{% endmacro %}
13 changes: 13 additions & 0 deletions dbt/macros/standardize.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro standardize_cont(columns) %}
{% for c in columns %}
{{ c }} as {{ c }}_original, (({{ c }} - (avg({{ c }}) over ())) / (stddev_samp({{ c }}) over ()))::double precision as {{ c }}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endmacro %}

{% macro standardize_cat(columns) %}
{% for c in columns %}
{{ c }} as {{ c }}_original, (dense_rank() over (order by {{ c }})) - 1 as {{ c }}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endmacro %}
69 changes: 69 additions & 0 deletions dbt/macros/tag_regions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- Tag regions with their containing/most intersecting/closest parent regions.
-- child_table: table with the child regions
-- parent_table: table with the parent regions
-- max_distance: maximum distance to consider a region as a parent (meters)
{% macro tag_regions(child_table, parent_table, max_distance=100) %}
(
-- the not materialized keyword allows us to use indexes on the child and parent
-- tables
with child as not materialized (
select * from {{child_table}}
)
, parent as not materialized (
select * from {{parent_table}}
)
, within as (
select child.id as child_id
, parent.id as parent_id
, child.valid * parent.valid as valid
from
child
inner join parent
on ST_Within (child.geom, parent.geom)
and child.valid && parent.valid
)
, not_within as (
select * from child
where not exists (select child_id from within where child_id = id)
)
, largest_overlap as (
select distinct on (child.id)
child.id as child_id
, parent.id as parent_id
, child.valid * parent.valid as valid
from
not_within as child
inner join parent
on ST_Intersects (child.geom, parent.geom)
and child.valid && parent.valid
order by
child_id,
ST_Area (ST_Intersection (child.geom, parent.geom)) desc
)
, no_overlap as (
select * from not_within
where not exists (
select child_id from largest_overlap where child_id = id
)
)
, closest as (
select distinct on (child.id)
child.id as child_id
, parent.id as parent_id
, child.valid * parent.valid as valid
from
no_overlap as child
inner join parent
on child.valid && parent.valid
and ST_DWithin (child.geom, parent.geom, {{max_distance}})
order by
child_id,
ST_Distance (child.geom, parent.geom)
)
select *, 'within' as type_ from within
union all
select *, 'most_overlap' as type_ from largest_overlap
union all
select *, 'closest' as type_ from closest
)
{% endmacro %}
15 changes: 15 additions & 0 deletions dbt/models/acs_block_group.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{
config(
materialized='table',
indexes = [
{'columns': ['census_block_group', 'year_', 'name_'], 'unique': true},
]
)
}}

select
year::smallint as year_,
code as name_,
statefp || countyfp || tractce || blkgrpce as census_block_group,
case when "value" < 0 then null else "value" end as value_
from {{ source('minneapolis', 'acs_bg_raw') }}
15 changes: 15 additions & 0 deletions dbt/models/acs_tract.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{
config(
materialized='table',
indexes = [
{'columns': ['census_tract', 'year_', 'name_'], 'unique': true},
]
)
}}

select
year::smallint as year_,
code as name_,
statefp || countyfp || tractce as census_tract,
case when "value" < 0 then null else "value" end as value_
from {{ source('minneapolis', 'acs_tract_raw') }}
16 changes: 16 additions & 0 deletions dbt/models/api/api__census_tracts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{
config(
materialized='table',
indexes = [
{'columns': ['year_']}
]
)
}}

with census_tracts as (select * from {{ ref('census_tracts_in_city_boundary') }})
select
census_tract
, year_
, st_transform(geom, 4269) as geom
from
census_tracts
34 changes: 34 additions & 0 deletions dbt/models/api/api__demographics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{
config(
materialized='table',
indexes = [
{'columns': ['description']}
]
)
}}

-- This is used by the web app. It has a row for each tract, demographic
-- variable pair and a column for each year.
with
demographics as (select * from {{ ref('demographics') }}),
census_tracts as (select * from {{ ref('census_tracts_in_city_boundary') }}),
demographics_filtered as (
select demographics.*
from demographics
inner join census_tracts using (census_tract, year_)
),
final_ as (
select
description,
census_tract as tract_id,
{{ dbt_utils.pivot('year_',
dbt_utils.get_column_values(ref('demographics'),
'year_',
order_by='year_'),
then_value='value_',
else_value='null',
agg='max') }}
from demographics_filtered
group by 1, 2
)
select * from final_
Loading
Loading