Skip to content

Instantly share code, notes, and snippets.

@migurski
migurski / lambert.js
Created May 14, 2012 03:29
Lambert projection for Modest Maps
var LambertProjection = function(lat0, lon0, lat1, lat2, xmin, ymin, xmax, ymax)
{
var pi = Math.PI, ln = Math.log, pow = Math.pow,
sin = Math.sin, cos = Math.cos, tan = Math.tan,
atan = Math.atan, sqrt = Math.sqrt;
function sec(t) { return 1 / cos(t); }
function cot(t) { return 1 / tan(t); }
function deg2rad(deg) { return pi * deg / 180; }
@migurski
migurski / gunicorn-slayer.py
Created June 21, 2012 06:41
Gunicorn Slayer
from sys import argv
from time import time
from glob import glob
from os import stat, kill, getuid
from os.path import basename, dirname, join
from datetime import datetime
from random import choice
from signal import SIGTERM
if __name__ == '__main__':
@migurski
migurski / Lambert.py
Created July 5, 2012 18:16
Lambert Conformal Conic implementation with US-specific transformation
"""
>>> usa = USA()
>>> loc = Location(48.38544, -124.72916)
>>> pt = usa.locationProj(loc)
>>> abs(-2109470 - pt.x) < 1
True
>>> abs( 1532790 - pt.y) < 1
True
@migurski
migurski / remotefile.py
Created August 1, 2012 18:44
RemoteFileObject is a simple mapping from HTTP Range headers to a local file-like object
from time import time
from urlparse import urlparse
from httplib import HTTPConnection
from os.path import basename
from cStringIO import StringIO
from datetime import timedelta
from os import SEEK_SET, SEEK_CUR, SEEK_END
class RemoteFileObject:
""" Implement enough of this to be useful:
@migurski
migurski / measure-sizes.py
Created August 3, 2012 21:15
Two scripts for creating heatmap of OSM data size, e.g. https://tiles.mapbox.com/migurski/map/osmsize
from sys import argv, stdout, stderr
from subprocess import Popen
from os import stat, unlink
from math import log, ceil
from stat import ST_SIZE
from time import time
from ModestMaps.OpenStreetMap import Provider
from ModestMaps.Core import Coordinate
@migurski
migurski / extract-routes.py
Created October 18, 2012 23:04
OSM Route Relation Refiner, see also data at http://teczno.com/s/c4c
''' Extract route relations from osm2pgsql-generated database to CSV.
'''
from sys import argv
from csv import writer
from psycopg2 import connect
if __name__ == '__main__':
db = connect(database='raster_osm', user='osm').cursor()
@migurski
migurski / snip-lines.py
Created October 30, 2012 05:33
Sketch for topological simplification of national hydrology dataset
'''
ogr2ogr -overwrite -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs' PG:"user=nhd dbname=nhd" NHDFlowline.shp
create table nhdflowline_snapped ( ogc_fid integer primary key );
select addgeometrycolumn('', 'nhdflowline_snapped', 'geometry', 900914, 'LINESTRING', 2);
create index nhdflowline_snapped_geom_idx on nhdflowline_snapped using gist (geometry);
insert into nhdflowline_snapped
@migurski
migurski / cut-up-routes2.py
Created October 31, 2012 17:53
Skeletron Dumper and Cutter of Routes
'''
#!/bin/sh -x
apt-get -y update
apt-get -y install htop osmosis git python-pip python-shapely python-pyproj python-networkx qhull-bin
git clone git://github.com/migurski/Skeletron.git ~/Skeletron
cd ~/Skeletron
python setup.py install
@migurski
migurski / rasterize.py
Created December 15, 2012 05:51
Script to generate CONUS raster of various OSM metrics connected to TIGER extent. Counties2012 table is in spherical Albers projection: `+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +nadgrids=@null +units=m +no_defs`
from itertools import product
from multiprocessing import Pool
from numpy import zeros, ubyte
from psycopg2 import connect
from osgeo import gdal, osr
from PIL import Image
def measure_quad(x, y, cellsize):
'''
#!/usr/bin/env python
''' Convert binary data to UTF8.
If you want binary data in Javascript over XMLHttpRequest but don't
have ArrayBuffer available, one option is to read strings of text and
use xhr.responseText.charCodeAt() to read them. Because this expects to
work with text, it will return the unicode of the character at that
position. This script converts raw binary bytes to unicode text that
evaluates to the same 8-bit unsigned int at each position. The overall
length will increase slightly.