Skip to content

Instantly share code, notes, and snippets.

View underchemist's full-sized avatar

Sebastien Tremblay-Johnston underchemist

View GitHub Profile
@underchemist
underchemist / pelican-worfklow
Created December 5, 2017 22:46
My workflow for writing, building locally, publishing, and deploying builds of my pelican blog to ysebastien.me
# Overview
You have two github repositories linked to your blog.
- blog-source > root dir of blog, contains all pelican conf and output
- underchemist.github.io > only the contents of output/
# Writing and building locally
## fabric
The fabfile.py in the root of blog-source has a bunch of helper functions to aid with development.
- fab build > uses pelicanconf.py for config
@underchemist
underchemist / setup-gdal-compile-env.bat
Created October 10, 2019 02:13
Compiling GDAL python bindings on windows
rem Assumes visual studio build tools installed
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat
conda activate <env name>
conda install -c conda-forge gdal
rem Uninstalling pre-installed python bindings to show that we can compile ourselves
pip uninstall gdal
rem Set environment variables
set INCLUDE=C:\<conda install location>\envs\<env name>\Library\include;%INCLUDE%
@underchemist
underchemist / install-rasterio-win10
Last active November 14, 2020 17:59
Install rasterio from source on windows
conda create -n gdal242 python=3.7
conda activate gdal242
conda install -c gdal==2.4.2
# from source with pip (MSVC build tools required)
git clone https://github.com/mapbox/rasterio.git
cd clone rasterio
set GDAL_VERSION=2.4.2
# if you have a newer conda you can use %CONDA_PREFIX%
# note: --no-use-pep517 seems to be required as a workaround
@underchemist
underchemist / geojsonio-extent.sh
Created October 18, 2019 21:25
view extent of raster(s) on geojson.io
# requires rasterio, fiona, geojsonio-cli (npm or python clone)
rio bounds --feature *.tif | fio collect | geojsonio
# alternative
# find /path/to/some/dir -name "*.tif" -exec rio bounds --feature {} \; | fio collect | geojsonio
set CURL_CA_BUNDLE=C:\path\to\cert # for some reason this alone is not enough
set GDAL_HTTP_UNSAFESSL=YES
# get aws credentials i.e. through okta-awscli
gdalinfo /vsis3/bucket/key
@underchemist
underchemist / segmentize_polygon.py
Created February 15, 2020 00:56
Segmentize shapely polygons
""" Segmentize an input shapely polygon
For an input polygon, generate a new polygon composed of n points
interpolated along the linear geometry of each vertex of the input
polygon
"""
import numpy as np
import shapely
import shapely.geometry
import shapely.ops
LINEAR_GEOMS = (shapely.geometry.LineString, shapely.geometry.LinearRing)
VALID_GEOMS = (*LINEAR_GEOMS, shapely.geometry.Polygon)
def linspace(start, stop, n):
assert n >= 0
try:
step = (stop - start) / (n-1)
except ZeroDivisionError:
step = 0
@underchemist
underchemist / block_windows.py
Last active March 14, 2020 07:17
rasterio inspired block_windows implementation using itertools
from itertools import product, chain
def block_windows(src, xsize, ysize):
height, width = src.shape
nrows, rmod = divmod(height, ysize)
ncols, cmod = divmod(width, xsize)
offsets = product(range(0, width, xsize), range(0, height, ysize))
sizes = product(chain([xsize]*ncols, [cmod]), chain([ysize]*nrows, [rmod]))
for (xoff, yoff), (xsize, ysize) in zip(offsets, sizes):
yield xoff, yoff, xsize, ysize
from osgeo import gdal
import joblib
from itertools import cycle, product, chain
from more_itertools import ichunked
import time
import os
SRC = 'RS2_OK76385_PK678064_DK606753_F2N_20080419_142127_HH_HV_SLC/RS2_OK76385_PK678064_DK606753_F2N_20080419_142127_HH_HV_SLC'
def block_windows(src, xsize, ysize):
@underchemist
underchemist / gdal-translate-invalid-schema.py
Created April 4, 2020 20:12
GDAL translate produces VRT with invalid schema
from osgeo import gdal, osr
driver = gdal.GetDriverByName("GTiff")
src = driver.Create('/vsimem/test.tif', 32, 32, 1)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
src.SetGCPs((gdal.GCP(0, 0, 0, 0, 0),), srs)
dst = gdal.Translate('/vsimem/test.vrt', src)
print(dst.GetMetadata('xml:vrt'))