Analyzing Geospatial Data with BigQuery GIS

Analyzing Geospatial Data with BigQuery GIS

Location! Location!! Location!!! In today's world of data science and machine learning, the answer to many critical business decisions revolve around location data. A significant portion of business data that is collected today contains one or more types of location information.

For example, you may record the latitude and longitude of your delivery vehicles or packages over time. You may also record customer transactions and join the data to another table with store location data. You can use this type of location data to determine when a package is likely to arrive or to determine which customers should receive a mailer for a particular store location.

In general terms, datasets that contain location information are referred to as geospatial data. Geospatial data includes different types of location information such as GPS coordinates, zip codes, addresses, etc.

What is BigQuery?

BigQuery is Google Cloud's fully managed, petabyte-scale, and cost-effective analytics data warehouse that lets you run analytics over vast amounts of data in near real-time. BigQuery is serverless which means there's no infrastructure to set up or manage, letting you focus on finding meaningful insights from your data.

Googles BigQuery has amazing features and functionalities that make it outstanding even when compared with a top data warehouse tool like AWS Redshift. Aside from BigQuery GIS which provides some GIS capabilities on working with large datasets, BigQuey also includes a machine learning feature (BigQuery ML) which makes it easy to develop machine learning models on large datasets using just standard SQL.

Introduction to BigQuery GIS

Google's BigQuery is a data warehouse tool with first-class support for GIS functions and data types. BigQuery GIS lets you analyze and visualize geospatial data in BigQuery by using geography data types and standard SQL geography functions.

You can visualize your BigQuery GIS Data using BigQuery Geo Viz . BigQuery Geo Viz is a web tool for visualizing geospatial data in BigQuery on a map, one query at a time. You can run a SQL query and display the results on an interactive map.

When you use BigQuery GIS, your charges are based on how much data is stored in the tables that contain BigQuery GIS data (storage pricing) and the amount of data scanned by the queries you run against the data (query pricing).

BigQuery GIS Functions

BigQuery supports many GIS functions that can be used to analyze geographical data. For the purpose of this tutorial, we made use of the 3 most commonly used BigQuery GIS functions to analyze some publicly available datasets on Google Cloud.

Analysis 1 (ST_AREA)

Objective: To determine the five largest states in the United States.

Given the geometry (state_geom) of all the states in the United State, we are able to calculate the area of each state in square kilometers ( using the ST_AREA BigQuery GIS function.

This function returns the area in square meters covered by a given geometry. As seen in the SQL statement below, the area was then converted from square meters to square kilometer by dividing it with 1,000,000.

  ST_AREA(state_geom)/1000000 AS area_sqkm,
  area_sqkm DESC

The diagram below highlights the use of BigQuery Geo Viz to visualize the result of our query by plotting it on an interactive web map. image.png

Analysis 2 (ST_WITHIN)

Objective: Find the list of hospitals in the United States that fall within a given perimeter boundary.

For this use case, we make use of the BigQuery GIS function ST_WITHIN. This function returns true if a given hospital falls within the boundary and false otherwise.

By executing the SQL statement below, we get back the list of hospitals that falls within the boundary that we have set.

  ST_X(ST_GEOGFROMTEXT(location)) AS longitude,
  ST_Y(ST_GEOGFROMTEXT(location)) AS latitude,
  ST_GEOGFROMTEXT(location) AS location
    ST_GEOGFROMTEXT('POLYGON((-74.02107238769531 40.59257812608644, -73.88734817504883 40.59257812608644, -73.88734817504883 40.67868529354439, -74.02107238769531 40.67868529354439, -74.02107238769531 40.59257812608644))') )
  hospital_name ASC

As seen below in the diagram, we are able to visualize the hospitals that fall within our set boundary on an interactive map. image.png

Analysis 3 (ST_DISTANCE)

Objective: Determine the average distance is radius covered by all bike trips that started from the "Elizabeth St & Hester St" bike station in 2015.

The dataset used for our third analysis is the NewYork city bike trips from 2014 - 2016. Using the BigQuery GIS function ST_DISTANCE, we were able to compute the radius of the distance of all bike trips that originated from our interested bike station in 2015.

After getting the distance of each trip, we then compute the average of the trips using the SQL function AVG. The result of the average distance covered is in meters.

        end_station_latitude))) AS avg_radius_distance
  start_station_name = "Elizabeth St & Hester St"



It's important to remember that running BigQuery queries on a GCP Project that is connected to a billing account will incur some charges. To avoid these charges, kindly make sure you configure BigQuery Sandbox by disabling billing from the GCP Project you are using to run the queries.

To make it easy to follow along, the datasets used in the query can be found on BigQuery public datasets made available on the Google Cloud Platform (GCP). Except for use cases outside the scope of this tutorial, there is no need to import data from additional data sources.

Many thanks for your time. If you want to chat about GIS data analytics using BigQuery, feel free to connect with me on LinkedIn.