diff --git a/src/pg/sql/40_observatory_utility.sql b/src/pg/sql/40_observatory_utility.sql index c3fc1f9..bf01fe0 100644 --- a/src/pg/sql/40_observatory_utility.sql +++ b/src/pg/sql/40_observatory_utility.sql @@ -48,21 +48,43 @@ CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetColumnData( RETURNS SETOF JSON AS $$ BEGIN + + -- figure out highest-weight geometry_id/timespan pair for the first data column + -- TODO this should be done for each data column separately + IF geometry_id IS NULL OR timespan IS NULL THEN + EXECUTE ' + SELECT data_t.timespan timespan, geom_c.id boundary_id + FROM observatory.obs_table data_t, + observatory.obs_column_table data_ct, + observatory.obs_column data_c, + observatory.obs_column_table geoid_ct, + observatory.obs_column_to_column c2c, + observatory.obs_column geom_c + WHERE data_c.id = $2 + AND data_ct.column_id = data_c.id + AND data_ct.table_id = data_t.id + AND geoid_ct.table_id = data_t.id + AND geoid_ct.column_id = c2c.source_id + AND c2c.reltype = ''geom_ref'' + AND geom_c.id = c2c.target_id + AND CASE WHEN $3 IS NULL THEN True ELSE $3 = timespan END + AND CASE WHEN $1 IS NULL THEN True ELSE $1 = geom_c.id END + ORDER BY geom_c.weight DESC, + data_t.timespan DESC + LIMIT 1; + ' INTO timespan, geometry_id + USING geometry_id, column_ids[1], timespan; + END IF; + RETURN QUERY EXECUTE ' WITH geomref AS ( SELECT t.table_id id FROM observatory.OBS_column_to_column c2c, - observatory.OBS_column c, - observatory.OBS_column_table geom_ct, - observatory.OBS_column_table data_ct, - observatory.OBS_table t + observatory.OBS_column_table ct WHERE c2c.reltype = ''geom_ref'' - AND c.column_id = c2c.target_id AND c2c.target_id = $1 AND c2c.source_id = ct.column_id - ORDER BY weight DESC - LIMIT 1 ), column_ids as ( select row_number() over () as no, a.column_id as column_id from (select unnest($2) as column_id) a diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index e1ea9d7..0094351 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -37,6 +37,9 @@ t obs_getmeasure_zhvi_point_test t (1 row) +obs_getmeasure_zhvi_point_default_latest_test +t +(1 row) obs_getmeasure_total_pop_point_test t (1 row) diff --git a/src/pg/test/sql/41_observatory_augmentation_test.sql b/src/pg/test/sql/41_observatory_augmentation_test.sql index a874bbd..0899926 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -139,6 +139,12 @@ SELECT abs(OBS_GetMeasure_zhvi_point - 583600) / 583600 < 0.001 AS OBS_GetMeasur 'us.zillow.AllHomes_Zhvi', 'area', 'us.census.tiger.zcta5', '2014-01' ) As t(OBS_GetMeasure_zhvi_point); +-- Point-based OBS_GetMeasure with zillow default to latest +SELECT abs(OBS_GetMeasure_zhvi_point_default_latest - 583600) / 583600 < 0.001 AS OBS_GetMeasure_zhvi_point_default_latest_test FROM cdb_observatory.OBS_GetMeasure( + ST_SetSRID(ST_Point( -73.94602417945862, 40.6768220087458), 4326), + 'us.zillow.AllHomes_Zhvi' +) As t(OBS_GetMeasure_zhvi_point_default_latest); + -- Point-based OBS_GetMeasure, default normalization (area) -- is result within 0.1% of expected SELECT abs(OBS_GetMeasure_total_pop_point - 10923.093200390833950) / 10923.093200390833950 < 0.001 As OBS_GetMeasure_total_pop_point_test FROM