Skip to content

Instantly share code, notes, and snippets.

View wriglz's full-sized avatar

Simon Wrigley wriglz

View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Convert a Shapefile to a Geopackage

In a terminal, use:

ogr2ogr -f GPKG your.gpkg firstfile.shp

You can then add a second shapefile to the same geopackage using:

ogr2ogr -append -f GPKG your.gpkg secondfile.shp

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');