Spatial Extension for BigQuery

Spatial Extension for BigQuery

An H3 grid of Starbucks locations and simple cannibalization analysis

Bulding the H3 grid

We are going to demonstrate how fast and easy it is to make a visualization of an H3 grid to identify the concentration of Starbucks locations in the US.

The first step is to import the Starbucks locations dataset into a BigQuery table called starbucks_locations_usa. Then, with a single query, we are going to calculate how many Starbucks locations fall within each H3 grid cell of resolution 4.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
WITH
  data AS (
  SELECT
    bqcarto.h3.ST_ASH3(geog, 4) AS h3id,
    COUNT(*) AS agg_total
  FROM `cartobq.docs.starbucks_locations_usa`
  GROUP BY h3id
  )
SELECT
  h3id, 
  agg_total,
  bqcarto.h3.ST_BOUNDARY(h3id) AS geom
FROM
  data

This query adds two new columns to our dataset: geom, representing the boundary of each of the H3 grid cells where there’s at least one Starbucks, and agg_total, containing the total number of locations that fall within each cell. Finally, we can visualize the result.

Note: this visualization is made using Builder, where you can easily import your BigQuery data using our connector, but you can also create a quick visualization using BigQuery Geo Viz.

Using finer resolution H3 for simple cannibalization analysis

Next, we will analyze in finer detail the grid cell that we have identified contains the highest concentration of Starbucks locations, with ID 8428d55ffffffff.

Multiresolution quadkeys
H3 grid of resolution 4 with 293 Starbucks locations.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
WITH
  data AS (
  SELECT
    bqcarto.h3.ST_ASH3(geog, 9) AS h3id,
    COUNT(*) AS agg_total
  FROM `cartobq.docs.starbucks_locations_usa`
  WHERE
    ST_INTERSECTS(geog,
      bqcarto.h3.ST_BOUNDARY('8428d55ffffffff'))
  GROUP BY h3id
  )
SELECT
  h3id,
  agg_total,
  bqcarto.h3.ST_BOUNDARY(h3id) AS geom
FROM
  data

We can clearly identify that there are two H3 cells with the highest concentration of Starbucks locations, and therefore at risk of suffering cannibalization. These are cells with IDs 8928d542c17ffff and 8928d542c87ffff respectively. Finally, to complete our analysis, we can calculate how many locations are within one cell distance of this first cell:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
WITH
  data AS (
  SELECT
    bqcarto.h3.ST_ASH3(geog, 9) AS h3id,
    COUNT(*) AS agg_total
  FROM `cartobq.docs.starbucks_locations_usa`
  WHERE
    ST_INTERSECTS(geog,
      bqcarto.h3.ST_BOUNDARY('8428d55ffffffff'))
  GROUP BY h3id
  )
SELECT 
SUM(agg_total)
FROM data
WHERE h3id IN UNNEST(bqcarto.h3.KRING('8928d542c17ffff', 1))
-- 13