This post was originally published in 2020 and has been updated in 2023.

Mastering Geographical Information Systems, better known simply as GIS, can be considered in some ways as a rite of passage. The complexities and challenges involved in learning, which are ostensibly non-IT concepts, are steep. However, as they say, “There’s more than one way to skin a cat.” I’d like to share with you one way to tackle this challenge.

Let me introduce you to PostGIS.

What is PostGIS?

PostGIS is a PostgreSQL extension that adds GIS capabilities to this RDBMS. Its popularity stems not only from being “free” but because it’s considered to be among the leading GIS implementations in the world today. Virtually every major front-end application provides the hooks for a PostGIS, PostgreSQL-enabled back-end.

The PostGIS project, which is BSD licensed, began back in 2001. It turns our vanilla-flavored postgres into a spatial database and includes spatial datatypes (geometry, geography), spatial indexes (r-tree, quad-tree, kd-tree), and spatial functions.

Try Now: Get Percona Support for PostgreSQL

Essential PostGIS Tools and Applications

Working with GIS normally requires several layers of technology of Geo-Spatial Software, for example:

Boundless Server (formerly the OpenGeo Suite)

  • PostGIS – Spatially enabled object-relational database.
  • GeoServer – Software server for loading and sharing geospatial data
  • GeoWebCache – Tile cache server that accelerates the serving of maps
  • Composer – Web-based map configuration and styling utility
  • WPS Builder – Web-based graphical constructor for generating server-side processes
  • QuickView – Web application for composing and styling web maps

Note: Boundless Server GitHub repository

Advantages of Using PostGIS for Better Performance

PostGIS offers a solution to provide geospatial data capabilities in PostgreSQL for indexing, storing, and querying geographical data.

Spatial Indexing

Spatial indexing accelerates the querying and retrieval of geospatial data in PostgreSQL, improving response times for applications and analytics. It uses various spatial indexing techniques, such as R-tree and GiST (Generalized Search Tree), to organize data based on their spatial relationships, allowing the database to quickly narrow down the search space.

Geospatial Functions and Queries

PostGIS offers a wide array of geospatial functions for PostgreSQL, allowing users to perform complex spatial operations such as distance and area computations, geometric alterations, and buffering. Optimized spatial queries, like finding nearby points or intersecting polygons, use these functions to retrieve geospatial data efficiently.

Spatial Data Types

PostGIS supports spatial data types like Point, LineString, and Polygon in 2D and 3D. These allow efficient spatial data storage and retrieval due to their optimized indexing mechanisms. They enable spatial indexing, enabling faster query processing by organizing data based on geometric properties.

Querying The Spatial Database

It’s amazing the kinds of answers one can get by a single, well-composed, and yet standard query by asking such simple questions as:

  • How far is it from here to there?
  • What’s the closest point between two meandering streets?
  • Is a street found in a certain zip code?
  • How many homes are susceptible to flooding?

Here are a few example queries using a PostGIS-powered database. Refer here for more community-related shapefiles for Kamloops British Columbia:

Learn More: Find out how to improve PostgreSQL query performance insights with pg_stat_monitor

Learn More: How to tune PostgreSQL database parameters to optimize performance

PostGIS Use Cases

PostGIS extends PostgreSQL’s capabilities to handle geospatial data for solutions in real-world scenarios. From mapping to visualizations to logistics, PostGIS helps users to make informed decisions based on spatial insights.

Location-Based Services

PostGIS plays a role in location-based applications by enabling spatial data storage and analysis. For instance, a grocery delivery service could utilize PostGIS to optimize delivery for certain areas. A query that finds available drivers within a certain distance, facilitated by PostGIS’s spatial indexing, would significantly enhance the platform’s responsiveness.

Geospatial Analytics

PostGIS utilizes geospatial data analysis by offering advanced tools for operations such as identifying the nearest points or calculating areas within polygons.  The functions are optimized through spatial indexing for quicker and better queries of complex geospatial analyses, improving overall performance.

Mapping and Visualization

PostGIS improves map rendering and visualization through efficient geospatial data handling. It allows for quickly retrieving map data to ensure smooth and responsive displays.

Related: Watch the PostgreSQL high-performance tuning and optimization webinar sessions

How to Install the PostGIS Extension for PostgreSQL

Basic Installation

The following installation instructions assume one is using PostgreSQL version 12 on Linux/CENTOS-7, although any major version of postgres and OS can be used.

Note: Although it is on the roadmap, the Percona Distribution for PostgreSQL does not currently include PostGIS. But you should be able to install the community PostGIS packages to it with the pertinent YUM/RPM package switches.

Complete Installation for PostGIS Extension

The following provides a more complete installation of all PostGIS capabilities. Refer to the PostGIS documentation for more information.

Working With Shapefiles

There are literally hundreds of terabytes available online. Ironically, the most precious data one can get is free to download because it’s been generated by governments from all over the world for the public good.

Now it’s time to get some data. The most common format is “shapefile.”

PostGIS includes these two command-line utilities:

Working With ZipCodes From The Us Census Data: Tiger Files

TIGER/Line files are a digital database of geographic features, such as roads, railroads, rivers, lakes, legal boundaries, census statistical boundaries, etc., covering the entire United States and are freely available here.

Once you’ve navigated to this website, you can download a multitude of fascinating pieces of data.

Example:

Step 1: Execute The Following As A Bash Script

Step 2: Download The 2019 ZipCode Shapefile

The shapefile is stored in a zip file. Unzip the entire archive before attempting an upload.

URL: https://www.census.gov/cgi-bin/geo/shapefiles/index.php

Once the upload into the postgres database is complete, this is what you’ll get:

Step 3: An Example Query

On The Road To Mastering GIS

There are a couple of things to keep in mind when querying PostGIS-powered databases:

  • Follow the PostGIS reference documentation closely: it’s easy to read, explains many of the ideas behind the function call, and provides many examples.
  • The GIS standard requires that functions be documented using mixed case.
  • GIS function calls, implemented in postgres, are written using lowercase.
  • For the adventurous, here’s a comprehensive and freely available dataset of shapefiles for the city of Kamloops, British Columbia.

Because so much of our modern big data insights depend upon raw GIS data, directly querying a GIS database empowers one to create even more powerful and precise insights. PostGIS is an excellent way for the DEV, DBA, and SRA to learn all things GIS.

Have Fun!

Get PostGIS Support From Percona

PostGIS is included in Percona Distribution for PostgreSQL, which provides the best and most critical enterprise components from the open source community in a single distribution. And with Percona support for PostGIS, you can reliably run spatial data on PostgreSQL in production without fear of vendor lock-in.

  • 24x7x365 consultative or hands-on support from PostgreSQL experts
  • Industry-best, 15-minute SLAs 
  • Simplified support and pricing
  • No restrictive commitments or lock-in 
  • Assistance on any environment – on-premises, cloud, or hybrid

 

Support for PostGIS

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments