Skip to content

Instantly share code, notes, and snippets.

View mattwigway's full-sized avatar

Matthew Wigginton Bhagat-Conway mattwigway

  • Department of City and Regional Planning, University of North Carolina at Chapel Hill
  • Durham, NC, USA
View GitHub Profile
@mattwigway
mattwigway / twisted-xmlrpc.py
Created October 13, 2011 05:20
Quick twisted XMLRPC test
from twisted.web import xmlrpc, server
from twisted.internet import reactor, defer
class rpctest(xmlrpc.XMLRPC):
def xmlrpc_test(self):
return "Hello, world!"
if __name__ = '__main__':
r = rpctest()
@mattwigway
mattwigway / selectline.sql
Created November 9, 2011 23:23
Selecting a line from NextBus data
SELECT loc_a.oid, loc_a.vehicle, loc_a.route, loc_a.direction, transform(ST_MakeLine(loc_a.the_geom, loc_b.the_geom), 26945) AS the_geom,
(ST_Length(transform(ST_MakeLine(loc_a.the_geom, loc_b.the_geom), 26945))/
(EXTRACT(EPOCH FROM loc_b.time) - EXTRACT(EPOCH FROM loc_a.time))) *
2.23693629 AS mph, loc_a.time AS starttime, loc_b.time AS endtime
INTO acrt.lametrolines
FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY vehicle, time) AS num FROM acrt.nextbus) AS loc_a
JOIN
(SELECT *, ROW_NUMBER() OVER (ORDER BY vehicle, time) AS num FROM acrt.nextbus) AS loc_b
ON (loc_a.vehicle = loc_b.vehicle AND
@mattwigway
mattwigway / foothill.sql
Created November 10, 2011 05:09
Selecting the demographic data from Foothill Enrollment
-- All that mess of parens is b/c, if you're left joining, you have to do
-- ((a LEFT JOIN b) LEFT JOIN c) and so on; a LEFT JOIN b LEFT JOIN c does not work
SELECT a.Zip_Code, a.total, af.africanamerican, asi.asian, f.filipino, h.hispanic, me.multiethnic, fn.firstnations, o.other, p.pacificislander,
u.unrecorded, w.white
INTO enrollment_by_zip
FROM
((((((((((SELECT Zip_Code, count(*) AS total FROM enrollment GROUP BY Zip_Code) a LEFT JOIN
(SELECT Zip_Code, count(*) AS africanamerican FROM enrollment WHERE ethnicity='African American' GROUP BY Zip_Code) af ON a.zip_code = af.zip_code) LEFT JOIN
(SELECT Zip_Code, count(*) AS asian FROM enrollment WHERE ethnicity='Asian' GROUP BY Zip_Code) asi ON a.zip_code = asi.zip_code) LEFT JOIN
(SELECT Zip_Code, count(*) AS filipino FROM enrollment WHERE ethnicity='Filipino' GROUP BY Zip_Code) f ON a.zip_code = f.zip_code) LEFT JOIN
@mattwigway
mattwigway / convert.sql
Created November 12, 2011 17:33
Convert time varchar(19) to time::timestamp
ALTER TABLE nextbus RENAME COLUMN time TO time_orig; -- save the old column
ALTER TABLE nextbus ADD COLUMN time timestamp; -- create the new column
UPDATE acrt.nextbus SET time = time_orig::timestamp; -- set the timestamps. This takes a VERY VERY LONG time; for a 395k point table, it took over 654 (!) seconds
ALTER TABLE nextbus DROP COLUMN time_orig;
@mattwigway
mattwigway / lines.sql
Created November 15, 2011 04:57
Make Transit Speed Maps from GTFS
-- (C) Matthew Conway, CC-BY-NC-SA
SELECT ROW_NUMBER() OVER () AS oid, t1.trip_id, t1.stop_id as from_id, t1.departure_time as from_time, t1.stop_sequence AS from_seq,
t2.stop_id as to_id, t2.arrival_time as to_time, t2.stop_sequence AS to_seq,
EXTRACT(EPOCH FROM t2.arrival_time::time - t1.departure_time::time) AS traversal,
(SELECT ST_MakeLine(geom) AS the_geom
FROM vta.stop_times
JOIN vta.stops USING (stop_id)
WHERE trip_id = t1.trip_id AND stop_sequence BETWEEN t1.stop_sequence AND t2.stop_sequence) AS the_geom
INTO vta.trip_segments
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY trip_id, departure_time NULLS LAST) AS num FROM vta.stop_times WHERE departure_time IS NOT NULL) t1
@mattwigway
mattwigway / gtfsmap.sql
Created December 23, 2011 17:37
GTFS maps
CREATE TABLE vta.segments AS
SELECT
-- make a speed placeholder, update later
s.trip_id, s.stop_id AS start_id, e.stop_id AS end_id, s.time AS start_time, e.time AS end_time,
trips.route_id, trips.trip_headsign, -1.01 AS speed,
-- get the time, and deal with wrapped times like 24:21 for a trip that runs past midnight
-- if the time is negative, add 24:00:00
CASE
-- segment crosses midnight: wrap it
WHEN SUBSTR(e.time, 1, 2)::int2 >= 24 AND SUBSTR(s.time, 1, 2)::int2 < 24
@mattwigway
mattwigway / otpeverywhere.py
Created December 31, 2011 04:01
OTP to Everywhere
#!/usr/bin/python
# otpeverywhere - make otp maps like
# http://www.flickr.com/photos/walkingsf/6536396399/
# Copyright 2011 Matt Conway
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@mattwigway
mattwigway / otpanalyst.xml
Created January 30, 2012 02:59
GDAL WMS def for OTP Analyst
<GDAL_WMS>
<Service name="WMS">
<!-- I don't think the version matters -->
<Version>1.1.1</Version>
<!-- Set the special parameters here. There is no other way to
specify special parameters as far as I know -->
<ServerUrl>http://localhost:8080/opentripplanner-analyst-core/wms?DIM_ORIGINLAT=37.36234646943972&DIM_ORIGINLON=-122.12711334228516&time=2012-01-12T23:30:00Z&</ServerUrl>
<!-- This state plane CA zone 3 meters
You should set it to your local projection. -->
<SRS>EPSG:26943</SRS>
@mattwigway
mattwigway / graph-builder.xml
Created February 29, 2012 01:31
SF Graph Builder Config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="graphBundle" class="org.opentripplanner.model.GraphBundle">
<property name="path" value="/var/otp/graphs/sf/" />
</bean>
@mattwigway
mattwigway / exception
Created February 29, 2012 01:35
exception planning trip
SEVERE: exception planning trip:
java.lang.NullPointerException
at org.opentripplanner.routing.impl.StreetVertexIndexServiceImpl.getClosestVertex(StreetVertexIndexServiceImpl.java:244)
at org.opentripplanner.routing.impl.StreetVertexIndexServiceImpl.getClosestVertex(StreetVertexIndexServiceImpl.java:186)
at org.opentripplanner.routing.impl.GenericPathService.getVertexForPlace(GenericPathService.java:72)
at org.opentripplanner.routing.impl.GenericPathService.getVertexForPlace(GenericPathService.java:58)
at org.opentripplanner.routing.impl.ContractionPathServiceImpl.plan(ContractionPathServiceImpl.java:99)
at org.opentripplanner.api.ws.PlanGenerator.generate(PlanGenerator.java:104)
at org.opentripplanner.api.ws.Planner.getItineraries(Planner.java:282)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)