Skip to content

Instantly share code, notes, and snippets.

@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)))

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 / 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()
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 / 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',
@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 / Rock, paper, scissors.py
Created December 17, 2016 09:21
Rock, paper, scissors created by walkermatt - https://repl.it/EsHd/15
# Rock, paper, scissors
def beats(thing1, thing2):
""" Determine who wins rock, paper, scissors
by passing each players choice """
if thing1 == thing2:
# Draw, both players chose the same thing
return None
# Determine which thing beats thing2
victors = {
@walkermatt
walkermatt / spatialtype.md
Created June 22, 2015 11:02
mapinfo.mapinfo_mapcatalog spatialtype values for PostGIS

MapInfo requires a mapinfo.mapinfo_mapcatalog relation to be present in a PostGIS database in order to load relations. The spatialtype column is a float which indicates the geometry type of the relation:

19.0 = POINT, MULTIPOINT
19.1 = LINESTRING, MULTILINESTRING
19.2 = POLYGON, MULTIPOLYGON
19.3 = GEOMETRY
@walkermatt
walkermatt / a.gml
Created November 26, 2013 09:40
Test case to reproduce ogr2ogr error loading feature types with no geometry into PostgreSQL 9.1 / PostGIS 1.5. Sample data has been munged to avoid copyright issues.
<?xml version="1.0"?>
<FeatureCollection osgb="http://www.ordnancesurvey.co.uk/xml/namespaces/osgb" gml="http://www.opengis.net/gml" fid="GDS-1389940-6811" xsi="http://www.w3.org/2001/XMLSchema-instance" xlink="http://www.w3.org/1999/xlink" schemaLocation="http://www.ordnancesurvey.co.uk/xml/namespaces/osgb http://www.ordnancesurvey.co.uk/xml/schema/v7/OSDNFFeatures.xsd">
<description>Ordnance Survey, (c) Crown Copyright. All rights reserved, 2013-10-28</description>
<roadMember>
<Road fid="osgbfoo">
<version>5</version>
<versionDate>2006-02-06</versionDate>
<theme>Road Network</theme>
<changeHistory>
<changeDate>2003-03-13</changeDate>
@walkermatt
walkermatt / astun.py
Created November 13, 2013 19:52
Example of configuring Astun Tech QGIS-Gazetteer-Plugin (https://github.com/AstunTechnology/QGIS-Gazetteer-Plugin) to use a specific iShare location search (Surrey Heath Borough Council in this instance).
from json import loads
from collections import namedtuple
url = "http://isharemaps.surreyheath.gov.uk/getdata.aspx"
params = {
'type': 'json',
'RequestType': 'LocationSearch',
'gettotals': 'true',
'axuid': '1344265603167',
'mapsource': 'SurreyHeath/AllMaps',