Skip to content

Instantly share code, notes, and snippets.

View wriglz's full-sized avatar

Simon Wrigley wriglz

View GitHub Profile
@wriglz
wriglz / rename_multiple_columns.sql
Created December 5, 2018 16:25
Rename columns across multiple tables in the same schema in Postgres
-- Call function with the schema name
CREATE OR REPLACE FUNCTION rename_columns(_schema text) RETURNS VOID AS $func$
DECLARE
rec RECORD;
table RECORD;
result RECORD;
sql TEXT := '';
i INTEGER;
BEGIN
@wriglz
wriglz / drop_all_connections.sql
Last active January 9, 2020 14:41
Drop all connections to a Postgres database so you can drop the DB. Make sure you are not currently connected to the database that you wish to drop!
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB' -- ← change this to your DB
AND pid <> pg_backend_pid();
@wriglz
wriglz / delete_lines_from_txt_file_containing_specific_string.sh
Last active January 22, 2020 16:57
Remove lines from a text file containing a specific string
# Directly modify file & create a backup
sed -i.bak '/pattern to match/d' ./input_file
# Directly modify file - no backup
sed -i '' '/pattern to match/d' ./infile
from pathlib import Path
import pandas
import geopandas
folder = Path("/path/to/shapefile/folder")
shapefiles = folder.glob("shapefile_name_with_wildcards_*.shp")
gdf = pandas.concat([
geopandas.read_file(shp)
for shp in shapefiles
]).pipe(geopandas.GeoDataFrame)
WITH data AS
(SELECT column_to_test,
count(*) OVER (PARTITION BY column_to_test), other_columns_to_output
FROM schema.table )
SELECT *
FROM data
WHERE count > 1
ORDER BY column_to_test;
# Remove all merged, closed branches from your local machine, leaving unmerged branches in place
git remote prune origin
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d
networkx
geopandas
pillow
matplotlib
import pandas as pd
import geopandas as gpd
from shapely import wkt
import time
# Read pub points from CSV to a pandas dataframe
pubs = pd.read_csv('~/Documents/pub_points.csv',
header=None, names=['name', 'coordinates'])
# Parse the WKT coordinate format from PostGIS
@wriglz
wriglz / bigquery_GeoJSON.sql
Created July 14, 2022 08:11
SQL to parse geometry object from GeoJSON feature collection for use in BigQuery
UPDATE
`project.dataset.table`
SET
geom = ST_GEOGFROMGEOJSON(
JSON_QUERY(geo_json,'$.geometry'), make_valid => TRUE)
WHERE
geom IS NULL
@wriglz
wriglz / ndjson.sh
Last active August 4, 2022 10:47
Parse JSON to new line delimited JSON using JQ
# NB. First install jq using homebrew
cat original.geojson | jq -c '.[]' > new_line_delimited.json