# Geometry Processing of PostGIS_ 1 ST_Buffer&&ST_BuildArea

Posted by uidzer0 on Wed, 20 Oct 2021 20:52:27 +0200

## 1. ST_Buffer

ST_Buffer - (T) returns a geometry that covers all points within a given distance from the input geometry.

### 1.1. Summary

```geometry ST_Buffer(geometry g1, float radius_of_buffer);
geometry ST_Buffer(geometry g1, float radius_of_buffer, integer num_seg_quarter_circle);
geometry ST_Buffer(geometry g1, float radius_of_buffer, text buffer_style_parameters);
geography ST_Buffer(geography g1, float radius_of_buffer, integer num_seg_quarter_circle);
geography ST_Buffer(geography g1, float radius_of_buffer, text buffer_style_parameters);
```

### 1.2. Description

Returns a geometric / geographic value representing all points whose distance from this geometric / geographic value is less than or equal to the distance.
Geometry: Calculation in the spatial reference system of geometry. In 1.5, different end cap and inclined cap settings are introduced to control the shape.

• Negative radius: for polygons, you can use a negative radius, which shrinks the polygon instead of expanding it.
• Geography: for geography, this is actually a thin package of geometric implementation. It first determines the best SRID of the bounding box suitable for geographical objects (conducive to UTM, Lambert Azimuthal Equal Area (LAEA) North / South Pole, and falls back to Mercator in the worst case), then references the buffer in the plane space, and converts it back to WGS84 geography.

For geography, this may not be as expected if the object is large enough, it is between two UTM areas or across the international date change line enhancement: 2.5.0 ST_Buffer geometry supports enhancements while allowing buffer specifications to work together = | about availability: 1.5 - ST_Buffer enhancement, supporting different end descriptions and connection types. These are useful, for example, to convert a road line string to a polygonal road with flat or square edges rather than rounded edges. A thin wrapper with geographic information added. GEOS > = 3.2 is required to take advantage of advanced geometry features.

The optional third parameter (currently only applicable to geometry) can specify the number of segments used to approximate the quarter circle (integer case, the default is 8) or a blank separated list of key value pairs (string case), so as to adjust the operation as follows:

• quad_segs = #: the number of segments used to approximate a quarter circle (8 by default).
• Endcap = round|flat|square: endcap style (the default is "round", which requires a value of GEOS-3.2 or higher). "But" can also be used as a synonym for "flat".
• join=round|mitre|bevel: connection style (the default is "round", which requires a value of GEOS-3.2 or higher). " miter 'is also considered a synonym for' mitre '.
• mitre_limit = #. #: miter ratio limit (only affects miter connection style). " miter_limit 'can also be used as' mitre'_ A synonym for 'limit'.
• side=both|left|right: "left" or "right" performs one-sided buffering on the geometry, and the buffered side is relative to the direction of the line. This relates only to LINESTRING geometry and does not affect point or polygon geometry. By default, end caps are square.

The unit of radius is measured in the unit of spatial reference system.
The input parameters can be POINTS, MULTIPOINTS, LINESTRINGS, MULTILINESTRINGS, POLYGONS, MULTIPOLYGONS and GeometryCollections

This function ignores the third dimension (z) and always gives a 2d buffer even when rendering 3d geometry.
People often make the mistake of using this function to do radius search. Creating buffers for radius searches is slow and pointless. Use ST_DWithin instead.

### 1.3. Example

```SELECT ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50, 'quad_segs=8');
```

```SELECT ST_Buffer(ST_GeomFromText('POINT(100 90)'),50, 'quad_segs=2');
```

1. endcap=round join=round (default)
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'endcap=round join=round');
```

1. endcap=square
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'endcap=square join=round');
```

1. join=bevel
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'join=bevel');
```

1. join=mitre mitre_limit=5.0 (default mitre limit)
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'join=mitre mitre_limit=5.0');

```

1. side=left
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'side=left');
```

1. side=right
```SELECT ST_Buffer(ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10, 'side=right');
```

1. right-hand-winding, polygon boundary side=left
```SELECT ST_Buffer(ST_ForceRHR(ST_Boundary(ST_GeomFromText('POLYGON ((50 50, 50 150, 150 150, 150 50, 50 50))'))),), 20, 'side=left');
```

1. right-hand-winding, polygon boundary side=right
```SELECT ST_Buffer(ST_ForceRHR(ST_Boundary(ST_GeomFromText('POLYGON ((50 50, 50 150, 150 150, 150 50, 50 50))'))), 20,'side=right')
```

1. Specific use
• The buffer point approximates a circle
• Buffer point forcing approximation (see Figure)
• Every quarter circle has 2 points and is a polygon with 8 sides (see Figure)
```SELECT ST_NPoints(ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50)) As
promisingcircle_pcount,ST_NPoints(ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50, 2)) As lamecircle_pcount;
```
promisingcircle_pcountlamecircle_pcount
339
• A lighter but more lame circle
• Only 2 points in every quarter circle is an octagon
• Below is a 100 meter octagon
Note that the coordinates are in NAD 83 long lat, we convert them to mass state plane meters, and then measure them in meters;
```SELECT ST_AsText(ST_Buffer(ST_Transform(ST_SetSRID(ST_MakePoint(-71.063526, 42.35785),4269), 26986),100,2)) As octagon;
```
octagon
POLYGON((236057.59057465 900908.759918696,236028.301252769 900838.049240578,235 957.59057465 900808.759918696,235886.879896532 900838.049240578,235857.59057465 900908.759918696,235886.879896532 900979.470596815,235957.59057465 901008.759918 696,236028.301252769 900979.470596815,236057.59057465 900908.759918696))

## 2. ST_BuildArea

ST_BuildArea - creates an area geometry consisting of the component lines of a given geometry

### 2.1. Summary

```geometry ST_BuildArea(geometry A);
```

### 2.2. Description

Creates an area geometry consisting of the constituent lines of a given geometry. The return type can be polygon or multipolygon, depending on the input. NULL if the input line does not form a polygon. The inputs can be LINESTRINGS, MULTILINESTRINGS, polygon, multipolygon, and GeometryCollections.

• This function assumes that all internal geometry represents holes
The input line must be the correct point for this function to work properly

### 2.3. Example

• This will create a donut
```SELECT ST_BuildArea(ST_Collect(smallc,bigc)) FROM (SELECTST_Buffer(ST_GeomFromText('POINT(100 90)'), 25) As smallc,ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As bigc) As foo;

```

• This will create a gaping hole inside the circle with prongs sticking out
```SELECT ST_BuildArea(ST_Collect(line,circle))
FROM (SELECTST_Buffer(ST_MakeLine(ST_MakePoint(10, 10),ST_MakePoint(190, 190)),5) As line,ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As circle) As foo;
```
```--This creates the same big hole