0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

osmium-tool の tags-filter 処理(シェルスクリプト)

0
Last updated at Posted at 2021-06-07

こんにちは。
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"
  1. 複数条件を指定すると or 扱いです(osmium-tool の tags-filter の仕様です)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?