ST_PolyFromWKB function
The ST_PolyFromWKB function takes a well-known binary (WKB) representation of a polygon and, optionally, a spatial reference system identifier as input parameters and returns the corresponding polygon.
If the specified WKB format is null, then null is returned.
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 well-known binary format.
Syntax
Parameters
- wkb
- A value of type VARBINARY or BLOB(2G) that contains the well-known binary format of the resulting polygon.
- 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 SYSGEO.ST_SPATIAL_REFERENCE_SYSTEMS, then an exception condition is raised (SQLSTATE 38SU1).
Return type
ST_Polygon
Example
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_PolyFromWKB can be used to create a polygon from its well-known binary format. The geometry is a polygon in spatial reference system 1. In this example, the polygon gets stored with ID = 1115 in the GEOMETRY column of the SAMPLE_POLYS table, and then the WKB column is updated with its well-known binary format (using the ST_AsBinary function). Finally, the ST_PolyFromWKB function is used to return the multipolygon from the WKB column. The X and Y coordinates for this geometry are: (50, 20) (50, 40) (70, 30).
CREATE TABLE sample_polys (id INTEGER, geometry ST_Polygon,
wkb varbinary(32000))
INSERT INTO sample_polys
VALUES (10, ST_Polygon ('polygon ((50 20, 50 40, 70 30, 50 20))', 1) )
UPDATE sample_polys AS temporary_correlated
SET wkb = ST_AsBinary( geometry )
WHERE id = temporary_correlated.id
In the following SELECT statement, the ST_PolyFromWKB
function is used to retrieve the polygon from the WKB column.
SELECT id, CAST( ST_AsText( ST_PolyFromWKB (wkb, 1) )
AS VARCHAR(120) ) POLYGON
FROM sample_polys
WHERE id = 1115
Results:
ID POLYGON
---------- --------------------------------------------------------------
1115 POLYGON ((50.000000000 20.000000000,
70.000000000 30.000000000,
50.000000000 40.000000000,
50.000000000 20.000000000))
