Skip to content

Instantly share code, notes, and snippets.

View ericrobskyhuntley's full-sized avatar
🌹

Eric Robsky Huntley ericrobskyhuntley

🌹
  • MIT Department of Urban Studies and Planning
  • Cambridge, MA
  • 22:53 (UTC -04:00)
View GitHub Profile
@ericrobskyhuntley
ericrobskyhuntley / index.html
Last active April 10, 2024 15:01
Demoing functions to offset circles by a data-driven quantity (hack necessary because of lack of data-driven styling on `'circle-translate'`).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add a line to a map using a GeoJSON source</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
@ericrobskyhuntley
ericrobskyhuntley / owner_occupied_units.R
Last active September 18, 2023 15:41
R script to approximate the number of units in owner-occupied 2-, 3-, and 4- unit buildings in Somerville.
df <- sf::st_read("M274_parcels_CY22_FY23_sde.gdb", layer = "M274Assess") |>
dplyr::mutate(
simp = dplyr::case_when(
stringr::str_detect(USE_CODE, "^0?101") ~ "101",
stringr::str_detect(USE_CODE, "^0?104") ~ "104",
stringr::str_detect(USE_CODE, "^0?105") ~ "105",
stringr::str_detect(USE_CODE, "^0?109") ~ "109",
stringr::str_detect(USE_CODE, "^0?111") ~ "111",
stringr::str_detect(USE_CODE, "^0?112") ~ "112",
),
@ericrobskyhuntley
ericrobskyhuntley / pums_mobile_homes.R
Last active May 25, 2023 18:46
Script to aggregate PUMS responses to reproduce and extend table characterizing the demographics of mobile home residents found in Esther Sullivan, Manufactured Insecurity Mobile Home Parks and Americans’ Tenuous Right to Place. (Berkeley, CA: University of California Press, 2018). Made for David Tisel for a forthcoming article in the _Review of…
library(tidycensus)
library(dplyr)
library(tidyr)
library(gt)
# vars <- tidycensus::pums_variables %>%
# dplyr::filter(year == 2017, survey == "acs5")
#
# vars_housing <- vars %>%
# dplyr::filter(level == "housing")
@ericrobskyhuntley
ericrobskyhuntley / pair_locator.py
Last active August 30, 2021 04:17
Align aerial imagery from O-D points (current use case is mines and industry headquarters.
import geopandas as gpd
import pandas as pd
import contextily as ctx
from rasterio import open as rioopen
from rasterio import band
from rasterio.mask import mask
from rasterio.plot import reshape_as_image, show
from rasterio.transform import Affine
from rasterio.warp import calculate_default_transform, reproject, Resampling
import rioxarray
---
output:
pdf_document: default
---
# Spatial Multilevel Modeling of Life Expectancy
```{r message=FALSE, warning=FALSE, echo=FALSE}
require('tidycensus')
require('readstata13')
require('stringr')
@ericrobskyhuntley
ericrobskyhuntley / prop_split.R
Created November 13, 2019 16:20
Estimate population adjacent to MBTA stops using Census API and MBTA API.
require('tidyverse')
require('tidycensus')
library('jsonlite')
require('sf')
census_api_key("<yourapikey>")
stops <- fromJSON("https://api-v3.mbta.com/stops/?filter[route]=Red", simplifyVector = TRUE)[[1]]$attributes %>%
st_as_sf(coords = c('longitude', 'latitude')) %>%
st_set_crs(4326) %>%
@ericrobskyhuntley
ericrobskyhuntley / generate_mult_grid.py
Last active April 10, 2019 19:40
Generate Grids for Dispersed Study Regions Based on Regional Extents
from qgis import *
import processing
proj_path = QgsProject.instance().readPath("./")
layers = iface.mapCanvas().layers()
for n, i in enumerate(layers):
ext = i.extent()
size = 5280
@ericrobskyhuntley
ericrobskyhuntley / geojson-merge.py
Created March 20, 2019 14:55
Python script to merge all GeoJSON files in a directory into a single GeometryCollection and write as a GeoJSON.
import os
import glob
import geojson
json_dir_name = "./"
json_pattern = os.path.join(json_dir_name,'*.geojson')
file_list = glob.glob(json_pattern)
collection = []
@ericrobskyhuntley
ericrobskyhuntley / global.R
Created March 15, 2019 20:01
R Shiny with Mapdeck
#
# In this global.R field, we create all objects and run all code
# that we only want to run once when we start our app and that we
# want accessible to all parts of our app. This includes database
# queries (in this case at least), API keys, etc.
#
# First, we load and attach packages. A package must be installed before
# loading; for example, to install RPostgreSQL, we simply type
# install.packages("RPostgreSQL").
#
@ericrobskyhuntley
ericrobskyhuntley / floodingmap.js
Last active March 13, 2019 20:57
Leaflet/CARTO Parcel Flooding Map Demonstrating CARTO PostGIS Queries. Files for workshop available here: http://duspviz.mit.edu/resources/carto_demo_s19.zip
var nwCorner = L.latLng(42.341055,-71.07289),
seCorner = L.latLng(42.398887,-70.948951),
bounds = L.latLngBounds(nwCorner, seCorner)
// Create Leaflet map object
var map = L.map('parcelMap',{
center: [42.371353, -71.034150],
zoom: 15,
minZoom: 14,