From d766f08b031f281404c4eb2cc4ee395929226d9f Mon Sep 17 00:00:00 2001 From: John Krauss Date: Fri, 22 Jul 2016 15:18:43 +0000 Subject: [PATCH 1/2] calculate area normalization of a polygon by square kilometer, not square meter. fixes #158 --- src/pg/sql/41_observatory_augmentation.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index e34e5c8..5d0c0e4 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -439,7 +439,7 @@ BEGIN WHERE ST_Intersects(%L, geom.%I) AND ST_Area(ST_Intersection(%L, geom.%I)) / ST_Area(geom.%I) > 0) SELECT SUM(numer.%I * (SELECT _geom.overlap FROM _geom WHERE _geom.geom_ref = numer.%I)) / - ST_Area(%L::Geography) + (ST_Area(%L::Geography) / 1000000) FROM observatory.%I numer WHERE numer.%I = ANY ((SELECT ARRAY_AGG(geom_ref) FROM _geom)::TEXT[])', geom, geom_colname, geom_colname, From 4c434f5448b9b658b1679d1ce05fa42e8119419a Mon Sep 17 00:00:00 2001 From: John Krauss Date: Fri, 22 Jul 2016 18:56:27 +0000 Subject: [PATCH 2/2] tests doublechecking NULL default handled correctly, and that area normalization for polygon is per square kilometer --- .../41_observatory_augmentation_test.out | 12 +++++++ .../sql/41_observatory_augmentation_test.sql | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index 2136326..b2efbf8 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -42,9 +42,21 @@ t obs_getmeasure_total_pop_point_test t (1 row) +obs_getmeasure_total_pop_point_null_normalization_test +t +(1 row) +obs_getmeasure_total_pop_point_area_test +t +(1 row) obs_getmeasure_total_pop_polygon_test t (1 row) +obs_getmeasure_total_pop_polygon_null_normalization_test +t +(1 row) +obs_getmeasure_total_pop_polygon_area_test +t +(1 row) obs_getmeasure_total_male_point_denominator 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 0a6808e..e024317 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -148,6 +148,22 @@ SELECT abs(OBS_GetMeasure_total_pop_point - 10923.093200390833950) / 10923.09320 'us.census.acs.B01003001' ) As t(OBS_GetMeasure_total_pop_point); +-- Point-based OBS_GetMeasure, default normalization by NULL (area) +-- is result within 0.1% of expected +SELECT abs(OBS_GetMeasure_total_pop_point_null_normalization - 10923.093200390833950) / 10923.093200390833950 < 0.001 As OBS_GetMeasure_total_pop_point_null_normalization_test FROM + cdb_observatory.OBS_GetMeasure( + cdb_observatory._TestPoint(), + 'us.census.acs.B01003001', NULL +) As t(OBS_GetMeasure_total_pop_point_null_normalization); + +-- Point-based OBS_GetMeasure, explicit area normalization area +-- is result within 0.1% of expected +SELECT abs(OBS_GetMeasure_total_pop_point_area - 10923.093200390833950) / 10923.093200390833950 < 0.001 As OBS_GetMeasure_total_pop_point_area_test FROM + cdb_observatory.OBS_GetMeasure( + cdb_observatory._TestPoint(), + 'us.census.acs.B01003001', 'area' +) As t(OBS_GetMeasure_total_pop_point_area); + -- Poly-based OBS_GetMeasure, default normalization (none) -- is result within 0.1% of expected SELECT abs(OBS_GetMeasure_total_pop_polygon - 12327.3133495107) / 12327.3133495107 < 0.001 As OBS_GetMeasure_total_pop_polygon_test FROM @@ -156,6 +172,22 @@ SELECT abs(OBS_GetMeasure_total_pop_polygon - 12327.3133495107) / 12327.31334951 'us.census.acs.B01003001' ) As t(OBS_GetMeasure_total_pop_polygon); +-- Poly-based OBS_GetMeasure, default normalization by NULL (none) +-- is result within 0.1% of expected +SELECT abs(OBS_GetMeasure_total_pop_polygon_null_normalization - 12327.3133495107) / 12327.3133495107 < 0.001 As OBS_GetMeasure_total_pop_polygon_null_normalization_test FROM + cdb_observatory.OBS_GetMeasure( + cdb_observatory._TestArea(), + 'us.census.acs.B01003001', NULL +) As t(OBS_GetMeasure_total_pop_polygon_null_normalization); + +-- Poly-based OBS_GetMeasure, explicit area normalization +-- is result within 0.1% of expected +SELECT abs(OBS_GetMeasure_total_pop_polygon_area - 15787.4325563538) / 15787.4325563538 < 0.001 As OBS_GetMeasure_total_pop_polygon_area_test FROM + cdb_observatory.OBS_GetMeasure( + cdb_observatory._TestArea(), + 'us.census.acs.B01003001', 'area' +) As t(OBS_GetMeasure_total_pop_polygon_area); + -- Point-based OBS_GetMeasure with denominator normalization SELECT (abs(cdb_observatory.OBS_GetMeasure( cdb_observatory._TestPoint(),