2016-02-18 18:28:26 +08:00
|
|
|
-- 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)
|
2016-02-18 23:11:29 +08:00
|
|
|
-- 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
|
2016-02-18 18:28:26 +08:00
|
|
|
CREATE OR REPLACE
|
2016-02-18 23:11:29 +08:00
|
|
|
FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text, schema_name text DEFAULT NULL)
|
2016-02-18 18:28:26 +08:00
|
|
|
RETURNS numeric AS
|
|
|
|
$$
|
|
|
|
DECLARE
|
|
|
|
result numeric;
|
2016-02-18 23:11:29 +08:00
|
|
|
qualified_name text;
|
2016-02-18 18:28:26 +08:00
|
|
|
BEGIN
|
2016-02-18 23:11:29 +08:00
|
|
|
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;
|
2016-02-18 18:28:26 +08:00
|
|
|
EXECUTE Format('
|
|
|
|
SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom))
|
2016-02-18 23:11:29 +08:00
|
|
|
FROM %s AS a
|
2016-02-18 18:28:26 +08:00
|
|
|
WHERE $1 && a.the_geom
|
2016-02-18 23:11:29 +08:00
|
|
|
', target_column, qualified_name)
|
2016-02-18 18:28:26 +08:00
|
|
|
USING geom
|
|
|
|
INTO result;
|
|
|
|
RETURN result;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|