Skip to content

Instantly share code, notes, and snippets.

View urschrei's full-sized avatar
💭
🌒

Stephan Hügel urschrei

💭
🌒
View GitHub Profile
@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 / 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`.
///
@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]')
#!/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 / 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
// Settings in here override those in "LSP/LSP.sublime-settings",
{
"clients":
{
"rls":
{
"auto_complete_triggers": [ {"selector": "source.rust", "characters": ".:"} ],
"diagnostics_highlight_style": "box",
"command": ["rustup", "run", "nightly", "rls"],
@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 / 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
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file target/release/lonlat_bng.dll
File Type: DLL
Section contains the following exports for lonlat_bng-f8cb032e62ae16ed.dll
@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