Skip to content

Instantly share code, notes, and snippets.

View lpinner's full-sized avatar

Luke Pinner lpinner

View GitHub Profile
@lpinner
lpinner / merge_mean.py
Last active February 7, 2025 22:39
rasterio merge mean
from contextlib import ExitStack
import numpy as np
from pathlib import Path
from rasterio.merge import merge
from rasterio.io import DatasetWriter, DatasetReader
import rasterio as rio
def merge_mean(
sources: list[Path | str | DatasetReader],
dst_path: Path | str | DatasetWriter | None = None,
@lpinner
lpinner / rio_snap.py
Created February 24, 2024 01:05
Rasterio snap window
from rasterio import windows
def snap(window):
""" Handle rasterio's floating point precision (sub pixel) windows """
# Adding the offset differences to the dimensions will handle case where width/heights can 1 pixel too small
# after the offsets are shifted.
# This ensures pixel contains the bounds that were originally passed to windows.from_bounds()
col_off, row_off = math.floor(window.col_off), math.floor(window.row_off)
col_diff, row_diff = window.col_off - col_off, window.row_off - row_off
@lpinner
lpinner / osterrain50.py
Last active February 15, 2025 07:45
OS Terrain50 to GeoTIFF
# https://gis.stackexchange.com/a/462686
# CC-BY-SA 4.0
import os
import zipfile
from osgeo import gdal
gdal.UseExceptions()
input = "/path/to/terr50_gagg_gb.zip"
@lpinner
lpinner / Add_COG.pyt
Last active December 10, 2024 15:42
Add remote COG to ArcGIS Pro by URL
# -*- coding: utf-8 -*-
import os
import arcpy
from osgeo import gdal
class Toolbox(object):
def __init__(self):
self.label = "AddCOG Toolbox"
# dask geopandas rasterstats distributed demo
from dask.dataframe import from_delayed
from dask.distributed import Client, LocalCluster, as_completed
import dask_geopandas as dgpd
import geopandas as gpd
import pandas as pd
import scipy.stats
from rasterstats import zonal_stats
from functools import partial
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from rasterstats import gen_zonal_stats # if using ArcGIS Pro, clone arcgispro-py3 and install rasterstats into the clone.
def histogram(arr, bins=10):
"""Generate a histogram
@lpinner
lpinner / flatpak-themes
Last active October 22, 2020 22:28
Copy user or system GTK themes to flatpak runtimes
#!/usr/bin/python3
#licensed under the Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
""" Copy GTK themes to flatpak runtimes """
import sys
import shutil
import subprocess
from pathlib import Path
encoding = sys.getfilesystemencoding()
@lpinner
lpinner / resample_raster.py
Last active November 23, 2023 21:11
rasterio resample raster
# Copyright 2019 Luke Pinner
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@lpinner
lpinner / overlapping_windows.py
Last active May 28, 2021 12:17
rasterio overlapping windows
# Copyright 2019 Luke Pinner
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@lpinner
lpinner / sampling.py
Last active November 8, 2018 04:54
Sampling
from enum import Enum
import numpy as np
class Transform(Enum):
linear = 'linear'
log = 'log'
equal = 'equal'
halfcauchy = 'halfcauchy'