Skip to content

Instantly share code, notes, and snippets.

@fitnr
fitnr / insert.sql
Last active November 10, 2019 16:02
Example of inserting missing service_id into a GTFS
INSERT INTO gtfs.calendar (feed_index, service_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday, start_date, end_date)
SELECT a.feed_index, a.service_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday, start_date, end_date
FROM (
SELECT feed_index, service_id
FROM gtfs.trips a
LEFT JOIN gtfs.calendar b using (feed_index, service_id)
WHERE COALESCE(b.monday, b.friday) IS NULL
GROUP BY feed_index, service_id
) a
INNER JOIN gtfs.calendar b ON a.feed_index = b.feed_index AND (
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 = {}
@fitnr
fitnr / group-by-table-names.sh
Last active April 7, 2019 20:38
Group census API variables.json by data set
curl http://api.census.gov/data/2014/acs5/variables.json |
jq -r '[ .variables | to_entries[] | .value.concept ] | unique[]'
> tables.txt
#!/bin/bash
set -e
DIR=$1
SHP=$2
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
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 https://github.com/fitnr/gtfs-sql-importer
-- Used this GTFS file:
-- https://transitfeeds.com/p/association-of-train-operating-companies/284
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
@fitnr
fitnr / table2csv.js
Created January 30, 2015 17:47
convert table to csv (uses JQuery)
// encoded:
// javascript:(function()%7B$('table').each(function()%7Bvar%20$table=$(this);$('%3Ctextarea%3E').css('width',$table.width()).css('height','400px').html($.map($table.find('tr'),function(tr)%7Breturn%20$.map($(tr).find('th,%20td'),function(e)%7Breturn'%22'+$(e).text().replace('%22','%22%22').replace('%5Cn','%20')+'%22'%7D).join(',')%7D).join('&%2313;&%2310;')).insertAfter($table);%7D)%7D)()
javascript:(function() {
$('table').each(function() {
var $table = $(this);
$('<textarea>').css('width', $table.width())
.css('height', '400px')
.html(
$.map(
$table.find('tr'), function(tr) {
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
fitnr / readme.md
Last active December 17, 2018 09:14
Bookmarklet for switching between two versions of a site, say dev and production, or localhost and dev

How to

Change the left_domain_name and right_domain_name variables to reflect your project. If your sites have exactly matching URL schemas (where localhost/my/path is always equivalent to production.com/my/path), you can ignore that big comment block. If not, check it out and rewrite the two functions to fit your setup.

Compress the javascript, say with UglifyJS.

Urlencode the result, say with this tool.

Add javascript: to the front and save it as a bookmark. Depending on your browser, the easiest way to do this might be to make a new bookmark for a random page and then edit both the address and name.

@fitnr
fitnr / s2cover.py
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