From 987cfde7b62fdcd0039f514fe5a711b2a13b02f6 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 19 Apr 2016 17:54:56 +0200 Subject: [PATCH] Make OBS_GetDemographicSnapshot return json --- src/pg/sql/41_observatory_augmentation.sql | 103 +----------------- .../41_observatory_augmentation_test.out | 6 +- .../sql/41_observatory_augmentation_test.sql | 4 +- 3 files changed, 9 insertions(+), 104 deletions(-) diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 2eec6f0..a1d0f41 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -24,108 +24,13 @@ 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 json -AS $$ - BEGIN - RETURN row_to_json(cdb_observatory._OBS_GetDemographicSnapshot(geom, time_span, geometry_level)); -END; -$$ 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' ) -RETURNS TABLE( - total_pop NUMERIC, - male_pop NUMERIC, - female_pop NUMERIC, - median_age NUMERIC, - white_pop NUMERIC, - black_pop NUMERIC, - asian_pop NUMERIC, - hispanic_pop NUMERIC, - amerindian_pop NUMERIC, - other_race_pop NUMERIC, - two_or_more_races_pop NUMERIC, - not_hispanic_pop NUMERIC, - --not_us_citizen_pop NUMERIC, - --workers_16_and_over NUMERIC, - --commuters_by_car_truck_van NUMERIC, - --commuters_drove_alone NUMERIC, - --commuters_by_carpool NUMERIC, - --commuters_by_public_transportation NUMERIC, - --commuters_by_bus NUMERIC, - --commuters_by_subway_or_elevated NUMERIC, - --walked_to_work NUMERIC, - --worked_at_home NUMERIC, - --children NUMERIC, -- TODO we should be able to get this at BG - households NUMERIC, - --population_3_years_over NUMERIC, - --in_school NUMERIC, - --in_grades_1_to_4 NUMERIC, - --in_grades_5_to_8 NUMERIC, - --in_grades_9_to_12 NUMERIC, - --in_undergrad_college NUMERIC, - pop_25_years_over NUMERIC, - high_school_diploma NUMERIC, - less_one_year_college NUMERIC, - one_year_more_college NUMERIC, - associates_degree NUMERIC, - bachelors_degree NUMERIC, - masters_degree NUMERIC, - --pop_5_years_over NUMERIC, - --speak_only_english_at_home NUMERIC, - --speak_spanish_at_home NUMERIC, - --pop_determined_poverty_status NUMERIC, - --poverty NUMERIC, - median_income NUMERIC, - gini_index NUMERIC, - income_per_capita NUMERIC, - housing_units NUMERIC, - vacant_housing_units NUMERIC, - vacant_housing_units_for_rent NUMERIC, - vacant_housing_units_for_sale NUMERIC, - median_rent NUMERIC, - percent_income_spent_on_rent NUMERIC, - owner_occupied_housing_units NUMERIC, - million_dollar_housing_units NUMERIC, - mortgaged_housing_units NUMERIC, - --pop_15_and_over NUMERIC, - --pop_never_married NUMERIC, - --pop_now_married NUMERIC, - --pop_separated NUMERIC, - --pop_widowed NUMERIC, - --pop_divorced NUMERIC, - commuters_16_over NUMERIC, - commute_less_10_mins NUMERIC, - commute_10_14_mins NUMERIC, - commute_15_19_mins NUMERIC, - commute_20_24_mins NUMERIC, - commute_25_29_mins NUMERIC, - commute_30_34_mins NUMERIC, - commute_35_44_mins NUMERIC, - commute_45_59_mins NUMERIC, - commute_60_more_mins NUMERIC, - aggregate_travel_time_to_work NUMERIC, - income_less_10000 NUMERIC, - income_10000_14999 NUMERIC, - income_15000_19999 NUMERIC, - income_20000_24999 NUMERIC, - income_25000_29999 NUMERIC, - income_30000_34999 NUMERIC, - income_35000_39999 NUMERIC, - income_40000_44999 NUMERIC, - income_45000_49999 NUMERIC, - income_50000_59999 NUMERIC, - income_60000_74999 NUMERIC, - income_75000_99999 NUMERIC, - income_100000_124999 NUMERIC, - income_125000_149999 NUMERIC, - income_150000_199999 NUMERIC, - income_200000_or_more NUMERIC, - land_area NUMERIC) AS $$ DECLARE target_cols text[]; names text[]; vals NUMERIC[]; q text; + rec record; BEGIN target_cols := Array['total_pop', 'male_pop', @@ -225,12 +130,12 @@ BEGIN cdb_observatory._OBS_BuildSnapshotQuery(target_cols) || ' FROM a'; - RETURN QUERY EXECUTE q - USING geom, target_cols, time_span, geometry_level; + USING geom, target_cols, time_span, geometry_level + INTO rec; - RETURN; + RETURN row_to_json(rec); END; $$ LANGUAGE plpgsql; diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index 07ce113..9dd023b 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -21,9 +21,9 @@ Loading obs_11ee8b82c877c073438bc935a91d3dfccef875d1.sql fixture file... Done. Loading obs_d34555209878e8c4b37cf0b2b3d072ff129ec470.sql fixture file... Done. - 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 | households | pop_25_years_over | high_school_diploma | less_one_year_college | one_year_more_college | associates_degree | bachelors_degree | masters_degree | 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 | 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 -------------------+------------------+------------------+------------+------------------+------------------+------------------+------------------+----------------+----------------+-----------------------+------------------+------------------+-------------------+---------------------+-----------------------+-----------------------+-------------------+------------------+------------------+---------------+------------+-------------------+------------------+----------------------+-------------------------------+-------------------------------+-------------+------------------------------+------------------------------+------------------------------+-------------------------+-------------------+----------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+----------------------+-------------------------------+-------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+----------------------+----------------------+----------------------+-----------------------+----------- - 9516.27915900609 | 6152.51885204623 | 3363.76030695986 | 28.8 | 5301.51624447348 | 149.500458087105 | 230.000704749392 | 3835.26175169611 | 0 | 0 | 0 | 5681.01740730998 | 3323.51018362871 | 7107.02177675621 | 1040.753188991 | 69.0002114248176 | 793.502431385402 | 327.751004267883 | 2742.7584041365 | 931.502854235037 | 66304 | 0.3494 | 28291 | 3662.76122313407 | 339.251039505353 | 120.750369993431 | 0 | 1764 | 35.3 | 339.251039505353 | 0 | 224.250687130657 | 6549.27006773893 | 327.751004267883 | 28.750088093674 | 201.250616655718 | 621.001902823358 | 373.751145217762 | 1851.5056732326 | 1414.50433420876 | 1115.50341803455 | 615.251885204623 | | 57.500176187348 | 0 | 212.750651893187 | 408.251250930171 | 0 | 155.25047570584 | 109.250334755961 | 92.0002818997568 | 63.2501938060828 | 184.000563799514 | 621.001902823358 | 552.001691398541 | 327.751004267883 | 333.501021886618 | 126.500387612166 | | + snapshot +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + {"total_pop":9516.27915900609,"male_pop":6152.51885204623,"female_pop":3363.76030695986,"median_age":28.8,"white_pop":5301.51624447348,"black_pop":149.500458087105,"asian_pop":230.000704749392,"hispanic_pop":3835.26175169611,"amerindian_pop":0,"other_race_pop":0,"two_or_more_races_pop":0,"not_hispanic_pop":5681.01740730998,"households":3323.51018362871,"pop_25_years_over":7107.02177675621,"high_school_diploma":1040.753188991,"less_one_year_college":69.0002114248176,"one_year_more_college":793.502431385402,"associates_degree":327.751004267883,"bachelors_degree":2742.7584041365,"masters_degree":931.502854235037,"median_income":66304,"gini_index":0.3494,"income_per_capita":28291,"housing_units":3662.76122313407,"vacant_housing_units":339.251039505353,"vacant_housing_units_for_rent":120.750369993431,"vacant_housing_units_for_sale":0,"median_rent":1764,"percent_income_spent_on_rent":35.3,"owner_occupied_housing_units":339.251039505353,"million_dollar_housing_units":0,"mortgaged_housing_units":224.250687130657,"commuters_16_over":6549.27006773893,"commute_less_10_mins":327.751004267883,"commute_10_14_mins":28.750088093674,"commute_15_19_mins":201.250616655718,"commute_20_24_mins":621.001902823358,"commute_25_29_mins":373.751145217762,"commute_30_34_mins":1851.5056732326,"commute_35_44_mins":1414.50433420876,"commute_45_59_mins":1115.50341803455,"commute_60_more_mins":615.251885204623,"aggregate_travel_time_to_work":null,"income_less_10000":57.500176187348,"income_10000_14999":0,"income_15000_19999":212.750651893187,"income_20000_24999":408.251250930171,"income_25000_29999":0,"income_30000_34999":155.25047570584,"income_35000_39999":109.250334755961,"income_40000_44999":92.0002818997568,"income_45000_49999":63.2501938060828,"income_50000_59999":184.000563799514,"income_60000_74999":621.001902823358,"income_75000_99999":552.001691398541,"income_100000_124999":327.751004267883,"income_125000_149999":333.501021886618,"income_150000_199999":126.500387612166,"income_200000_or_more":null,"land_area":null} (1 row) dimension | dimension_value diff --git a/src/pg/test/sql/41_observatory_augmentation_test.sql b/src/pg/test/sql/41_observatory_augmentation_test.sql index 4eacbe1..6a1f082 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -1,11 +1,11 @@ \i test/sql/load_fixtures.sql -- SELECT * FROM - cdb_observatory._OBS_GetDemographicSnapshot( + cdb_observatory.OBS_GetDemographicSnapshot( cdb_observatory._TestPoint(), '2009 - 2013', '"us.census.tiger".block_group' - ) As snapshot; + ) AS snapshot; -- -- dimension | dimension_value