allow _obs_geomtable to take a timespan, and default to most recent

This commit is contained in:
John Krauss 2016-05-05 14:45:56 -04:00 committed by Andy Eschbacher
parent 9f9c27e8bc
commit 1ffa6b48e9
2 changed files with 21 additions and 6 deletions

View File

@ -5,7 +5,8 @@
-- geometries.
CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GeomTable(
geom geometry,
geometry_id text
geometry_id text,
time_span text DEFAULT NULL
)
RETURNS TEXT
AS $$
@ -22,10 +23,12 @@ BEGIN
WHERE type ILIKE ''geometry''
AND coltable.column_id = col.id
AND coltable.table_id = tab.id
AND col.id = $1
AND col.id = $1,
AND CASE WHEN $2 IS NOT NULL THEN timespan = $2 ELSE TRUE END
ORDER BY timespan DESC LIMIT 1
)
'
USING geometry_id, geom
USING geometry_id, geom, time_span
INTO result;
return result;

View File

@ -8,7 +8,8 @@
SELECT
cdb_observatory._OBS_GeomTable(
ST_SetSRID(ST_Point(-74.0059, 40.7128), 4326),
'us.census.tiger.census_tract'
'us.census.tiger.census_tract',
'2013'
);
-- get null for unknown geometry_id
@ -32,6 +33,17 @@ SELECT
-- -----------|-----------------|-----------
-- geoid | obs_{hex table} | null
-- total_pop | obs_{hex table} | sum
--WITH result as (
--SELECT
-- array_agg(a) expected from cdb_observatory._OBS_GetColumnData(
-- 'us.census.tiger.census_tract',
-- Array['us.census.tiger.census_tract_geoid', 'us.census.acs.B01001001'],
-- '2009 - 2013') a
--)
--select (expected)[1]::text = '{"colname":"geoid","tablename":"obs_d34555209878e8c4b37cf0b2b3d072ff129ec470","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_1,
-- (expected)[2]::text = '{"colname":"geoid","tablename":"obs_ab038198aaab3f3cb055758638ee4de28ad70146","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_2
--from result;
WITH result as (
SELECT
array_agg(a) expected from cdb_observatory._OBS_GetColumnData(
@ -39,8 +51,8 @@ SELECT
Array['us.census.tiger.census_tract_geoid', 'us.census.acs.B01001001'],
'2009 - 2013') a
)
select (expected)[1]::text = '{"colname":"geoid","tablename":"obs_d34555209878e8c4b37cf0b2b3d072ff129ec470","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_1,
(expected)[2]::text = '{"colname":"geoid","tablename":"obs_ab038198aaab3f3cb055758638ee4de28ad70146","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_2
select (expected)[1]::text as test_get_obs_column_with_geoid_and_census_1,
(expected)[2]::text as test_get_obs_column_with_geoid_and_census_2
from result;