Skip to content

Instantly share code, notes, and snippets.

node buildLegend.js -w ${WIDTH} -h ${HEIGHT} # Build and output legend.svg, minus first line so it can be merged with topo.svg
tail -n +1 < legend.svg >> topo.svg
# clean up dir
find . -name "*.zip" -exec rm -f {} \;
rm states.geojson metroareas.geojson circles.geojson topo.geojson legend.svg
@lukewhyte
lukewhyte / buildLegend.js
Last active January 28, 2019 21:34
3rd example using node-D3
#! /usr/bin/env node
const fs = require('fs');
const D3Node = require('d3-node');
const parseCSV = require('csv-parse/lib/sync');
// Create a new D3Node instance and then save the path to the d3 library for standalone use.
const d3n = new D3Node();
const d3 = d3n.d3;
// Load in our CSV file, we'll use this to set our radius domain again.
@lukewhyte
lukewhyte / convert-geojson-to-svg.sh
Last active January 28, 2019 21:14
Convert GeoJSON to SVG
geoproject "${PROJECTION}.fitSize([${WIDTH}, ${HEIGHT}], d)" < topo.geojson | # Scale the geojson to needed specs
ndjson-split 'd.features' | # convert to ndjson
geo2svg --newline-delimited -w ${WIDTH} -h ${HEIGHT} | # convert ndjson to svg
sed '$d' > topo.svg # remove last line for splice with legend and output
@lukewhyte
lukewhyte / mapAndMergeStates.js
Created January 28, 2019 20:47
2nd example from Medium article
#! /usr/bin/env node
const fs = require('fs');
// Import the state geodata and the metro area circles geodata
const states = JSON.parse(fs.readFileSync('./states.geojson', 'utf8'));
const circles = JSON.parse(fs.readFileSync('./circles.geojson', 'utf8'));
// Filter and map the state features to include as little data as possible
const parseStates = features => features.map(({ type, properties, geometry }) => ({
type,
@lukewhyte
lukewhyte / mapMetroPathsToCircles.js
Created January 28, 2019 20:32
Example from Medium article on Turf/D3 maps
#! /usr/bin/env node
const fs = require('fs');
const turf = require('@turf/turf');
const parseCSV = require('csv-parse/lib/sync');
const d3 = require('d3');
// Import the metro areas geodata as an object and the percent of undocumented residents data as an array of obj.
const metro = JSON.parse(fs.readFileSync('./metroareas.geojson', 'utf8'));
const undocumented = parseCSV(fs.readFileSync('./metro-area-percent-undocumented.csv', 'utf8'), { columns: true });
@lukewhyte
lukewhyte / convert-shp-to-json.sh
Created November 12, 2018 00:10
Convert shapefiles to GeoJSON
shp2json cb_2017_us_state_20m.shp > states.geojson # Convert state shapefile to geojson
shp2json cb_${YEAR}_us_cbsa_20m.shp > metroareas.geojson # Convert metro area shapefile to geojson
@lukewhyte
lukewhyte / download-extract.sh
Last active January 29, 2019 21:46
Download and extract Census and Pew data
#!/bin/bash
# Year to pull census geo data from
YEAR=2017
# Conditionally download the Pew data
if [ ! -f metro-area-percent-undocumented.csv ]; then
curl -Lo metro-area-percent-undocumented.csv \
"https://query.data.world/s/awyahzfiikyoqi5ygpvauqslwmqltr"
fi
// Includes functions for exporting active sheet or all sheets as JSON object (also Python object syntax compatible).
// Tweak the makePrettyJSON_ function to customize what kind of JSON to export.
var FORMAT_ONELINE = 'One-line';
var FORMAT_MULTILINE = 'Multi-line';
var FORMAT_PRETTY = 'Pretty';
var LANGUAGE_JS = 'JavaScript';
var LANGUAGE_PYTHON = 'Python';
@lukewhyte
lukewhyte / warningregex.js
Created July 7, 2016 21:57
Regex to hide err msgs beginning with "Warning:" in chrome dev tools
^(?!Warning:).*$
@lukewhyte
lukewhyte / filter_sources.js
Created May 14, 2016 00:29
Handles retrieval, pagination and filtering of a list of data sources returned from an API
/**
* used to populate the source List... (so we can add sources to reports)
*/
$scope.filterSources = {
filterVal: '',
privatePublic: 'all',
resetPagination: false,