4.6.  Polygon #

The functions spoly and spoly_deg can be used to create spherical polygons. Function spoly is overloaded and can accept arrays of float8 or spoint elements. There are the same restrictions as for using the input function of spherical polygon (see Section 3.8).

Create a spherical polygon from an array of pair-consecutive numbers (lng, lat). The coordinates are specified in radians.

sql> SELECT spoly(ARRAY[0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0])
            

Create a spherical polygon from an array of spoint elements.

sql> SELECT spoly(ARRAY[spoint(0, 0), spoint(0, 0.5), spoint(0.5, 0.5), spoint(0.5, 0)])
            

Create a spherical polygon from an array of pair-consecutive numbers (lng, lat). The coordinates are specified in degrees.

sql> SELECT spoly_deg(ARRAY[0, 0, 0, 10, 10, 10, 10, 0])
            

The aggregate function spoly can be used to create a polygon from a set of spherical points. The function returns NULL, if the polygon could not be created.

Example 4.7.  Create a spherical polygon using a set of spherical points

Create a table and put in some spherical points with a unique ID. Then, create two polygons with different edge sequences.

sql> SELECT set_sphere_output('DEG');
 set_sphere_output 
-------------------
 SET DEG
(1 row)

sql> CREATE TABLE points ( i int PRIMARY KEY, p spoint );
sql> INSERT INTO points VALUES (1, '( 0d, 0d)');
sql> INSERT INTO points VALUES (2, '(10d, 0d)');
sql> INSERT INTO points VALUES (3, '( 0d,10d)');
sql> SELECT spoly(data.p) FROM ( SELECT p FROM points ORDER BY i ASC ) AS data ;
               spoly               
-----------------------------------
 {(0d , 0d),(10d , 0d),(0d , 10d)}
(1 row)

sql> SELECT spoly(data.p) FROM ( SELECT p FROM points ORDER BY i DESC ) AS data ;
               spoly               
-----------------------------------
 {(0d , 10d),(10d , 0d),(0d , 0d)}
(1 row)