GEOG 868
Spatial Database Management

Introduction to Spatial Select Queries

PrintPrint

What sets spatial databases apart from their non-spatial counterparts is their support for answering geometric and topological questions. Let's have a look at some simple examples to demonstrate. We'll continue working with the states table we created in the last section.

  1. Return to the Query dialog in pgAdmin and execute the following query:

    SELECT name, ST_Centroid(geom) AS centroid
    FROM states
    WHERE sub_region = 'Soda';

    The obvious difference between this and our earlier queries is that it calls upon a function called ST_Centroid(). Like the functions we worked with in Lesson 1, the ST_Centroid() function accepts inputs and returns outputs. Here we supply the geom column as an input to the function and it returns the geometric centers of the shapes stored in that column.

    You've probably noticed that the output from ST_Centroid() is not human friendly. It contains the coordinates of a point in the coordinate system of the input column, but expressed in hexadecimal notation. To display the coordinate values in a more readable form, we can nest the call to the ST_Centroid() function within a call to a function named ST_AsText().
  2. Modify the query as follows, then execute:

    SELECT name, ST_AsText(ST_Centroid(geom)) AS centroid
    FROM states
    WHERE sub_region = 'Soda';

    In this version of the query the hexadecimal value returned by ST_Centroid() is immediately passed to the ST_AsText() function, which returns a value formatted for human consumption (with longitude listed first, latitude second).

    Now, let's try retrieving the areas of the states using ST_Area().
  3. Modify the query as follows then execute:

    SELECT name, ST_AsText(ST_Centroid(geom)) AS centroid, ST_Area(geom) AS area
    FROM states
    WHERE sub_region = 'Soda';
    In the area column take note of the values returned by ST_Area(). They are in the units of the input geometry, squared. Recall that the Lesson 3 shapefiles are in latitude/longitude coordinates which means the area values we're seeing are in square degrees. Hopefully you recognize that this is a poor way to compute area since a square degree represents a different area depending on the part of the globe you're dealing with. The ST_Transform() function exists exactly for situations like this. It takes a geometry (in whatever spatial reference) as input and re-projects it into some other spatial reference.
  4. Modify the query as follows, then execute:

    SELECT name, ST_AsText(ST_Centroid(geom)) AS centroid, ST_Area(ST_Transform(geom,2163)) AS area
    FROM states
    WHERE sub_region = 'Soda';
    Take note of the values now displayed in the area column. In this version of the query the ST_Transform() function is first used to re-project the geometry into the spatial reference 2163 before ST_Area() is called. That spatial reference is an equal-area projection in meters that is suitable for the continental US. Don't worry, we'll discuss how you'd find that information later in this lesson.

    Note: ST_Transform() re-projects input geometries in memory only, the input geometries stored in the table remain in the same spatial reference they had before.

We'll spend much more time discussing the spatial functions that are available in PostGIS later. Right now let's go over the geometry types that are supported.