How the ST_BUFFER() function works in MariaDB?

The ST_BUFFER() function in MariaDB is used to create a buffer geometry around a given geometry value, allowing for efficient analysis and manipulation of spatial data.

Posted on

The ST_BUFFER() function in MariaDB is used to create a buffer geometry around a given geometry value. A buffer is a polygon that represents the area within a specified distance from the input geometry. This function is particularly useful in spatial analysis and data processing tasks, such as identifying areas of influence or creating proximity zones around specific features.

Syntax

The syntax for the MariaDB ST_BUFFER() function is as follows:

ST_BUFFER(geom, dist [, strategy])
  • geom: The geometry value around which the buffer needs to be created.
  • dist: The distance (in the same units as the geometry) by which the buffer should be created around the input geometry.
  • strategy (optional): An integer value that specifies the buffer strategy to be used. The available strategies are:
    • 1: End-cap style buffers (default)
    • 2: Mitered join style buffers
    • 3: Round join style buffers

The function returns a Polygon or MultiPolygon geometry representing the buffer area around the input geometry.

Examples

Example 1: Create a buffer around a Point geometry

This example demonstrates how to create a buffer around a Point geometry.

SET @point = ST_GeomFromText('POINT(1 1)');
SELECT ST_AsText(ST_BUFFER(@point, 1));

The following is the output:

POLYGON((2 1, 1.707106781186548 1.707106781186547, 1 2, 0.2928932188134524 1.707106781186547, 0 1, 0.2928932188134524 0.2928932188134524, 1 0, 1.707106781186548 0.2928932188134524, 2 1))

This example creates a buffer polygon with a radius of 1 unit around the Point geometry at (1, 1).

Example 2: Create a buffer around a LineString geometry

This example shows how to create a buffer around a LineString geometry.

SET @line = ST_GeomFromText('LINESTRING(0 0, 2 2)');
SELECT ST_AsText(ST_BUFFER(@line, 0.5));

The following is the output:

POLYGON((0.5 -0.5, 2.5 1.5, 2 2.5, 1.5 2.5, 0 1.5, 0 0.5, 0.5 0.5, 0.5 -0.5))

In this example, a buffer polygon with a width of 0.5 units is created around the LineString geometry connecting points (0, 0) and (2, 2).

Example 3: Create a buffer around a Polygon geometry

This example demonstrates how to create a buffer around a Polygon geometry.

SET @poly = ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
SELECT ST_AsText(ST_BUFFER(@poly, 2, 3));

The following is the output:

POLYGON((12 12, 12 -2, 10 -2, 10 0, 0 0, 0 10, -2 10, -2 12, 12 12))

This example creates a buffer polygon with a distance of 2 units around the square polygon, using the round join style buffer strategy (strategy 3).

Example 4: Create a buffer around a MultiPolygon geometry

This example shows how to create a buffer around a MultiPolygon geometry.

SET @multipoly = ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((20 20, 30 20, 30 30, 20 30, 20 20)))');
SELECT ST_AsText(ST_BUFFER(@multipoly, 1));

The following is the output:

POLYGON((31 31, 31 19, 30 19, 30 20, 20 20, 20 30, 19 30, 19 31, 11 31, 11 19, 9 19, 9 11, 11 11, 11 9, 19 9, 19 11, 31 11, 31 31))

In this example, a buffer polygon with a distance of 1 unit is created around the MultiPolygon geometry, which consists of two square polygons.

Example 5: Create a buffer around a geometry from a table

This example demonstrates how to create a buffer around a geometry stored in a table.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly GEOMETRY);
INSERT INTO polygons (id, poly) VALUES
    (1, ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));
SELECT id, ST_AsText(ST_BUFFER(poly, 2)) AS buffer FROM polygons;

The following is the output:

+----+------------------------------------------------------------------+
| id | buffer                                                           |
+----+------------------------------------------------------------------+
|  1 | POLYGON((12 12, 12 -2, 10 -2, 10 0, 0 0, 0 10, -2 10, -2 12, 12 12)) |
+----+------------------------------------------------------------------+

In this example, a table named polygons is created with an id column and a poly column of type GEOMETRY. A polygon geometry is inserted into the table, and then the ST_BUFFER() function is used to create a buffer polygon with a distance of 2 units around the stored geometry for each row in the table.

The following are some functions related to the MariaDB ST_BUFFER() function:

  • The MariaDB ST_Distance() function is used to calculate the distance between two geometries.
  • The MariaDB ST_Intersection() function is used to calculate the intersection between two geometries.
  • The MariaDB ST_Union() function is used to combine multiple geometries into a single geometry.
  • The MariaDB ST_Difference() function is used to calculate the difference between two geometries.

Conclusion

The ST_BUFFER() function in MariaDB is a powerful spatial function that allows you to create buffer geometries around existing geometries. By specifying the desired distance and buffer strategy, you can generate polygons or multipolygons that represent the area within a certain proximity of the input geometry. This function finds numerous applications in spatial analysis, such as identifying areas of influence, creating safety zones, or performing proximity-based spatial queries. With the help of examples, this article has demonstrated how to use the ST_BUFFER() function for different geometry types and scenarios.