removing table returning census snapshot function for json returning one for now

This commit is contained in:
Stuart Lynn
2016-04-25 10:45:54 -04:00
parent 20ec7ef25a
commit ace34f6ad8
2 changed files with 307 additions and 213 deletions
+302 -206
View File
@@ -22,217 +22,313 @@
-- Creates a table of demographic snapshot -- Creates a table of demographic snapshot
CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetDemographicSnapshot(geom geometry, time_span text default '2009 - 2013', geometry_level text default '"us.census.tiger".block_group') CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetDemographicSnapshotJ(geom geometry, time_span text default '2009 - 2013', geometry_level text default '"us.census.tiger".block_group')
RETURNS json RETURNS SETOF JSON
AS $$ AS $$
DECLARE
target_cols text[];
BEGIN BEGIN
RETURN row_to_json(cdb_observatory._OBS_GetDemographicSnapshot(geom, time_span, geometry_level)); target_cols := Array['total_pop',
'male_pop',
'female_pop',
'median_age',
'white_pop',
'black_pop',
'asian_pop',
'hispanic_pop',
'amerindian_pop',
'other_race_pop',
'two_or_more_races_pop',
'not_hispanic_pop',
--'not_us_citizen_pop',
--'workers_16_and_over',
--'commuters_by_car_truck_van',
--'commuters_drove_alone',
--'commuters_by_carpool',
--'commuters_by_public_transportation',
--'commuters_by_bus',
--'commuters_by_subway_or_elevated',
--'walked_to_work',
--'worked_at_home',
--'children',
'households',
--'population_3_years_over',
--'in_school',
--'in_grades_1_to_4',
--'in_grades_5_to_8',
--'in_grades_9_to_12',
--'in_undergrad_college',
'pop_25_years_over',
'high_school_diploma',
'less_one_year_college',
'one_year_more_college',
'associates_degree',
'bachelors_degree',
'masters_degree',
--'pop_5_years_over',
--'speak_only_english_at_home',
--'speak_spanish_at_home',
--'pop_determined_poverty_status',
--'poverty',
'median_income',
'gini_index',
'income_per_capita',
'housing_units',
'vacant_housing_units',
'vacant_housing_units_for_rent',
'vacant_housing_units_for_sale',
'median_rent',
'percent_income_spent_on_rent',
'owner_occupied_housing_units',
'million_dollar_housing_units',
'mortgaged_housing_units',
--'pop_15_and_over',
--'pop_never_married',
--'pop_now_married',
--'pop_separated',
--'pop_widowed',
--'pop_divorced',
'commuters_16_over',
'commute_less_10_mins',
'commute_10_14_mins',
'commute_15_19_mins',
'commute_20_24_mins',
'commute_25_29_mins',
'commute_30_34_mins',
'commute_35_44_mins',
'commute_45_59_mins',
'commute_60_more_mins',
'aggregate_travel_time_to_work',
'income_less_10000',
'income_10000_14999',
'income_15000_19999',
'income_20000_24999',
'income_25000_29999',
'income_30000_34999',
'income_35000_39999',
'income_40000_44999',
'income_45000_49999',
'income_50000_59999',
'income_60000_74999',
'income_75000_99999',
'income_100000_124999',
'income_125000_149999',
'income_150000_199999',
'income_200000_or_more',
'land_area'];
RETURN QUERY
'select * from cdb_observatory._OBS_GetCensus($1, $2 )'
USING geom, target_cols
RETURN;
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetDemographicSnapshot(geom geometry, time_span text default '2009 - 2013', geometry_level text default '"us.census.tiger".block_group' ) -- CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetDemographicSnapshot(geom geometry, time_span text default '2009 - 2013', geometry_level text default '"us.census.tiger".block_group' )
RETURNS TABLE( -- RETURNS TABLE(
total_pop NUMERIC, -- total_pop NUMERIC,
male_pop NUMERIC, -- male_pop NUMERIC,
female_pop NUMERIC, -- female_pop NUMERIC,
median_age NUMERIC, -- median_age NUMERIC,
white_pop NUMERIC, -- white_pop NUMERIC,
black_pop NUMERIC, -- black_pop NUMERIC,
asian_pop NUMERIC, -- asian_pop NUMERIC,
hispanic_pop NUMERIC, -- hispanic_pop NUMERIC,
amerindian_pop NUMERIC, -- amerindian_pop NUMERIC,
other_race_pop NUMERIC, -- other_race_pop NUMERIC,
two_or_more_races_pop NUMERIC, -- two_or_more_races_pop NUMERIC,
not_hispanic_pop NUMERIC, -- not_hispanic_pop NUMERIC,
--not_us_citizen_pop NUMERIC, -- --not_us_citizen_pop NUMERIC,
--workers_16_and_over NUMERIC, -- --workers_16_and_over NUMERIC,
--commuters_by_car_truck_van NUMERIC, -- --commuters_by_car_truck_van NUMERIC,
--commuters_drove_alone NUMERIC, -- --commuters_drove_alone NUMERIC,
--commuters_by_carpool NUMERIC, -- --commuters_by_carpool NUMERIC,
--commuters_by_public_transportation NUMERIC, -- --commuters_by_public_transportation NUMERIC,
--commuters_by_bus NUMERIC, -- --commuters_by_bus NUMERIC,
--commuters_by_subway_or_elevated NUMERIC, -- --commuters_by_subway_or_elevated NUMERIC,
--walked_to_work NUMERIC, -- --walked_to_work NUMERIC,
--worked_at_home NUMERIC, -- --worked_at_home NUMERIC,
--children NUMERIC, -- TODO we should be able to get this at BG -- --children NUMERIC, -- TODO we should be able to get this at BG
households NUMERIC, -- households NUMERIC,
--population_3_years_over NUMERIC, -- --population_3_years_over NUMERIC,
--in_school NUMERIC, -- --in_school NUMERIC,
--in_grades_1_to_4 NUMERIC, -- --in_grades_1_to_4 NUMERIC,
--in_grades_5_to_8 NUMERIC, -- --in_grades_5_to_8 NUMERIC,
--in_grades_9_to_12 NUMERIC, -- --in_grades_9_to_12 NUMERIC,
--in_undergrad_college NUMERIC, -- --in_undergrad_college NUMERIC,
pop_25_years_over NUMERIC, -- pop_25_years_over NUMERIC,
high_school_diploma NUMERIC, -- high_school_diploma NUMERIC,
less_one_year_college NUMERIC, -- less_one_year_college NUMERIC,
one_year_more_college NUMERIC, -- one_year_more_college NUMERIC,
associates_degree NUMERIC, -- associates_degree NUMERIC,
bachelors_degree NUMERIC, -- bachelors_degree NUMERIC,
masters_degree NUMERIC, -- masters_degree NUMERIC,
--pop_5_years_over NUMERIC, -- --pop_5_years_over NUMERIC,
--speak_only_english_at_home NUMERIC, -- --speak_only_english_at_home NUMERIC,
--speak_spanish_at_home NUMERIC, -- --speak_spanish_at_home NUMERIC,
--pop_determined_poverty_status NUMERIC, -- --pop_determined_poverty_status NUMERIC,
--poverty NUMERIC, -- --poverty NUMERIC,
median_income NUMERIC, -- median_income NUMERIC,
gini_index NUMERIC, -- gini_index NUMERIC,
income_per_capita NUMERIC, -- income_per_capita NUMERIC,
housing_units NUMERIC, -- housing_units NUMERIC,
vacant_housing_units NUMERIC, -- vacant_housing_units NUMERIC,
vacant_housing_units_for_rent NUMERIC, -- vacant_housing_units_for_rent NUMERIC,
vacant_housing_units_for_sale NUMERIC, -- vacant_housing_units_for_sale NUMERIC,
median_rent NUMERIC, -- median_rent NUMERIC,
percent_income_spent_on_rent NUMERIC, -- percent_income_spent_on_rent NUMERIC,
owner_occupied_housing_units NUMERIC, -- owner_occupied_housing_units NUMERIC,
million_dollar_housing_units NUMERIC, -- million_dollar_housing_units NUMERIC,
mortgaged_housing_units NUMERIC, -- mortgaged_housing_units NUMERIC,
--pop_15_and_over NUMERIC, -- --pop_15_and_over NUMERIC,
--pop_never_married NUMERIC, -- --pop_never_married NUMERIC,
--pop_now_married NUMERIC, -- --pop_now_married NUMERIC,
--pop_separated NUMERIC, -- --pop_separated NUMERIC,
--pop_widowed NUMERIC, -- --pop_widowed NUMERIC,
--pop_divorced NUMERIC, -- --pop_divorced NUMERIC,
commuters_16_over NUMERIC, -- commuters_16_over NUMERIC,
commute_less_10_mins NUMERIC, -- commute_less_10_mins NUMERIC,
commute_10_14_mins NUMERIC, -- commute_10_14_mins NUMERIC,
commute_15_19_mins NUMERIC, -- commute_15_19_mins NUMERIC,
commute_20_24_mins NUMERIC, -- commute_20_24_mins NUMERIC,
commute_25_29_mins NUMERIC, -- commute_25_29_mins NUMERIC,
commute_30_34_mins NUMERIC, -- commute_30_34_mins NUMERIC,
commute_35_44_mins NUMERIC, -- commute_35_44_mins NUMERIC,
commute_45_59_mins NUMERIC, -- commute_45_59_mins NUMERIC,
commute_60_more_mins NUMERIC, -- commute_60_more_mins NUMERIC,
aggregate_travel_time_to_work NUMERIC, -- aggregate_travel_time_to_work NUMERIC,
income_less_10000 NUMERIC, -- income_less_10000 NUMERIC,
income_10000_14999 NUMERIC, -- income_10000_14999 NUMERIC,
income_15000_19999 NUMERIC, -- income_15000_19999 NUMERIC,
income_20000_24999 NUMERIC, -- income_20000_24999 NUMERIC,
income_25000_29999 NUMERIC, -- income_25000_29999 NUMERIC,
income_30000_34999 NUMERIC, -- income_30000_34999 NUMERIC,
income_35000_39999 NUMERIC, -- income_35000_39999 NUMERIC,
income_40000_44999 NUMERIC, -- income_40000_44999 NUMERIC,
income_45000_49999 NUMERIC, -- income_45000_49999 NUMERIC,
income_50000_59999 NUMERIC, -- income_50000_59999 NUMERIC,
income_60000_74999 NUMERIC, -- income_60000_74999 NUMERIC,
income_75000_99999 NUMERIC, -- income_75000_99999 NUMERIC,
income_100000_124999 NUMERIC, -- income_100000_124999 NUMERIC,
income_125000_149999 NUMERIC, -- income_125000_149999 NUMERIC,
income_150000_199999 NUMERIC, -- income_150000_199999 NUMERIC,
income_200000_or_more NUMERIC, -- income_200000_or_more NUMERIC,
land_area NUMERIC) -- land_area NUMERIC)
AS $$ -- AS $$
DECLARE -- DECLARE
target_cols text[]; -- target_cols text[];
names text[]; -- names text[];
vals NUMERIC[]; -- vals NUMERIC[];
q text; -- q text;
BEGIN -- BEGIN
target_cols := Array['total_pop', -- target_cols := Array['total_pop',
'male_pop', -- 'male_pop',
'female_pop', -- 'female_pop',
'median_age', -- 'median_age',
'white_pop', -- 'white_pop',
'black_pop', -- 'black_pop',
'asian_pop', -- 'asian_pop',
'hispanic_pop', -- 'hispanic_pop',
'amerindian_pop', -- 'amerindian_pop',
'other_race_pop', -- 'other_race_pop',
'two_or_more_races_pop', -- 'two_or_more_races_pop',
'not_hispanic_pop', -- 'not_hispanic_pop',
--'not_us_citizen_pop', -- --'not_us_citizen_pop',
--'workers_16_and_over', -- --'workers_16_and_over',
--'commuters_by_car_truck_van', -- --'commuters_by_car_truck_van',
--'commuters_drove_alone', -- --'commuters_drove_alone',
--'commuters_by_carpool', -- --'commuters_by_carpool',
--'commuters_by_public_transportation', -- --'commuters_by_public_transportation',
--'commuters_by_bus', -- --'commuters_by_bus',
--'commuters_by_subway_or_elevated', -- --'commuters_by_subway_or_elevated',
--'walked_to_work', -- --'walked_to_work',
--'worked_at_home', -- --'worked_at_home',
--'children', -- --'children',
'households', -- 'households',
--'population_3_years_over', -- --'population_3_years_over',
--'in_school', -- --'in_school',
--'in_grades_1_to_4', -- --'in_grades_1_to_4',
--'in_grades_5_to_8', -- --'in_grades_5_to_8',
--'in_grades_9_to_12', -- --'in_grades_9_to_12',
--'in_undergrad_college', -- --'in_undergrad_college',
'pop_25_years_over', -- 'pop_25_years_over',
'high_school_diploma', -- 'high_school_diploma',
'less_one_year_college', -- 'less_one_year_college',
'one_year_more_college', -- 'one_year_more_college',
'associates_degree', -- 'associates_degree',
'bachelors_degree', -- 'bachelors_degree',
'masters_degree', -- 'masters_degree',
--'pop_5_years_over', -- --'pop_5_years_over',
--'speak_only_english_at_home', -- --'speak_only_english_at_home',
--'speak_spanish_at_home', -- --'speak_spanish_at_home',
--'pop_determined_poverty_status', -- --'pop_determined_poverty_status',
--'poverty', -- --'poverty',
'median_income', -- 'median_income',
'gini_index', -- 'gini_index',
'income_per_capita', -- 'income_per_capita',
'housing_units', -- 'housing_units',
'vacant_housing_units', -- 'vacant_housing_units',
'vacant_housing_units_for_rent', -- 'vacant_housing_units_for_rent',
'vacant_housing_units_for_sale', -- 'vacant_housing_units_for_sale',
'median_rent', -- 'median_rent',
'percent_income_spent_on_rent', -- 'percent_income_spent_on_rent',
'owner_occupied_housing_units', -- 'owner_occupied_housing_units',
'million_dollar_housing_units', -- 'million_dollar_housing_units',
'mortgaged_housing_units', -- 'mortgaged_housing_units',
--'pop_15_and_over', -- --'pop_15_and_over',
--'pop_never_married', -- --'pop_never_married',
--'pop_now_married', -- --'pop_now_married',
--'pop_separated', -- --'pop_separated',
--'pop_widowed', -- --'pop_widowed',
--'pop_divorced', -- --'pop_divorced',
'commuters_16_over', -- 'commuters_16_over',
'commute_less_10_mins', -- 'commute_less_10_mins',
'commute_10_14_mins', -- 'commute_10_14_mins',
'commute_15_19_mins', -- 'commute_15_19_mins',
'commute_20_24_mins', -- 'commute_20_24_mins',
'commute_25_29_mins', -- 'commute_25_29_mins',
'commute_30_34_mins', -- 'commute_30_34_mins',
'commute_35_44_mins', -- 'commute_35_44_mins',
'commute_45_59_mins', -- 'commute_45_59_mins',
'commute_60_more_mins', -- 'commute_60_more_mins',
'aggregate_travel_time_to_work', -- 'aggregate_travel_time_to_work',
'income_less_10000', -- 'income_less_10000',
'income_10000_14999', -- 'income_10000_14999',
'income_15000_19999', -- 'income_15000_19999',
'income_20000_24999', -- 'income_20000_24999',
'income_25000_29999', -- 'income_25000_29999',
'income_30000_34999', -- 'income_30000_34999',
'income_35000_39999', -- 'income_35000_39999',
'income_40000_44999', -- 'income_40000_44999',
'income_45000_49999', -- 'income_45000_49999',
'income_50000_59999', -- 'income_50000_59999',
'income_60000_74999', -- 'income_60000_74999',
'income_75000_99999', -- 'income_75000_99999',
'income_100000_124999', -- 'income_100000_124999',
'income_125000_149999', -- 'income_125000_149999',
'income_150000_199999', -- 'income_150000_199999',
'income_200000_or_more', -- 'income_200000_or_more',
'land_area']; -- 'land_area'];
--
q := 'WITH a As ( -- q :=
SELECT -- $query$
dimension As names, -- WITH a As (
dimension_value As vals -- SELECT
FROM cdb_observatory._OBS_GetCensus($1,$2,$3,$4) -- array_agg(_OBS_GetCensusJ->>'name') As names,
)' || -- array_agg(_OBS_GetCensusJ->>'value') As vals
cdb_observatory._OBS_BuildSnapshotQuery(target_cols) || -- FROM cdb_observatory._OBS_GetCensusJ($1,$2,$3,$4)
' FROM a'; -- )$query$ ||
-- cdb_observatory._OBS_BuildSnapshotQuery(target_cols) ||
RETURN QUERY -- ' FROM a'
EXECUTE -- ;
q --
USING geom, target_cols, time_span, geometry_level; -- RETURN QUERY
-- EXECUTE
RETURN; -- q
END; -- USING geom, target_cols, time_span, geometry_level;
$$ LANGUAGE plpgsql; --
-- RETURN;
-- END;
-- $$ LANGUAGE plpgsql;
--Base functions for performing augmentation --Base functions for performing augmentation
@@ -1,12 +1,10 @@
\i test/sql/load_fixtures.sql \i test/sql/load_fixtures.sql
-- --
SELECT * FROM WITH result as(
cdb_observatory._OBS_GetDemographicSnapshot( Select count(OBS_GetDemographicSnapshot->>'value') expected_columns
cdb_observatory._TestPoint(), FROM cdb_observatory.OBS_GetDemographicSnapshot(_test_point())
'2009 - 2013', ) select expected_columns ='58' as OBS_GetDemographicSnapshot_test_no_returns
'"us.census.tiger".block_group' FROM result
) As snapshot;
-- --
-- dimension | dimension_value -- dimension | dimension_value
-- ----------|---------------- -- ----------|----------------