Skip to content

Instantly share code, notes, and snippets.

View jwass's full-sized avatar

Jacob Wasserman jwass

View GitHub Profile
@jwass
jwass / geojsonio_embed.ipynb
Created August 18, 2014 18:02
Embed geojson.io in a Jupyter / IPython notebook using geojsonio.py
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jwass
jwass / convert.py
Created August 10, 2013 21:13
Simple script used to convert some shapefiles to GeoJSON, leaving out a few undesired properties. Requires shapely 1.2.18.
import functools
import fiona
import geojson
import pyproj
import shapely.geometry
import shapely.ops
omit = ['SHAPE_AREA', 'SHAPE_LEN']
@jwass
jwass / convert.py
Created August 15, 2013 21:52
Simple Shapefile to GeoJSON converter. Using the shapefile from here: http://www.mass.gov/anf/research-and-tech/it-serv-and-support/application-serv/office-of-geographic-information-massgis/datalayers/senate2012.html it will result in an error "ValueError: Record's geometry type does not match collection schema's geometry type: 'Polygon' != 'Unk…
import fiona
import fiona.crs
def convert(f_in, f_out):
with fiona.open(f_in) as source:
with fiona.open(
f_out,
'w',
driver='GeoJSON',
crs = fiona.crs.from_epsg(4326),
@jwass
jwass / delaunay.py
Last active February 27, 2019 13:42
Example using proposed Delaunay triangulation method in Shapely
import matplotlib.pyplot as plt
import numpy as np
from shapely.geometry import LineString
from shapely.ops import triangulate
def uniform_sample(poly, n=100):
"""
Uniformly sample the Delaunay triangulation of a polygon. If the polygon
is convex, this will uniformly sample its area.
@jwass
jwass / cambridge_raw_osm.geojson
Last active April 11, 2016 04:12
Demonstrate using GeoPandas and geojsonio.py (http://github.com/jwass/geojsonio.py) to view styled data in a few lines. Running the script will open a browser with the final URL: http://geojson.io/#id=gist:/10218442
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jwass
jwass / README.md
Last active December 29, 2015 04:02
GeoPandas Tornado Analysis

Thinking about the Turf tornado analysis from https://www.mapbox.com/blog/60-years-of-tornadoes-with-turf/ and what the similar approacoh is in GeoPandas.

The two programs take slightly different approaches to the counting. Turf loops over the counties, counting how many tornadoes fall inside its borders. GeoPandas performs a spatial join - first forming a spatial index on the tornadoes. The joined GeoDataFrame combines the columns (properties) of both sets. Then a groupby operation is performed counting the number of entries for each county.

The Turf version is significantly faster - most likely due to the slow spatial join operation in GeoPandas and that all columns are included resulting in a large final DataFrame - and probably just that node is much faster than Python here. Overall, Turf's speed is impressive.

@jwass
jwass / contactinfo.py
Created August 25, 2013 18:08
Scrape MA state senate/representative contact info and merge it with the legislator's property info.
import difflib
import json
import re
import bs4
import pandas as pd
import requests
regex = re.compile('\((.*)\)')
@jwass
jwass / converted.geojson
Last active December 17, 2015 22:33
Example script using Pandas/GeoPandas to help @jqtrde. See https://gist.github.com/jacquestardie/ba32c3304bbe7d8e85b5
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jwass
jwass / README.md
Last active August 29, 2015 14:20
Logging imported Python modules

Quick idea to understand what modules get imported.

Example:

>>> import importlog

# Run a bunch of code
>>> import shapely.geometry
>>> p = shapely.geometry.Point(0.0, 0.0)
>>> b = p.buffer(1.0)
@jwass
jwass / example.json
Created March 20, 2015 16:17
Quick thoughts on representing a road network in JSON. Basically TopoJSON for OSM nodes/ways.
{
"type": "network",
"nodes": [
{"coordinates": [-71.13568, 42.38196], "properties": {}},
{"coordinates": [-71.13555, 42.382523], "properties": {"highway": "traffic_signals"}},
{"coordinates": [-71.134087, 42.38384], "properties": {"railway": "level_crossing"}},
{"coordinates": [-71.141007, 42.386862], "properties": {"highway": "crossing"}}
],
"geometries": [
{"type": "LineString", "nodes": [0, 1, 2], "properties": {"highway": "primary", "name": "Main Street"}},