Skip to content

Instantly share code, notes, and snippets.

View svHatch's full-sized avatar

Scott Hatcher svHatch

View GitHub Profile
@svHatch
svHatch / shapely_dissolve.py
Created April 11, 2019 18:44
Shapely dissolve
# Taken from https://gis.stackexchange.com/questions/149959/dissolving-polygons-based-on-attributes-with-python-shapely-fiona
from shapely.geometry import shape, mapping
from shapely.ops import unary_union
import fiona
import itertools
with fiona.open('cb_2013_us_county_20m.shp') as input:
# preserve the schema of the original shapefile, including the crs
meta = input.meta
with fiona.open('dissolve.shp', 'w', **meta) as output:
@svHatch
svHatch / Makefile
Created August 10, 2018 11:13 — forked from simonmichael/Makefile
Show UnionizeMe's latest expenditures with hledger
# Show UnionizeMe's latest expenditures with hledger
# https://www.unionizeme.org/donate-now#radical-transparency
# http://hledger.org/download (0.27 or greater suggested)
default: demo
unionizeme.csv:
curl -s -o unionizeme.csv 'https://docs.google.com/spreadsheets/d/1l1FoWxseOP07fBMauapG3hIX5DNVooGQTpQQC0sBNv4/export?format=csv&id=1l1FoWxseOP07fBMauapG3hIX5DNVooGQTpQQC0sBNv4&gid=0'
unionizeme.csv.rules:
# List unique values in a DataFrame column
# h/t @makmanalp for the updated syntax!
df['Column Name'].unique()
# Convert Series datatype to numeric (will error if column has non-numeric values)
# h/t @makmanalp
pd.to_numeric(df['Column Name'])
# Convert Series datatype to numeric, changing non-numeric values to NaN
# h/t @makmanalp for the updated syntax!
@svHatch
svHatch / tps-demo.py
Created February 20, 2018 20:35 — forked from bgshih/tps-demo.py
A simple example of Thin Plate Spline (TPS) transformation in Numpy.
import ipdb
import numpy as np
import numpy.linalg as nl
import matplotlib.pyplot as plt
from scipy.spatial.distance import pdist, cdist, squareform
def makeT(cp):
# cp: [K x 2] control points
# T: [(K+3) x (K+3)]
K = cp.shape[0]
@svHatch
svHatch / mp_fiona_reporject_test.py
Last active February 9, 2018 18:50 — forked from ischneider/mp_test.py
multiprocessing example using fiona and shapely
from multiprocessing import Pool
import sys
import fiona
from fiona.transform import transform_geom
from shapely.geometry import mapping, shape
import json
def reproject(f, srs_crs, dest_crs):
f['geometry'] = transform_geom(srs_crs, dest_crs, f['geometry'],
antimeridian_cutting=True,
@svHatch
svHatch / generate.py
Created May 11, 2017 18:54
Generating random geojson
from geojson import utils, Feature, FeatureCollection
import random
import json
group = []
for i in range(0, 1000):
feature = utils.generate_random(featureType='Point', numberFeatures=1, boundingBox=[144,-37,146,-38.5])
cartons = random.randrange(0, 3000)
rev = random.randrange(0, 30000)
newFeat = Feature(geometry=feature, properties={"cartons": cartons, "revenue": rev})