Skip to content

Instantly share code, notes, and snippets.

Avatar

Chao-Kuei Hung ckhung

View GitHub Profile
@ckhung
ckhung / gh2umap.sh
Last active Aug 29, 2015
convert GraphHopper exported gpx to geojson suitable for importing into umap
View gh2umap.sh
#!/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
View get-mail-header.perl
#!/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;
View slippy-tiles.py
#!/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 Sep 19, 2015
本程式把鄉鎮縣市的 geojson 檔裡面的 MultiPolygon 當中面積最大的一塊抓出來, 變成一個單獨的 Polygon, 就可以上傳至 umap 了。
View multi2maxpolygon.py
#!/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 Sep 19, 2015
find all "coordinates" field in a geojson file, and output x,y coordinates as csv
View geojson2csv.py
#!/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 Oct 25, 2015
convert all underscore variable names to cammel case
View toCammel.perl
#!/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 Dec 13, 2015
generate a random tree in .dot format (for graphviz to process)
View gen-dot-tree.py
#!/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 Dec 28, 2015
a legend (using d3-legend.js) that dynamically changes labels
View dynamic-legend.html
<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 Jan 2, 2016
filter adb logcat
View falc.perl
#!/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 Apr 3, 2016
tidy configuration file
View tidy.conf
# 詳見 「既輕鬆上手又無限擴充的跨平臺文字編輯器 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