Skip to content

Instantly share code, notes, and snippets.

View tastatham's full-sized avatar

Thomas Statham tastatham

View GitHub Profile
<style>
/* change background colours around elements */
.mapboxgl-ctrl.mapboxgl-ctrl-attrib {
background-color: rgba(86, 1, 1, 0.7);
}
.mapboxgl-ctrl.mapboxgl-ctrl-attrib a {
color: #d17d61 !important;
}
/* change background colours around elements */
.mapboxgl-ctrl-group {
---
title: "R Notebook"
output: html_notebook
---
```{r}
# create list of countries who qualified and their placement in the wc
qualified <- c('Spain','Nigeria','Egypt','Costa Rica','Japan','Tunisia', 'France','Switzerland','Portugal', 'Russia','Colombia','Peru','Senegal','United Kingdom','Denmark','Brazil','Germany','Iran (Islamic Republic of)','Korea, Republic of','Morocco', 'Belgium','Croatia','Saudi Arabia','Uruguay','Iceland','Argentina','Panama','Poland','Australia','Mexico','Sweden','Serbia')
# knocked out of group stage
var beforeMap = new mapboxgl.Map({
container: 'before',
style: 'mapbox://styles/tastatham1/cjjy2a3sd4lx02sqcse1jkgyd',
center: [0, 43.825234],
pitch:20,
zoom: 4
});
var afterMap = new mapboxgl.Map({
@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]")
@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 / 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_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 / 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
----------
import pandas
import geopandas
import rioxarray
from shapely.geometry import box
def _polygonize(rioxarray_obj):
""""""
poly_ls = []
@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