Skip to content

Instantly share code, notes, and snippets.

View wriglz's full-sized avatar

Simon Wrigley wriglz

View GitHub Profile
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
@wriglz
wriglz / lifeboat.sql
Created September 9, 2022 16:17
SQL to explore the RNLI open "Returns of Service" data set
-- Data source: https://data-rnli.opendata.arcgis.com/
-- Remove outliers which are beyond the reach of the maximum lifeboat range:
SELECT
rescue.*
FROM
`RNLI.RNLI_Returns_of_Service` rescue
LEFT JOIN
`RNLI.RNLI_Lifeboat_Station_Locations` station
ON
@wriglz
wriglz / random_number.sql
Created September 7, 2022 12:54
SQL to generate a random number within a specified <min> <max> range
-- Generate a random number within a specified <min> <max> range
SELECT
CAST(<min> + RAND() * (<max> - <min>) AS INT) AS random_number