Skip to content

Instantly share code, notes, and snippets.

View urschrei's full-sized avatar
💭
🌒

Stephan Hügel urschrei

💭
🌒
View GitHub Profile
@urschrei
urschrei / shapefile.py
Last active September 29, 2021 08:24
Open a shapefile using Fiona, and plot its features using Matplotlib and Descartes
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from descartes import PolygonPatch
import fiona
from shapely.geometry import Polygon, MultiPolygon, shape
# We can extract the London Borough boundaries by filtering on the AREA_CODE key
mp = MultiPolygon(
[shape(pol['geometry']) for pol in fiona.open('data/boroughs/boroughs.shp')
if pol['properties']['AREA_CODE'] == 'LBO'])
@urschrei
urschrei / contour.ipynb
Last active March 14, 2021 16:11
Map interpolated values using a contour plot, and a scatter plot
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@urschrei
urschrei / basemap_descartes.py
Last active November 6, 2020 02:49
How to plot Shapely Points using Matplotlib, Basemap, and Descartes
"""
required packages:
numpy
matplotlib
basemap: http://matplotlib.org/basemap/users/installing.html
shapely: https://pypi.python.org/pypi/Shapely
descartes: https://pypi.python.org/pypi/descartes
random
@urschrei
urschrei / excel_things.py
Created May 7, 2011 12:41
Open CSV or Excel files, return the contents as a list of lists, write out to Excel file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
excel_things.py
Created by Stephan Hügel on 2011-05-07
Read XLS or CSV files, and return contents as unicode strings in nested lists
One row per list item, one column per nested list item:
[[u'foo', u'bar'], [u'baz', u'abc123'], … ]
@urschrei
urschrei / latlon_to_bng.py
Last active December 27, 2019 16:29
A function to convert WGS84 lat, long points to BNG (OSGB36) Eastings and Northings. Based entirely on code by Hannah Fry: http://hannahfry.co.uk/2012/02/01/converting-british-national-grid-to-latitude-and-longitude-ii/
import math
def bng(input_lat, input_lon):
"""
Convert WGS84 lat and long (ie. from GPS) to OSGB36 (BNG)
Expects two floats as input
Returns a tuple of Easting, Northing floats
Accurate to ~5m
For testing purposes:
@urschrei
urschrei / hexagram.py
Last active October 4, 2019 08:42
Generate and dump a hexagram in PNG format
# -*- coding: utf-8 -*-
# Requires PIL (pillow) and NumPy
# Copyright (C) Stephan Hügel, 2016
# License: MIT
import sys
from PIL import Image
import numpy as np
@urschrei
urschrei / jenks_zero.py
Created September 22, 2013 17:45
Calculate Jenks natural breaks on a dataset containing zero values, using Pandas and Pysal
import pandas as pd
import numpy as np
from pysal.esda.mapclassify import Natural_Breaks as nb
df = pd.DataFrame({'density': np.random.randint(0, 10, 500)})
# replace zero values with NaN
df.replace(to_replace={'density': {0: np.nan}}, inplace=True)
breaks = nb(df[df['density'].notnull()].density.values, k=5)
# this index will allow us to perform the join correctly
@urschrei
urschrei / lib.rs
Last active September 25, 2018 12:24
Detect whether an (extended) ASCII string is a palindrome anagram using Rust
// Given a string, how do you determine whether it's an anagram of a palindrome?
// Solution: Given the unique set of chars, at most 1 should appear in the string
// an odd number of times.
// chars() returns Unicode Scalar Values, and these might not match up
// with grapheme clusters, so this can't be assumed to work correctly
// for anything other than (extended) ASCII
fn unique_chars(s: &str) -> String {
let mut v: Vec<char> = s.chars().collect();
// dedup removes consecutive identical elements, so we need a sort first
#!/usr/bin/env python
# needs Pandas, Geopandas, Shapely. Should work on 2.7.x and 3.6.x
# this will only dump properties that are open or partially open today
# this can be adjusted by commenting out or altering the values given to isin()
from datetime import date
import geopandas as gp
import pandas as pd
from shapely.geometry import Point, LineString, Polygon, MultiPolygon, MultiPoint, box
@urschrei
urschrei / glasshole.sh
Created May 31, 2014 14:13
The below script will find and detect Google Glass on the local network and kick them off. Read the comments for more details.
#!/bin/bash
# original: http://julianoliver.com/output/log_2014-05-30_20-52
# GLASSHOLE.SH
#
# Find and kick Google Glass devices from your local wireless network. Requires
# 'beep', 'arp-scan', 'aircrack-ng' and a GNU/Linux host. Put on a BeagleBone
# black or Raspberry Pi. Plug in a good USB wireless NIC (like the TL-WN722N)
# and wear it, hide it in your workplace or your exhibition.
#
# Save as glasshole.sh, 'chmod +x glasshole.sh' and exec as follows: