Skip to content

Instantly share code, notes, and snippets.

View richpsharp's full-sized avatar

Richard Sharp richpsharp

View GitHub Profile
@richpsharp
richpsharp / projected_vs_epsglatlng.py
Created October 5, 2020 18:47
IsProjected vs. EPSGTreatsAsLatLong
from osgeo import osr
projection_wkt = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]'
srs = osr.SpatialReference()
srs.ImportFromWkt(projection_wkt)
print(projection_wkt)
print(f'''IsProjected: {
srs.IsProjected()}. EPSGTreatsAsLatLong: {srs.EPSGTreatsAsLatLong()}''')
@richpsharp
richpsharp / eo_test.py
Created October 11, 2020 23:15
EO Pollination Test script
"""
EO Pollination test script.
docker run --rm -it -v "%CD%":/usr/local/workspace therealspring/inspring:latest eo_test.py
"""
import logging
import sys
import multiprocessing
import inspring
from osgeo import gdal
@richpsharp
richpsharp / pgp_entry_point.py
Created October 16, 2020 20:00
Entry point for base pygeoprocessing script
"""These calculations are for the Critical Natural Capital paper."""
import pygeoprocessing
gdal.SetCacheMax(2**27)
logging.basicConfig(
level=logging.DEBUG,
format=(
'%(asctime)s (%(relativeCreated)d) %(levelname)s %(name)s'
' [%(funcName)s:%(lineno)d] %(message)s'))
@richpsharp
richpsharp / datetime64_safe_serialization.py
Last active November 18, 2020 21:28
Example of pickleless numpy serialization
# coding=UTF-8
import io
import multiprocessing
import numpy
def numpy_dumps(numpy_array):
"""Safely pickle numpy array to string.
@richpsharp
richpsharp / flow_accum_study.py
Created February 4, 2021 02:13
Script to test many flow accumulation threshold values on a DEM while taking advantage of multiprocessing and avoided reexecution
"""TFA script for Stacie to experiment with TFAs."""
import logging
import os
import pygeoprocessing.routing
import multiprocessing
import sys
import taskgraph
WORKSPACE_DIR = 'workspace'
@richpsharp
richpsharp / time_slice.py
Created February 4, 2021 06:58
Time different kinds of slicing.
import time
import numpy
def mask_op(array_a, array_b, nodata):
result = numpy.full(array_a.shape, nodata, dtype=numpy.float)
valid_mask = numpy.ones(array_a.shape, dtype=numpy.bool)
valid_mask &= (array_a != nodata) & (array_b != nodata)
result[valid_mask] = array_a[valid_mask]+array_b[valid_mask]
return result
@richpsharp
richpsharp / example_floodplain_call.py
Last active February 12, 2021 18:56
Demo showing how to invoke the `floodplain_extraction` function
"""Tracer for floodplain extraction function"""
"""
def floodplain_extraction(
t_return_parameter,
min_flow_accum_threshold,
dem_path,
stream_gauge_vector_path,
gauge_table_path,
gauge_id_field,
@richpsharp
richpsharp / example_floodplain_custom_params_call.py
Created February 13, 2021 21:06
Floodplain extraction with custom power parameters
"""Tracer for floodplain extraction function with custom parameters."""
import logging
import os
import sys
from inspring.floodplain_extraction.floodplain_extraction import floodplain_extraction_custom_power_params
logging.basicConfig(
level=logging.DEBUG,
@richpsharp
richpsharp / example_stream_subwatershed_script.py
Created February 13, 2021 21:11
Demo of how to extract streams and subwatersheds
"""Tracer for floodplain extraction function with custom parameters."""
import logging
import os
import sys
import pygeoprocessing
import pygeoprocessing.routing
import numpy
logging.basicConfig(
@richpsharp
richpsharp / ndr_plus_scenario.py
Last active March 1, 2021 20:44
NDR plus scenario example
"""Tracer for NDR watershed processing."""
import argparse
import glob
import logging
import multiprocessing
import os
import shutil
import subprocess
import threading
import urllib