@@ -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
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ Function to generate a contour map from an scatter dataset of points, using one
|
|||||||
| method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW|
|
| method | integer | 0:nearest neighbor, 1: barycentric, 2: IDW|
|
||||||
| classmethod | integer | 0:equals, 1: heads&tails, 2:jenks, 3:quantiles |
|
| classmethod | integer | 0:equals, 1: heads&tails, 2:jenks, 3:quantiles |
|
||||||
| steps | integer | Number of steps in the classification|
|
| steps | integer | Number of steps in the classification|
|
||||||
| max_time | integer | Max time in milliseconds for processing time
|
| max_time | integer | if <= 0: max processing time in seconds (smart resolution) , if >0: resolution in meters
|
||||||
|
|
||||||
### Returns
|
### Returns
|
||||||
Returns a table object
|
Returns a table object
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
+15
-12
@@ -17,16 +17,15 @@ RETURNS TABLE(
|
|||||||
DECLARE
|
DECLARE
|
||||||
cell_count integer;
|
cell_count integer;
|
||||||
tin geometry[];
|
tin geometry[];
|
||||||
|
resolution integer;
|
||||||
BEGIN
|
BEGIN
|
||||||
-- calc the cell size in web mercator units
|
|
||||||
-- WITH center as (
|
-- nasty trick to override issue #121
|
||||||
-- SELECT ST_centroid(ST_Collect(geomin)) as c
|
IF max_time = 0 THEN
|
||||||
-- )
|
max_time = -90;
|
||||||
-- SELECT
|
END IF;
|
||||||
-- round(resolution / cos(ST_y(c) * pi()/180))
|
resolution := max_time;
|
||||||
-- INTO cell
|
max_time := -1 * resolution;
|
||||||
-- FROM center;
|
|
||||||
-- raise notice 'Resol: %', cell;
|
|
||||||
|
|
||||||
-- calc the optimal number of cells for the current dataset
|
-- calc the optimal number of cells for the current dataset
|
||||||
SELECT
|
SELECT
|
||||||
@@ -70,9 +69,13 @@ BEGIN
|
|||||||
),
|
),
|
||||||
resolution as(
|
resolution as(
|
||||||
SELECT
|
SELECT
|
||||||
round(|/ (
|
CASE WHEN resolution <= 0 THEN
|
||||||
ST_area(geom) / cell_count
|
round(|/ (
|
||||||
)) as cell
|
ST_area(geom) / abs(cell_count)
|
||||||
|
))
|
||||||
|
ELSE
|
||||||
|
resolution
|
||||||
|
END AS cell
|
||||||
FROM envelope3857
|
FROM envelope3857
|
||||||
),
|
),
|
||||||
grid as(
|
grid as(
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ SELECT
|
|||||||
foo.*
|
foo.*
|
||||||
FROM
|
FROM
|
||||||
a,
|
a,
|
||||||
cdb_crankshaft.CDB_contour(a.g, a.vals, 0.0, 1, 3, 5, 60) foo
|
cdb_crankshaft.CDB_contour(a.g, a.vals, 0.0, 1, 3, 5, -60) foo
|
||||||
)
|
)
|
||||||
SELECT bin, avg_value from b order by bin;
|
SELECT bin, avg_value from b order by bin;
|
||||||
|
|||||||
@@ -30,18 +30,13 @@ def moran(subquery, attr_name,
|
|||||||
|
|
||||||
query = pu.construct_neighbor_query(w_type, qvals)
|
query = pu.construct_neighbor_query(w_type, qvals)
|
||||||
|
|
||||||
plpy.notice('** Query: %s' % query)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = plpy.execute(query)
|
result = plpy.execute(query)
|
||||||
# if there are no neighbors, exit
|
# if there are no neighbors, exit
|
||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
return pu.empty_zipped_array(2)
|
return pu.empty_zipped_array(2)
|
||||||
plpy.notice('** Query returned with %d rows' % len(result))
|
except plpy.SPIError, e:
|
||||||
except plpy.SPIError:
|
plpy.error('Analysis failed: %s' % e)
|
||||||
plpy.error('Error: areas of interest query failed, check input parameters')
|
|
||||||
plpy.notice('** Query failed: "%s"' % query)
|
|
||||||
plpy.notice('** Error: %s' % plpy.SPIError)
|
|
||||||
return pu.empty_zipped_array(2)
|
return pu.empty_zipped_array(2)
|
||||||
|
|
||||||
## collect attributes
|
## collect attributes
|
||||||
@@ -79,9 +74,8 @@ def moran_local(subquery, attr,
|
|||||||
# if there are no neighbors, exit
|
# if there are no neighbors, exit
|
||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
return pu.empty_zipped_array(5)
|
return pu.empty_zipped_array(5)
|
||||||
except plpy.SPIError:
|
except plpy.SPIError, e:
|
||||||
plpy.error('Error: areas of interest query failed, check input parameters')
|
plpy.error('Analysis failed: %s' % e)
|
||||||
plpy.notice('** Query failed: "%s"' % query)
|
|
||||||
return pu.empty_zipped_array(5)
|
return pu.empty_zipped_array(5)
|
||||||
|
|
||||||
attr_vals = pu.get_attributes(result)
|
attr_vals = pu.get_attributes(result)
|
||||||
@@ -111,18 +105,13 @@ def moran_rate(subquery, numerator, denominator,
|
|||||||
|
|
||||||
query = pu.construct_neighbor_query(w_type, qvals)
|
query = pu.construct_neighbor_query(w_type, qvals)
|
||||||
|
|
||||||
plpy.notice('** Query: %s' % query)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = plpy.execute(query)
|
result = plpy.execute(query)
|
||||||
# if there are no neighbors, exit
|
# if there are no neighbors, exit
|
||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
return pu.empty_zipped_array(2)
|
return pu.empty_zipped_array(2)
|
||||||
plpy.notice('** Query returned with %d rows' % len(result))
|
except plpy.SPIError, e:
|
||||||
except plpy.SPIError:
|
plpy.error('Analysis failed: %s' % e)
|
||||||
plpy.error('Error: areas of interest query failed, check input parameters')
|
|
||||||
plpy.notice('** Query failed: "%s"' % query)
|
|
||||||
plpy.notice('** Error: %s' % plpy.SPIError)
|
|
||||||
return pu.empty_zipped_array(2)
|
return pu.empty_zipped_array(2)
|
||||||
|
|
||||||
## collect attributes
|
## collect attributes
|
||||||
@@ -160,10 +149,8 @@ def moran_local_rate(subquery, numerator, denominator,
|
|||||||
# if there are no neighbors, exit
|
# if there are no neighbors, exit
|
||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
return pu.empty_zipped_array(5)
|
return pu.empty_zipped_array(5)
|
||||||
except plpy.SPIError:
|
except plpy.SPIError, e:
|
||||||
plpy.error('Error: areas of interest query failed, check input parameters')
|
plpy.error('Analysis failed: %s' % e)
|
||||||
plpy.notice('** Query failed: "%s"' % query)
|
|
||||||
plpy.notice('** Error: %s' % plpy.SPIError)
|
|
||||||
return pu.empty_zipped_array(5)
|
return pu.empty_zipped_array(5)
|
||||||
|
|
||||||
## collect attributes
|
## collect attributes
|
||||||
@@ -186,7 +173,6 @@ def moran_local_bv(subquery, attr1, attr2,
|
|||||||
"""
|
"""
|
||||||
Moran's I (local) Bivariate (untested)
|
Moran's I (local) Bivariate (untested)
|
||||||
"""
|
"""
|
||||||
plpy.notice('** Constructing query')
|
|
||||||
|
|
||||||
qvals = OrderedDict([("id_col", id_col),
|
qvals = OrderedDict([("id_col", id_col),
|
||||||
("attr1", attr1),
|
("attr1", attr1),
|
||||||
@@ -205,7 +191,6 @@ def moran_local_bv(subquery, attr1, attr2,
|
|||||||
except plpy.SPIError:
|
except plpy.SPIError:
|
||||||
plpy.error("Error: areas of interest query failed, " \
|
plpy.error("Error: areas of interest query failed, " \
|
||||||
"check input parameters")
|
"check input parameters")
|
||||||
plpy.notice('** Query failed: "%s"' % query)
|
|
||||||
return pu.empty_zipped_array(4)
|
return pu.empty_zipped_array(4)
|
||||||
|
|
||||||
## collect attributes
|
## collect attributes
|
||||||
@@ -219,13 +204,9 @@ def moran_local_bv(subquery, attr1, attr2,
|
|||||||
lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight,
|
lisa = ps.esda.moran.Moran_Local_BV(attr1_vals, attr2_vals, weight,
|
||||||
permutations=permutations)
|
permutations=permutations)
|
||||||
|
|
||||||
plpy.notice("len of Is: %d" % len(lisa.Is))
|
|
||||||
|
|
||||||
# find clustering of significance
|
# find clustering of significance
|
||||||
lisa_sig = quad_position(lisa.q)
|
lisa_sig = quad_position(lisa.q)
|
||||||
|
|
||||||
plpy.notice('** Finished calculations')
|
|
||||||
|
|
||||||
return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
|
return zip(lisa.Is, lisa_sig, lisa.p_sim, weight.id_order)
|
||||||
|
|
||||||
# Low level functions ----------------------------------------
|
# Low level functions ----------------------------------------
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ def spatial_markov_trend(subquery, time_cols, num_classes=7,
|
|||||||
)
|
)
|
||||||
if len(query_result) == 0:
|
if len(query_result) == 0:
|
||||||
return zip([None], [None], [None], [None], [None])
|
return zip([None], [None], [None], [None], [None])
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, e:
|
||||||
plpy.debug('Query failed with exception %s: %s' % (err, pu.construct_neighbor_query(w_type, qvals)))
|
plpy.debug('Query failed with exception %s: %s' % (err, pu.construct_neighbor_query(w_type, qvals)))
|
||||||
plpy.error('Query failed, check the input parameters')
|
plpy.error('Analysis failed: %s' % e)
|
||||||
return zip([None], [None], [None], [None], [None])
|
return zip([None], [None], [None], [None], [None])
|
||||||
|
|
||||||
## build weight
|
## build weight
|
||||||
|
|||||||
Reference in New Issue
Block a user