From 00b136155183ae522ca458e540a8cf29bf525e74 Mon Sep 17 00:00:00 2001 From: typebrook Date: Thu, 27 Feb 2020 16:32:37 +0800 Subject: update --- tools/check_upstream | 25 ++++++ tools/csv.move_column | 18 +++++ tools/csv.reorder | 17 ++++ tools/csv2geojson | 46 +++++++++++ tools/gpx/footer | 1 + tools/gpx/gpx.check.py | 85 +++++++++++++++++++ tools/gpx/gpx.merge_gpx.sh | 5 ++ tools/gpx/gpx.merge_trk.sh | 4 + tools/gpx/header | 10 +++ tools/load-settings.sh | 24 ++++++ tools/match-road.sh | 148 ++++++++++++++++++++++++++++++++++ tools/mvt_decode.py | 11 +++ tools/osm/josm_install.sh | 8 ++ tools/osm/osm | 17 ++++ tools/osm/osm.api.changeset.add | 10 +++ tools/osm/osm.api.changeset.close | 3 + tools/osm/osm.api.changeset.create | 21 +++++ tools/osm/osm.api.changeset.update | 7 ++ tools/osm/osm.api.changeset.upload | 6 ++ tools/osm/osm.api.fetch | 6 ++ tools/osm/osm.api.fetch.full | 5 ++ tools/osm/osm.api.fetch.history | 6 ++ tools/osm/osm.api.member.relation | 4 + tools/osm/osm.api.referenced.relation | 6 ++ tools/osm/osm.api.referenced.way | 5 ++ tools/osm/osm.api.upload.to | 12 +++ tools/osm/osm.file.get | 4 + tools/osm/osm.file.get.full | 4 + tools/osm/osm.file.query | 4 + tools/osm/osm.goto | 3 + tools/osm/osm.help | 34 ++++++++ tools/osm/osm.list.ids | 3 + tools/osm/osm.list.tag | 10 +++ tools/osm/osm.list.tags | 15 ++++ tools/osm/osm.member.relation | 8 ++ tools/osm/osm.osc.by_member | 31 +++++++ tools/osm/osm.osc.by_tag | 45 +++++++++++ tools/osm/osm.osm.remove | 9 +++ tools/osm/osm.pbf.update | 39 +++++++++ tools/osm/osm.query | 3 + tools/osm/sequence_number.sh | 22 +++++ tools/refresh-todo.sh | 5 ++ tools/sync.sh | 6 ++ tools/upload-github-release-asset.sh | 84 +++++++++++++++++++ tools/yaoren.sh | 16 ++++ 45 files changed, 855 insertions(+) create mode 100755 tools/check_upstream create mode 100755 tools/csv.move_column create mode 100755 tools/csv.reorder create mode 100755 tools/csv2geojson create mode 100644 tools/gpx/footer create mode 100755 tools/gpx/gpx.check.py create mode 100755 tools/gpx/gpx.merge_gpx.sh create mode 100755 tools/gpx/gpx.merge_trk.sh create mode 100644 tools/gpx/header create mode 100755 tools/load-settings.sh create mode 100755 tools/match-road.sh create mode 100644 tools/mvt_decode.py create mode 100755 tools/osm/josm_install.sh create mode 100644 tools/osm/osm create mode 100755 tools/osm/osm.api.changeset.add create mode 100755 tools/osm/osm.api.changeset.close create mode 100755 tools/osm/osm.api.changeset.create create mode 100755 tools/osm/osm.api.changeset.update create mode 100755 tools/osm/osm.api.changeset.upload create mode 100755 tools/osm/osm.api.fetch create mode 100755 tools/osm/osm.api.fetch.full create mode 100755 tools/osm/osm.api.fetch.history create mode 100755 tools/osm/osm.api.member.relation create mode 100755 tools/osm/osm.api.referenced.relation create mode 100755 tools/osm/osm.api.referenced.way create mode 100755 tools/osm/osm.api.upload.to create mode 100755 tools/osm/osm.file.get create mode 100755 tools/osm/osm.file.get.full create mode 100755 tools/osm/osm.file.query create mode 100755 tools/osm/osm.goto create mode 100755 tools/osm/osm.help create mode 100755 tools/osm/osm.list.ids create mode 100755 tools/osm/osm.list.tag create mode 100755 tools/osm/osm.list.tags create mode 100755 tools/osm/osm.member.relation create mode 100755 tools/osm/osm.osc.by_member create mode 100755 tools/osm/osm.osc.by_tag create mode 100755 tools/osm/osm.osm.remove create mode 100755 tools/osm/osm.pbf.update create mode 100755 tools/osm/osm.query create mode 100755 tools/osm/sequence_number.sh create mode 100755 tools/refresh-todo.sh create mode 100755 tools/sync.sh create mode 100755 tools/upload-github-release-asset.sh create mode 100755 tools/yaoren.sh (limited to 'tools') diff --git a/tools/check_upstream b/tools/check_upstream new file mode 100755 index 0000000..3313e5b --- /dev/null +++ b/tools/check_upstream @@ -0,0 +1,25 @@ +#! /bin/bash + +# This script is for repo forked from others +# check $1(repo) if upstream branch origin/master is +# ahead of local branch $2(default to dev) + +if [ ! -d "$1" ]; then + return 0 +fi + +head='dev' +if [ $# -eq 2 ] +then + head=$2 +fi + +cd "$1" && \ +git fetch origin && \ +if ! git rev-list "$head" | grep "$(git rev-parse origin/master)" > /dev/null +then + [[ $(git pull my) == 'Alrady up to date.' ]] || \ + echo "New commit at" "$1" +fi + +echo "$(date)" check "$1" >> "$SETTING_DIR/log" diff --git a/tools/csv.move_column b/tools/csv.move_column new file mode 100755 index 0000000..a62701f --- /dev/null +++ b/tools/csv.move_column @@ -0,0 +1,18 @@ +#! /bin/bash + +# show each field with index in csv +echo -------------- > /dev/tty +head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty +echo -------------- > /dev/tty +echo > /dev/tty + +# get index of lon/lat column +read -p "Move which column? " origin_col +read -p "To which index? " new_col + +cat $1 | +# move lon and lat to the first and second column +awk -F',' -v OFS="," -v origin_th=$origin_col -v new_th=$new_col '\ + {for (i=1; i<= NF; i++) if (i == new_th) printf $origin_th OFS $i OFS; else if (i == origin_th); else printf $i OFS; print ""}\ + ' |\ +sed 's/,$//g' diff --git a/tools/csv.reorder b/tools/csv.reorder new file mode 100755 index 0000000..8a64239 --- /dev/null +++ b/tools/csv.reorder @@ -0,0 +1,17 @@ +#! /bin/bash + +# show each field with index in csv +echo -------------- > /dev/tty +head -1 < $1 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty +echo -------------- > /dev/tty +echo > /dev/tty + +read -p "type column numbers by new order, like 3 2 1: " order + +arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',') + +cat $1 |\ +awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\ +tee /tmp/csv + +echo "Also copied to /tmp/csv" > /dev/tty diff --git a/tools/csv2geojson b/tools/csv2geojson new file mode 100755 index 0000000..028aed7 --- /dev/null +++ b/tools/csv2geojson @@ -0,0 +1,46 @@ +#! /bin/bash + +# -s to skip specify columns of longitude and latitude +for i in "$@" +do +case $i in + -s) + lon_col=0; lat_col=1 + shift;; + + *) + csv=$i + shift;; +esac +done + +# if no -s option, just read from input +if [ "$lon_col" != "0" ]; then + # show each field with index in csv + echo -------------- > /dev/tty + head -1 < $csv | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty + echo -------------- > /dev/tty + echo > /dev/tty + + # get index of lon/lat column + read -p "Number of latitude column: " lat_col + read -p "Number of longitude column: " lon_col +fi + +cat $csv | +# move lon and lat to the first and second column +awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ + BEGIN{OFS=","}\ + {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ + ' |\ +# change csv into array format, like [lon, lat, "field1", field2...] +sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ +# wrap other fields as a json object, like [lon, lat, {...}] +jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\ +# create array of geojson point features +jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\ +# wrap features as geojson format +jq -s '{"type": "FeatureCollection", "features": .}' |\ +tee /tmp/geojson + +echo stored into /tmp/geojson > /dev/tty diff --git a/tools/gpx/footer b/tools/gpx/footer new file mode 100644 index 0000000..d0759c0 --- /dev/null +++ b/tools/gpx/footer @@ -0,0 +1 @@ + diff --git a/tools/gpx/gpx.check.py b/tools/gpx/gpx.check.py new file mode 100755 index 0000000..10be97c --- /dev/null +++ b/tools/gpx/gpx.check.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import sys +import os +import argparse +import copy +import fileinput +from osgeo import ogr +import osr +import urllib.parse + +def rewrite_gpx(filename): + for line in fileinput.input(filename, inplace=True): + if fileinput.isfirstline() and "'" in line: + line = '' + if fileinput.filelineno() == 2 and "version" not in line: + line = line.replace('= threshold: + if not flag: + print(f'{filename} has problem, the following urls shows the points with distance far from {threshold}m:') + print() + flag = True + if add_prefix: + dir = os.path.dirname(filename) + if dir: + dir += '/' + os.rename(filename, f'{dir}invalid_{os.path.basename(filename)}') + + geojson = '{{"type": "LineString", "coordinates": [[{}, {}], [{}, {}]]}}'.format( + geom1.GetX(), geom1.GetY(), + geom2.GetX(), geom2.GetY() + ) + encoded = urllib.parse.quote(geojson) + print('http://geojson.io/#data=data:application/json,{}'.format(encoded)) + print() + else: + break + trkpt = nextTrkpt + +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument('file', help="you can add multiple gpx files at the same time", nargs='+') + parser.add_argument("-i", help="add prefix to invalid files", action="store_true") + parser.add_argument("-d", help="distance of tolerance(m), 100 by default", dest="distance", default=100) + args = parser.parse_args() + for file in args.file: + check_valid(file, float(args.distance), args.i) + +if __name__ == '__main__': + main(sys.argv) diff --git a/tools/gpx/gpx.merge_gpx.sh b/tools/gpx/gpx.merge_gpx.sh new file mode 100755 index 0000000..4b024a7 --- /dev/null +++ b/tools/gpx/gpx.merge_gpx.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +GPX_DIR=$(dirname $0) + +sed '// p' -nr | cat $GPX_DIR/header - $GPX_DIR/footer diff --git a/tools/gpx/gpx.merge_trk.sh b/tools/gpx/gpx.merge_trk.sh new file mode 100755 index 0000000..c3a72d4 --- /dev/null +++ b/tools/gpx/gpx.merge_trk.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +sed '//,/<\/name>/ d; /<\/trk>/ d; /<\/gpx>/ i \ \ <\/trk>' |\ +awk '// && !x {print " \n combined_trk"; x=1} 1' diff --git a/tools/gpx/header b/tools/gpx/header new file mode 100644 index 0000000..1912e8b --- /dev/null +++ b/tools/gpx/header @@ -0,0 +1,10 @@ + + + + + + Garmin International + + + + diff --git a/tools/load-settings.sh b/tools/load-settings.sh new file mode 100755 index 0000000..30a29b0 --- /dev/null +++ b/tools/load-settings.sh @@ -0,0 +1,24 @@ +if [[ -z "$SETTING_DIR" ]]; then + SETTING_DIR=$HOME/settings +fi + +# load custom aliases +source $SETTING_DIR/alias + +# Add custom scripts into PATH +PATH=$PATH:$SETTING_DIR/tools +find $SETTING_DIR/tools -type d | sed 1d |\ +while read dir; do + PATH=$PATH:$dir +done + +# sync with important git repos +$SETTING_DIR/tools/sync.sh + +# load custom functions +OSM_UTIL_DIR=$SETTING_DIR/tools/osm +source $OSM_UTIL_DIR/osm + +# go +PATH=$PATH:$HOME/go/bin + diff --git a/tools/match-road.sh b/tools/match-road.sh new file mode 100755 index 0000000..d9beaae --- /dev/null +++ b/tools/match-road.sh @@ -0,0 +1,148 @@ +#!/usr/bin/env bash +# +# Author: Pham +# +# This script accepts a single GPX file as parameter and +# output the processed GPX body to STDOUT, using Mapbox Map Matching API v4. +# read doc at: https://docs.mapbox.com/api/legacy/map-matching-v4/ +# +# Example: +# +# match-road.sh raw.gpx > new.gpx +# +# Hint: +# +# Remember to put Mapbox Access Token at the top! + +#set -x +set -e + +# put yout Mapbox token here +ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) +# number of coordinates for each Mapbox Map Matching API request, Maximum value is 100 +LIMIT=100 +# define the lowest confidence of accepted matched points +THRESHOLD=0.0001 + +if [[ -z $1 ]]; then echo "You need to give a gpx file!"; exit 1; fi +ORIGIN_DATA=/tmp/$(basename $1).origin +RESPONSES=/tmp/$(basename $1).responses && true > $RESPONSES +MATCHED=/tmp/$(basename $1).matched + +# extract data from the given gpx file +# only keep first point and remove the rest which in the same "second" +# input: [gpx format] +# output: [121.0179739,14.5515336] 1984-01-01T08:00:46 +get_data() { + sed -nr '// {H; /<\/trkpt>/ {x; s/\n/ /g; p; s/.*//; x}}' $1 | + sed -nr 'h; s/.*lon="([^"]+).*/\1/; H; g + s/.*lat="([^"]+).*/\1/; H; g + # If trkpt has no time, leave it blank + /time/ { + s/.*