Skip to content

Instantly share code, notes, and snippets.

@walkermatt
walkermatt / ObjectifyJSONEncoder.py
Last active December 20, 2016 17:29 — forked from aisipos/objectifiedJson.py
JSON encoder that can handle simple lxml objectify types, based on the original: https://gist.github.com/aisipos/345559, extended to accommodate encoding child nodes with the same tag name as a list.
import json
import lxml
from lxml import objectify
class ObjectifyJSONEncoder(json.JSONEncoder):
""" JSON encoder that can handle simple lxml objectify types,
based on the original: https://gist.github.com/aisipos/345559, extended
to accommodate encoding child nodes with the same tag name as a list.
Usage:
@walkermatt
walkermatt / osgb_map.js
Created July 2, 2012 18:25
Leaflet Map using Proj4Leaflet to display a projected TMS base map from MapProxy
// Bounding box of our TMS that define our area of interest
var bbox = [-600000, -300000, 1300000, 1600000];
// Resolutions of our TMS that the tiles will be displayed at calculated so
// that at resolution 0 the bounds fit one 256x256 tile: (maxx - minx)/256
var res = [7421.875, 3710.9375, 1855.46875, 927.734375];
// Define the Proj4Leaflet coordinate reference system base on British National Grid
var crs = L.CRS.proj4js(
'EPSG:27700',
import itertools
import qgis
from qgis.gui import QgsMessageBar
from qgis.core import QgsMapLayer
def layer_info(layer):
print "Layer name: %s" % layer.name()
print "CRS: %s" % layer.crs().authid()
print "Extent: %s" % layer.extent().asWktCoordinates()
@walkermatt
walkermatt / pyqgis_filter_vector_features.py
Created March 17, 2017 16:31
For use in the QGIS Python console
# Just the selected features
layer = iface.activeLayer()
for feature in layer.selectedFeatures():
print feature['name']
# Filter by current extent of the map
extent = iface.mapCanvas().extent()
request = QgsFeatureRequest()
request.setFilterRect(extent)
layer = iface.activeLayer()

Python psycopg2 & PostGIS

Load some data into the database

From the OSGeo4W Shell

ogr2ogr -f PostgreSQL PG:"dbname=postgis host=localhost user=postgres password=postgres" -a_srs "EPSG:27700" county_region.shp -nlt MULTIPOLYGON

Use psycopg2 in the Python Interpreter

@walkermatt
walkermatt / fizzbuzz.clj
Last active July 12, 2017 14:08
fizzbuzz without conditionals in Clojure
;; fizzbuzz without conditionals in Clojure
; Simple patten matching using a single map lookup
(defn fizzbuzz [x]
(let [v [(= (mod x 3) 0) (= (mod x 5) 0)]]
({[true false] "fizz"
[false true] "buzz"
[true true] "fizzbuzz"
[false false] x} v)))
""" Minimal MapProxy Middleware demonstrating wrapping MapProxy and working
with the query string
To run:
1. Install MapProxy in a virtual enviroment together with Gunicorn
2. Create a basic MapProxy config and copy this file into the same directory as mapproxy.yaml
2. Activate virtual environment
3. Change to the directory containing this file
4. Run:
gunicorn -k eventlet --workers=1 --log-file=- mapproxy_filter:application
This file has been truncated, but you can view the full file.
// OpenLayers. See https://openlayers.org/
// License: https://raw.githubusercontent.com/openlayers/openlayers/master/LICENSE.md
// Version: v4.5.0-70-gfca0b07
;(function (root, factory) {
if (typeof exports === "object") {
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
define([], factory);
} else {
root.ol = factory();
@walkermatt
walkermatt / scratch.js
Last active January 16, 2018 09:07
Ramda stuff
var R = require('ramda');
var states = [
{symbol: 'CT', name: 'Connecticut', pop: 3574097},
{symbol: 'ME', name: 'Maine', pop: 1328361},
{symbol: 'MA', name: 'Massachusetts', pop: 6547629},
{symbol: 'NH', name: 'New Hampshire', pop: 1316470},
{symbol: 'RI', name: 'Rhode Island', pop: 1052567},
{symbol: 'VT', name: 'Vermont', pop: 623741},
];
@walkermatt
walkermatt / confluence-table-to-csv.js
Last active February 21, 2018 11:42
Copy the data out of a Confluence table as CSV using Chrome devtools
function escapeAsCsv(str) {
// Double up double quotes and quote the entire string if necessary
if (str.includes(',') || str.includes('\n') || str.includes('"')) {
return '"' + str.replace(/"/g, '""') + '"';
}
return str;
}
// Export Confluence table as CSV
// Select the tbody element in Chrome devtools Elements panel then run the