MaxPair — It takes as input parameter an array of two double values, and returns the maximum second value of the array.
Array MaxPair(Array [double A
double B]);
It takes as input parameter an array of two double values, and returns the maximum second value of the array.
This function can be used to obtain the maximum distance between a set of points, as shown in the following examples. Within PostgreSQL it is used with the spatial index to reduce the search space.
| 2D | 3D | M |
|---|---|---|
- | - | - |
| OGC SFS for SQL. 1.1 (1999) | OGC SFS for SQL. 1.1.0 (2005) | OGC SFS for SQL. 1.2.0 (2006) | SQL-MM Part 3 |
|---|---|---|---|
| - | - | - | - |
PostgreSQL
create table a(c1 double precision, c2 double precision);
insert into a values (10,2.456);
insert into a values (5,1.456);
insert into a values (20,2.256);
select MaxPair(Array[c1,c2]) from a;
maxpair
------------
{10,2.456}
(1 row)H2
create table a(c1 integer, c2 double); insert into a values (10,2.456); insert into a values (5,1.456); insert into a values (20,2.256); select Maxpair((c1::double,c2)) from a; --Result (10.0, 2.456)
Suppose you have a table of points and you want to get which is the farthest point to each point and the longest distance between them.
CREATE TABLE points(gid serial PRIMARY KEY);
SELECT AddGeometryColumn ('points','geom',-1,'POINT',2);
--H2: SELECT AddGeometryColumn ('POINTS','GEOM',-1,'POINT',2);
INSERT INTO points(geom) VALUES (ST_GeomfromText('POINT (8 10)'));
...
gid | st_astext
-----+---------------
1 | POINT (8 10)
2 | POINT (18 15)
3 | POINT (11 15)
4 | POINT (13 13)
5 | POINT (11 12)
6 | POINT (0 11)
7 | POINT (15 3)
8 | POINT (28 17)
9 | POINT (19 23)
10 | POINT (27 8)
11 | POINT (7 2)
12 | POINT (20 10)
13 | POINT (7 23)
(13 rows) | ![]() |
PostgreSQL
SELECT d1.gid, MaxPair(ARRAY[d2.gid,distance(d1.geom,d2.geom)]), count(*)
FROM points d1, points d2
WHERE (ST_Expand (d1.geom,30) && d2.geom) and distance (d1.geom,d2.geom) > 0
GROUP BY d1.gid;
gid | maxpair | count
-----+-----------------------+-------
1 | {8,21.1896201004171} | 12
2 | {6,18.4390889145858} | 12
3 | {10,17.464249196573} | 12
4 | {8,15.52417469626} | 12
5 | {8,17.7200451466693} | 12
6 | {8,28.6356421265527} | 12
7 | {13,21.540659228538} | 12
8 | {6,28.6356421265527} | 12
9 | {11,24.1867732448956} | 12
10 | {6,27.1661554144122} | 12
11 | {8,25.8069758011279} | 12
12 | {6,20.0249843945008} | 12
13 | {10,25} | 12
(13 rows)H2
SELECT d1.gid, MinPair((d2.gid::double,distance(d1.geom,d2.geom))) as minpair, count(*) FROM points d1, points d2 WHERE distance (d1.geom,d2.geom) > 0 GROUP BY d1.gid; GID MINPAIR COUNT(*) 1 (8.0, 21.18962010041709) 12 2 (6.0, 18.439088914585774) 12 3 (10.0, 17.46424919657298) 12 4 (8.0, 15.524174696260024) 12 5 (8.0, 17.72004514666935) 12 6 (8.0, 28.635642126552707) 12 7 (13.0, 21.540659228538015) 12 8 (6.0, 28.635642126552707) 12 9 (11.0, 24.186773244895647) 12 10 (6.0, 27.16615541441225) 12 11 (8.0, 25.80697580112788) 12 12 (6.0, 20.024984394500787) 12 13 (10.0, 25.0) 12