Skip to content

Instantly share code, notes, and snippets.

View caged's full-sized avatar
🎸

Justin Palmer caged

🎸
View GitHub Profile
@caged
caged / postgres-ordered-sets.md
Last active April 5, 2016 03:13
Examples of ordered set aggregates in Postgres

Examples of ordered set aggregates in Postgres.

SELECT round(avg(pie)::numeric, 2),
       percentile_cont(array[0.25, 0.5, 0.75, 0.95]) WITHIN GROUP (ORDER BY pie) AS percentiles
FROM player_stats_advanced
WHERE permode = 'pergame';

round | percentiles

@caged
caged / postgres-regex.sql
Last active December 26, 2015 21:52
Extract the first character of each string part separated by the '-'. There can be an unknown number of potential parts.
select array_to_string(array_agg(i), '') from
(select (regexp_matches('Letter-1-2', '[A-Z0-9]', 'g'))[1] i) t;
location
Luverne, Alabama
Madison, Alabama
Theodore, Alabama
Oneonta, Alabama
Odenville, Alabama
Heflin, Alabama
Jasper, Alabama
Midfield, Alabama
Greenville, Alabama
@caged
caged / index.html
Last active September 19, 2015 19:46 — forked from mbostock/.block
Cubehelix Color Picker
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
@caged
caged / point-dispersal.sql
Last active July 24, 2021 11:26
Naive dispersal of overlapping points in PostGIS
drop table if exists inspection_point_buffers;
-- Group identical overlapping points and count how many occupy
-- the space.
create temporary table inspection_overlappoing_points as
select a.geom as geom,
count(*)
from latest_inspections a,
latest_inspections b
where st_equals(a.geom, b.geom)
<!doctype html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: OpenSans, Helvetica;
}
.title {
margin: 0;
column_name
--------------------------------------------------------
crash_id
record_type
vehicle_id
participant_id
participant_display_seq
vehicle_coded_seq
participant_vehicle_seq
serial_
@caged
caged / feedback.md
Last active August 29, 2015 14:19
Quick feedback for municipalities working with open data
  • Datasets first, APIs second - Doing any kind of aggregate analysis usually requires working with complete datasets. REST APIs aren't ideal for this use case. APIs are not data, they are a means of exposing it.
  • Machine-friendly retrieval of raw datasets - Avoid the assumption that there's a human, using a web browser, manually clicking a link. For example, scripts that fetch new daily crime data via curl would be a likely scenario. Make it easy for machines by removing authentication, unnecessary redirects, JavaScript-based retrieval or POST-style retrieval.
  • Document long column names - Shapefile attributes are limited to 10 characters. This makes many attributes difficult to decipher without associated metadata. For example, here are a few attributes from a Garbage Collection dataset. Include a file with the long column name mappings and include both the long and short name in the metada
drop table if exists combined_geometries;
with boston_area_geometries as
( select name,
msa_code,
geom
from divisions
inner join
( select distinct on (msa_code) msa_code
from area_definitions ) ad on ad.msa_code = nctadvfp ),
@caged
caged / graphite-live.coffee
Created December 16, 2014 17:41
Autoupdating graphite timeseries in d3
#= require d3
# Draw timeseries graphs to the screen. Each element can contain a set of
# data-* attributes used to configure the graph. The graph should always include
# a data-url attribute pointing to an endpoint for time series JSON data.
#
# Any graph that includes a data-realtime attribute will update automatically.
#
# Examples:
# <div class="js-graph" data-url="/graphite?target=github.unicorn.{browser,api}.cpu_time.mean&amp;from=-1hour" data-realtime></div>