Skip to content

Instantly share code, notes, and snippets.

View Nate-Wessel's full-sized avatar
🍁

Nate Wessel Nate-Wessel

🍁
View GitHub Profile
@Nate-Wessel
Nate-Wessel / points-within-polys.py
Created October 2, 2017 02:48
script for generating a discrete number of random points within each of a set of polygons
# assign a random location to every person in greater cinci
# inside their home census block
import psycopg2, random
from shapely import wkb
from shapely.geometry import Point
# connect to the census DB
conn_string = ("host='localhost' dbname='' user='' password=''")
census_conn = psycopg2.connect(conn_string)
@Nate-Wessel
Nate-Wessel / tesselate-polygon.r
Created July 3, 2017 08:42
centroidal voronoi tesselation of a complex polygon in R
library('deldir') # voronoi stuff
library('rgdal') # postgis and spatial stuff
library('sp') # spatial stuff
library('raster') # more spatial stuff
library('splancs') # csr() for generating random points
# function from http://carsonfarmer.com/2009/09/voronoi-polygons-with-r/
voronoipolygons = function(layer,bounding_box) {
require(deldir)
crds = layer@coords
@Nate-Wessel
Nate-Wessel / import_TTC_GTFS.sql
Created March 24, 2017 15:10
SQL script for importing GTFS into postGIS, adding geometry fields where possible
DROP TABLE IF EXISTS gtfs_ttc_agency;
CREATE TABLE gtfs_ttc_agency(
agency_id int,
agency_name text,
agency_url text,
agency_timezone text,
agency_lang text,
agency_phone text,
agency_fare_url text
);
@Nate-Wessel
Nate-Wessel / logit-test.r
Created January 27, 2017 00:16
playing around with binomial regression in R
# toy code for playing with discrete choice models and logistic regression
# sample size
n = 2000
# generate random independents
x1 = scale(runif(n))
x2 = scale(runif(n))
x3 = scale(runif(n))
# 'actual' utility function that I made up
uy = -3 + -2 * x1 + 2 * x2 + 0.5 * x3
@Nate-Wessel
Nate-Wessel / tarjan.r
Created October 1, 2016 00:01
Tarjan's algorithm on OSM (osm2po) data for modes (car,bike,foot)
# run tarjan with igraph
# name of the table
city = 'helsinki'
modes = c('car','bike','foot')
# connect to database
library("RPostgreSQL")
driver <- dbDriver("PostgreSQL")
@Nate-Wessel
Nate-Wessel / areal sum.sql
Last active September 19, 2016 22:16
areal apportionment of SUM with weight
/*
generic areal apportionment of SUM
*/
--table1.id
--table1.geom
--table1.value
--table2.geom
--table2.weight
@Nate-Wessel
Nate-Wessel / areal average.sql
Last active September 19, 2016 20:27
generic areal apportionment of average SQL script
/*
generic areal apportionment of average
*/
--table1.id
--table1.geom
--table1.value
--table2.geom
--table2.weight
@Nate-Wessel
Nate-Wessel / kde-ratio-map.r
Last active September 19, 2016 20:50
R smooth ratio raster map
# make dead-ends ratio map
library('ks')
library('raster')
# connect to database
library("RPostgreSQL")
driver <- dbDriver("PostgreSQL")
connection <- dbConnect(driver, dbname="", password="", user="", host="localhost")
@Nate-Wessel
Nate-Wessel / radial-KDE.r
Created August 26, 2016 18:13
radial KDE in R
library("RPostgreSQL")
driver <- dbDriver("PostgreSQL")
connection <- dbConnect(driver, dbname="", password="", user="", host="localhost")
# get the full result set from the table
d <- dbGetQuery(connection,"SELECT t1, t2, ST_Azimuth(ST_StartPoint(line)::geography, ST_EndPoint(line)::geography) AS azimuth, ST_Distance(ST_StartPoint(line)::geography,ST_EndPoint(line)::geography)/1000 AS distance, white AS commuters FROM wbcommute_38300 WHERE t1 != t2 AND t1 = 684;")
dbDisconnect(connection)
# this is the number of commuter-kilometers