Skip to content

Instantly share code, notes, and snippets.

View ckhung's full-sized avatar

Chao-Kuei Hung ckhung

View GitHub Profile
@ckhung
ckhung / gh2umap.sh
Last active August 29, 2015 14:25
convert GraphHopper exported gpx to geojson suitable for importing into umap
#!/bin/bash
# usage: gh2umap.sh directions.gpx > directions.geojson
# see http://newtoypia.blogspot.tw/2015/07/fs-biking.html
TMPDIR=/tmp/gh2umap-$$
mkdir $TMPDIR
ogr2ogr -f geojson $TMPDIR/tr.geojson $1 tracks
jq '.features' $TMPDIR/tr.geojson > $TMPDIR/tracks.geojson
ogr2ogr -f geojson $TMPDIR/rp.geojson $1 route_points
opencc < $TMPDIR/rp.geojson | jq '.features | map(.properties+={_storage_options: {"iconClass": "Ball"}} | .properties.name=.properties.desc )' > $TMPDIR/route_points.geojson
#!/usr/bin/perl -w
# http://newtoypia.blogspot.com/2015/09/gmail-mbox.html
# 備份/清空 gmail, 以及命令列查詢/切割 mbox 格式檔案
use MIME::Base64;
while (<STDIN>) {
foreach $kw (@ARGV) {
next unless (($line) = /^$kw:\s*(.*)/);
$line = decode_base64($1)
if $line =~ /=\?utf-8\?\w+\?(.*)/i;
#!/usr/bin/python
# http://newtoypia.blogspot.com/2015/09/vector-tiles.html
# 用向量圖磚 (vector tiles) 自製陽春離線地圖
import math, sys, argparse, os, re
def deg2tile_id(zoom,lon,lat):
lat_rad = math.radians(lat)
n = 2.0 ** zoom
xtile = int((lon + 180.0) / 360.0 * n)
@ckhung
ckhung / multi2maxpolygon.py
Last active September 19, 2015 01:55
本程式把鄉鎮縣市的 geojson 檔裡面的 MultiPolygon 當中面積最大的一塊抓出來, 變成一個單獨的 Polygon, 就可以上傳至 umap 了。
#!/usr/bin/python
# -*- coding: utf-8 -*-
# http://newtoypia.blogspot.tw/2015/08/admin-boundary.html
# 「臺灣各級行政區域(縣市/鄉鎮/村里)邊界座標檔, 自選解析度、 存成 csv、 畫成 svg 」
import json, argparse, sys, math, warnings
def area(coords):
n = len(coords)
if n < 3:
@ckhung
ckhung / geojson2csv.py
Last active September 19, 2015 02:09
find all "coordinates" field in a geojson file, and output x,y coordinates as csv
#!/usr/bin/python
# -*- coding: utf-8 -*-
# http://newtoypia.blogspot.tw/2015/08/admin-boundary.html
# 「臺灣各級行政區域(縣市/鄉鎮/村里)邊界座標檔, 自選解析度、 存成 csv、 畫成 svg 」
import json, argparse, sys, math, warnings
def ispoint(coords):
return isinstance(coords, list) and isinstance(coords[0], (int, long, float)) and len(coords)==2
@ckhung
ckhung / toCammel.perl
Created October 25, 2015 10:41
convert all underscore variable names to cammel case
#!/usr/bin/perl -wp
my @saved = ();
while (s#((['"]).*?\2)#<<toCammelSkip>>#) {
push @saved, $1;
}
while (/([a-z0-9]+)(_[a-z0-9]+)+/i) {
$cammelled = $orig = $&;
$cammelled =~ s/(_[a-z0-9]+)/$x=substr($1,1); "\u$x"/ieg;
@ckhung
ckhung / gen-dot-tree.py
Created December 13, 2015 12:34
generate a random tree in .dot format (for graphviz to process)
#!/usr/bin/python
import argparse, numpy as np
def gen_tree(depth, id, branch):
label = id if args.label is None else args.label
print ' '*depth + id + '[label="' + label + '"];'
if depth >= args.depth:
return
nchild = np.random.poisson(branch)
@ckhung
ckhung / dynamic-legend.html
Last active December 28, 2015 06:12
a legend (using d3-legend.js) that dynamically changes labels
<html>
<head>
</head>
<body>
<div id="main-content">
</div>
<script type="text/javascript" src="d3.js"></script>
<script type="text/javascript" src="d3-legend.js"></script>
<script>
var canvas = d3.select('#main-content')
@ckhung
ckhung / falc.perl
Last active January 2, 2016 09:17
filter adb logcat
#!/usr/bin/perl -w
# http://newtoypia.blogspot.tw/2015/08/cordova-debug.html
# 徒手除錯 Cordova
$kw = ($ARGV[0] or "console");
while (<STDIN>) {
if (m#console#i) {
print "\x1b[30;46m$_\x1b[m";
} elsif (m#$kw#i) {
print "\x1b[31m$_\x1b[m";
} elsif (not m#^[VDIW]/#) {
@ckhung
ckhung / tidy.conf
Last active April 3, 2016 07:54
tidy configuration file
# 詳見 「既輕鬆上手又無限擴充的跨平臺文字編輯器 geany」
# http://newtoypia.blogspot.tw/2016/03/geany.html
tidy-mark: yes
markup: yes
wrap: 40
tab-size: 8
indent: auto
indent-spaces: 2
output-xhtml: yes
doctype: loose