crankshaft/pg/sql/0.0.1/03_overlap_sum.sql
2016-02-18 11:28:26 +01:00

21 lines
573 B
PL/PgSQL

-- Function by Stuart Lynn for a simple interpolation of a value
-- from a polygon table over an arbitrary polygon
-- (weighted by the area proportion overlapped)
CREATE OR REPLACE
FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text)
RETURNS numeric AS
$$
DECLARE
result numeric;
BEGIN
EXECUTE Format('
SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom))
FROM %I AS a
WHERE $1 && a.the_geom
', target_column, target_table_name)
USING geom
INTO result;
RETURN result;
END;
$$ LANGUAGE plpgsql;