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 / download-qeii.sh
Last active October 6, 2023 03:22
Experiments with a commit time modification in a Kart archive for pre-existing geospatial data
#!/bin/bash
CLEAR='\033[0m'
RED='\033[0;31m'
function usage() {
if [ -n "$1" ]; then
echo -e "${RED}👉 $1${CLEAR}\n";
fi
@alpha-beta-soup
alpha-beta-soup / stratified_sample.py
Last active April 2, 2023 22:52
Stratified random sampling of GPKG files
import sys
from pathlib import Path
import pandas as pd
import geopandas as gpd
def stratified_sample(df: pd.DataFrame, groupby_column: str, sampling_rate: float = 0.01) -> pd.DataFrame:
assert 0.0 < sampling_rate <= 1.0
assert groupby_column in df.columns
num_rows = int((df.shape[0] * sampling_rate) // 1)
@alpha-beta-soup
alpha-beta-soup / simplify.py
Created October 10, 2022 20:45
Geometry simplification script (GPKG, Snakemake, geopandas, pygeos)
import logging
from pathlib import Path
import sys
import warnings
smk = snakemake # type: ignore
logging.basicConfig(filename=Path(str(smk.log)), level=smk.params.get('logLevel', logging.INFO))
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) # Log to stdout as well
@alpha-beta-soup
alpha-beta-soup / chalet-demo-may-2022.ipynb
Created May 8, 2022 22:34
Demonstration of Uber's H3 DGGS which I did at the Palmerston North Regional GIS Forum (May 2022)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@alpha-beta-soup
alpha-beta-soup / h3_raster_windows.py
Created April 30, 2021 05:13
Utility functions for converting from a row/column index of a raster window, to longitude/latitude or an H3 index. Useful for H3-indexing raster data read with rasterio.
#!/usr/bin/env python3
import pyproj
import h3.api.basic_int as h3
# A window_transformation is the affine transformation of a window, see https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html?highlight=window_transform#window-transforms
# Transformer
transformer = lambda s_srs, t_srs: pyproj.Transformer.from_proj(pyproj.Proj(s_srs), pyproj.Proj(t_srs), always_xy=True)
# Row/column conversion to lon, lat; given a window transformation and pyproj.Transformer
rc2lonlat = lambda r, c, window_transformation, transformer: transformer.transform(*(window_transformation * (c, r)))
@alpha-beta-soup
alpha-beta-soup / sample-crontab
Last active January 8, 2020 04:23
Sets GNOME wallpaper to latest Himawari image
# Update wallpaper every 15 minutes
15 * * * * /bin/bash /home/richard/scripts/set-wallpaper.sh
@alpha-beta-soup
alpha-beta-soup / cartopy-nztm.py
Created August 29, 2019 21:32
Cartopy CRS definition for NZTM2000
import cartopy
import shapely.geometry as sgeom
class NZTM(cartopy.crs.TransverseMercator):
def __init__(self):
'''
https://epsg.io/2193
https://www.linz.govt.nz/data/geodetic-system/datums-projections-and-heights/geodetic-datums/new-zealand-geodetic-datum-2000-nzgd2000
'''
inverse_flattening = 298.257222101
@alpha-beta-soup
alpha-beta-soup / geojson-stringify.js
Last active July 5, 2019 02:50
GeoJSON serialiser that doesn't explode your nested coordinate arrays
const replacerWrapper = val => val.replace(/\"\[/g, '[').replace(/\]\"/g,']')
const replacer = (name, val) => {
console.log({name, val})
if (name === 'coordinates' && Array.isArray(val)) {
val = JSON.stringify(val)
}
return val
}
module.exports = {
replacer, replacerWrapper,
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://vocab.org/relationship/> .
@prefix wikipedia: <http://en.wikipedia.org/wiki/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
@prefix bio: <http://vocab.org/relationship/> .
<me> a foaf:PersonalProfileDocument ;
@alpha-beta-soup
alpha-beta-soup / Makefile
Last active April 4, 2019 21:49
gdalwarp as a gdal_merge.py alternative in a GNU Makefile
INPUT := a.tif b.tif c.tif d.tif e.tif
# This is how you could use gdal_merge.py to combine all the input files
# BUT: gdal_merge.py loads them all into memory first, so it can exceed your system memory very easily!
.ONESHELL:
all.gdal_merge.tif : $(INPUT)
gdal_merge.py -o $@ -of GTiff -co TILED=YES -co COMPRESS=LZW -co TFW=YES -co BLOCKXSIZE=128 -co BLOCKYSIZE=128 -co NUM_THREADS=ALL_CPUS -ot UInt16 -n 0 $^
# gdalwarp does not suffer from this limitation, but is a bit harder to use, so here's how I do it in a Makefile
all.gdalwarp.tif : $(INPUT)