Skip to content

Instantly share code, notes, and snippets.

View snorfalorpagus's full-sized avatar

Joshua Arnott snorfalorpagus

View GitHub Profile
@snorfalorpagus
snorfalorpagus / raster_statistics.py
Created November 5, 2013 14:49
Calculate raster statistics (min, max, mean) for each polygon intersecting a raster
#!/usr/bin/env python
import numpy
try:
from osgeo import gdal
except:
import gdal
try:
from osgeo import ogr
except:
@snorfalorpagus
snorfalorpagus / grid.geojson
Created January 14, 2014 21:36
Intersection sample data
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@snorfalorpagus
snorfalorpagus / export_to_csv.sh
Last active March 8, 2024 17:13
Simple conversion utility for Microsoft Access databases (.mdb) to SQLite3.
#!/bin/bash
# usage: export_to_csv.sh <database.sqlite>
sqlite3 $1 "SELECT tbl_name FROM sqlite_master WHERE type='table' and tbl_name not like 'sqlite_%';" | while read table; do
echo $table
sqlite3 $1 <<!
.headers on
@snorfalorpagus
snorfalorpagus / basic.py
Created September 27, 2014 13:28
Shapely and multiprocessing
#!/usr/bin/env python
'''
A simple geoprocessing task: for each feature in layer1, find the distance to
all features in layer2 that are within 5 map units
'''
import fiona
from shapely.geometry import shape
from shapely.ops import nearest_points
import time
@snorfalorpagus
snorfalorpagus / res.py
Created September 3, 2015 10:27
Simple linear programme for a reservoir system with a target level
'''
A simple model of a reservoir, which supplies water to a demand center and can
request water from another supply. The objective is to minimise the deficit in
supply to the demand center, then to minimise the difference between the
reservoir level at the end of the timestep and the target level.
variables:
- volume demanded by reservoir
- deficit between target volume and final volume
- volume supplied by reservoir
@snorfalorpagus
snorfalorpagus / demo2.c
Last active September 8, 2015 23:13
lp_solve demo
/*
A very simple example using lp_solve, with the following problem:
Objective function
min: -9 C1 -10 C2;
Constraints
R1: -0 <= +C1 <= 10;
+C1 -C2 = 0;
@snorfalorpagus
snorfalorpagus / fiona_cache.py
Created November 10, 2015 14:23
Speedups geoprocessing with Fiona and LRU caching
class CollectionLRU(fiona.Collection):
"""Wrapper for Fiona Collection that provides LRU read caching
"""
def __init__(self, *args, **kwargs):
fiona.Collection.__init__(self, *args, **kwargs)
self.cache = LRU(200)
def __getitem__(self, key):
try:
feature = self.cache[key]
except KeyError:
@snorfalorpagus
snorfalorpagus / pyqt_signal_slot_wrapper.py
Created January 13, 2016 15:15
Example of connecting a PyQt signal to a method with a decorator
# -*- coding: utf-8 -*-
from PySide import QtCore, QtGui
def wrapper(fn):
print('inside wrapper...')
def wrapped(*args, **kwargs):
print('inside wrapped!')
fn(*args, **kwargs)
return wrapped
@snorfalorpagus
snorfalorpagus / split.py
Created January 23, 2016 10:02
Shapely ST_Split
from shapely.geometry import Point, LineString, Polygon, GeometryCollection, MultiLineString
from shapely.ops import polygonize, unary_union, nearest_points
from shapely.wkt import loads as load_wkt
def split_polygon_line(polygon, line):
"""Split a Polygon with a LineString"""
assert(isinstance(polygon, Polygon))
assert(isinstance(line, LineString))
boundary = polygon.boundary
union = boundary.union(line)
@snorfalorpagus
snorfalorpagus / README.md
Created January 25, 2016 10:37
Using distutils and build_clib to build a C library

Using distutils and build_clib to build a C library

This repository demonstrates how to build a C library that is included in the module distribution.

To build the C library:

python setup.py build_clib