diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 6c2cf3a..583d2ec 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -760,18 +760,25 @@ BEGIN ' THEN cdb_observatory.FIRST(' || numer_tablename || '.' || numer_colname || ' / NullIf(' || denom_tablename || '.' || denom_colname || ', 0))' || -- denominated polygon interpolation - -- SUM ((numer / denom) * (% user geom in OBS geom)) + -- SUM (numer * (% OBS geom in user geom)) / SUM (denom * (% OBS geom in user geom)) ' ELSE ' || - --' NULL END ' - ' SUM((' || numer_tablename || '.' || numer_colname || '/' || - ' NullIf(' || denom_tablename || '.' || denom_colname || ', 0)) ' || - ' * CASE WHEN ST_Within(_geoms.geom, ' || geom_tablename || '.' || geom_colname || ') THEN 1 ' || - ' WHEN ST_Within(' || geom_tablename || '.' || geom_colname || ', _geoms.geom) THEN ' || - ' ST_Area(' || geom_tablename || '.' || geom_colname || ') ' || - ' / ST_Area(_geoms.geom)' || + ' SUM(' || numer_tablename || '.' || numer_colname || ' ' || + ' * CASE WHEN ST_Within(_geoms.geom, ' || geom_tablename || '.' || geom_colname || ') ' || + ' THEN ST_Area(_geoms.geom) / ST_Area(' || geom_tablename || '.' || geom_colname || ') ' || + ' WHEN ST_Within(' || geom_tablename || '.' || geom_colname || ', _geoms.geom) ' || + ' THEN 1 ' || ' ELSE (ST_Area(ST_Intersection(_geoms.geom, ' || geom_tablename || '.' || geom_colname || ')) ' || - ' / ST_Area(_geoms.geom))' || - ' END) END ' + ' / ST_Area(' || geom_tablename || '.' || geom_colname || '))' || + ' END) / ' + ' SUM(' || denom_tablename || '.' || denom_colname || ' ' || + ' * CASE WHEN ST_Within(_geoms.geom, ' || geom_tablename || '.' || geom_colname || ') ' || + ' THEN ST_Area(_geoms.geom) / ST_Area(' || geom_tablename || '.' || geom_colname || ') ' || + ' WHEN ST_Within(' || geom_tablename || '.' || geom_colname || ', _geoms.geom) ' || + ' THEN 1 ' || + ' ELSE (ST_Area(ST_Intersection(_geoms.geom, ' || geom_tablename || '.' || geom_colname || ')) ' || + ' / ST_Area(' || geom_tablename || '.' || geom_colname || '))' || + ' END) ' + ' END ' -- areaNormalized WHEN LOWER(normalization) LIKE 'area%' OR (normalization IS NULL AND numer_aggregate ILIKE 'sum') THEN ' CASE ' || diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index 9328b96..54b2a75 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -9,18 +9,6 @@ t obs_get_median_income_at_null_island t (1 row) -obs_getpoints_for_test_point_value|obs_getpoints_for_test_point_name|obs_getpoints_for_test_point_tablename|obs_getpoints_for_test_point_aggregate|obs_getpoints_for_test_point_type|obs_getpoints_for_test_point_description -t|t|t|t|t|t -(1 row) -obs_getpoints_for_null_island -t -(1 row) -obs_getpolygons_for_test_point -t -(1 row) -obs_getpolygons_for_null_island -t -(1 row) test_point_segmentation t (1 row) diff --git a/src/pg/test/expected/42_observatory_exploration_test.out b/src/pg/test/expected/42_observatory_exploration_test.out index 5a312cb..dc5d0e0 100644 --- a/src/pg/test/expected/42_observatory_exploration_test.out +++ b/src/pg/test/expected/42_observatory_exploration_test.out @@ -114,7 +114,7 @@ t _obs_getavailablegeometries_foobarbaz_denom_not_in_usa_bg t (1 row) -_obs_getavailablegeometries_bg_2014 +_obs_getavailablegeometries_bg_2015 t (1 row) _obs_getavailablegeometries_bg_not_1996 diff --git a/src/pg/test/sql/42_observatory_exploration_test.sql b/src/pg/test/sql/42_observatory_exploration_test.sql index 98e782a..3cc84a0 100644 --- a/src/pg/test/sql/42_observatory_exploration_test.sql +++ b/src/pg/test/sql/42_observatory_exploration_test.sql @@ -278,9 +278,9 @@ AS _obs_getavailablegeometries_foobarbaz_denom_not_in_usa_bg; SELECT 'us.census.tiger.block_group' IN (SELECT geom_id FROM cdb_observatory.OBS_GetAvailableGeometries( ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326), - NULL, NULL, NULL, '2014' + NULL, NULL, NULL, '2015' ) WHERE valid_timespan = True) -AS _obs_getavailablegeometries_bg_2014; +AS _obs_getavailablegeometries_bg_2015; SELECT 'us.census.tiger.block_group' NOT IN (SELECT geom_id FROM cdb_observatory.OBS_GetAvailableGeometries( @@ -357,7 +357,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.county', 'us.census.tiger.zcta5']); + 'us.census.tiger.county', 'us.census.tiger.zcta5']) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', @@ -366,7 +367,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 5000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.county', 'us.census.tiger.zcta5']); + 'us.census.tiger.county', 'us.census.tiger.zcta5']) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.census_tract', 'us.census.tiger.block_group', @@ -375,7 +377,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY[ 'us.census.tiger.zcta5', 'us.census.tiger.census_tract', @@ -384,7 +387,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.county', 'us.census.tiger.zcta5', @@ -393,7 +397,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 2500000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = '{ "us.census.tiger.block_group" : 9, "us.census.tiger.census_tract" : 3, "us.census.tiger.zcta5" : 0, "us.census.tiger.county" : 0 }' @@ -401,7 +406,8 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = '{ "us.census.tiger.block_group" : 899, "us.census.tiger.census_tract" : 328, "us.census.tiger.zcta5" : 45, "us.census.tiger.county" : 1 }' @@ -409,7 +415,8 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 5000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = '{ "us.census.tiger.block_group" : 12112, "us.census.tiger.census_tract" : 3792, "us.census.tiger.zcta5" : 550, "us.census.tiger.county" : 14 }' @@ -417,32 +424,36 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = - '{ "us.census.tiger.block_group" : 48420, "us.census.tiger.census_tract" : 15774, "us.census.tiger.zcta5" : 6533, "us.census.tiger.county" : 304 }' + '{ "us.census.tiger.block_group" : 48420, "us.census.tiger.census_tract" : 15775, "us.census.tiger.zcta5" : 6533, "us.census.tiger.county" : 304 }' AS _obs_geometryscores_numgeoms_500km_buffer FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text = - '{ "us.census.tiger.block_group" : 165475, "us.census.tiger.census_tract" : 55128, "us.census.tiger.zcta5" : 26499, "us.census.tiger.county" : 2622 }' + '{ "us.census.tiger.block_group" : 169119, "us.census.tiger.census_tract" : 56380, "us.census.tiger.zcta5" : 26498, "us.census.tiger.county" : 2704 }' AS _obs_geometryscores_numgeoms_2500km_buffer FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 2500000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county']); + 'us.census.tiger.zcta5', 'us.census.tiger.county']) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = - ARRAY['us.census.tiger.county', 'us.census.tiger.zcta5', - 'us.census.tiger.census_tract', 'us.census.tiger.block_group'] + ARRAY['us.census.tiger.county', 'us.census.tiger.census_tract', + 'us.census.tiger.zcta5', 'us.census.tiger.block_group'] AS _obs_geometryscores_500km_buffer_50_geoms FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county'], 50); + 'us.census.tiger.zcta5', 'us.census.tiger.county'], 50) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.zcta5', 'us.census.tiger.census_tract', @@ -451,7 +462,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county'], 500); + 'us.census.tiger.zcta5', 'us.census.tiger.county'], 500) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.census_tract', 'us.census.tiger.block_group', @@ -460,7 +472,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county'], 2500); + 'us.census.tiger.zcta5', 'us.census.tiger.county'], 2500) + WHERE table_id LIKE '%2015%'; SELECT ARRAY_AGG(column_id ORDER BY score DESC) = ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', @@ -469,7 +482,8 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) = FROM cdb_observatory._OBS_GetGeometryScores( ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326), ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract', - 'us.census.tiger.zcta5', 'us.census.tiger.county'], 25000); + 'us.census.tiger.zcta5', 'us.census.tiger.county'], 25000) + WHERE table_id LIKE '%2015%'; -- -- OBS_LegacyBuilderMetadata tests