diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 0579bb5..740704f 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -11,7 +11,7 @@ ----DECLARE ----target_cols text[]; ----names text[]; -----vals numeric[];- +----vals NUMERIC[];- ----q text; ----BEGIN ----target_cols := Array[<%=get_dimensions_for_tag(tag_name)%>], @@ -115,9 +115,9 @@ AS $$ DECLARE target_cols text[]; names text[]; - vals numeric[]; + vals NUMERIC[]; q text; - BEGIN +BEGIN target_cols := Array['total_pop', 'male_pop', 'female_pop', @@ -203,17 +203,16 @@ DECLARE 'income_75000_99999', 'income_100000_124999', 'income_125000_149999', - 'income_150000_199999' -]; + 'income_150000_199999']; - q = 'WITH a As ( + q := 'WITH a As ( SELECT dimension As names, dimension_value As vals - FROM OBS_GetCensus($1,$2) + FROM OBS_GetCensus($1, $2) )' || OBS_BuildSnapshotQuery(target_cols) || - ' FROM a'; + ' FROM a'; RETURN QUERY EXECUTE @@ -237,13 +236,13 @@ CREATE OR REPLACE FUNCTION OBS_GetCensus( time_span text DEFAULT '2009 - 2013', geometry_level text DEFAULT '"us.census.tiger".census_tract' ) -RETURNS TABLE(dimension text[], dimension_value numeric[]) +RETURNS TABLE(dimension text[], dimension_value NUMERIC[]) AS $$ DECLARE ids text[]; BEGIN - ids = OBS_LookupCensusHuman(dimension_names); + ids := OBS_LookupCensusHuman(dimension_names); RETURN QUERY SELECT names, vals FROM OBS_Get(geom, ids, time_span, geometry_level); @@ -262,7 +261,7 @@ CREATE OR REPLACE FUNCTION OBS_Get( RETURNS TABLE(names text[], vals NUMERIC[]) AS $$ DECLARE - results numeric[]; + results NUMERIC[]; geom_table_name text; names text[]; query text; @@ -274,7 +273,7 @@ BEGIN IF geom_table_name IS NULL THEN RAISE NOTICE 'Point % is outside of the data region', geom; - RETURN QUERY SELECT '{}'::text[], '{}'::numeric[]; + RETURN QUERY SELECT '{}'::text[], '{}'::NUMERIC[]; END IF; data_table_info := OBS_GetColumnData(geometry_level, @@ -302,7 +301,7 @@ BEGIN results := Array[]; END IF; - RETURN QUERY (SELECT names, results); + RETURN QUERY SELECT names, results; END; $$ LANGUAGE plpgsql; @@ -314,15 +313,18 @@ CREATE OR REPLACE FUNCTION OBS_GetPoints( geom_table_name text, data_table_info OBS_ColumnData[] -) RETURNS NUMERIC[] AS $$ +) +RETURNS NUMERIC[] +AS $$ DECLARE result NUMERIC[]; query text; i int; geoid text; - area numeric; + area NUMERIC; BEGIN + -- TODO: does 'geoid' need to be generalized to geom_ref?? EXECUTE format('SELECT geoid FROM observatory.%I @@ -331,7 +333,7 @@ BEGIN USING geom INTO geoid; - RAISE NOTICE 'geoid is % geometry table is % ', geoid, geom_table_name; + RAISE NOTICE 'geoid is %, geometry table is % ', geoid, geom_table_name; EXECUTE format('SELECT ST_Area(the_geom::geography) / (1000 * 1000) @@ -354,13 +356,13 @@ BEGIN area); END IF; - IF i < array_upper(data_table_info, 1) + IF i < array_upper(data_table_info, 1) THEN - query = query || ','; + query := query || ','; END IF; END LOOP; - query = query || format(' ] + query := query || format(' ] FROM observatory.%I WHERE %I.geoid = %L ', @@ -375,21 +377,22 @@ BEGIN USING geom; RETURN result; - END; $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION OBS_GetPolygons ( +CREATE OR REPLACE FUNCTION OBS_GetPolygons( geom geometry, geom_table_name text, data_table_info OBS_ColumnData[] -) returns numeric[] AS $$ +) +RETURNS NUMERIC[] +AS $$ DECLARE - result numeric[]; + result NUMERIC[]; q_select text; q_sum text; q text; - i numeric; + i NUMERIC; BEGIN q_select := 'select geoid, '; @@ -397,19 +400,19 @@ BEGIN FOR i IN 1..array_upper(data_table_info, 1) LOOP - q_select = q_select || format( '%I ', ((data_table_info)[i]).colname); + q_select := q_select || format( '%I ', ((data_table_info)[i]).colname); IF ((data_table_info)[i]).aggregate ='sum' THEN - q_sum = q_sum || format('sum(overlap_fraction * COALESCE(%I, 0)) ',((data_table_info)[i]).colname,((data_table_info)[i]).colname); + q_sum := q_sum || format('sum(overlap_fraction * COALESCE(%I, 0)) ',((data_table_info)[i]).colname,((data_table_info)[i]).colname); ELSE - q_sum = q_sum || ' null '; + q_sum := q_sum || ' null '; END IF; IF i < array_upper(data_table_info,1) THEN - q_select = q_select || format(','); - q_sum = q_sum || format(','); + q_select := q_select || format(','); + q_sum := q_sum || format(','); END IF; END LOOP; @@ -425,9 +428,9 @@ BEGIN values As ( ', geom_table_name); - q = q || q_select || format('FROM observatory.%I ', ((data_table_info)[1].tablename)); + q := q || q_select || format('FROM observatory.%I ', ((data_table_info)[1].tablename)); - q = q || ' ) ' || q_sum || ' ] FROM _overlaps, values + q := q || ' ) ' || q_sum || ' ] FROM _overlaps, values WHERE values.geoid = _overlaps.geoid'; EXECUTE @@ -445,50 +448,50 @@ CREATE OR REPLACE FUNCTION OBS_GetSegmentSnapshot( ) RETURNS TABLE( segment_name TEXT, - total_pop_quantile Numeric, - male_pop_quantile Numeric, - female_pop_quantile Numeric, - median_age_quantile Numeric, - white_pop_quantile Numeric, - black_pop_quantile Numeric, - asian_pop_quantile Numeric, - hispanic_pop_quantile Numeric, - not_us_citizen_pop_quantile Numeric, - workers_16_and_over_quantile Numeric, - commuters_by_car_truck_van_quantile Numeric, - commuters_by_public_transportation_quantile Numeric, - commuters_by_bus_quantile Numeric, - commuters_by_subway_or_elevated_quantile Numeric, - walked_to_work_quantile Numeric, - worked_at_home_quantile Numeric, - children_quantile Numeric, - households_quantile Numeric, - population_3_years_over_quantile Numeric, - in_school_quantile Numeric, - in_grades_1_to_4_quantile Numeric, - in_grades_5_to_8_quantile Numeric, - in_grades_9_to_12_quantile Numeric, - in_undergrad_college_quantile Numeric, - pop_25_years_over_quantile Numeric, - high_school_diploma_quantile Numeric, - bachelors_degree_quantile Numeric, - masters_degree_quantile Numeric, - pop_5_years_over_quantile Numeric, - speak_only_english_at_home_quantile Numeric, - speak_spanish_at_home_quantile Numeric, - pop_determined_poverty_status_quantile Numeric, - poverty_quantile Numeric, - median_income_quantile Numeric, - gini_index_quantile Numeric, - income_per_capita_quantile Numeric, - housing_units_quantile Numeric, - vacant_housing_units_quantile Numeric, - vacant_housing_units_for_rent_quantile Numeric, - vacant_housing_units_for_sale_quantile Numeric, - median_rent_quantile Numeric, - percent_income_spent_on_rent_quantile Numeric, - owner_occupied_housing_units_quantile Numeric, - million_dollar_housing_units_quantile Numeric + total_pop_quantile NUMERIC, + male_pop_quantile NUMERIC, + female_pop_quantile NUMERIC, + median_age_quantile NUMERIC, + white_pop_quantile NUMERIC, + black_pop_quantile NUMERIC, + asian_pop_quantile NUMERIC, + hispanic_pop_quantile NUMERIC, + not_us_citizen_pop_quantile NUMERIC, + workers_16_and_over_quantile NUMERIC, + commuters_by_car_truck_van_quantile NUMERIC, + commuters_by_public_transportation_quantile NUMERIC, + commuters_by_bus_quantile NUMERIC, + commuters_by_subway_or_elevated_quantile NUMERIC, + walked_to_work_quantile NUMERIC, + worked_at_home_quantile NUMERIC, + children_quantile NUMERIC, + households_quantile NUMERIC, + population_3_years_over_quantile NUMERIC, + in_school_quantile NUMERIC, + in_grades_1_to_4_quantile NUMERIC, + in_grades_5_to_8_quantile NUMERIC, + in_grades_9_to_12_quantile NUMERIC, + in_undergrad_college_quantile NUMERIC, + pop_25_years_over_quantile NUMERIC, + high_school_diploma_quantile NUMERIC, + bachelors_degree_quantile NUMERIC, + masters_degree_quantile NUMERIC, + pop_5_years_over_quantile NUMERIC, + speak_only_english_at_home_quantile NUMERIC, + speak_spanish_at_home_quantile NUMERIC, + pop_determined_poverty_status_quantile NUMERIC, + poverty_quantile NUMERIC, + median_income_quantile NUMERIC, + gini_index_quantile NUMERIC, + income_per_capita_quantile NUMERIC, + housing_units_quantile NUMERIC, + vacant_housing_units_quantile NUMERIC, + vacant_housing_units_for_rent_quantile NUMERIC, + vacant_housing_units_for_sale_quantile NUMERIC, + median_rent_quantile NUMERIC, + percent_income_spent_on_rent_quantile NUMERIC, + owner_occupied_housing_units_quantile NUMERIC, + million_dollar_housing_units_quantile NUMERIC ) AS $$ DECLARE @@ -546,38 +549,39 @@ target_cols := Array[ EXECUTE $query$ - select (categories)[1] - from OBS_GetCategories($1,Array['"us.census.spielman_singleton_segments".X10']) - limit 1 + SELECT (categories)[1] + FROM OBS_GetCategories($1, + Array['"us.census.spielman_singleton_segments".X10']) + LIMIT 1 $query$ INTO segment_name - USING geom; - q = + q := format( $query$ WITH a As ( SELECT names As names, vals As vals - FROM OBS_Get($1, - $2, - '2009 - 2013', - $3) + FROM OBS_Get($1, + $2, + '2009 - 2013', + $3) - ), percentiles as ( + ), percentiles As ( %s FROM a) - select $4, percentiles.* - from percentiles - $query$, OBS_BuildSnapshotQuery(target_cols) ); + SELECT $4, percentiles.* + FROM percentiles + $query$, OBS_BuildSnapshotQuery(target_cols)); RETURN QUERY EXECUTE q - USING geom, target_cols, geometry_level, segment_name ; + USING geom, target_cols, geometry_level, segment_name; -END $$ LANGUAGE plpgsql ; +END; +$$ LANGUAGE plpgsql; --Get categorical variables from point @@ -598,9 +602,10 @@ DECLARE BEGIN geom_table_name := OBS_GeomTable(geom, geometry_level); + IF geom_table_name IS NULL THEN - RAISE NOTICE 'Point % is outside of the data region', geom; + RAISE NOTICE 'Point % is outside of the data region', ST_AsText(geom); RETURNS QUERY SELECT '{}'::text[], '{}'::text[]; END IF; @@ -627,11 +632,11 @@ BEGIN query = query || format('%I ', lower(((data_table_info)[i]).colname)); IF i < array_upper(data_table_info, 1) THEN - query = query || ','; + query := query || ','; END IF; END LOOP; - query = query || format(' ] + query := query || format(' ] FROM observatory.%I WHERE %I.geoid = %L ', @@ -646,8 +651,8 @@ BEGIN USING geom; RETURN QUERY - select names,results + SELECT names,results RETURN; -END +END; $$ LANGUAGE plpgsql;