Merge pull request #129 from CartoDB/add-interpolation

[interpolation] Add NN(s) method
This commit is contained in:
Carla 2016-09-21 17:19:29 +02:00 committed by GitHub
commit eee612b12d
3 changed files with 28 additions and 11 deletions

View File

@ -2,7 +2,7 @@
Function to interpolate a numeric attribute of a point in a scatter dataset of points, using one of three methos: Function to interpolate a numeric attribute of a point in a scatter dataset of points, using one of three methos:
* [Nearest neighbor](https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation) * [Nearest neighbor(s)](https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)
* [Barycentric](https://en.wikipedia.org/wiki/Barycentric_coordinate_system) * [Barycentric](https://en.wikipedia.org/wiki/Barycentric_coordinate_system)
* [IDW](https://en.wikipedia.org/wiki/Inverse_distance_weighting) * [IDW](https://en.wikipedia.org/wiki/Inverse_distance_weighting)
@ -15,7 +15,7 @@ Function to interpolate a numeric attribute of a point in a scatter dataset of p
| query | text | query that returns at least `the_geom` and a numeric value as `attrib` | | query | text | query that returns at least `the_geom` and a numeric value as `attrib` |
| point | geometry | The target point to calc the value | | point | geometry | The target point to calc the value |
| method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW| | method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW|
| p1 | integer | IDW: limit the number of neighbors, 0->no limit| | p1 | integer | limit the number of neighbors, IDW: 0->no limit, NN: 0-> closest one|
| p2 | integer | IDW: order of distance decay, 0-> order 1| | p2 | integer | IDW: order of distance decay, 0-> order 1|
### CDB_SpatialInterpolation (geom geometry[], values numeric[], point geometry, method integer DEFAULT 1, p1 integer DEFAULT 0, ps integer DEFAULT 0) ### CDB_SpatialInterpolation (geom geometry[], values numeric[], point geometry, method integer DEFAULT 1, p1 integer DEFAULT 0, ps integer DEFAULT 0)
@ -28,7 +28,7 @@ Function to interpolate a numeric attribute of a point in a scatter dataset of p
| values | numeric[] | Array of points' values for the param under study| | values | numeric[] | Array of points' values for the param under study|
| point | geometry | The target point to calc the value | | point | geometry | The target point to calc the value |
| method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW| | method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW|
| p1 | integer | IDW: limit the number of neighbors, 0->no limit| | p1 | integer | limit the number of neighbors, IDW: 0->no limit, NN: 0-> closest one|
| p2 | integer | IDW: order of distance decay, 0-> order 1| | p2 | integer | IDW: order of distance decay, 0-> order 1|
### Returns ### Returns
@ -37,6 +37,9 @@ Function to interpolate a numeric attribute of a point in a scatter dataset of p
|-------------|------|-------------| |-------------|------|-------------|
| value | numeric | Interpolated value at the given point, `-888.888` if the given point is out of the boundaries of the source points set | | value | numeric | Interpolated value at the given point, `-888.888` if the given point is out of the boundaries of the source points set |
Default values:
* -888.888: when using Barycentric, the target point is out of the realm of the input points
* -777.777: asking for a method not available
#### Example Usage #### Example Usage

View File

@ -1,6 +1,8 @@
-- 0: nearest neighbor -- 0: nearest neighbor(s)
-- 1: barymetric -- 1: barymetric
-- 2: IDW -- 2: IDW
-- 3: krigin ---> TO DO
CREATE OR REPLACE FUNCTION CDB_SpatialInterpolation( CREATE OR REPLACE FUNCTION CDB_SpatialInterpolation(
IN query text, IN query text,
@ -50,12 +52,19 @@ DECLARE
vc numeric; vc numeric;
output numeric; output numeric;
BEGIN BEGIN
output := -999.999; -- output := -999.999;
-- nearest
-- nearest neighbors
-- p1: limit the number of neighbors, 0-> closest one
IF method = 0 THEN IF method = 0 THEN
WITH a as (SELECT unnest(geomin) as g, unnest(colin) as v) IF p1 = 0 THEN
SELECT a.v INTO output FROM a ORDER BY point<->a.g LIMIT 1; p1 := 1;
END IF;
WITH a as (SELECT unnest(geomin) as g, unnest(colin) as v),
b as (SELECT a.v as v FROM a ORDER BY point<->a.g LIMIT p1::integer)
SELECT avg(b.v) INTO output FROM b;
RETURN output; RETURN output;
-- barymetric -- barymetric
@ -121,6 +130,11 @@ BEGIN
SELECT sum(b.f)/sum(b.k) INTO output FROM b; SELECT sum(b.f)/sum(b.k) INTO output FROM b;
RETURN output; RETURN output;
-- krigin
ELSIF method = 3 THEN
-- TO DO
END IF; END IF;
RETURN -777.777; RETURN -777.777;

View File

@ -1,7 +1,7 @@
SET client_min_messages TO WARNING; SET client_min_messages TO WARNING;
\set ECHO none \set ECHO none
nn | nni | idw nn | nni | idw
-----+--------------------------+----------------- ----------------------+--------------------------+-----------------
200 | 238.41059602632179224595 | 341.46260750526 200.0000000000000000 | 238.41059602632179224595 | 341.46260750526
(1 row) (1 row)