Skip to content

Instantly share code, notes, and snippets.

View fredj's full-sized avatar

Frédéric Junod fredj

View GitHub Profile
from pylons.decorators.util import get_pylons
from decorator import decorator
from geojson import FeatureCollection, dumps
@decorator
def geojsonify(func, *args, **kwargs):
pylons = get_pylons(args)
pylons.response.content_type = 'application/json'
data = func(*args, **kwargs)
if isinstance(data, list): # sucks !!
from pylons import request, response
from simplejson import dumps
def jsonify(data):
func = request.params.get('jsonp') or \
request.params.get('callback') or \
request.params.get('cb')
body = dumps(data, indent=None if request.is_xhr else 2)
response.charset = 'utf8'
if func is not None:
-module(spatial).
-export([bounds/1]).
bounds({Atom, Coords}) when Atom == point; Atom == linestring; Atom == polygon ->
{lists:min([X || {X, _} <- Coords]),
lists:min([Y || {_, Y} <- Coords]),
lists:max([X || {X, _} <- Coords]),
lists:max([Y || {_, Y} <- Coords])};
bounds(_) ->
error.
@fredj
fredj / gist:1167619
Created August 24, 2011 08:53
use decimal to convert string to float
from decimal import *
if 'x' in request.params:
# deactivate all traps
getcontext().traps = {}
x = Decimal(request.params['x'])
if x.is_infinite() or x.is_nan():
# x is NaN, -Infinity or +Infinity
pass
else:
@fredj
fredj / index.html
Created March 21, 2012 12:39
raster tile color
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>OpenLayers Example</title>
<link rel="stylesheet" href="http://www.openlayers.org/dev/theme/default/style.css" type="text/css">
<style>
.into-water {
@fredj
fredj / gist:3054199
Created July 5, 2012 15:01
postgresql update enum (< 9.1)
BEGIN;
ALTER TABLE missionevents ADD COLUMN type_text text;
UPDATE missionevents SET type_text = type::text;
DROP TYPE missionevent_type CASCADE;
CREATE TYPE missionevent_type AS ENUM ('location', 'battery_level', 'start', 'end', 'cancel', 'light', 'expire', 'battery_low', 'comment', 'warning', 'error', 'temperature_list', 'temperature_low', 'temperature_high');
ALTER TABLE missionevents ADD COLUMN type missionevent_type;
UPDATE missionevents SET type = type_text::missionevent_type;
ALTER TABLE missionevents DROP COLUMN type_text;
@fredj
fredj / gist:3413732
Created August 21, 2012 08:57
shpxy
"type": "MultiLineString",
"coordinates": [[shpxy xh="[" yf="]" cs=", " ph="[ " pf=" ]" ps=","]]
"type": "MultiPolygon",
"coordinates": [[shpxy xh="[" yf="]" cs=", " ph="[ " pf=" ]" ps="," irh="[" irf="]" orh="[" orf="]"]]
@fredj
fredj / awb.py
Created October 5, 2012 12:19
AWB (Air waybill) number validation
import re
def valid(awb):
match = re.match('\d{3}(?P<serial>\d{7})(?P<check>\d{1})', str(awb))
if match:
serial, check = map(int, match.group('serial', 'check'))
_, rem = divmod(serial, 7)
return rem == check
else:
@fredj
fredj / awb.py
Created October 25, 2012 15:38 — forked from twpayne/awb.py
AWB (Airway bill) number validation
import re
def valid(awb):
m = re.match('\d{3}(?P<serial>\d{7})(?P<check>\d)\Z', str(awb))
return int(m.group('serial')) % 7 == int(m.group('check')) if m else False
@fredj
fredj / gist:4160631
Created November 28, 2012 11:35
overpass-api
http://www.overpass-api.de/api/xapi_meta?way[builing=*]