Add optional schema name to cdb_overlap_sum

This commit is contained in:
Javier Goizueta 2016-02-18 16:11:29 +01:00
parent 0742e3f72a
commit 19e61c7041

View File

@ -1,18 +1,36 @@
-- Function by Stuart Lynn for a simple interpolation of a value -- Function by Stuart Lynn for a simple interpolation of a value
-- from a polygon table over an arbitrary polygon -- from a polygon table over an arbitrary polygon
-- (weighted by the area proportion overlapped) -- (weighted by the area proportion overlapped)
-- Aereal weighting is a very simple form of aereal interpolation.
--
-- Parameters:
-- * geom a Polygon geometry which defines the area where a value will be
-- estimated as the area-weighted sum of a given table/column
-- * target_table_name table name of the table that provides the values
-- * target_column column name of the column that provides the values
-- * schema_name optional parameter to defina the schema the target table
-- belongs to, which is necessary if its not in the search_path.
-- Note that target_table_name should never include the schema in it.
-- Return value:
-- Aereal-weighted interpolation of the column values over the geometry
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text) FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text, schema_name text DEFAULT NULL)
RETURNS numeric AS RETURNS numeric AS
$$ $$
DECLARE DECLARE
result numeric; result numeric;
qualified_name text;
BEGIN BEGIN
IF schema_name IS NULL THEN
qualified_name := Format('%I', target_table_name);
ELSE
qualified_name := Format('%I.%s', schema_name, target_table_name);
END IF;
EXECUTE Format(' EXECUTE Format('
SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom)) SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom))
FROM %I AS a FROM %s AS a
WHERE $1 && a.the_geom WHERE $1 && a.the_geom
', target_column, target_table_name) ', target_column, qualified_name)
USING geom USING geom
INTO result; INTO result;
RETURN result; RETURN result;