MariaDB ST_BUFFER() 函数的基础用法与实例

MariaDB ST_Buffer() 函数用来创建一个几何图形周围的缓冲区。它可以为输入的几何图形添加一个指定宽度的区域,生成一个新的更大的几何图形。

发布于

MariaDB ST_Buffer() 函数用来创建一个几何图形周围的缓冲区。它可以为输入的几何图形添加一个指定宽度的区域,生成一个新的更大的几何图形。这个函数在许多领域都有应用,例如测量和分析空间数据、生成影响区域等。

语法

MairaDB ST_Buffer() 函数的语法如下:

ST_Buffer(g, radius)
  • g: 要创建缓冲区的几何图形。
  • radius: 缓冲区的宽度,使用与 g 相同的单位。正值表示向外扩张,负值表示向内收缩。

该函数会返回一个新的几何图形,表示输入几何图形加上指定宽度的缓冲区。

实例

为一个点创建缓冲区

这个实例将展示如何为一个点创建指定宽度的缓冲区。

DROP TABLE IF EXISTS point_table;
CREATE TABLE point_table (id INT PRIMARY KEY, pt POINT NOT NULL);
INSERT INTO point_table VALUES (1, POINT(0, 0));
SELECT ST_AsText(ST_Buffer(pt, 1)) FROM point_table;

以下是该语句的输出:

POLYGON((0 -1,-0.04906767432741802 -0.9987954562051724,-0.0980171403295606 -0.9951847266721968,-0.1467304744553618 -0.989176509964781,-0.1950903220161283 -0.9807852804032304,-0.2429801799032639 -0.970031253194544,-0.2902846772544623 -0.9569403357322089,-0.3368898533922201 -0.9415440651830208,-0.3826834323650898 -0.9238795325112867,-0.4275550934302821 -0.9039892931234433,-0.4713967368259976 -0.8819212643483549,-0.5141027441932217 -0.8577286100002721,-0.5555702330196022 -0.8314696123025452,-0.5956993044924334 -0.8032075314806448,-0.6343932841636455 -0.773010453362737,-0.6715589548470183 -0.7409511253549591,-0.7071067811865475 -0.7071067811865475,-0.7409511253549591 -0.6715589548470183,-0.773010453362737 -0.6343932841636455,-0.8032075314806448 -0.5956993044924334,-0.8314696123025452 -0.5555702330196022,-0.8577286100002721 -0.5141027441932217,-0.8819212643483549 -0.4713967368259976,-0.9039892931234433 -0.4275550934302821,-0.9238795325112867 -0.3826834323650898,-0.9415440651830208 -0.3368898533922201,-0.9569403357322089 -0.2902846772544623,-0.970031253194544 -0.2429801799032639,-0.9807852804032304 -0.1950903220161283,-0.989176509964781 -0.1467304744553618,-0.9951847266721968 -0.0980171403295606,-0.9987954562051724 -0.04906767432741802,-1 0,-0.9951847266721968 0.0980171403295606,-0.989176509964781 0.1467304744553618,-0.9807852804032304 0.1950903220161283,-0.970031253194544 0.2429801799032639,-0.9569403357322089 0.2902846772544623,-0.9415440651830208 0.3368898533922201,-0.9238795325112867 0.3826834323650898,-0.9039892931234433 0.4275550934302821,-0.8819212643483549 0.4713967368259976,-0.8577286100002721 0.5141027441932217,-0.8314696123025452 0.5555702330196022,-0.8032075314806448 0.5956993044924334,-0.773010453362737 0.6343932841636455,-0.7409511253549591 0.6715589548470183,-0.7071067811865475 0.7071067811865475,-0.6715589548470183 0.7409511253549591,-0.6343932841636455 0.773010453362737,-0.5956993044924334 0.8032075314806448,-0.5555702330196022 0.8314696123025452,-0.5141027441932217 0.8577286100002721,-0.4713967368259976 0.8819212643483549,-0.4275550934302821 0.9039892931234433,-0.3826834323650898 0.9238795325112867,-0.3368898533922201 0.9415440651830208,-0.2902846772544623 0.9569403357322089,-0.2429801799032639 0.970031253194544,-0.1950903220161283 0.9807852804032304,-0.1467304744553618 0.989176509964781,-0.0980171403295606 0.9951847266721968,-0.04906767432741802 0.9987954562051724,0 1,0.04906767432741802 0.9987954562051724,0.0980171403295606 0.9951847266721968,0.1467304744553618 0.989176509964781,0.1950903220161283 0.9807852804032304,0.2429801799032639 0.970031253194544,0.2902846772544623 0.9569403357322089,0.3368898533922201 0.9415440651830208,0.3826834323650898 0.9238795325112867,0.4275550934302821 0.9039892931234433,0.4713967368259976 0.8819212643483549,0.5141027441932217 0.8577286100002721,0.5555702330196022 0.8314696123025452,0.5956993044924334 0.8032075314806448,0.6343932841636455 0.773010453362737,0.6715589548470183 0.7409511253549591,0.7071067811865475 0.7071067811865475,0.7409511253549591 0.6715589548470183,0.773010453362737 0.6343932841636455,0.8032075314806448 0.5956993044924334,0.8314696123025452 0.5555702330196022,0.8577286100002721 0.5141027441932217,0.8819212643483549 0.4713967368259976,0.9039892931234433 0.4275550934302821,0.9238795325112867 0.3826834323650898,0.9415440651830208 0.3368898533922201,0.9569403357322089 0.2902846772544623,0.970031253194544 0.2429801799032639,0.9807852804032304 0.1950903220161283,0.989176509964781 0.1467304744553618,0.9951847266721968 0.0980171403295606,0.9987954562051724 0.04906767432741802,1 0,0.9951847266721968 -0.0980171403295606,0.989176509964781 -0.1467304744553618,0.9807852804032304 -0.1950903220161283,0.970031253194544 -0.2429801799032639,0.9569403357322089 -0.2902846772544623,0.9415440651830208 -0.3368898533922201,0.9238795325112867 -0.3826834323650898,0.9039892931234433 -0.4275550934302821,0.8819212643483549 -0.4713967368259976,0.8577286100002721 -0.5141027441932217,0.8314696123025452 -0.5555702330196022,0.8032075314806448 -0.5956993044924334,0.773010453362737 -0.6343932841636455,0.7409511253549591 -0.6715589548470183,0.7071067811865475 -0.7071067811865475,0.6715589548470183 -0.7409511253549591,0.6343932841636455 -0.773010453362737,0.5956993044924334 -0.8032075314806448,0.5555702330196022 -0.8314696123025452,0.5141027441932217 -0.8577286100002721,0.4713967368259976 -0.8819212643483549,0.4275550934302821 -0.9039892931234433,0.3826834323650898 -0.9238795325112867,0.3368898533922201 -0.9415440651830208,0.2902846772544623 -0.9569403357322089,0.2429801799032639 -0.970031253194544,0.1950903220161283 -0.9807852804032304,0.1467304744553618 -0.989176509964781,0.0980171403295606 -0.9951847266721968,0.04906767432741802 -0.9987954562051724,0 -1))

输出是以点(0,0)为中心,半径为 1 的圆形缓冲区。

使用不同的策略生成缓冲区

这个实例将展示使用不同的缓冲区生成策略会产生不同的结果。

DROP TABLE IF EXISTS line_table;
CREATE TABLE line_table (id INT PRIMARY KEY, ln LINESTRING NOT NULL);
INSERT INTO line_table VALUES (1, ST_GeomFromText('LINESTRING(0 0, 1 1, 2 0)'));
SELECT ST_AsText(ST_Buffer(ln, 0.2)) FROM line_table;

以下是该语句的输出:

POLYGON((0 -0.19999999999999998,-0.009813534865483614 -0.19975909124103447,-0.01960342806591213 -0.19903694533443939,-0.029346094891072344 -0.19783530199295618,-0.03901806440322565 -0.1961570560806461,-0.048596035980652805 -0.1940062506389088,-0.058056935450892466 -0.19138806714644174,-0.067377970678444 -0.18830881303660416,-0.07653668647301795 -0.18477590650225736,-0.08551101868605641 -0.18079785862468867,-0.09427934736519956 -0.17638425286967102,-0.10282054883864433 -0.1715457220000544,-0.11111404660392044 -0.16629392246050906,-0.11913986089848666 -0.160641506296129,-0.1268786568327291 -0.15460209067254738,-0.13431179096940368 -0.1481902250709918,-0.1414213562373095 -0.1414213562373095,-0.1481902250709918 -0.13431179096940368,-0.15460209067254738 -0.1268786568327291,-0.160641506296129 -0.11913986089848666,-0.16629392246050906 -0.11111404660392044,-0.1715457220000544 -0.10282054883864433,-0.17638425286967102 -0.09427934736519956,-0.18079785862468867 -0.08551101868605641,-0.18477590650225736 -0.07653668647301795,-0.18830881303660416 -0.067377970678444,-0.19138806714644174 -0.058056935450892466,-0.1940062506389088 -0.048596035980652805,-0.1961570560806461 -0.03901806440322565,-0.19783530199295618 -0.029346094891072344,-0.19903694533443939 -0.01960342806591213,-0.19975909124103447 -0.009813534865483614,-0.19999999999999998 0,-0.19975909124103447 0.009813534865483614,-0.19903694533443939 0.01960342806591213,-0.19783530199295618 0.029346094891072344,-0.1961570560806461 0.03901806440322565,-0.1940062506389088 0.048596035980652805,-0.19138806714644174 0.058056935450892466,-0.18830881303660416 0.067377970678444,-0.18477590650225736 0.07653668647301795,-0.18079785862468867 0.08551101868605641,-0.17638425286967102 0.09427934736519956,-0.1715457220000544 0.10282054883864433,-0.16629392246050906 0.11111404660392044,-0.160641506296129 0.11913986089848666,-0.15460209067254738 0.1268786568327291,-0.1481902250709918 0.13431179096940368,-0.1414213562373095 0.1414213562373095,0.8585786437626906 1.1414213562373094,0.8656882090305963 1.1481902250709919,0.8731213431672709 1.1546020906725474,0.8808601391015134 1.160641506296129,0.8888859533960796 1.1662939224605091,0.8971794511613557 1.1715457220000545,0.9057206526348004 1.176384252869671,0.9144889813139436 1.1807978586246888,0.9234633135269821 1.1847759065022574,0.932622029321556 1.1883088130366042,0.9419430645491076 1.1913880671464416,0.9514039640193472 1.1940062506389089,0.9609819355967744 1.196157056080646,0.9706539051089277 1.1978353019929562,0.9803965719340879 1.1990369453344394,0.9901864651345164 1.1997590912410345,1 1.2,1.0098135348654835 1.1997590912410345,1.019603428065912 1.1990369453344394,1.0293460948910724 1.1978353019929562,1.0390180644032256 1.196157056080646,1.0485960359806528 1.1940062506389089,1.0580569354508924 1.1913880671464416,1.067377970678444 1.1883088130366042,1.076536686473018 1.1847759065022574,1.0855110186860564 1.1807978586246888,1.0942793473651995 1.176384252869671,1.1028205488386442 1.1715457220000545,1.1111140466039204 1.1662939224605091,1.1191398608984866 1.160641506296129,1.1268786568327291 1.1546020906725474,1.1343117909694036 1.1481902250709919,1.1414213562373094 1.1414213562373094,2.1414213562373097 0.1414213562373095,2.1546020906725474 0.1268786568327291,2.160641506296129 0.11913986089848666,2.166293922460509 0.11111404660392044,2.1715457220000545 0.10282054883864433,2.176384252869671 0.09427934736519956,2.180797858624689 0.08551101868605641,2.1847759065022574 0.07653668647301795,2.188308813036604 0.067377970678444,2.1913880671464416 0.058056935450892466,2.1940062506389086 0.048596035980652805,2.1961570560806463 0.03901806440322565,2.1978353019929564 0.029346094891072344,2.1990369453344396 0.01960342806591213,2.1997590912410345 0.009813534865483614,2.2 0,2.1997590912410345 -0.009813534865483614,2.1990369453344396 -0.01960342806591213,2.1978353019929564 -0.029346094891072344,2.1961570560806463 -0.03901806440322565,2.1940062506389086 -0.048596035980652805,2.1913880671464416 -0.058056935450892466,2.188308813036604 -0.067377970678444,2.1847759065022574 -0.07653668647301795,2.180797858624689 -0.08551101868605641,2.176384252869671 -0.09427934736519956,2.1715457220000545 -0.10282054883864433,2.166293922460509 -0.11111404660392044,2.160641506296129 -0.11913986089848666,2.1546020906725474 -0.1268786568327291,2.1481902250709917 -0.13431179096940368,2.1414213562373097 -0.1414213562373095,2.1343117909694036 -0.1481902250709918,2.126878656832729 -0.15460209067254738,2.119139860898487 -0.160641506296129,2.1111140466039204 -0.16629392246050906,2.102820548838644 -0.1715457220000544,2.0942793473651995 -0.17638425286967102,2.0855110186860566 -0.18079785862468867,2.076536686473018 -0.18477590650225736,2.067377970678444 -0.18830881303660416,2.0580569354508924 -0.19138806714644174,2.048596035980653 -0.1940062506389088,2.039018064403226 -0.1961570560806461,2.029346094891072 -0.19783530199295618,2.019603428065912 -0.19903694533443939,2.0098135348654838 -0.19975909124103447,2 -0.19999999999999998,1.9901864651345165 -0.19975909124103447,1.980396571934088 -0.19903694533443939,1.9706539051089276 -0.19783530199295618,1.9609819355967744 -0.1961570560806461,1.9514039640193472 -0.1940062506389088,1.9419430645491076 -0.19138806714644174,1.932622029321556 -0.18830881303660416,1.923463313526982 -0.18477590650225736,1.9144889813139436 -0.18079785862468867,1.9057206526348005 -0.17638425286967102,1.8971794511613558 -0.1715457220000544,1.8888859533960796 -0.16629392246050906,1.8808601391015134 -0.160641506296129,1.8731213431672709 -0.15460209067254738,1.8656882090305964 -0.1481902250709918,1.8585786437626906 -0.1414213562373095,1 0.7171572875253811,0.1414213562373095 -0.1414213562373095,0.1268786568327291 -0.15460209067254738,0.11913986089848666 -0.160641506296129,0.11111404660392044 -0.16629392246050906,0.10282054883864433 -0.1715457220000544,0.09427934736519956 -0.17638425286967102,0.08551101868605641 -0.18079785862468867,0.07653668647301795 -0.18477590650225736,0.067377970678444 -0.18830881303660416,0.058056935450892466 -0.19138806714644174,0.048596035980652805 -0.1940062506389088,0.03901806440322565 -0.1961570560806461,0.029346094891072344 -0.19783530199295618,0.01960342806591213 -0.19903694533443939,0.009813534865483614 -0.19975909124103447,0 -0.19999999999999998))

相关函数

以下是几个与 MairaDB ST_Buffer() 相关的几个函数:

  • MariaDB ST_Intersection() 函数用来计算两个几何图形的交集。
  • MariaDB ST_Union() 函数用来计算两个几何图形的并集。
  • MariaDB ST_Difference() 函数用来计算第一个几何图形减去第二个几何图形的部分。
  • MariaDB ST_SymDifference() 函数用来计算两个几何图形的对称差集。

结论

MariaDB ST_Buffer() 函数提供了一种方便创建几何图形缓冲区的方法。通过指定不同的缓冲宽度和策略,可以满足各种场景的需求。利用这个函数,您可以对空间数据进行各种几何运算和分析。本文介绍的示例涵盖了点、线、面和集合类型几何图形,希望对您使用这个强大的函数有所帮助。