Skip to content

Instantly share code, notes, and snippets.

View bitner's full-sized avatar

David Bitner bitner

View GitHub Profile
@bitner
bitner / tipg2pgstac.ipynb
Last active December 12, 2023 14:35
tipg2pgstac.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bitner
bitner / tipg-pgstac.ipynb
Created December 12, 2023 13:55
TiPG STAC Items
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bitner
bitner / collection_summaries.sql
Last active August 31, 2022 21:52
collection_summaries.sql
/*
This create function script must be run as a user that owns the collections table!
By setting SECURITY DEFINER on the function, we are telling Postgres that this function will run
with the permissions of the user that created the function rather than the user that is calling the function.
Because this does add a bit of security risk, we take a bit of extra precaution and schema qualify
any tables that we are referencing and set the SEARCH_PATH to only explicity include pg_catalog and pg_temp schemas.
By running the function as the user that owns the collections table, we are able to enable and disable the triggers that are set on
that table.
@bitner
bitner / pg_funstuff_for_tammo.sql
Last active February 23, 2022 21:44
pg_funstuff_for_tammo.sql
-- Make sure that you do all of this in your own schema
-- Postgres has a search path where it is always going to create new things (tables, functions, etc) in the first entry that exists in your search_path
SHOW search_path;
-- pgstac lives in the pgstac schema, to use pgstac without needing to prefix everything
-- you always want to have public in your search_path as that is where all the main things in the database are (including postgis - think of it like /usr/bin)
SET SEARCH_PATH to pgstac, public;
-- Create your own schema to play with these things
CREATE SCHEMA playground;
@bitner
bitner / pgstac_aggregate.sql
Last active March 3, 2022 23:54
pgstac_aggregate.sql
-- Function to take an x, y, z and turn it into a quadkey text string
CREATE OR REPLACE FUNCTION quadkey(zoom int, tx int, ty int) RETURNS text AS $$
DECLARE
i int;
digit int;
quadkey text := '';
mask int;
BEGIN
FOR i IN REVERSE zoom..1 LOOP
digit := 0;
import psycopg
import os
import time
# Get a an iterator to use for sample of half million ids cached to a file
def sample_ids():
file='sampleids.csv'
if not os.path.exists(file):
with open(file, 'wb') as f:
with psycopg.connect(os.environ.get('PGURL')) as conn:
@bitner
bitner / incremental_summary.sql
Last active January 20, 2022 22:31
incremental_summary.sql
\set ON_ERROR_STOP ON
BEGIN;
DROP SCHEMA IF EXISTS anthony_test CASCADE;
CREATE SCHEMA anthony_test;
SET SEARCH_PATH to anthony_test, public;
CREATE TABLE downloads(
id bigint GENERATED ALWAYS AS IDENTITY primary key,
created_at timestamptz NOT NULL DEFAULT now(),
username text NOT NULL,
@bitner
bitner / mailersend.http
Created November 30, 2021 20:38
mailersend.http
‎‎​
@bitner
bitner / jsonb_replace_text.sql
Created October 27, 2021 18:58
jsonb_replace_text.sql
‎‎​
@bitner
bitner / dump_smallsats.sh
Last active November 8, 2021 15:09
dump_smallsats.sh
#!/bin/bash
psql <<EOD
SET SEARCH_PATH to public, data;
DROP VIEW IF EXISTS items_as_ndjson;
CREATE OR REPLACE VIEW items_as_ndjson AS
SELECT
date_trunc('month', datetime)::date as month,
datetime,
jsonb_build_object(
'id', items.id,