From 2ff20e596ee8f8ec0dbc30b53a25777262b27694 Mon Sep 17 00:00:00 2001 From: abelvm Date: Tue, 20 Sep 2016 12:57:41 +0200 Subject: [PATCH 1/4] add NN(s) support --- doc/08_interpolation.md | 6 +++--- src/pg/sql/08_interpolation.sql | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/doc/08_interpolation.md b/doc/08_interpolation.md index 22fc1bc..87b5124 100644 --- a/doc/08_interpolation.md +++ b/doc/08_interpolation.md @@ -2,7 +2,7 @@ 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) * [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` | | point | geometry | The target point to calc the value | | 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| ### 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| | point | geometry | The target point to calc the value | | 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| ### Returns diff --git a/src/pg/sql/08_interpolation.sql b/src/pg/sql/08_interpolation.sql index 0937f09..608583d 100644 --- a/src/pg/sql/08_interpolation.sql +++ b/src/pg/sql/08_interpolation.sql @@ -1,4 +1,4 @@ --- 0: nearest neighbor +-- 0: nearest neighbor (s) -- 1: barymetric -- 2: IDW @@ -51,11 +51,17 @@ DECLARE output numeric; BEGIN output := -999.999; - -- nearest + + -- nearest neighbors + -- p1: limit the number of neighbors, 0-> closest one IF method = 0 THEN + IF p1 = 0 THEN + p1 := 1; + END IF; + WITH a as (SELECT unnest(geomin) as g, unnest(colin) as v) - SELECT a.v INTO output FROM a ORDER BY point<->a.g LIMIT 1; + SELECT avg(a.v) INTO output FROM a ORDER BY point<->a.g LIMIT p1::integer; RETURN output; -- barymetric @@ -121,6 +127,11 @@ BEGIN SELECT sum(b.f)/sum(b.k) INTO output FROM b; RETURN output; + -- krigin + ELSIF method = 3 THEN + + + END IF; RETURN -777.777; From 4902f6a9d487f9f9754c54353fec83741e7a8f4a Mon Sep 17 00:00:00 2001 From: abelvm Date: Tue, 20 Sep 2016 13:37:50 +0200 Subject: [PATCH 2/4] add NN(s) support --- src/pg/sql/08_interpolation.sql | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/pg/sql/08_interpolation.sql b/src/pg/sql/08_interpolation.sql index 608583d..d13d8e7 100644 --- a/src/pg/sql/08_interpolation.sql +++ b/src/pg/sql/08_interpolation.sql @@ -1,6 +1,8 @@ --- 0: nearest neighbor (s) +-- 0: nearest neighbor(s) -- 1: barymetric -- 2: IDW +-- 3: krigin + CREATE OR REPLACE FUNCTION CDB_SpatialInterpolation( IN query text, @@ -60,8 +62,9 @@ BEGIN p1 := 1; END IF; - WITH a as (SELECT unnest(geomin) as g, unnest(colin) as v) - SELECT avg(a.v) INTO output FROM a ORDER BY point<->a.g LIMIT p1::integer; + 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; -- barymetric From 37e4fc7cad6510a80281832ee25513387a1f85aa Mon Sep 17 00:00:00 2001 From: abelvm Date: Tue, 20 Sep 2016 13:57:58 +0200 Subject: [PATCH 3/4] add NN(s) support --- src/pg/test/expected/08_interpolation_test.out | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pg/test/expected/08_interpolation_test.out b/src/pg/test/expected/08_interpolation_test.out index 1e4502a..e2c9287 100644 --- a/src/pg/test/expected/08_interpolation_test.out +++ b/src/pg/test/expected/08_interpolation_test.out @@ -1,7 +1,7 @@ SET client_min_messages TO WARNING; \set ECHO none - nn | nni | idw ------+--------------------------+----------------- - 200 | 238.41059602632179224595 | 341.46260750526 + nn | nni | idw +----------------------+--------------------------+----------------- + 200.0000000000000000 | 238.41059602632179224595 | 341.46260750526 (1 row) From 754e66c02cc4159be161098cc8389da7d48179f6 Mon Sep 17 00:00:00 2001 From: abelvm Date: Wed, 21 Sep 2016 14:24:35 +0200 Subject: [PATCH 4/4] leftovers and docs --- doc/08_interpolation.md | 3 +++ src/pg/sql/08_interpolation.sql | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/08_interpolation.md b/doc/08_interpolation.md index 87b5124..c17269e 100644 --- a/doc/08_interpolation.md +++ b/doc/08_interpolation.md @@ -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 | +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 diff --git a/src/pg/sql/08_interpolation.sql b/src/pg/sql/08_interpolation.sql index d13d8e7..cb0a20a 100644 --- a/src/pg/sql/08_interpolation.sql +++ b/src/pg/sql/08_interpolation.sql @@ -1,7 +1,7 @@ -- 0: nearest neighbor(s) -- 1: barymetric -- 2: IDW --- 3: krigin +-- 3: krigin ---> TO DO CREATE OR REPLACE FUNCTION CDB_SpatialInterpolation( @@ -52,7 +52,7 @@ DECLARE vc numeric; output numeric; BEGIN - output := -999.999; + -- output := -999.999; -- nearest neighbors -- p1: limit the number of neighbors, 0-> closest one @@ -133,7 +133,7 @@ BEGIN -- krigin ELSIF method = 3 THEN - + -- TO DO END IF;