From dfbdb3c9ad0d561eb63e949e3e1af043b9aec994 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 10 May 2016 15:09:07 -0400 Subject: [PATCH 1/4] updating geometry types to (geometry/point, 4326) where appropriate --- src/pg/sql/40_observatory_utility.sql | 6 +-- src/pg/sql/41_observatory_augmentation.sql | 50 +++++++++++----------- src/pg/sql/42_observatory_exploration.sql | 2 +- src/pg/sql/44_observatory_geometries.sql | 4 +- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/pg/sql/40_observatory_utility.sql b/src/pg/sql/40_observatory_utility.sql index 03164e5..38ba4a6 100644 --- a/src/pg/sql/40_observatory_utility.sql +++ b/src/pg/sql/40_observatory_utility.sql @@ -4,7 +4,7 @@ -- table where there is multiple sources for a column from multiple -- geometries. CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GeomTable( - geom geometry, + geom geometry(Geometry, 4326), geometry_id text, time_span text DEFAULT NULL ) @@ -84,7 +84,7 @@ $$ LANGUAGE plpgsql; --Test point cause Stuart always seems to make random points in the water CREATE OR REPLACE FUNCTION cdb_observatory._TestPoint() - RETURNS geometry + RETURNS geometry(Point, 4326) AS $$ BEGIN -- new york city @@ -95,7 +95,7 @@ $$ LANGUAGE plpgsql; --Test polygon cause Stuart always seems to make random points in the water -- TODO: remove as it's not used anywhere? CREATE OR REPLACE FUNCTION cdb_observatory._TestArea() - RETURNS geometry + RETURNS geometry(Geometry, 4326) AS $$ BEGIN -- Buffer NYC point by 500 meters diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 655da6a..d6b43a1 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -22,7 +22,7 @@ -- Creates a table of demographic snapshot -CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetDemographicSnapshot(geom geometry, +CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetDemographicSnapshot(geom geometry(Geometry, 4326), time_span text DEFAULT NULL, boundary_id text DEFAULT NULL) RETURNS SETOF JSON @@ -140,7 +140,7 @@ $$ LANGUAGE plpgsql; -- Base augmentation fucntion. CREATE OR REPLACE FUNCTION cdb_observatory._OBS_Get( - geom geometry, + geom geometry(Geometry, 4326), column_ids text[], time_span text, geometry_level text @@ -163,26 +163,24 @@ BEGIN RETURN QUERY SELECT '{}'::text[], '{}'::NUMERIC[]; END IF; - execute' - select array_agg( _obs_getcolumndata) from cdb_observatory._OBS_GetColumnData($1, - $2, - $3);' - INTO data_table_info - using geometry_level, column_ids, time_span; + EXECUTE + 'SELECT array_agg(_obs_getcolumndata) + FROM cdb_observatory._OBS_GetColumnData($1, $2, $3);' + INTO data_table_info + USING geometry_level, column_ids, time_span; IF 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, - geom_table_name, - data_table_info); + geom_table_name, + data_table_info); ELSIF ST_GeometryType(geom) IN ('ST_Polygon', 'ST_MultiPolygon') THEN - -- RAISE EXCEPTION 'polygons not supported for now'; results := cdb_observatory._OBS_GetPolygons(geom, - geom_table_name, - data_table_info); + geom_table_name, + data_table_info); END IF; RETURN QUERY @@ -199,7 +197,7 @@ $$ LANGUAGE plpgsql; -- If the variable of interest is just a rate return it as such, -- otherwise normalize it to the census block area and return that CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetPoints( - geom geometry, + geom geometry(Geometry, 4326), geom_table_name text, data_table_info json[] ) @@ -208,17 +206,17 @@ AS $$ DECLARE result NUMERIC[]; json_result json[]; - query text; + query text; i int; geoid text; - area NUMERIC; + area NUMERIC; BEGIN -- TODO: does 'geoid' need to be generalized to geom_ref?? EXECUTE format('SELECT geoid FROM observatory.%I - WHERE ST_WITHIN($1, the_geom)', + WHERE ST_Within($1, the_geom)', geom_table_name) USING geom INTO geoid; @@ -283,7 +281,7 @@ BEGIN meta->>'name' As name, meta->>'tablename' As tablename, meta->>'aggregate' As aggregate, - meta->>'type' As type, + meta->>'type' As type, meta->>'description' As description FROM (SELECT unnest($1) As values, unnest($2) As meta) b ) t @@ -297,7 +295,7 @@ $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetMeasure( - geom GEOMETRY, + geom geometry(Geometry, 4326), measure_id TEXT, normalize TEXT DEFAULT 'area', -- TODO none/null boundary_id TEXT DEFAULT NULL, @@ -352,7 +350,7 @@ $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetCategory( - geom GEOMETRY, + geom geometry(Geometry, 4326), category_id TEXT, boundary_id TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL @@ -386,7 +384,7 @@ END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetUSCensusMeasure( - geom GEOMETRY, + geom geometry(Geometry, 4326), name TEXT, normalize TEXT DEFAULT 'area', boundary_id TEXT DEFAULT NULL, @@ -419,7 +417,7 @@ END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetUSCensusCategory( - geom GEOMETRY, + geom geometry(Geometry, 4326), name TEXT, boundary_id TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL @@ -453,7 +451,7 @@ END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetPopulation( - geom geometry, + geom geometry(Geometry, 4326), normalize TEXT DEFAULT 'area', boundary_id TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL @@ -478,7 +476,7 @@ $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetPolygons( - geom geometry, + geom geometry(Geometry, 4326), geom_table_name text, data_table_info json[] ) @@ -558,7 +556,7 @@ $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetSegmentSnapshot( - geom geometry, + geom geometry(Geometry, 4326), boundary_id text DEFAULT NULL ) RETURNS JSON @@ -666,7 +664,7 @@ $$ LANGUAGE plpgsql; --Get categorical variables from point CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetCategories( - geom geometry, + geom geometry(Geometry, 4326), dimension_names text[], boundary_id text DEFAULT NULL, time_span text DEFAULT NULL diff --git a/src/pg/sql/42_observatory_exploration.sql b/src/pg/sql/42_observatory_exploration.sql index 3c2a8c8..1b25419 100644 --- a/src/pg/sql/42_observatory_exploration.sql +++ b/src/pg/sql/42_observatory_exploration.sql @@ -82,7 +82,7 @@ $$ LANGUAGE plpgsql; -- TODO add test response CREATE OR REPLACE FUNCTION OBS_GetAvailableBoundaries( - geom geometry, + geom geometry(Geometry, 4326), timespan text DEFAULT null) RETURNS TABLE(boundary_id text, description text, time_span text, tablename text) as $$ DECLARE diff --git a/src/pg/sql/44_observatory_geometries.sql b/src/pg/sql/44_observatory_geometries.sql index cccbcd6..1546fff 100644 --- a/src/pg/sql/44_observatory_geometries.sql +++ b/src/pg/sql/44_observatory_geometries.sql @@ -22,7 +22,7 @@ -- CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetBoundary( - geom geometry(Geometry, 4326), + geom geometry(Point, 4326), boundary_id text, time_span text DEFAULT NULL) RETURNS geometry(Geometry, 4326) @@ -104,7 +104,7 @@ $$ LANGUAGE plpgsql; -- CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetBoundaryId( - geom geometry(Geometry, 4326), + geom geometry(Point, 4326), boundary_id text, time_span text DEFAULT NULL ) From f9394129d93e069b941039c59b311e4f691b9079 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 10 May 2016 15:21:58 -0400 Subject: [PATCH 2/4] add missing schema to function --- src/pg/sql/42_observatory_exploration.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pg/sql/42_observatory_exploration.sql b/src/pg/sql/42_observatory_exploration.sql index 1b25419..338298e 100644 --- a/src/pg/sql/42_observatory_exploration.sql +++ b/src/pg/sql/42_observatory_exploration.sql @@ -81,7 +81,7 @@ $$ LANGUAGE plpgsql; -------------------------------------------------------------------------------- -- TODO add test response -CREATE OR REPLACE FUNCTION OBS_GetAvailableBoundaries( +CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetAvailableBoundaries( geom geometry(Geometry, 4326), timespan text DEFAULT null) RETURNS TABLE(boundary_id text, description text, time_span text, tablename text) as $$ From 7c655dcaa6dbda0fd2a982bb08008df7c298fb62 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 10 May 2016 15:44:58 -0400 Subject: [PATCH 3/4] converting to point geoms --- src/pg/sql/44_observatory_geometries.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pg/sql/44_observatory_geometries.sql b/src/pg/sql/44_observatory_geometries.sql index 1546fff..27459a6 100644 --- a/src/pg/sql/44_observatory_geometries.sql +++ b/src/pg/sql/44_observatory_geometries.sql @@ -344,7 +344,7 @@ $$ LANGUAGE plpgsql; -- -- TODO: move to ST_DWithin instead of buffer + intersects? CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetBoundariesByPointAndRadius( - geom geometry(Geometry, 4326), -- point + geom geometry(Point, 4326), -- point radius numeric, -- radius in meters boundary_id text, time_span text DEFAULT NULL, @@ -485,7 +485,7 @@ $$ LANGUAGE plpgsql; -- CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetPointsByPointAndRadius( - geom geometry(Geometry, 4326), -- point + geom geometry(Point, 4326), -- point radius numeric, -- radius in meters boundary_id text, time_span text DEFAULT NULL, From 3839261d892beb73236d98f60db6f6c27d849ed7 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 10 May 2016 15:54:27 -0400 Subject: [PATCH 4/4] add all default args to function descriptions, fix return types --- doc/methods.md | 182 ++++++++++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 77 deletions(-) diff --git a/doc/methods.md b/doc/methods.md index acc124c..5a18a87 100644 --- a/doc/methods.md +++ b/doc/methods.md @@ -14,7 +14,9 @@ Name |Description --- | --- point_geometry | a WGS84 point geometry (the_geom) measure_name | a human readable string name of a US Census variable. The glossary of measure_names is [available below]('measure_name table'). -normalize | for measures that are are **sums** (e.g. population) the default normalization is 'area' and response comes back as a rate per square kilometer. Other options are 'denominator', which will use the denominator specified in the +normalize | for measures that are are **sums** (e.g., population) the default normalization is 'area' and response comes back as a rate per square kilometer. Other options are 'denominator', which will use the denominator specified in the [Data Catalog](http://cartodb.github.io/bigmetadata/index.html) (optional) +boundary_id | source of geometries to pull measure from (e.g., 'us.census.tiger.census_tract') +time_span | time span of interest (e.g., 2010 - 2014) #### Returns @@ -29,7 +31,8 @@ value | the raw or normalized measure Add a Measure to an empty column based on point locations in your table ```SQL -UPDATE tablename SET local_male_population = OBS_GetUSCensusMeasure(the_geom, 'Male Population') +UPDATE tablename +SET local_male_population = OBS_GetUSCensusMeasure(the_geom, 'Male Population') ``` Get a measure at a single point location @@ -54,6 +57,8 @@ Name |Description point_geometry | a WGS84 polygon geometry (the_geom) measure_name | a human readable string name of a US Census variable. The glossary of measure_names is [available below]('measure_name table'). normalize | for measures that are are **sums** (e.g. population) the default normalization is 'none' and response comes back as a raw value. Other options are 'denominator', which will use the denominator specified in the [Data Catalog](http://cartodb.github.io/bigmetadata/index.html) (optional) +boundary_id | source of geometries to pull measure from (e.g., 'us.census.tiger.census_tract') +time_span | time span of interest (e.g., 2010 - 2014) #### Returns @@ -68,13 +73,18 @@ value | the raw or normalized measure Add a Measure to an empty column based on polygons in your table ```SQL -UPDATE tablename SET local_male_population = OBS_GetUSCensusMeasure(the_geom, 'Male Population') +UPDATE tablename +SET local_male_population = OBS_GetUSCensusMeasure(the_geom, 'Male Population') ``` Get a measure at a single polygon ```SQL -SELECT OBS_GetMeasure(ST_Buffer(CDB_LatLng(40.7, -73.9),0.001), 'Male Population') +SELECT OBS_GetMeasure( + ST_Buffer( + CDB_LatLng(40.7, -73.9)::geography, + 1000)::geometry, + 'Male Population') ```