ST_PolyFromText function
The ST_PolyFromText function takes a well-known text (WKT) format or a GeoJSON format of a polygon and, optionally, a spatial reference system identifier as input parameters and returns the corresponding polygon.
- A WKT format
- GeoJSON format
The recommended function for achieving the same result is the ST_Polygon function. It is recommended because of its flexibility: ST_Polygon takes additional forms of input as well as the WKT or GeoJSON formats.
If the specified WKT or GeoJSON format is null, then null is returned.
For details about the supported formats, see Supported data formats.
Syntax
Parameters
- wkt
- A value of type VARCHAR or CLOB(2G) that contains the WKT format of the resulting polygon.
- geojson
- A value of type VARCHAR or CLOB(2G) that represents the resulting polygon using GeoJSON.
- srs_id
- A value of type INTEGER that identifies the spatial reference system for the resulting polygon.
If the srs_id parameter is omitted, the spatial reference system with the numeric identifier 4326 is used.
If srs_id does not identify a spatial reference system listed in the catalog view DB2GSE.ST_SPATIAL_REFERENCE_SYSTEMS, then an exception condition is raised (SQLSTATE 38SU1).
Return type
ST_Polygon
Examples
- Example 1:
-
In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.
This example illustrates how ST_PolyFromText can be used to create and insert a polygon from its WKT format. The record that is inserted has ID = 1110, and the geometry is a polygon in spatial reference system 1. The polygon is in the WKT format of a polygon. The X and Y coordinates for this geometry are: (50, 20) (50, 40) (70, 30).
The following SELECT statement returns the polygon that was recorded in the table:CREATE TABLE sample_polys (id INTEGER, geometry ST_Polygon) INSERT INTO sample_polys VALUES (1110, ST_PolyFromText ('polygon ((50 20, 50 40, 70 30, 50 20))', 1) )
Results:SELECT id, CAST( ST_AsText( geometry ) AS VARCHAR(120) ) POLYGON FROM sample_polys WHERE id = 1110
ID POLYGON ---------- -------------------------------------------------------------------- 1110 POLYGON ((50.000000 20.000000, 70.000000 30.000000, 50.000000 40.000000, 50.000000 20.000000))
- Example 2:
-
This example illustrates how ST_PolyFromText can be used to create and insert a polygon from its GeoJSON format using the geometry data from Example 1.
The record that is inserted has ID = 1111, and the geometry is a polygon in spatial reference system 4326 (the default). The polygon is in the GeoJSON format of a polygon.CREATE TABLE sample_polys (id INTEGER, geometry ST_Polygon) INSERT INTO sample_polys VALUES (1111, ST_PolyFromText ('{ "type": "polygon", "coordinates": [ [[50, 20], [50, 40], [70, 30], [50, 20]] ] }') )
The following SELECT statement returns the polygon that was recorded in the table:SELECT id, CAST( ST_AsText( geometry ) AS VARCHAR(120) ) POLYGON FROM sample_polys WHERE id = 1111
Results:ID POLYGON ---------- -------------------------------------------------------------------- 1111 POLYGON ((50.000000000 20.000000000, 70.000000000 30.000000000, 50.000000000 40.000000000, 50.000000000 20.000000000))