diff --git a/src/pg/sql/40_observatory_utility.sql b/src/pg/sql/40_observatory_utility.sql index 848424a..9caf91a 100644 --- a/src/pg/sql/40_observatory_utility.sql +++ b/src/pg/sql/40_observatory_utility.sql @@ -206,16 +206,16 @@ $$ LANGUAGE plpgsql; -- Create a function that always returns the first non-NULL item -CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) +CREATE OR REPLACE FUNCTION cdb_observatory.first_agg ( anyelement, anyelement ) RETURNS anyelement LANGUAGE SQL IMMUTABLE STRICT AS $$ SELECT $1; $$; -DROP AGGREGATE IF EXISTS public.FIRST (anyelement); +DROP AGGREGATE IF EXISTS cdb_observatory.FIRST (anyelement); -- And then wrap an aggregate around it -CREATE AGGREGATE public.FIRST ( - sfunc = public.first_agg, +CREATE AGGREGATE cdb_observatory.FIRST ( + sfunc = cdb_observatory.first_agg, basetype = anyelement, stype = anyelement ); diff --git a/src/pg/sql/42_observatory_exploration.sql b/src/pg/sql/42_observatory_exploration.sql index a0da84e..ec35866 100644 --- a/src/pg/sql/42_observatory_exploration.sql +++ b/src/pg/sql/42_observatory_exploration.sql @@ -121,9 +121,9 @@ BEGIN END $$ LANGUAGE plpgsql; -DROP FUNCTION cdb_observatory.OBS_GetAvailableNumerators( - bounds GEOMETRY, filter_tags TEXT[], denom_id TEXT, geom_id TEXT, timespan TEXT -); +-- Functions the interface works from to identify available numerators, +-- denominators, geometries, and timespans + CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetAvailableNumerators( bounds GEOMETRY, filter_tags TEXT[] DEFAULT NULL, @@ -164,7 +164,7 @@ BEGIN $1 = ANY(denoms) valid_denom, $2 = ANY(geoms) valid_geom, $3 = ANY(timespans) valid_timespan - FROM obs_meta_numer + FROM observatory.obs_meta_numer WHERE st_intersects(the_geom, $5) AND (numer_tags ?& $4 OR CARDINALITY($4) = 0) $string$ @@ -173,21 +173,6 @@ BEGIN END $$ LANGUAGE plpgsql; -SELECT * FROM cdb_observatory.OBS_GetAvailableNumerators( - ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - NULL, 'us.census.acs.B01003001', 'us.census.tiger.census_tract', '' -) where valid_denom IS true and valid_geom IS true; - -SELECT * FROM cdb_observatory.OBS_GetAvailableNumerators( - ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - ARRAY['unit/tags.money'], '', '', '' -); - - --- DENOMS -DROP FUNCTION cdb_observatory.OBS_GetAvailableDenominators( - bounds GEOMETRY, filter_tags TEXT[], numer_id TEXT, geom_id TEXT, timespan TEXT -); CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetAvailableDenominators( bounds GEOMETRY, filter_tags TEXT[] DEFAULT NULL, @@ -228,7 +213,7 @@ BEGIN $1 = ANY(numers) valid_numer, $2 = ANY(geoms) valid_geom, $3 = ANY(timespans) valid_timespan - FROM obs_meta_denom + FROM observatory.obs_meta_denom WHERE st_intersects(the_geom, $5) AND (denom_tags ?& $4 OR CARDINALITY($4) = 0) $string$ @@ -237,15 +222,6 @@ BEGIN END $$ LANGUAGE plpgsql; -SELECT * FROM cdb_observatory.OBS_GetAvailableDenominators( - ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - NULL, 'us.census.acs.B03002006', 'us.census.tiger.census_tract', '' -) where valid_numer IS true and valid_geom IS true; - ---- GEOMS -DROP FUNCTION cdb_observatory.OBS_GetAvailableGeometries( - bounds GEOMETRY, filter_tags TEXT[], numer_id TEXT, denom_id TEXT, timespan TEXT -); CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetAvailableGeometries( bounds GEOMETRY, filter_tags TEXT[] DEFAULT NULL, @@ -280,7 +256,7 @@ BEGIN $1 = ANY(numers) valid_numer, $2 = ANY(denoms) valid_denom, $3 = ANY(timespans) valid_timespan - FROM obs_meta_geom + FROM observatory.obs_meta_geom WHERE st_intersects(the_geom, $5) AND (geom_tags ?& $4 OR CARDINALITY($4) = 0) $string$ @@ -289,15 +265,6 @@ BEGIN END $$ LANGUAGE plpgsql; -SELECT * FROM cdb_observatory.OBS_GetAvailableGeometries( - ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - NULL, 'us.census.acs.B03002006', 'us.census.acs.B01003001', '' -) where valid_numer IS true and valid_denom IS true; - --- TIMESPANS -DROP FUNCTION cdb_observatory.OBS_GetAvailableTimespans( - bounds GEOMETRY, filter_tags TEXT[], numer_id TEXT, denom_id TEXT, geom_id TEXT -); CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetAvailableTimespans( bounds GEOMETRY, filter_tags TEXT[] DEFAULT NULL, @@ -334,7 +301,7 @@ BEGIN $2 = ANY(denoms) valid_denom, $3 = ANY(geoms) valid_geom_id, NULL::JSONB stats - FROM obs_meta_timespan + FROM observatory.obs_meta_timespan WHERE st_intersects(the_geom, $5) AND (timespan_tags ?& $4 OR CARDINALITY($4) = 0) $string$ @@ -342,83 +309,3 @@ BEGIN RETURN; END $$ LANGUAGE plpgsql; - -SELECT * FROM cdb_observatory.OBS_GetAvailableTimespans( - ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - NULL, 'us.census.acs.B03002006', 'us.census.acs.B01003001', 'us.census.tiger.census_tract' -) where valid_numer IS true and valid_denom IS true AND valid_geom IS true; - - --- notes: add the_geom index to obs_meta --- change the_geom type to geometry(geometery, 4326) - - -DROP TABLE IF EXISTS obs_meta_numer; -CREATE TABLE obs_meta_numer AS -SELECT numer_id::TEXT, - FIRST(numer_name)::TEXT numer_name, - FIRST(numer_description)::TEXT numer_description, - FIRST(numer_tags)::JSONB numer_tags, - FIRST(numer_weight)::NUMERIC numer_weight, - FIRST(numer_extra)::JSONB numer_extra, -- cannot include ct_extra because it depends on table - FIRST(numer_type)::TEXT numer_type, - ARRAY_AGG(DISTINCT denom_id)::TEXT[] denoms, - ARRAY_AGG(DISTINCT geom_id)::TEXT[] geoms, - ARRAY_AGG(DISTINCT numer_timespan)::TEXT[] timespans, - ST_Union(DISTINCT ST_SetSRID(the_geom, 4326)) the_geom -FROM observatory.obs_meta -GROUP BY numer_id; -CREATE INDEX ON obs_meta_numer USING GIST (the_geom); - -DROP TABLE IF EXISTS obs_meta_denom; -CREATE TABLE obs_meta_denom AS -SELECT denom_id::TEXT, - FIRST(denom_name)::TEXT denom_name, - FIRST(denom_description)::TEXT denom_description, - NULL::JSONB denom_tags, - FIRST(denom_weight)::NUMERIC denom_weight, - 'denominator'::TEXT reltype, - FIRST(denom_extra)::JSONB denom_extra, - FIRST(denom_type)::TEXT denom_type, - ARRAY_AGG(DISTINCT numer_id)::TEXT[] numers, - ARRAY_AGG(DISTINCT geom_id)::TEXT[] geoms, - ARRAY_AGG(DISTINCT numer_timespan)::TEXT[] timespans, - ST_Union(DISTINCT ST_SetSRID(the_geom, 4326)) the_geom -FROM observatory.obs_meta -GROUP BY denom_id; -CREATE INDEX ON obs_meta_denom USING GIST (the_geom); - -DROP TABLE IF EXISTS obs_meta_geom; -CREATE TABLE obs_meta_geom AS -SELECT geom_id::TEXT, - FIRST(geom_name)::TEXT geom_name, - FIRST(geom_description)::TEXT geom_description, - NULL::JSONB geom_tags, - FIRST(geom_weight)::NUMERIC geom_weight, - FIRST(geom_extra)::JSONB geom_extra, - FIRST(geom_type)::TEXT geom_type, - ST_SetSRID(FIRST(the_geom), 4326)::GEOMETRY(GEOMETRY, 4326) the_geom, - NULL::raster summary_geom, - ARRAY_AGG(DISTINCT numer_id)::TEXT[] numers, - ARRAY_AGG(DISTINCT denom_id)::TEXT[] denoms, - ARRAY_AGG(DISTINCT numer_timespan)::TEXT[] timespans -FROM observatory.obs_meta -GROUP BY geom_id; -CREATE INDEX ON obs_meta_geom USING GIST (the_geom); - -DROP TABLE IF EXISTS obs_meta_timespan; -CREATE TABLE obs_meta_timespan AS -SELECT numer_timespan::TEXT timespan_id, - numer_timespan::TEXT timespan_name, - NULL::TEXT timespan_description, - NULL::JSONB timespan_tags, - NULL::NUMERIC timespan_weight, - NULL::JSONB timespan_extra, - NULL::TEXT timespan_type, - ARRAY_AGG(DISTINCT numer_id)::TEXT[] numers, - ARRAY_AGG(DISTINCT denom_id)::TEXT[] denoms, - ARRAY_AGG(DISTINCT geom_id)::TEXT[] geoms, - ST_Union(DISTINCT ST_SetSRID(the_geom, 4326)) the_geom -FROM observatory.obs_meta -GROUP BY numer_timespan; -CREATE INDEX ON obs_meta_geom USING GIST (the_geom); diff --git a/src/pg/test/sql/42_observatory_exploration_test.sql b/src/pg/test/sql/42_observatory_exploration_test.sql index c79b2f9..ee59de4 100644 --- a/src/pg/test/sql/42_observatory_exploration_test.sql +++ b/src/pg/test/sql/42_observatory_exploration_test.sql @@ -33,3 +33,52 @@ SELECT COUNT(*) > 0 AS _OBS_GetAvailableBoundariesExist FROM cdb_observatory.OBS_GetAvailableBoundaries( cdb_observatory._TestPoint() ) AS t(boundary_id, description, time_span, tablename); + +-- +-- OBS_GetAvailableNumerators tests +-- + +/* +SELECT * FROM cdb_observatory.OBS_GetAvailableNumerators( + ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), + NULL, 'us.census.acs.B01003001', 'us.census.tiger.census_tract', '' +) where valid_denom IS true and valid_geom IS true; + +SELECT * FROM cdb_observatory.OBS_GetAvailableNumerators( + ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), + ARRAY['unit/tags.money'], '', '', '' +); +*/ + +-- +-- OBS_GetAvailableDenominators tests +-- + +/* +SELECT * FROM cdb_observatory.OBS_GetAvailableDenominators( + ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), + NULL, 'us.census.acs.B03002006', 'us.census.tiger.census_tract', '' +) where valid_numer IS true and valid_geom IS true; +*/ + +-- +-- OBS_GetAvailableGeometries tests +-- + +/* +SELECT * FROM cdb_observatory.OBS_GetAvailableGeometries( + ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), + NULL, 'us.census.acs.B03002006', 'us.census.acs.B01003001', '' +) where valid_numer IS true and valid_denom IS true; +*/ + +-- +-- OBS_GetAvailableTimespans tests +-- + +/* +SELECT * FROM cdb_observatory.OBS_GetAvailableTimespans( + ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), + NULL, 'us.census.acs.B03002006', 'us.census.acs.B01003001', 'us.census.tiger.census_tract' +) where valid_numer IS true and valid_denom IS true AND valid_geom IS true; +*/