Skip to content

Instantly share code, notes, and snippets.

fitnr /
Last active December 17, 2021 14:11
Convert between pixel/geo values with a gdal geotransform
Where transform is a geotransform array, e.g.
>>> from osgeo import gdal
>>> src = gdal.Open('example.tiff')
>>> transform = src.GetGeoTransform()
>>> (-82.254, 3.0858676207514586e-05, 0.0, 41.3163, 0.0, -3.086680761099182e-05)
>>> # [x origin (left), pixel width, x skew, y origin (top), y skew, y pixel height (negative because counting from top of image)]
def px2geo(geotransform, coord):
'''Convert pixel coordinates to projected geographic coordinates.'''
fitnr / mv_depend.sql
Created November 12, 2021 19:06
recursively get dependent matviews
* Arguments:
* :matview - schema-qualified name of materialized view
SELECT DISTINCT ON (objid, refobjid) v.oid::regclass AS view, 1 AS level
FROM pg_depend AS d
JOIN pg_rewrite AS r ON r.oid = d.objid
JOIN pg_class AS v ON v.oid = r.ev_class
JOIN pg_class as c ON c.oid = d.refobjid AND c.oid::regclass != v.oid::regclass
fitnr / table_size.sql
Created March 28, 2021 15:19
get table and index sizes in postgresql
to_char(round(reltuples::numeric, -2), '999,999,999,999,999') as est_row_count,
pg_size_pretty(pg_indexes_size(c.oid)) index_size,
pg_size_pretty(pg_relation_size(c.oid)) table_size
FROM pg_class c
LEFT JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE (nspname <> ALL (ARRAY['pg_catalog', 'information_schema']::name[]))
import sys
import json
from io import BytesIO
import geomet.wkb
def bytes2dict(x):
'''Decode a dictionary encoded in bytes using the dicAsBytes method.'''
b = BytesIO(x)
count = read_int(b)
result = {}
set -e
TMP=$(mktemp -d)
export DIR
downsample() {
station_id terminal_id route_id agency_name route_long_name
ACB EUS 176599 Serco Caledonian Sleeper CS train service from DEE to EUS
ACB EUS 192409 Virgin Trains VT train service from EUS to WBQ
ACB EUS 214187 Serco Caledonian Sleeper CS train service from ABD to EUS
ACB EUS 214314 Serco Caledonian Sleeper CS train service from EUS to ABD
ACB EUS 24722 Virgin Trains VT train service from LIV to EUS
ACB EUS 24780 Virgin Trains VT train service from BPN to EUS
ACB EUS 24808 Virgin Trains VT train service from EUS to LAN
ACB EUS 24825 Virgin Trains VT train service from EUS to LIV
ACB EUS 24895 Virgin Trains VT train service from GLC to EUS
fitnr / query.sql
Last active January 8, 2019 18:59
Find the number of rail stations in GB that have direct service to a London terminus
-- Load data from a UK-wide GTFS into postgres using
-- Used this GTFS file:
with terminals as ( -- ID codes for London terminal stations
values ('BFR'), ('CST'), ('CHX'), ('CTK'), ('EUS'), ('FST'), ('KGX'), ('LST'), ('LBG'), ('MYB'), ('MOG'), ('OLD'), ('PAD'), ('STP'), ('SPX'), ('VXH'), ('VIC'), ('WAT'), ('WAE')
, london_trips as ( -- select trips that include the above codes
select distinct feed_index, route_id, trip_id
from gtfs.stops
statefp countyfp name population
48 179 Gray 23028
39 067 Harrison 15521
1 069 Houston 103891
48 013 Atascosa 47710
17 031 Cook 5227575
1 001 Autauga 55049
38 025 Dunn 4284
17 091 Kankakee 111493
49 001 Beaver 6437
fitnr / hexbin.sql
Last active June 27, 2020 07:33
create hex bins in postgis
-- adapted from
-- usage: select * FROM hexbin(numeric, geometry)
-- cover a table with something like:
-- select row_number() over () id, geom
-- from hexbin(5280, st_transform(st_setsrid(st_expand(ST_EstimatedExtent('table', 'geom'), 0.1), 4269), 2255));
CREATE OR REPLACE FUNCTION hexbin (height numeric, minx numeric, miny numeric, maxx numeric, maxy numeric, srid integer)
RETURNS TABLE (geom geometry(polygon))
AS $$
fitnr /
Created September 26, 2018 14:59
Returns an s2 covering as ndgeojson
#!/usr/bin/env python3
import json
import argparse
import s2sphere as s2
def point2coord(point):
return s2.LatLng.longitude(point).degrees, s2.LatLng.latitude(point).degrees