Skip to content

Instantly share code, notes, and snippets.

View underchemist's full-sized avatar

Yann-Sebastien Tremblay-Johnston underchemist

View GitHub Profile
@underchemist
underchemist / ipython-sqlite3.gotcha
Created October 31, 2022 19:35
WIN GOTCHA: IPython and sqlite3.dll
If you are working with python module that depends on a custom sqlite3.dll, IPython on startup will load sqlite3.dll from the installation's vendored DLLs. This can cause a conflict resulting in a failed DLL load since the module that needs the custom sqlite3.dll cannot override the currently loaded DLL (DLL HELL).
WORKAROUND: copy the custom sqlite3.dll to the installation location
@underchemist
underchemist / wheel_repair.py
Last active November 16, 2020 16:01
Windows version of auditwheel adapted from @vinayak-mehta and @duburcqa
#!/usr/bin/env python
# This tool has been copied from https://github.com/vinayak-mehta/pdftopng/blob/main/scripts/wheel_repair.py
# and extended to supported hierarchical folder architecture with mulitple .pyd
# to update, and to move all the DLL in a common folder *package*.lib installed
# jointly with the package itself, similarly to auditwheel on Linux platform.
#(see also https://discuss.python.org/t/delocate-auditwheel-but-for-windows/2589/9).
# Additionally adapted from https://github.com/Wandercraft/jiminy/blob/648c0ec918ca2c2f734a32bada1dbfaf6226de48/build_tools/wheel_repair.py
# added sorting of pyd and dll files such that repaired dlls pointing to their own
@underchemist
underchemist / python39dllloading
Last active November 16, 2020 05:36
Python 3.9 DLL loading change
>>> import os
>>> os.add_dll_directory('C:\path\to\dll\dir')
>>> import rasterio
@underchemist
underchemist / rasterio-warp-with-rpcs-example.py
Created November 5, 2020 02:25
rasterio warp with rpcs example
"""
RPC_DEM_SRS only used in GDAL >= 3.2.0, otherwise ignored
"""
import rasterio
import rasterio.warp
from rasterio import logging
logging.basicConfig(filename='rasterio.log', level=logging.DEBUG)
@underchemist
underchemist / projsync-no-ssl-check.bat
Created June 23, 2020 18:14
Disabling ssl checks while using projsync
rem https://github.com/OSGeo/PROJ/blob/475e7fd9f11af184e7e1e1618b3e1bb110a79714/src/networkfilemanager.cpp#L1613
set PROJ_UNSAFE_SSL=YES
projsync --list-files
@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'))
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 / 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
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 / 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