こんにちは。
OpenStreetMap データ(osm.pbf ファイル)に対して、osmium-tool の tags-filter コマンドの処理を行うシェルスクリプトを作り、and 条件を指定可能としました(&& で区切った条件を与えます)。GeoJSONL データを標準出力します。
動作例
- 第一条件 tags-filter:
w/highway=motorway,motorway_link1で絞った後、 - 第二条件 tags-filter:
w/moped=yes w/bicycle=yesでさらに絞る例です:
$ ./tags_filter.sh "w/highway=motorway,motorway_link && w/moped=yes w/bicycle=yes" japan-latest.osm.pbf > output.geojsonl
source code
tags_filter.sh
#!/bin/sh
filters="$1"
inputfile="$2"
prog_name=$(basename "$0")
commands_required="osmium"
## function ##
print_usage_f() {
cat << EOS
usage:
$prog_name "w/moped && w/highway=motorway,motorway_link" japan-highway.osm.pbf | less
$prog_name "w/moped=no && w/highway!=motorway,motorway_link" japan-highway.osm.pbf | less
$prog_name "w/highway=service && w/name" japan-motorway.osm.pbf | less
EOS
}
realpath_f() {
case "$1" in /*) ;; *) printf '%s/' "$PWD";; esac
echo "$1"
}
osmpbf2jsonl_f() {
osmium export --overwrite --no-progress -f geojsonseq --format-option=print_record_separator=false "$1"
}
## check ##
for c in $commands_required; do
command -v "$c" >/dev/null 2>&1 || { echo "$c not found" >&2; exit 1; }
done
case "$1" in -h|--help) print_usage_f; exit;; esac
[ -f "$inputfile" ] || exit 1
## main
inputfile=$(realpath_f "$inputfile")
infile=in.osm.pbf
outfile=out.osm.pbf
tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/$prog_name.XXXXXXXXXX") || exit 1
cd "$tmpdir" || exit 1
cp "$inputfile" "$outfile"
echo "$filters" | perl -pe 's/&&/\n/g' | { while IFS= read -r filter; do
mv -f "$outfile" "$infile"
osmium tags-filter "$infile" "$filter" -o "$outfile"
done }
osmpbf2jsonl_f "$outfile"
rm -rf "$tmpdir"
-
複数条件を指定すると
or扱いです(osmium-tool の tags-filter の仕様です)。 ↩