Book Image

Geospatial Analysis with SQL

By : Bonny P McClain
Book Image

Geospatial Analysis with SQL

By: Bonny P McClain

Overview of this book

Geospatial analysis is industry agnostic and a powerful tool for answering location questions. Combined with the power of SQL, developers and analysts worldwide rely on database integration to solve real-world spatial problems. This book introduces skills to help you detect and quantify patterns in datasets through data exploration, visualization, data engineering, and the application of analysis and spatial techniques. You will begin by exploring the fundamentals of geospatial analysis where you’ll learn about the importance of geospatial analysis and how location information enhances data exploration. Walter Tobler’s second law of geography states, “the phenomenon external to a geographic area of interest affects what goes on inside.” This quote will be the framework of the geospatial questions we will explore. You’ll then observe the framework of geospatial analysis using SQL while learning to create spatial databases and SQL queries and functions. By the end of this book, you will have an expanded toolbox of analytic skills such as PostGIS and QGIS to explore data questions and analysis of spatial information.
Table of Contents (13 chapters)
Free Chapter
1
Section 1: Getting Started with Geospatial Analytics
6
Section 2: SQL for Spatial Analytics

Creating functions in SQL

Creating functions can be complex but like anything else, it becomes easier with practice. Let’s look through a few of the steps in detail:

  1. Begin with CREATE OR REPLACE FUNCTION. This is where you name your function.
  2. Next, define the function parameter(s) within parentheses.
  3. Insert the RETURNS TABLE function, followed by the data type. This is a text option as the actual function will add the variable when it is run.
  4. Now, select the language property of SQL as PostgreSQL since it is not limited by a single procedural language.
  5. The actual query will now be included inside $$ query $$. These are called dollar-quoted string constants ($$).

We need to create the function (as shown in the following code) and then pass to the function what we want it to do.

First, we want to select boundary_protected_area, which is where highways intersect. We are passing text (x text), which counts as one variable. The text is entered when...