Skip to content

Instantly share code, notes, and snippets.

View tastatham's full-sized avatar

Thomas Statham tastatham

View GitHub Profile
@tastatham
tastatham / rioxarray.py
Created May 5, 2022 08:28
A list of functions for rioxarray
def list_tifs(path):
import glob
tifs = []
for p in glob.glob(f"{path}/*/**/*.tif", recursive=True):
t = p
if "_HS_" not in t:
tifs.append(t)
return tifs
@tastatham
tastatham / updated_h3fy.py
Last active May 1, 2022 19:34
Get all h3 cells within total bounds, rather than evaluating all bounds - also much faster
def _h3fy_updated(gdf, resolution):
"""
h3_radius values calculated using;
```python
import numpy as np
import pandas as pd
df = pd.read_html("https://h3geo.org/docs/core-library/restable/")[0]
y = df["Average Hexagon Area (km2)"].to_numpy()
@tastatham
tastatham / gadm_usa.py
Created February 24, 2022 10:39
dask_geopandas
import geopandas
import dask_geopandas
path = "https://geodata.ucdavis.edu/gadm/gadm4.0/gpkg/gadm40_USA.gpkg"
gdf = geopandas.read_file(path, layer="ADM_1")
gdf.head()
# Filter and rename cols
gdf = gdf[["NAME_1", "geometry"]].rename(columns={"NAME_1": "State"})
@tastatham
tastatham / spatial_shuffle.py
Last active September 23, 2021 16:25
spatial_shuffle.py
def spatial_shuffle(ddf, by="hilbert", column=None, npartitions=20, p=10, **kwargs):
"""
A function that spatially shuffles a Dask-GeoSeries object by a method
or a user-defined column
Parameters
----------
by : str
partitioning method or column
import pandas
import geopandas
import rioxarray
from shapely.geometry import box
def _polygonize(rioxarray_obj):
""""""
poly_ls = []
@tastatham
tastatham / gpd_ring_buffers.py
Created April 13, 2021 09:34
Updated Geopandas (constant) multi-ring buffer function with example
import pandas as pd
import geopandas as gpd
def multi_ring_buffers(gdf, number, distance):
"""
Apply a function to a GeoDataFrame containing Point data and compute constant multi-ring buffers
Parameters
----------
@tastatham
tastatham / dask_geomodeling_zonalstats.py
Last active April 24, 2020 11:23
dask_geomodeling_zonalstats.py
def compute_zonal_stats(raster, vector, fid, col, statistics,tile,mode):
"""
Calculate zonal statistics using dask
Parameters
----------
raster : A GeoDataFrame to create multiple copies based on the list of density thresholds
@tastatham
tastatham / dask_geomodeling_quick_not_quick.py
Last active March 2, 2020 10:52
Dask-geomodeling; Quick start is not "quick"
import dask
import dask_geomodeling
request = {
"mode": "vals",
"bbox": (138000, 480000, 139000, 481000),
"projection": "epsg:28992",
"width": 256,
"height": 256
}
graph, name = add.get_compute_graph(**request)
@tastatham
tastatham / dask_geomodeling_zonal_stats.py
Created March 1, 2020 13:52
Zonal statistics using dask_geomodeling
# Import modules
from dask_geomodeling.raster import RasterBlock
from dask_geomodeling.raster import base
from dask_geomodeling.geometry import base
# Define paths
arg_rast_path = 'raster.tif'
arg_vec_path = 'vector.shp'
"""
@tastatham
tastatham / gpd_to_postgis_example.py
Last active February 24, 2020 11:42
Geopandas to postgis example
# Load pkgs and data
import geopandas as gpd
data = gpd.read_file("https://gist.githubusercontent.com/HTenkanen/456ec4611a943955823a65729c9cf2aa/raw/be56f5e1e5c06c33cd51e89f823a7d770d8769b5/ykr_basegrid.geojson")
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Subset argentina as single ply
argentina = world[world['name'] == 'Argentina' ]
# Define engine
engine = create_engine("postgresql+psycopg2://[user]:[password]@localhost:5432/[my_db]")