Spatial Extension for BigQuery

Spatial Extension for BigQuery

Identifying earthquake-prone areas in the state of California

In this example we are going to use some of the functions included in CARTO’s Spatial Extension in order to highlight zones prone to earthquakes, using a BigQuery public dataset.

First we define our region of interest, which in this case is a bounding box enclosing the state of California, using the function ST_MAKEENVELOPE. After filtering the earthquake locations with this bounding box, we compute the concave hull polygon enclosing the resulting points using the ST_CONCAVEHULL function. For visualization purposes, this polygon is smoothed out by means of the ST_BEZIERSPLINE function. Finally, we construct the polygon defining the earthquake-prone area using the ST_POLYGONIZE function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WITH bounds AS (
    SELECT bqcarto.constructors.ST_MAKEENVELOPE(-126.98746757203217, 31.72298737861544, -118.1856191911019, 40.871240645013735) AS bbox
),
data AS (
    SELECT ST_GEOGPOINT(longitude, latitude) AS points
    FROM `bigquery-public-data`.noaa_significant_earthquakes.earthquakes
    JOIN bounds
    ON ST_CONTAINS(bounds.bbox, ST_GEOGPOINT(longitude, latitude))
    WHERE longitude IS NOT NULL AND latitude IS NOT NULL
),
bezier_spline AS (
    SELECT bqcarto.constructors.ST_BEZIERSPLINE(
        ST_BOUNDARY(
        bqcarto.transformations.ST_CONCAVEHULL(ARRAY_AGG(points), 300, "kilometres")), 
        null,
        0.9) AS geom
    FROM data
),
polygon_array AS (
    SELECT bqcarto.processing.ST_POLYGONIZE(ARRAY_AGG(geom)) AS geom
    FROM bezier_spline
) 
SELECT unnested FROM polygon_array, UNNEST(geom) AS unnested