Skip to content

Instantly share code, notes, and snippets.

View invisiblefunnel's full-sized avatar

Danny Whalen invisiblefunnel

View GitHub Profile
def build_linestring_gdf(gdf, sortby, groupby):
crs = gdf.crs
gdf = gdf.sort_values(sortby).groupby(groupby).geometry.apply(lambda x: LineString(x.tolist()))
return GeoDataFrame({groupby: gdf.index}, crs=crs, geometry=gdf.values)
def build_point_gdf(df, xcol, ycol, crs={'init': 'epsg:4326'}):
geometry = [Point(x, y) for x, y in zip(df[xcol], df[ycol])]
df = df.drop([xcol, ycol], axis=1)
gdf = GeoDataFrame(df, crs=crs, geometry=geometry)
return gdf
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@invisiblefunnel
invisiblefunnel / e.py
Last active October 29, 2017 18:33
Approximating Euler's Number in Python
from random import random
def epproximate(count, debug=False):
cumsum = 0.
for i in range(int(count)):
x = 0
while x <= 1:
x += random()
cumsum += 1
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Guide to converting a postgres table to shapefile

Run brew install gdal --with-postgresql to install the ogr2ogr utility.

Then run the following command to create a shapefile from the table:

ogr2ogr -f "ESRI Shapefile" myshape.shp PG:"<db url>" "my_db_table"

Finally, you’ll need to zip the myshape.* files together.

@invisiblefunnel
invisiblefunnel / .block
Created July 4, 2017 23:35 — forked from mbostock/.block
Sankey Diagram
border: no
license: gpl-3.0
@invisiblefunnel
invisiblefunnel / total_cash_spec.rb
Last active February 17, 2017 17:31
Total Cash test in PostgreSQL
require "pg"
# Credit:
# https://git.io/vDQet
# https://git.io/vDQeZ
# Total Cash test
#
# Another famous test of snapshot isolation anomaly.
#
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
# users = User.includes(:orgs).order(:id).page(params[:page] || 1)
users = User.order(:id).page(params[:page] || 1)
render json: users, include: :orgs
end
end