Skip to content

Geometry Processing

Idan Sheinberg edited this page Dec 16, 2022 · 5 revisions

ST_Buffer

Buffer only variant

Description: Computes a a POLYGON or MULTIPOLYGON that represents all points whose distance from a geometry is less than or equal to a given distance.
Input Argument #1: Geometry (WKB)
Input Argument #2: Buffer Distance (double)
Output: The buffered geometry (WKB)

SELECT ST_AsText(
  ST_Buffer(
    ST_GeomFromText(
      'POINT(100 90)'
    ),1
  )
)

Result

POLYGON ((101 90, 100.98078528040323 89.80490967798387, 100.9238795325113 89.61731656763492, 100.83146961230254 89.44442976698039, 100.70710678118655 89.29289321881345, 100.55557023301961 89.16853038769746, 100.38268343236508 89.0761204674887, 100.19509032201613 89.01921471959677, 100 89, 99.80490967798387 89.01921471959677, 99.61731656763492 89.0761204674887, 99.44442976698039 89.16853038769746, 99.29289321881345 89.29289321881345, 99.16853038769746 89.44442976698039, 99.0761204674887 89.61731656763492, 99.01921471959677 89.80490967798387, 99 90, 99.01921471959677 90.19509032201613, 99.0761204674887 90.38268343236508, 99.16853038769746 90.55557023301961, 99.29289321881345 90.70710678118655, 99.44442976698039 90.83146961230254, 99.61731656763492 90.9238795325113, 99.80490967798387 90.98078528040323, 100 91, 100.19509032201613 90.98078528040323, 100.38268343236508 90.9238795325113, 100.55557023301961 90.83146961230254, 100.70710678118655 90.70710678118655, 100.83146961230254 90.55557023301961, 100.9238795325113 90.38268343236508, 100.98078528040323 90.19509032201613, 101 90))

Note #1
A negative distance shrinks the geometry rather than expanding it.
A negative distance may shrink a polygon completely, in which case POLYGON EMPTY is returned.
For points and lines negative distances always return empty results.

Parameter string variant

ST_Centroid

Description: Computes a point which is the geometric center of mass of a geometry. Input Argument #1: Geometry (WKB)
Output: A POINT representing the geometric mean/center of mass for the input geometry.

SELECT ST_AsText(
  ST_Centroid(
    ST_GeomFromText(
      'MULTIPOLYGON(((1 0,2 1,2 0, 1 0)),((-1 -1,2 2,-1 2,-1 -1)))'
    )
  )
);

Result

POINT (0.1666666666666667 0.9333333333333333)

Note #1
For [MULTI]POINT input, the centroid is the arithmetic mean of the input coordinates.
For [MULTI]LINESTRING input, the centroid is computed using the weighted length of each line segment.
For [MULTI]POLYGON input, the centroid is computed in terms of area.
For an empty geometry, an empty GEOMETRYCOLLECTION is returned.
For NULL, NULL is returned.

ST_ConvexHull

Description: Computes the convex hull of a geometry
Input Argument #1: Geometry (WKB)
Output: The smallest convex geometry that encloses all geometries in the input (WKB)

SELECT ST_AsText(
  ST_ConvexHull(
    ST_Collect(
      ST_GeomFromText(
        'MULTILINESTRING((100 190,10 8),(150 10, 20 30))'
      ),
      ST_GeomFromText(
        'MULTIPOINT(50 5, 150 30, 50 10, 10 10)'
      )
    )
  ) 
);

Result

POLYGON((50 5,10 8,10 10,100 190,150 30,150 10,50 5))

Note #1
This is not an aggregate function. To compute the convex hull of a set of geometries, use ST_Collect
to aggregate them into a geometry collection (e.g. ST_ConvexHull(ST_Collect(geom_column))

Note #2
Though the sample demonstrates ST_Collect 2 arguments variant, the probable use-case would be using ST_Collect aggregating variant