Skip to content

Instantly share code, notes, and snippets.

View alpha-beta-soup's full-sized avatar
🌏

Richard Law alpha-beta-soup

🌏
View GitHub Profile
@alpha-beta-soup
alpha-beta-soup / transitHeadways.py
Last active March 22, 2018 20:51
A Python function to calculate the headways of transit vehicles arranged sequentially for a week, accounting for time over midnight over one or more days. Uses NumPy.
import numpy as np
def headway_array(arrivals_array, maxseconds=24*60*60, daysinweek=7):
'''
Given a numpy.array object with...
> the number of rows equal to the number of days in a week, <daysinweek>
> the number of rows equal to the number of trips made on a public transport
route
> the array values equal to the time at which the vehicle arrives at the
stop, expressed in seconds since midnight for each day. None values can be
@alpha-beta-soup
alpha-beta-soup / interpolatedMedian.py
Last active August 29, 2015 14:01
A function to calculate the interpolated median of a list of values.
# interpolatedMedian.py
# Author: Richard Law
# Calculates the interpolated median of a list,
# which is a form of median that retains the
# important properties of a median (isn't swayed by
# unusually large or small values) but gives a better
# indication of the ditstribution of values
@alpha-beta-soup
alpha-beta-soup / NZ-post-street-types.csv
Last active August 29, 2015 14:11
Need to translate "St" to "Street" (and all other possibilities) for New Zealand addresses? No one likes PDFs. This is a space-separated CSV of Appendix C of this document: https://www.nzpost.co.nz/sites/default/files/uploads/shared/standards-guides/adv358-address-standards.pdf
Full Abbreviation
Access Accs
Dune Dune
Accessway Accswy
Elm Elm
Alley Aly
End End
Anchorage Ancg
Entrance Ent
Approach App
# Author: [email protected]
#
# License: BSD (3-clause)
# XXX clafify licensing
import numpy as np
from collections import namedtuple
from scipy.signal import detrend
from scipy import stats
@alpha-beta-soup
alpha-beta-soup / obtain_corners.py
Created May 10, 2015 05:59
Click on a raster layer in the QGIS table of contents, and then run this script. The corners of the bounding box of the layer will be printed to the console in the project's projection system. This is useful for supplying these values to Leaflet when using `imageOverlay`, which requires the top left and bottom right coordinates.
from osgeo import gdal, osr
# Click on your layer in the TOC
alayer = qgis.utils.iface.activeLayer()
bag = gdal.Open(alayer.source())
bag_gtrn = bag.GetGeoTransform()
bag_proj = bag.GetProjectionRef()
bag_srs = osr.SpatialReference(bag_proj)
geo_srs =bag_srs.CloneGeogCS()
transform = osr.CoordinateTransformation( bag_srs, geo_srs)
@alpha-beta-soup
alpha-beta-soup / zoom2me.py
Created August 16, 2015 08:54
Zoom to your current location in QGIS (assuming EPSG 4326)
import qgis.utils
import pycurl
import StringIO
import json
buffer = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://ipinfo.io/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
import pint
ur = pint.UnitRegistry()
Q = ur.Quantity
def convert(array, source, target):
'''
Convert a list in one unit to a list in another, using Pint.
Simple but saves me from writing this repeatedly in ipython.
<source> and <target> must be Pint units, e.g.
import pint
ureg = pint.UnitRegistry()
def is_str_valid_pint_unit(unit):
try:
ureg(unit)
except pint.errors.UndefinedUnitError:
return False
return True
@alpha-beta-soup
alpha-beta-soup / Makefile
Last active May 21, 2017 06:41
d3 map experiment with NZTM
build/ESRI_Census_Based_2013_NZTM.zip:
mkdir -p $(dir $@)
curl -o $@ http://www3.stats.govt.nz/digitalboundaries/census/$(notdir $@)
build/ESRI_Census_Based_2013_NZTM.shp: build/ESRI_Census_Based_2013_NZTM.zip
unzip -od $(dir $@) $<
touch $@
build/wards.geojson: build/ESRI\ shapefile\ Output/2013\ Digital\ Boundaries\ Generalised\ Clipped/WARD2013_GV_Clipped.shp
ogr2ogr -f GeoJSON build/wards.geojson build/ESRI\ shapefile\ Output/2013\ Digital\ Boundaries\ Generalised\ Clipped/WARD2013_GV_Clipped.shp -s_srs EPSG:2193 -t_srs EPSG:2193
@alpha-beta-soup
alpha-beta-soup / rasterstats.py
Last active March 14, 2024 19:10
Example of running standalone QGIS 3 processing algorithms in Ubuntu Linux (typical installation)
#!/usr/bin/env python3
import os
import sys
import warnings
from functools import wraps
def ignore_warnings(f):
@wraps(f)
def inner(*args, **kwargs):