Skip to content

Instantly share code, notes, and snippets.

View urschrei's full-sized avatar
💭
🌒

Stephan Hügel urschrei

💭
🌒
View GitHub Profile
@urschrei
urschrei / parseml.py
Last active March 25, 2024 02:20
Extract attachments from EML files in the current dir, and write them to the output subdir
#!/usr/bin/env python
"""
2020 update:
- More iterators, fewer lists
- Python 3 compatible
- Processes files in parallel
(one thread per CPU, but that's not really how it works)
"""
@urschrei
urschrei / epsg:2169.txt
Created October 31, 2023 09:35
proj4 string obtained by running "projinfo epsg:2169" using a PROJ 9.3 installation
PROJ.4 string:
+proj=tmerc +lat_0=49.8333333333333 +lon_0=6.16666666666667 +k=1 +x_0=80000 +y_0=100000 +ellps=intl +towgs84=-189.6806,18.3463,-42.7695,-0.33746,-3.09264,2.53861,0.4598 +units=m +no_defs +type=crs
WKT2:2019 string:
PROJCRS["LUREF / Luxembourg TM",
BASEGEOGCRS["LUREF",
DATUM["Luxembourg Reference Frame",
ELLIPSOID["International 1924",6378388,297,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
@urschrei
urschrei / extract_exif_gps.py
Last active October 26, 2023 20:11
Extract GPS data from jpg files, and write it to a CSV. Requires the PIL. Tested (haha) on Python 2.7.x
"""
Extract GPS coordinates and filename, output to CSV file
Run this file from the same directory the images are in
run using ./process_exif.py or python process_exif.py, or
%run process_exif.py from an IPython instance
Ensure you have PIL installed
refer to http://www.exiv2.org/tags.html for a full detailed tag listing
This is what the GPSInfo dict looks like for an iPhone 5 jpg:
@urschrei
urschrei / extract_from_gpx.py
Last active February 23, 2023 22:38
Extract basic GPS data (lat, lon, elevation, timestamp) from GPX, and dump it into a CSV file. Requires the gpxpy library.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Output Elevation, Lat, Long, and Timestamp series from GPX to CSV
Requires gpxpy
This script expects your input GPX to be located in a subdir named 'data'
"""
import os
@urschrei
urschrei / ci.py
Created January 31, 2014 12:15
Calculate and plot Statsmodels OLS and WLS confidence intervals
from statsmodels.stats.outliers_influence import summary_table
from statsmodels.sandbox.regression.predstd import wls_prediction_std
# carry out yr fit
# ols cinv
st, data, ss2 = summary_table(ols_fit, alpha=0.05)
fittedvalues = data[:,2]
predict_mean_se = data[:,3]
@urschrei
urschrei / png_to_polygon_to_geojson.py
Last active November 16, 2022 16:58
Read a PNG into a numpy array, convert it to a Shapely Polygon, and dump it as GeoJSON
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created by Stephan Hügel on 2017-03-02
The MIT License (MIT)
Copyright (c) 2017 Stephan Hügel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@urschrei
urschrei / hexgrid.py
Last active May 4, 2022 11:57
Python Hexgrid
import math
def calculate_polygons(startx, starty, endx, endy, radius):
"""
Calculate a grid of hexagon coordinates of the given radius
given lower-left and upper-right coordinates
Returns a list of lists containing 6 tuples of x, y point coordinates
These can be used to construct valid regular hexagonal polygons
@urschrei
urschrei / horror.py
Created September 8, 2019 14:36 — forked from sbma44/horror.py
Lovecraft word pairs by rarity of vocab
# sample invocation:
# pdftotext -f 5 /tmp/The_Complete_Works_of_H.P._Lovecraft.pdf - | python3 horror.py | uniq > lovecraft_word_pairs_sorted.txt
import re
import sys
import nltk
import wordfreq
re_word = re.compile(r'[^\-\w]')
@urschrei
urschrei / minimum_polygon_distance.md
Last active November 29, 2021 22:18
An algorithm for determining the minimum distance between two non-convex polygons

Adapted from https://www.quora.com/How-do-you-compute-the-distance-between-two-non-convex-polygons-in-linear-time/answer/Tom-Dreyfus

Calculating the Minimum Distance Between Two Non-Convex Polygons

See Amato, Nancy M (1994) for details of the difference between separation (sigma: σ) and closest visible vertex (CVV).

Refer to P and Q as the two polygons with n and m vertices, respectively.
For the purposes of this discussion, a key insight is that it is enough to find the closest edge to each vertex in order to compute the minimum separation between P and Q.
This means iterating over all vertices, and finding a nearest neighbour. Thus, a time complexity in O((m + n) * log(m * n)) should be expected.

@urschrei
urschrei / bresenham.rs
Last active October 10, 2021 00:39
Return evenly-spaced samples from an array using Bresenham's line algorithm
/// Return evenly-spaced samples from an array using [Bresenham's line algorithm](https://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html)
/// Adapted from https://observablehq.com/@mbostock/evenly-spaced-sampling
///
/// # Explanation
/// This works because the problem is equivalent to drawing a line on a `num_samples` x `arr` grid of
/// discrete pixels:
/// `x` coordinates are in the range `0…arr - 1` and `y` coordinates are in the range `0…num_samples - 1`
/// We then proceed as if we were drawing a line from the origin to `arr - 1, num_samples - 1`:
/// Whenever the `y` coordinate changes we choose a new element from `x`.
///