Skip to content

Instantly share code, notes, and snippets.

View wriglz's full-sized avatar

Simon Wrigley wriglz

View GitHub Profile
WITH Sequences AS (SELECT 1 AS id, [0, 1, 1, 2, 3, 5] AS some_numbers, [10, 51, 17, 28, 31, 55] AS some_more_numbers
UNION ALL SELECT 2 AS id, [2, 4, 8, 16, 32] AS some_numbers, [231, 43, 8, 1, 77] AS some_more_numbers
UNION ALL SELECT 3 AS id, [5, 10] AS some_numbers, [5555, 13515] AS some_more_numbers)
SELECT id, flattened_numbers, more_flattened_numbers
FROM Sequences,
UNNEST(some_numbers) AS flattened_numbers,
UNNEST(some_more_numbers) AS more_flattened_numbers;
WITH Sequences AS
(SELECT 1 AS id, [0, 1, 1, 2, 3, 5] AS some_numbers
UNION ALL SELECT 2 AS id, [2, 4, 8, 16, 32] AS some_numbers
UNION ALL SELECT 3 AS id, [5, 10] AS some_numbers)
SELECT id, flattened_numbers
FROM Sequences
CROSS JOIN UNNEST(Sequences.some_numbers) AS flattened_numbers;
WITH
NN AS (
SELECT
a.osm_id,
-- Select the closest ID
ARRAY_AGG(STRUCT(b.osm_id,
ST_DISTANCE(a.geom, b.geom) AS dist)
ORDER BY
ST_DISTANCE(a.geom, b.geom)
LIMIT
@wriglz
wriglz / osm_lighthouses.sql
Created November 7, 2023 12:37
SQL to extract lighthouses from the BigQuery OSM dataset
SELECT
osm_id,
ST_CENTROID(geometry) AS geom,
(SELECT value FROM UNNEST(all_tags) WHERE key = 'name' ) AS name,
FROM
bigquery-public-data.geo_openstreetmap.planet_features
WHERE
('man_made', 'lighthouse') IN (
SELECT
(key, value)
@wriglz
wriglz / north_south_divide.sql
Created May 25, 2023 14:23
An SQL script to split the UK into North and South by it's population in CARTO
CREATE OR REPLACE TABLE
`dataset.north_south_divide`
CLUSTER BY
h3 AS(
WITH
england_grid AS (
SELECT
geoid AS h3,
population
FROM
@wriglz
wriglz / London_undeground_walk.sql
Created December 20, 2022 17:29
Code to create a coverage map of how many London Underground stations are within a 10 min walk of any given point.
SELECT
osm_id,
geometry,
-- Here we are going to extract the station name from the all_tags array:
(
SELECT
value
FROM
UNNEST(all_tags)
WHERE
@wriglz
wriglz / ST_LineLocatePoint.sql
Created December 16, 2022 17:15
Function to create a UDF to replicate ST_LineLocatePoint on Google BigQuery
CREATE OR REPLACE FUNCTION
demo.ST_LineLocatePoint(line GEOGRAPHY,
point GEOGRAPHY)
RETURNS FLOAT64 AS (ST_DISTANCE(ST_STARTPOINT(line),ST_CLOSESTPOINT(line, point) )/ST_LENGTH(line) );
@wriglz
wriglz / osm_edit_history.sql
Created November 8, 2022 14:23
Script utlising CARTO H3 support to visualise OSM Edits for the United Kingdom
DECLARE
bbox GEOGRAPHY;
SET
bbox = (
SELECT
geom
FROM
`global_boundaries_dataset`
WHERE
NAME_EN = 'United Kingdom');
@wriglz
wriglz / window_functions.sql
Last active October 20, 2022 14:40
SQL utilising window functions to explore NOAA Global Hurricane Tracks
WITH
hurricane_geometry AS (
SELECT
* EXCEPT (longitude, latitude), -- Select everything except lat & lon
ST_GEOGPOINT(longitude, latitude) AS geom, -- As we create a geometry here
MAX(usa_wind) OVER (PARTITION BY sid) AS max_wind_speed -- Calculate the max wind speed per storm
FROM
`bigquery-public-data.noaa_hurricanes.hurricanes`
WHERE
basin = 'NA' -- Select only storms in the North American basin
@wriglz
wriglz / osm_extract.sql
Created October 3, 2022 16:07
SQL to query OSM data on Google BigQuery
SELECT
osm_id,
feature_type,
osm_timestamp,
geometry,
-- Here we are going to extract a couple of attributes from the all_tags array:
(SELECT value FROM UNNEST(all_tags) WHERE key = 'name' ) AS name,
(SELECT value FROM UNNEST(all_tags) WHERE key = 'addr:city') AS city
FROM
bigquery-public-data.geo_openstreetmap.planet_features