How the PolyFromWKB() function works in Mariadb?
The PolyFromWKB()
function is a spatial function in Mariadb that creates a Polygon
object from a well-known binary (WKB) representation.
The PolyFromWKB()
function is a spatial function in Mariadb that creates a Polygon
object from a well-known binary (WKB) representation. A Polygon
object is a closed shape that consists of a set of linear rings. A linear ring is a closed and simple line string that does not cross itself. The first linear ring of a Polygon
object is the exterior ring, and the subsequent ones are the interior rings or holes.
Syntax
The syntax of the PolyFromWKB()
function is as follows:
PolyFromWKB(wkb, [srid])
The function takes two arguments:
wkb
: A binary string that represents the well-known binary (WKB) representation of the polygon. The WKB format is a binary encoding of the well-known text (WKT) format. For example, the WKB representation of the polygonPOLYGON((0 0, 0 10, 10 10, 10 0, 0 0))
is0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
.srid
: An optional integer that represents the spatial reference system identifier (SRID) of the polygon. If not specified, the default SRID is 0.
The function returns a Polygon
object if the input is valid, otherwise it returns NULL
.
Examples
Example 1: Creating a simple polygon
The following example creates a simple polygon with four vertices and no holes from a WKB string.
SELECT PolyFromWKB(0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) AS polygon;
The output is a binary representation of the polygon object, which can be converted to a human-readable format using the ST_AsText()
function.
SELECT ST_AsText(PolyFromWKB(0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)) AS polygon;
Example 2: Creating a polygon with a hole
The following example creates a polygon with a hole from a WKB string. The hole is a smaller polygon inside the larger polygon.
SELECT PolyFromWKB(0x000000000103080000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000001040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000) AS polygon;
The output can be converted to a human-readable format using the ST_AsText()
function.
SELECT ST_AsText(PolyFromWKB(0x000000000103080000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000001040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000)) AS polygon;
Example 3: Creating a polygon with a specific SRID
The following example creates a polygon with a specific SRID of 4326, which corresponds to the WGS 84 coordinate system.
SELECT PolyFromWKB(0x0000000001E610000003040000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) AS polygon;
The output is a binary representation of the polygon object, which can be converted to a human-readable format using the ST_AsText()
function.
SELECT ST_AsText(PolyFromWKB(0x0000000001E610000003040000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)) AS polygon;
Related Functions
There are some other functions that are related to the PolyFromWKB()
function, such as:
PolyFromText()
: This function creates aPolygon
object from a well-known text (WKT) representation. The WKT format is a text encoding of the spatial object. For example, the WKT representation of the polygonPOLYGON((0 0, 0 10, 10 10, 10 0, 0 0))
isPOLYGON((0 0, 0 10, 10 10, 10 0, 0 0))
. The syntax of the function isPolyFromText(wkt, [srid])
, wherewkt
is a string andsrid
is an optional integer.ST_PolyFromWKB()
: This function is a synonym for thePolyFromWKB()
function. The syntax of the function isST_PolyFromWKB(wkb, [srid])
, wherewkb
is a binary string andsrid
is an optional integer.
Conclusion
The PolyFromWKB()
function is a useful function to create a Polygon
object from a well-known binary (WKB) representation. A Polygon
object is a closed shape that consists of a set of linear rings. The function takes two arguments: a WKB string and an optional SRID. The function returns a Polygon
object if the input is valid, otherwise it returns NULL
. The function can be used to perform spatial operations on polygons, such as calculating the area, perimeter, centroid, etc. The function can also be combined with other spatial functions, such as ST_Contains()
, ST_Intersects()
, ST_Union()
, etc., to perform spatial analysis and queries on polygons.