Skip to content

Instantly share code, notes, and snippets.

View nautilytics's full-sized avatar

Nautilytics nautilytics

View GitHub Profile
@nautilytics
nautilytics / index.sql
Last active February 11, 2021 16:04
Create a GeoJSON of India from ne_10m_admin_0_countries
COPY
(
SELECT row_to_json(fc) AS json_out
FROM (
SELECT 'FeatureCollection' AS type,
array_to_json(array_agg(f)) AS features
FROM (
SELECT 'Feature' AS type,
st_asgeojson(st_transform(st_union(geom), 4326))::json AS geometry
FROM ne_10m_admin_0_countries
@nautilytics
nautilytics / map.jsx
Created January 28, 2021 19:31
UNHCR Mapbox implementation with React
<ReactMapGL
{...props.state.viewport}
width="100%"
height="100%"
mapStyle={state.mapStyle}
onViewportChange={_onViewportChange}
dragRotate={false}
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
onHover={_onHover}
onClick={_onMouseClick}
@nautilytics
nautilytics / index.js
Created January 27, 2021 15:52
Use turf.js to find all CharityNavigator services within a 50-mile radius/buffer of a city
import * as turf from '@turf/turf';
// Create a FeatureCollection from all of the resources
const featureCollection = turf.featureCollection(resources.features);
// Get the lat/long of the selected feature
const point = turf.point(rows.length ? rows[0]?.geometry?.coordinates : [0, 0]);
// Create a 50-mile buffer around the selected point
const buffered = turf.buffer(point, 50, { units: 'miles' });
@nautilytics
nautilytics / map.jsx
Created January 26, 2021 21:16
Rendering a performant Mapbox census tract map using react-map-gl and React Hooks
import * as React from 'react';
import ReactMapGL, { Layer, Source } from 'react-map-gl';
import { useEffect, useRef, useState } from 'react';
import { DATA_PROPERTY, LAYER_PROPERTIES, FILL_COLOR_PAINT, SELECTED_LAYER } from '../../constants';
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles((theme) => ({
map: {
width: '100%',
height: '100%',
@nautilytics
nautilytics / census-tract-exploration.sql
Last active January 26, 2021 17:12
Steps related to creating a census tract map using the Diversity Data Kids API, PostGIS, and Mapbox
# ...retrieve data from current and previous data sets - http://data.diversitydatakids.org [great API for census level data]
# ...and match current w/ previous census tract ids
# Drop table and view before starting
DROP
VIEW IF EXISTS clipped_census_tracts_with_all_data;
DROP
TABLE IF EXISTS census_tract_with_all_data_points;
@nautilytics
nautilytics / index.js
Last active November 24, 2020 17:14
A React Mapbox choropleth map generated from US Census Tract IDs using Mapbox Expressions
<ReactMapGL
...
mapStyle="mapbox://styles/graphicacy/<style_id>>"
mapboxApiAccessToken={ACCESS_TOKEN}
>
<Source id={LAYER_NAME} type="vector" url="mapbox://graphicacy.<dataset_id>" />
<Layer
type="fill"
id="fill_layer"
source={LAYER_NAME}
@nautilytics
nautilytics / import.sh
Created November 5, 2020 16:05
Retrieve all US Census Tract files from Census.gov and upload to a PostGIS database
#!/bin/bash
#######
# Note:
# This OS X tested script assumes you have a PostGIS-enabled database named gis_db
# and the command line tool shp2pgsql installed.
#######
# Easiest to begin inside the /Downloads directory
cd ~/Downloads
@nautilytics
nautilytics / handler.js
Last active November 15, 2020 17:48
A Node.js serverless implementation of appending an email to a Google Sheet via AWS API Gateway and AWS Lambda
'use strict';
const { GoogleSpreadsheet } = require('google-spreadsheet');
const middy = require('middy');
const { cors } = require('middy/middlewares');
const { to } = require('await-to-js');
const moment = require('moment');
const data = require('./primarycast-sheets.json');
@nautilytics
nautilytics / import.sh
Created September 24, 2020 13:55
An automated approach to retrieving Census Tract shape-files for every state and inserting into a Postgres database
#!/bin/bash
cd ~/Downloads
# Go through each state FIPs code and get their places SHP file
StateFIPS=(
"01" "02" "04" "05" "06" "08" "09" "10"
"11" "12" "13" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "27"
"28" "29" "30" "31" "32" "33" "34" "35"
@nautilytics
nautilytics / deploy_to_gh_pages.yml
Created September 16, 2020 20:34
Deploy a React application to Github Pages
name: Build and Deploy a React application to GitHub-Pages
on:
push:
branches:
- master
jobs:
build-and-deploy-to-gh-pages:
runs-on: ubuntu-latest