Skip to content

Instantly share code, notes, and snippets.

View lpinner's full-sized avatar

Luke Pinner lpinner

View GitHub Profile
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
# 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
@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"
@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 / 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 / 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,