Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Schep dschep

View GitHub Profile
@dschep
dschep / ST_LineChunk.sql
Last active Jan 10, 2021
Line chunking implementations for PostGIS
View ST_LineChunk.sql
CREATE OR REPLACE FUNCTION ST_LineChunk(geom geometry, max_length float8) RETURNS SETOF geometry AS $$
WITH
points AS (
SELECT generate_series(0, CEIL(ST_Length(geom) / max_length)::int)
/ CEIL(ST_Length(geom) / max_length) "end"
),
line_points AS (SELECT LAG("end", 1) OVER (ORDER BY "end") "start", "end" FROM points)
SELECT ST_LineSubstring(geom, "start", "end")
FROM line_points
WHERE "start" IS NOT NULL AND "start" <> 1
@dschep
dschep / ST_TileEnvelope.sql
Last active Jan 9, 2021
ST_TileEnvelope backport
View ST_TileEnvelope.sql
-- ported from https://github.com/pramsey/minimal-mvt/blob/58f3e695a305f42024dcf0ba395590bf39b0b573/minimal-mvt.py#L63-L81
CREATE OR REPLACE FUNCTION ST_TileEnvelope(tileZoom integer, tileX integer, tileY integer) RETURNS geometry AS $$
-- Width of world in EPSG:3857
DECLARE worldMercMax float = 20037508.3427892;
DECLARE worldMercMin float = -1 * worldMercMax;
DECLARE worldMercSize float = worldMercMax - worldMercMin;
-- Width in tiles
DECLARE worldTileSize float = power(2, tileZoom);
-- Tile width in EPSG:3857
DECLARE tileMercSize float = worldMercSize / worldTileSize;
@dschep
dschep / partially.py
Created Oct 1, 2020
A decorator version of functools.partial
View partially.py
def partially(*args, **kwargs):
"""
A decorator version of functools.partial
eg, this:
def _foo(x, y):
pass
foo = partial(_foo, 1)
is the same as:
@partially(1)
View useLocalStorageState.js
import { useState } from "react";
const useLocalStorageState = (storageKey, defaultValue) => {
const [value, setValue] = useState(
JSON.parse(localStorage.getItem(storageKey) || "null") || defaultValue
);
const wrappedSetValue = (newValue) => {
localStorage.setItem(storageKey, JSON.stringify(newValue));
setValue(newValue);
@dschep
dschep / metro.svg
Created Jan 23, 2020
Metro SVG from DC Metro Pro
View metro.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dschep
dschep / top9.py
Last active Aug 17, 2021
Instagram Top 9 - example output: https://www.instagram.com/p/B6vVp93Bb5F/
View top9.py
"""
An Instagram Top 9 generator that doesn't require connecting your account
to an untrustworthy 3rd party Instagram app.
Install the requirements with `pip3 install igramscraper Pillow click`
then run `python3 top9.py`
When done, you will have a YOURUSERNAME-top9.jpg in your working directory.
"""
from datetime import datetime
View data.json
{
"features": [
{
"geometry": {
"coordinates": [
[
[
-76.99545991625094,
38.8494249445368
],
@dschep
dschep / bookmarklet.js
Last active Mar 3, 2020
Restore AWS shortcuts
View bookmarklet.js
javascript:(() => {
const newSc = prompt('enter shortcuts to restore');
/* Parse cookies into a map */
const cookies = new Map(document.cookie.split(/\s*;\s*/g).map(kv => kv.split('=')));
/* decode and parse the noflush_awscnm cookie */
const noflush_awscnm = JSON.parse(decodeURIComponent(cookies.get('noflush_awscnm')));
/* set shortcuts to those input by user */
noflush_awscnm.sc = newSc.split(',');
/* set the noflush_awscnm cookie (stringified & encoded) */
document.cookie = `noflush_awscnm=${encodeURIComponent(JSON.stringify(noflush_awscnm))}`;
@dschep
dschep / bookmarklet.js
Last active Nov 28, 2018
Get list of current AWS shortcuts
View bookmarklet.js
javascript:(() => {
/* Parse cookies into a map */
const cookies = new Map(document.cookie.split(/\s*;\s*/g).map(kv => kv.split('=')));
/* decode and parse the noflush_awscnm cookie */
const noflush_awscnm = JSON.parse(decodeURIComponent(cookies.get('noflush_awscnm')));
/* display the comma delimited shortcuts to user */
alert(`current shortcuts: ${noflush_awscnm.sc.join(',')}`);
})()
View dirtfest-camping.py
#!/usr/bin/env python
from requests_html import HTMLSession
session = HTMLSession()
resp = session.get('https://mailchi.mp/badcee76d7db/groupcamping?e=f74653273d')
groups = resp.html.find('#docs-internal-guid-242d83ee-6477-f6f4-5448-18ced87471f3', first=True).text.split('\n')
sites = resp.html.find('#docs-internal-guid-349ccfaa-6479-e25f-134f-289c9f089e3a', first=True).text.split('\n')
for group, site in zip(groups, sites):
print(f'{group}: {site}')