DEV Community

Cover image for Understanding Polygons and `ST_` Functions in PostGIS
Hitesh
Hitesh

Posted on

Understanding Polygons and `ST_` Functions in PostGIS

PostGIS is a powerful spatial extension for PostgreSQL that allows you to store, query, and manipulate geographic objects. If you're working with maps, geometry, or location-based data, PostGIS is an essential tool. In this article, weโ€™ll explore what a polygon is in PostGIS, what the parameters in a polygon definition mean, and the role of ST_ functions in spatial queries.


๐Ÿงฑ What is a Polygon in PostGIS?

A polygon in PostGIS is a two-dimensional geometric object that represents an enclosed area defined by one or more rings:

  • The outer ring defines the exterior boundary.
  • Inner rings (optional) define holes within the polygon.

Each ring is made up of at least four coordinate points, and the first and last point must be the same to form a closed loop.


๐Ÿ“ Example: Creating a Polygon

Consider the following SQL query:

SELECT ST_GeomFromText('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))');
Enter fullscreen mode Exit fullscreen mode

This uses WKT (Well-Known Text) format to define a simple square polygon.

Breaking down the parameters:

POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))
Enter fullscreen mode Exit fullscreen mode
Point Coordinates Description
1 0 0 Bottom-left corner (starting point)
2 0 5 Top-left corner
3 5 5 Top-right corner
4 5 0 Bottom-right corner
5 0 0 Closes the polygon (must match point 1)

This creates a square with sides of 5 units.


๐Ÿงญ What Does ST_ Mean in PostGIS?

The ST_ prefix stands for "Spatial Type" and is used for all spatial functions in PostGIS. It follows standards defined by the Open Geospatial Consortium (OGC).

Why use ST_?

  • To clearly distinguish spatial functions from regular SQL functions.
  • To comply with OGC naming conventions.

๐Ÿ› ๏ธ Common ST_ Functions

Function Purpose
ST_GeomFromText() Converts WKT into a PostGIS geometry
ST_AsText() Converts geometry back to WKT
ST_Area() Calculates the area of a polygon
ST_Length() Measures the length of a linestring
ST_Intersects() Checks if two geometries intersect
ST_Contains() Tests whether one geometry completely contains another
ST_Buffer() Expands a geometry outward by a given distance
ST_Distance() Measures the distance between two geometries

๐Ÿงช Example Usage

-- Create a polygon from WKT
SELECT ST_GeomFromText('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))');

-- Compute the area
SELECT ST_Area(ST_GeomFromText('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'));
Enter fullscreen mode Exit fullscreen mode

This example returns 25, the area of the 5ร—5 square.


๐Ÿ“ฆ Summary

  • A polygon in PostGIS is a closed shape defined by coordinate points.
  • The parameters in 'POLYGON((x y, x y, ..., x y))' represent the vertices.
  • The ST_ prefix identifies spatial functions that handle geometry or geography types.

Whether you're building map-based applications or analyzing spatial data, understanding how polygons and ST_ functions work is key to leveraging the full power of PostGIS.

Top comments (1)

Collapse
 
jjbb profile image
Jason Burkes

Really clear explanation, thanks!