What is Geometry in PostGIS?
In PostGIS, geometry is the backbone of spatial data. Every spatial feature—whether it's a city point, a river line, or a land parcel polygon—is stored as a geometry object. Geometries are stored in a special column type called geometry
, and can represent simple or complex shapes.
PostGIS supports a rich set of geometry types compliant with the OGC Simple Features Specification, enabling you to model virtually any spatial feature you can imagine.
Points – The Simplest Geometry
Points represent a single location in space defined by X and Y coordinates. Optionally, a point can also have Z (elevation) and M (measure) values.
CREATE TABLE landmarks (
id SERIAL PRIMARY KEY,
name TEXT,
geom GEOMETRY(Point, 4326)
);
INSERT INTO landmarks (name, geom) VALUES
('City Hall', ST_GeomFromText('POINT(77.59 12.97)', 4326)),
('Library', ST_GeomFromText('POINT(77.61 12.95)', 4326)),
('Hospital', ST_GeomFromText('POINT(77.58 12.99)', 4326));
Point Type | Description | Example |
---|---|---|
2D Point | X, Y coordinates | POINT(77.59 12.97) |
3D Point (Z) | X, Y, Z coordinates | POINT Z(77.59 12.97 250) |
Measured Point (M) | X, Y, M coordinates | POINT M(77.59 12.97 100) |
4D Point (ZM) | X, Y, Z, M coordinates | POINT ZM(77.59 12.97 250 100) |
Common Use Cases:
- Representing locations like schools, offices, fire stations
- Mapping sensors, wells, or weather stations
- GPS tracking of vehicles or people
- Store locations and points of interest
LineStrings – Connecting Points
A LineString is a linear geometry made of a series of connected points. It represents features with a direction or length.
CREATE TABLE roads (
id SERIAL PRIMARY KEY,
name TEXT,
geom GEOMETRY(LineString, 4326)
);
INSERT INTO roads (name, geom) VALUES
('Main Road', ST_GeomFromText('LINESTRING(77.58 12.96, 77.60 12.98)', 4326)),
('Highway 1', ST_GeomFromText('LINESTRING(77.55 12.95, 77.58 12.96, 77.60 12.98, 77.62 13.00)', 4326));
LineString Feature | Description | Example Usage |
---|---|---|
Simple LineString | Series of connected points | Roads, paths, boundaries |
3D LineString | LineString with elevation | Flight paths, hiking trails |
Measured LineString | LineString with measures | Mile markers, time stamps |
Common Use Cases:
- Mapping roads, rivers, pipelines, or flight paths
- Measuring distances and path optimization
- Visualizing movement or connectivity between points
- Network analysis and routing
Polygons – Enclosed Areas
Polygons are geometries with a closed ring of coordinates. They define an area with boundaries, optionally with holes.
CREATE TABLE parks (
id SERIAL PRIMARY KEY,
name TEXT,
geom GEOMETRY(Polygon, 4326)
);
INSERT INTO parks (name, geom) VALUES
('Central Park', ST_GeomFromText('POLYGON((77.58 12.96, 77.60 12.96, 77.60 12.98, 77.58 12.98, 77.58 12.96))', 4326)),
('Lake Park', ST_GeomFromText('POLYGON((77.55 12.93, 77.57 12.93, 77.57 12.95, 77.55 12.95, 77.55 12.93))', 4326));
Polygon Type | Description | Structure |
---|---|---|
Simple Polygon | Single outer ring | One closed ring of coordinates |
Polygon with Holes | Outer ring with inner holes | Outer ring + one or more inner rings |
3D Polygon | Polygon with elevation | All coordinates have Z values |
Common Use Cases:
- Defining land parcels, buildings, zones, and habitats
- Calculating area and perimeter
- Overlay analysis in urban and environmental planning
- Administrative boundaries and districts
Multi-Geometries – Collections
Collections group multiple geometries of the same or different type:
-- MultiPoint example
SELECT ST_GeomFromText('MULTIPOINT((77.59 12.97), (77.61 12.95), (77.58 12.99))');
-- MultiLineString example
SELECT ST_GeomFromText('MULTILINESTRING((77.58 12.96, 77.60 12.98), (77.55 12.95, 77.57 12.97))');
-- MultiPolygon example
SELECT ST_GeomFromText('MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((2 2,2 3,3 3,3 2,2 2)))');
-- GeometryCollection (mixed types)
SELECT ST_GeomFromText('GEOMETRYCOLLECTION(POINT(77.59 12.97), LINESTRING(77.58 12.96, 77.60 12.98))');
Collection Type | Contains | Use Case |
---|---|---|
MultiPoint | Multiple points | Chain stores, sensor networks |
MultiLineString | Multiple linestrings | Disconnected road segments |
MultiPolygon | Multiple polygons | Countries with islands |
GeometryCollection | Mixed geometry types | Complex spatial features |
Common Use Cases:
- Country borders with multiple islands
- Roads with multiple disconnected segments
- Aggregated results from spatial queries
- Complex geographic features
3D Geometry – The Z Coordinate
Z coordinates allow geometries to model 3D space by adding elevation or height information:
-- 3D Point with elevation
SELECT ST_GeomFromText('POINT Z(77.59 12.97 250)', 4326);
-- 3D LineString (flight path)
SELECT ST_GeomFromText('LINESTRING Z(77.58 12.96 100, 77.60 12.98 150, 77.62 13.00 200)', 4326);
-- 3D Polygon (building footprint with height)
SELECT ST_GeomFromText('POLYGON Z((77.58 12.96 0, 77.60 12.96 0, 77.60 12.98 0, 77.58 12.98 0, 77.58 12.96 0))', 4326);
Common Use Cases:
- 3D city models and building height data
- Geological surveys and topographic models
- Drone and aircraft flight path analysis
- Underground utility mapping
Measured Geometry – The M Coordinate
The M value is a measure used for storing extra information along a geometry:
-- Point with measure
SELECT ST_GeomFromText('POINT M(77.59 12.97 100)', 4326);
-- LineString with measures (linear referencing)
SELECT ST_GeomFromText('LINESTRING M(77.58 12.96 0, 77.59 12.97 1000, 77.60 12.98 2000)', 4326);
Common Use Cases:
- Linear referencing systems for roads and rivers
- Store event times along a route (bus stops)
- Distance measurements along paths
- Combine with Z to support 4D geometries
Advanced Geometry Types
Polyhedral Surfaces and TINs
These represent 3D surfaces by combining multiple triangle faces:
-- Triangulated Irregular Network (TIN)
SELECT ST_GeomFromText('TIN(((0 0 0,0 1 0,1 0 1,0 0 0)), ((0 1 0,1 1 1,1 0 1,0 1 0)))');
-- Polyhedral Surface
SELECT ST_GeomFromText('POLYHEDRALSURFACE(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)))');
Curved Geometries
PostGIS supports curved geometries for features that require arc segments:
-- Circular String
SELECT ST_GeomFromText('CIRCULARSTRING(0 0, 1 1, 1 0)', 4326);
-- Compound Curve
SELECT ST_GeomFromText('COMPOUNDCURVE(CIRCULARSTRING(0 0, 1 1, 1 0), (1 0, 0 0))', 4326);
-- Curve Polygon
SELECT ST_GeomFromText('CURVEPOLYGON(CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0))', 4326);
Curved Type | Description | Use Case |
---|---|---|
CircularString | Arc defined by 3+ points | Roundabouts, curved roads |
CompoundCurve | Mix of straight and curved segments | Complex road geometries |
CurvePolygon | Polygon with curved boundaries | Natural boundaries, CAD features |
Spatial Metadata – Catalog Tables
PostGIS provides catalog tables to store metadata about geometry columns:
-- View geometry column information
SELECT
f_table_name,
f_geometry_column,
type,
srid,
coord_dimension
FROM geometry_columns;
-- View geography column information
SELECT
f_table_name,
f_geography_column,
type,
srid,
coord_dimension
FROM geography_columns;
Catalog Table | Purpose | Key Information |
---|---|---|
geometry_columns | Geometry column metadata | Table name, column name, type, SRID |
geography_columns | Geography column metadata | Table name, column name, type, SRID |
spatial_ref_sys | Coordinate reference systems | SRID, projection definitions |
Best Practices
- Choose the right geometry type: Use the simplest type that meets your needs
- Specify SRID consistently: Always define the spatial reference system
- Validate geometries: Use ST_IsValid() to check geometry validity
- Index spatial columns: Create spatial indexes for better performance
- Consider data precision: Balance precision with storage requirements
Real-World Applications
- Urban Planning: Model buildings, roads, and zoning areas
- Environmental Analysis: Track habitats, watersheds, and protected areas
- Transportation: Route optimization and network analysis
- Utilities: Map infrastructure like power lines and pipelines
- Emergency Services: Dispatch optimization and coverage analysis