diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index cf6f910..5341945 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -179,7 +179,10 @@ BEGIN --raise notice 'Cannot find data table for boundary ID %, column_ids %, and time_span %', geometry_level, column_ids, time_span; END IF; - IF ST_GeometryType(geom) = 'ST_Point' + IF geom IS NULL + THEN + results := NULL; + ELSIF ST_GeometryType(geom) = 'ST_Point' THEN --raise notice 'geom_table_name %, data_table_info %', geom_table_name, data_table_info::json[]; results := cdb_observatory._OBS_GetPoints(geom, @@ -361,6 +364,10 @@ DECLARE sql TEXT; numer_name TEXT; BEGIN + IF geom IS NULL THEN + RETURN NULL; + END IF; + geom := ST_SnapToGrid(geom, 0.000001); EXECUTE @@ -525,6 +532,9 @@ DECLARE measure_val NUMERIC; data_geoid_colname TEXT; BEGIN + IF geom_ref IS NULL THEN + RETURN NULL; + END IF; EXECUTE $query$ @@ -573,6 +583,9 @@ DECLARE category_val TEXT; category_share NUMERIC; BEGIN + IF geom IS NULL THEN + RETURN NULL; + END IF; EXECUTE $query$ diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index 44764f7..621ade2 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -66,12 +66,18 @@ t obs_getmeasure_bad_geometry t (1 row) +obs_getmeasure_null +t +(1 row) obs_getcategory_point t (1 row) obs_getcategory_polygon t (1 row) +obs_getcategory_null +t +(1 row) obs_getpopulation t (1 row) @@ -81,6 +87,9 @@ t obs_getpopulation_polygon_null_test t (1 row) +obs_getpopulation_polygon_null_geom_test +t +(1 row) obs_getuscensusmeasure_point_male_pop t (1 row) @@ -90,12 +99,18 @@ t obs_getuscensusmeasure_null t (1 row) +obs_getuscensusmeasure_null_geom +t +(1 row) obs_getuscensuscategory_point t (1 row) obs_getuscensuscategory_polygon t (1 row) +obs_getuscensuscategory_null +t +(1 row) obs_getmeasurebyid_cartodb_census_tract t (1 row) @@ -108,3 +123,6 @@ t obs_getmeasurebyid_nulls t (1 row) +obs_getmeasurebyid_null_id +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 53d40d9..06f3370 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -203,6 +203,11 @@ SELECT abs(cdb_observatory.OBS_GetMeasure( cdb_observatory._ProblemTestArea(), 'us.census.acs.B01003001') - 96230.2929825897) / 96230.2929825897 < 0.001 As OBS_GetMeasure_bad_geometry; +-- OBS_GetMeasure with NULL Input +SELECT cdb_observatory.OBS_GetMeasure( + NULL, + 'us.census.acs.B01003001') IS NULL As OBS_GetMeasure_null; + -- Point-based OBS_GetCategory SELECT cdb_observatory.OBS_GetCategory( cdb_observatory._TestPoint(), 'us.census.spielman_singleton_segments.X10') = 'Wealthy, urban without Kids' As OBS_GetCategory_point; @@ -211,6 +216,10 @@ SELECT cdb_observatory.OBS_GetCategory( SELECT cdb_observatory.OBS_GetCategory( cdb_observatory._TestArea(), 'us.census.spielman_singleton_segments.X10') = 'Wealthy, urban without Kids' As obs_getcategory_polygon; +-- NULL Input OBS_GetCategory +SELECT cdb_observatory.OBS_GetCategory( + NULL, 'us.census.spielman_singleton_segments.X10') IS NULL As obs_getcategory_null; + -- Point-based OBS_GetPopulation, default normalization (area) SELECT (abs(OBS_GetPopulation - 10923.093200390833950) / 10923.093200390833950) < 0.001 As OBS_GetPopulation FROM cdb_observatory.OBS_GetPopulation( @@ -231,6 +240,13 @@ FROM cdb_observatory._TestArea(), NULL ) As m(obs_getpopulation_polygon_null); +-- Null input OBS_GetPopulation +SELECT obs_getpopulation_polygon_null_geom IS NULL As obs_getpopulation_polygon_null_geom_test +FROM + cdb_observatory.OBS_GetPopulation( + NULL, NULL + ) As m(obs_getpopulation_polygon_null_geom); + -- Point-based OBS_GetUSCensusMeasure, default normalization (area) SELECT (abs(cdb_observatory.obs_getuscensusmeasure( cdb_observatory._testpoint(), 'male population') - 6789.5647735060920500) / 6789.5647735060920500) < 0.001 As obs_getuscensusmeasure_point_male_pop; @@ -244,6 +260,11 @@ SELECT (abs(cdb_observatory.obs_getuscensusmeasure( SELECT (abs(cdb_observatory.obs_getuscensusmeasure( cdb_observatory._testarea(), 'male population', NULL) - 6043.63061042765) / 6043.63061042765) < 0.001 As obs_getuscensusmeasure_null; +-- Poly-based OBS_GetUSCensusMeasure, Null input geom +SELECT cdb_observatory.obs_getuscensusmeasure( + NULL, 'male population', NULL) IS NULL As obs_getuscensusmeasure_null_geom; + + -- Point-based OBS_GetUSCensusCategory SELECT cdb_observatory.OBS_GetUSCensusCategory( cdb_observatory._testpoint(), 'Spielman-Singleton Segments: 10 Clusters') = 'Wealthy, urban without Kids' As OBS_GetUSCensusCategory_point; @@ -252,6 +273,10 @@ SELECT cdb_observatory.OBS_GetUSCensusCategory( SELECT cdb_observatory.OBS_GetUSCensusCategory( cdb_observatory._testarea(), 'Spielman-Singleton Segments: 10 Clusters') = 'Wealthy, urban without Kids' As OBS_GetUSCensusCategory_polygon; +-- Null-input OBS_GetUSCensusCategory +SELECT cdb_observatory.OBS_GetUSCensusCategory( + NULL, 'Spielman-Singleton Segments: 10 Clusters') IS NULL As OBS_GetUSCensusCategory_null; + -- OBS_GetMeasureById tests -- typical query @@ -285,3 +310,11 @@ SELECT cdb_observatory.OBS_GetMeasureById( 'us.census.tiger.block_group', '2010 - 2014' ) IS NULL As OBS_GetMeasureById_nulls; + +-- NULL input id +SELECT cdb_observatory.OBS_GetMeasureById( + NULL, + 'us.census.acs.B01003001', + 'us.census.tiger.block_group', + '2010 - 2014' +) IS NULL As OBS_GetMeasureById_null_id;