This commit is contained in:
Andy Eschbacher 2016-04-13 09:31:35 -04:00
commit d01bfe7900
2 changed files with 93 additions and 10 deletions

View File

@ -39,11 +39,11 @@ __Get the Demographic Snapshot at Camp David__
https://example_account.cartodb.com/api/v2/sql?q=SELECT * FROM https://example_account.cartodb.com/api/v2/sql?q=SELECT * FROM
OBS_GetDemographicSnapshot(CDB_LatLng(39.648333, -77.465)) OBS_GetDemographicSnapshot(CDB_LatLng(39.648333, -77.465))
``` ```
__Get the Demographic Snapshot at Morning Side Heights__ __Get the Demographic Snapshot in the Upper West Side__
```text ```text
https://example_account.cartodb.com/api/v2/sql?q=SELECT * FROM https://example_account.cartodb.com/api/v2/sql?q=SELECT * FROM
OBS_GetDemographicSnapshot(CDB_LatLng(40.810201, -73.956601)) OBS_GetDemographicSnapshot(CDB_LatLng(40.80, -73.960))
``` ```
### API Response ### API Response

View File

@ -26,16 +26,22 @@
CREATE OR REPLACE FUNCTION OBS_GetDemographicSnapshot(geom geometry) CREATE OR REPLACE FUNCTION OBS_GetDemographicSnapshot(geom geometry)
RETURNS TABLE( RETURNS TABLE(
total_pop NUMERIC, total_pop NUMERIC,
female_pop NUMERIC,
male_pop NUMERIC, male_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,
other_race_pop NUMERIC,
two_or_more_races_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_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,
@ -51,6 +57,9 @@ RETURNS TABLE(
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,
one_year_more_college 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,
@ -69,7 +78,39 @@ RETURNS TABLE(
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_unit 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)
AS $$ AS $$
DECLARE DECLARE
target_cols text[]; target_cols text[];
@ -77,18 +118,23 @@ DECLARE
vals numeric[]; vals numeric[];
q text; q text;
BEGIN BEGIN
target_cols := Array[ target_cols := Array['total_pop',
'total_pop',
'female_pop',
'male_pop', 'male_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',
'other_race_pop',
'two_or_more_races_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_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',
@ -104,6 +150,9 @@ DECLARE
'in_undergrad_college', 'in_undergrad_college',
'pop_25_years_over', 'pop_25_years_over',
'high_school_diploma', 'high_school_diploma',
'less_one_year_college',
'one_year_more_college',
'associates_degree',
'bachelors_degree', 'bachelors_degree',
'masters_degree', 'masters_degree',
'pop_5_years_over', 'pop_5_years_over',
@ -122,8 +171,40 @@ DECLARE
'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_unit' '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'
];
q = 'WITH a As ( q = 'WITH a As (
SELECT SELECT
@ -164,7 +245,7 @@ BEGIN
ids = OBS_LookupCensusHuman(dimension_names); ids = OBS_LookupCensusHuman(dimension_names);
RETURN QUERY SELECT unnest(names), unnest(vals) RETURN QUERY SELECT names, vals
FROM OBS_Get(geom, ids, time_span, geometry_level); FROM OBS_Get(geom, ids, time_span, geometry_level);
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
@ -250,6 +331,8 @@ BEGIN
USING geom USING geom
INTO geoid; INTO geoid;
RAISE NOTICE 'geoid is % geometry table is % ', geoid, geom_table_name;
EXECUTE EXECUTE
format('SELECT ST_Area(the_geom::geography) / (1000 * 1000) format('SELECT ST_Area(the_geom::geography) / (1000 * 1000)
FROM observatory.%I FROM observatory.%I