liveBook · Manning

PostGIS

As explained in Just Use Postgres!

PostGIS is a PostgreSQL extension that adds storage, indexing, and querying capabilities for geospatial data.

PostGIS extends PostgreSQL into a geospatial database. It adds geometry and geography types, spatial functions, indexing support, and integrations for geodata visualization and processing. section 10.1.2 section 10.7

Choose the spatial model according to the calculation:

  • Use geometry for Cartesian calculations on a Euclidean plane. Define a subtype such as Point, LineString, or Polygon and specify a coordinate system, such as Web Mercator SRID 3857. section 10.1.2
  • Use geography for calculations that account for Earth’s curvature through a spherical model; its default coordinate system is WGS 84. section 10.1.2
  • Query and process spatial data with functions such as ST_Length and ST_Intersects, then optimize supported operations with GiST, SP-GiST, or BRIN indexes. section 10.5.3 section 10.7

Finding major roads in Miami

A spatial query first selects Miami’s boundary polygon, joins it to roads that intersect that boundary, filters for primary and secondary roads, and ranks results by length: WITH miami AS ( SELECT way AS boundaries FROM florida.planet_osm_polygon WHERE name = 'Miami' AND place = 'city' ) SELECT l.name, l.highway, ST_NPoints(l.way) AS points_number, round(ST_Length(l.way)) AS len_meters, round(ST_Length(l.way) * 3.28084) AS len_feet FROM florida.planet_osm_line l JOIN miami m ON ST_Intersects(l.way, m.boundaries) WHERE l.highway IN ('primary', 'secondary') ORDER BY len_meters DESC LIMIT 10; The output includes road names, classifications, point counts, and lengths; Southwest 22nd Avenue appears first at 1,422 meters. section 10.5.3

Listing 10.1 Starting Postgres with PostGIS on Unix with amd64
docker volume create postgres-postgis-volume

docker run --name postgres-postgis \
    -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password \
    -p 5432:5432 \
    -v postgres-postgis-volume:/var/lib/postgresql/data \
    -d postgis/postgis:17-3.5

Run a PostGIS-enabled PostgreSQL 17 container with Docker, persistent storage, port 5432, and the postgis/postgis:17-3.5 image. Apple Silicon requires amd64 emulation in the supplied command. section 10.2

How Just Use Postgres! covers it

How deep it goes
Defines geometry and geography, demonstrates spatial queries, and stops short of comprehensive function coverage.
Examples
Uses Web Mercator points, Florida OpenStreetMap roads, and Miami boundary intersections.
What you can do after
Apply it via SQL and Docker to store points and query roads by spatial intersection and length.
How it connects
Connects PostgreSQL with OGC standards, coordinate systems, spatial functions, and GiST, SP-GiST, and BRIN indexes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.