What can you do with PostGIS?


Regina Obe

PostGIS In Action 3rd Edition available for Purchase

Correct pronounciation of PostGIS

Post-jihs

PostGIS is 20 years old and still charming

http://blog.cleverelephant.ca/2021/05/postgis-20-years.html

Why I started using PostGIS

I have land and projects. PostGIS ticked all the boxes.

PostGIS makes sense of data with location information

  • GPS data
  • Satellite data
  • Survey data about boundaries of land
  • Raster data (aerial imagery, pictures, thematic matrices like temperature, soil acidity, elevation)

PostGIS derives information using the key of space

  • Average sale price of an arbitrary area
  • Regions with highest traffic accidents
  • Income levels in your area
  • Distances from your area to closest high school, mall, elementary school, hospital
  • Approximate location of this spot based on postal address (using road network data)
  • Population impacted by outage

PostGIS is an extension to PostgreSQL and follows the SQL/MM standard

It is similar to below but unlike these, is an extension rather than part of Db proper

  • Oracle Spatial/Locator
  • SQL Server
  • MySQL / MariaDB

Where to get PostGIS

How you install extensions in PostGIS 3+

This is running in psql. If in pgAdmin just manually reconnect to your gisdb. Example is gisdb but do for any spatial databases you have.

CREATE DATABASE gisdb;
ALTER DATABASE gisdb SET search_path=public,postgis,tiger,contrib;
\c gisdb
CREATE SCHEMA IF NOT EXISTS postgis;
CREATE SCHEMA IF NOT EXISTS contrib;
CREATE EXTENSION postgis SCHEMA postgis; -- core includes geometry / geography
CREATE EXTENSION postgis_raster SCHEMA postgis; -- raster type and functions
CREATE EXTENSION postgis_sfcgal SCHEMA postgis; -- extended support for 3D geometries
CREATE EXTENSION postgis_topology; -- topogeometry relational view of spatial data
CREATE EXTENSION fuzzystrmatch SCHEMA contrib; -- needed by tiger geocoder
CREATE EXTENSION address_standardizer SCHEMA contrib; #for standardizing addresses
CREATE EXTENSION postgis_tiger_geocoder; -- geocoding and reverse geocoding using US TIGER data

How you upgrade

SELECT postgis_extensions_upgrade();

Check your installation

SELECT postgis_full_version();
screen
POSTGIS="3.1.2 3.1.2" [EXTENSION] 
PGSQL="130" GEOS="3.9.0-CAPI-1.14.0" SFCGAL="1.3.8" 
PROJ="7.1.1" 
GDAL="GDAL 3.2.0, released 2020/10/26" 
LIBXML="2.9.9" LIBJSON="0.12" LIBPROTOBUF="1.2.1" WAGYU="0.5.0 (Internal)" TOPOLOGY RASTER

PostGIS 3.0 enhanced GeoJSON support to now accept full features

very very old way of creating a feature collection - https://www.postgresonline.com/journal/archives/267-Creating-GeoJSON-Feature-Collections-with-JSON-and-PostGIS-functions.html (painful)

New way

SELECT json_build_object('type', 'FeatureCollection', 'features', 
    json_agg(ST_AsGeoJSON(r.*)::json) )
FROM recent_building_permits AS r;