Skip to content

Instantly share code, notes, and snippets.

View richpsharp's full-sized avatar

Richard Sharp richpsharp

View GitHub Profile
@richpsharp
richpsharp / gpw_fragmentation.json
Last active July 7, 2025 20:16
"Fragmentation Index" using global pasture watch
var grassland_fragmentation = function(aoi, year)
{
var global_pasture_watch = ee.ImageCollection('projects/global-pasture-watch/assets/ggc-30m/v1/grassland_c');
var clipped_gpw = global_pasture_watch.filterDate(year + '-01-01', (year + 1) + '-01-01').first().clip(aoi);
// Anything > 1 is grassland "i.e. rangeland"
var grassland = clipped_gpw.gte(1);
//original code made a 7x7 square kernel on 10m, so we're doing a 3x3 for 30m as close enough
@richpsharp
richpsharp / process_dannys_file_to_pcld_format.py
Created June 11, 2025 23:14
Convert Danny's R table to a CSV and reproject and rename
"""Script to process Danny's RDS file into something that can be analyzed by the PCLD."""
import pyreadr
import geopandas as gpd
PROJECTION_EPSG = 32610
FILE_PATH = "Karp_LU_UseCase.RDS"
def main():
@richpsharp
richpsharp / sipa_to_table_legend.py
Created September 4, 2024 18:51
SIPA to table for Shail on 2024 09 04
import pandas as pd
datasets={
"PH_Top 10% of service overlap for _PH conservation": {
'fig_title' :"Top 10% of service overlap for , PH conservation",
'country' :"PH",
'labels' :["1 service","2 services","3 services","4 services"],
'visParams':{
'min':1,
'max':4,
@richpsharp
richpsharp / test_repeating_decimal.py
Created October 2, 2023 21:27
Can I guess if a decimal is a repeating decimal from a fraction?
import pandas as pd
repeating_dec_12ths = ['0833', '1666', '3333', '4166', '5833', '6666', '8333', '9166']
# 0.0833 - 1/12
# 0.1666 - 1/6
# 0.3333 - 1/3
# 0.4166 - 5/12
# 0.5833 - 7/12
# 0.6666 - 2/3
@richpsharp
richpsharp / Dockerfile
Last active August 7, 2021 05:27
gurobi with prioritizr installed too that also launches an Rscript on runtime
# run as:
# docker run --rm -it -v %CD%:/usr/local/workspace --volume=%CD%/gurobi.lic:/opt/gurobi/gurobi.lic:ro --volume=%CD%/models:/models:ro prioritizr ./run-prioritizations.R
FROM gurobi/optimizer
RUN apt-get update -qq
RUN apt-get install -y libgdal-dev
RUN apt-get install -y r-base r-base-dev
RUN apt-get install -y libudunits2-dev
RUN apt-get install -y libssl-dev
@richpsharp
richpsharp / convert_isimpi2b_netcdf_to_gtiff.py
Created June 11, 2021 01:07
Convert ISIMPI2B netcat files to geotiffs
"""Convert isimib2 to geotiff."""
import os
import glob
import xarray as xr
from osgeo import gdal
from osgeo import osr
LOCAL_RASTER_CREATION_OPTIONS = (
'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=LZW',
@richpsharp
richpsharp / c_factor_min_evi.py
Created May 25, 2021 04:00
Find a good C factor
import numpy
import scipy.optimize
evi_vs_c = [
(0.02874, 0.95),
(0.18186, 0.25),
(0.3643000126, 0.13575),
(0.33039, 0.07075),
(0.54826, 0.035425),
(0.65346, 0.0001),
@richpsharp
richpsharp / align_clip_and_mask.py
Last active May 13, 2021 21:07
how to clip to area and mask by vector bounds and values
vector = gdal.OpenEx(vector_path, gdal.OF_VECTOR)
layer = vector.GetLayer()
layer.SetAttributeFilter('Country="USA"') # or whatever...
feature = layer.GetNextFeature()
geom = feature.GetGeometryRef()
envelope = geom.GetEnvelope()
target_bb = [envelope[i] for i in [0, 2, 1, 3]])
geom = None
feature = None
@richpsharp
richpsharp / beta_tests.py
Created April 12, 2021 20:53
is beta/m broken?
import numpy
import matplotlib.pyplot
slope_in_radians_list = numpy.linspace(0.089, 0.785, 100)
beta_list = []
m_list = []
for slope_in_radians in slope_in_radians_list:
beta = ((numpy.sin(slope_in_radians) / 0.0896) /
(3 * numpy.sin(slope_in_radians)**0.8 + 0.56))
m_list.append(beta/(1+beta))
@richpsharp
richpsharp / sdr.py
Created April 10, 2021 18:34
Modified LS factor Python code from rafa
"""InVEST Sediment Delivery Ratio (SDR) module.
The SDR method in this model is based on:
Winchell, M. F., et al. "Extension and validation of a geographic
information system-based method for calculating the Revised Universal
Soil Loss Equation length-slope factor for erosion risk assessments in
large watersheds." Journal of Soil and Water Conservation 63.3 (2008):
105-111.
"""
import os