diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-14 13:33:23 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-14 13:33:23 +0800 |
| commit | 6fae25b305d714b3ab7608fa003f1af9bf024545 (patch) | |
| tree | 05507b2c0505659d2fd847ecce988dacab63a236 /bin/osm | |
| parent | 41ad31a2dee9ff912f222652f022b4c55cddcbf7 (diff) | |
Rename tools into bin
Diffstat (limited to 'bin/osm')
31 files changed, 439 insertions, 0 deletions
diff --git a/bin/osm/josm_install.sh b/bin/osm/josm_install.sh new file mode 100755 index 0000000..520875a --- /dev/null +++ b/bin/osm/josm_install.sh | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # scripts from https://josm.openstreetmap.de/wiki/Download#Webstart | ||
| 4 | echo deb https://josm.openstreetmap.de/apt $(lsb_release -sc) universe | sudo tee /etc/apt/sources.list.d/josm.list > /dev/null &&\ | ||
| 5 | wget -q https://josm.openstreetmap.de/josm-apt.key -O- | sudo apt-key add - &&\ | ||
| 6 | sudo apt-get update &&\ | ||
| 7 | sudo apt-get remove josm josm-plugins &&\ | ||
| 8 | sudo apt-get install josm | ||
diff --git a/bin/osm/note b/bin/osm/note new file mode 100644 index 0000000..aa51031 --- /dev/null +++ b/bin/osm/note | |||
| @@ -0,0 +1 @@ | |||
| diff -y <(ls|sort) <(osm.help | cut -d' ' -f3 | sort | uniq) | |||
diff --git a/bin/osm/osm b/bin/osm/osm new file mode 100644 index 0000000..344f906 --- /dev/null +++ b/bin/osm/osm | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | export OSM_TEST_SERVER=https://master.apis.dev.openstreetmap.org | ||
| 4 | export OSM_SERVER=https://api.openstreetmap.org | ||
| 5 | export OSM_TES_SERVER=https://api.openstreetmap.org | ||
| 6 | |||
| 7 | export OSM_API=$OSM_SERVER/api/0.6 | ||
| 8 | export OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm 2>/dev/null) | ||
| 9 | |||
| 10 | FILENAME=$0 | ||
| 11 | |||
| 12 | utils.osm() { | ||
| 13 | vim $FILENAME && source $FILENAME | ||
| 14 | } | ||
| 15 | |||
| 16 | osm.api() { | ||
| 17 | curl -X GET $OSM_API/$1/$2 | ||
| 18 | } | ||
diff --git a/bin/osm/osm.api.changeset.add b/bin/osm/osm.api.changeset.add new file mode 100755 index 0000000..3e4878e --- /dev/null +++ b/bin/osm/osm.api.changeset.add | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | element=$(cat) | ||
| 4 | header=$(echo $element | grep -E "<(node|way|relation)\s") | ||
| 5 | ele_type=$(echo $header | sed -r 's/.*<(node|way|relation).*$/\1/') | ||
| 6 | id=$(echo $header | sed -r 's/.* id=\"([^"]+)\".*$/\1/') | ||
| 7 | |||
| 8 | echo $element | \ | ||
| 9 | sed -r "s/^( *<(node|way|relation).*version[^ ]+ )(.*)$/\1changeset=\"$1\">/" | \ | ||
| 10 | curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/$ele_type/$id | ||
diff --git a/bin/osm/osm.api.changeset.close b/bin/osm/osm.api.changeset.close new file mode 100755 index 0000000..3b016c3 --- /dev/null +++ b/bin/osm/osm.api.changeset.close | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close | ||
diff --git a/bin/osm/osm.api.changeset.commit b/bin/osm/osm.api.changeset.commit new file mode 100755 index 0000000..6b67833 --- /dev/null +++ b/bin/osm/osm.api.changeset.commit | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | set -e | ||
| 4 | shopt -s lastpipe | ||
| 5 | |||
| 6 | OSM_SERVER=https://api.openstreetmap.org | ||
| 7 | OSM_TEST_SERVER=https://master.apis.dev.openstreetmap.org | ||
| 8 | if [[ $@ =~ '--serious' ]]; then | ||
| 9 | SERVER=$OSM_SERVER | ||
| 10 | else | ||
| 11 | SERVER=$OSM_TEST_SERVER | ||
| 12 | fi | ||
| 13 | |||
| 14 | OSM_API=${SERVER}/api/0.6 | ||
| 15 | FILE=${@//--serious/} | ||
| 16 | |||
| 17 | # Prompt for comment and User:Password | ||
| 18 | if [[ ! -t 0 ]]; then | ||
| 19 | comment=$(cat) | ||
| 20 | else | ||
| 21 | read -e -p 'Type comment: ' -r comment </dev/tty | ||
| 22 | fi | ||
| 23 | if [ -z ${OSM_USER_PASSWD} ]; then | ||
| 24 | read -e -p 'Type USER:PASSWD: ' -r OSM_USER_PASSWD </dev/tty | ||
| 25 | fi | ||
| 26 | |||
| 27 | create_changeset() { | ||
| 28 | SOURCE_TAG="${SOURCE:+$(printf "<tag k='source' v='%s'/>" $SOURCE)}" | ||
| 29 | |||
| 30 | curl ${OSM_API}/changeset/create \ | ||
| 31 | --user ${OSM_USER_PASSWD} \ | ||
| 32 | --upload-file - \ | ||
| 33 | --silent \ | ||
| 34 | <<EOF | tail -1 | ||
| 35 | <osm> | ||
| 36 | <changeset> | ||
| 37 | ${SOURCE_TAG} | ||
| 38 | <tag k='comment' v='${comment}'/> | ||
| 39 | <tag k='created_by' v='bash script'/> | ||
| 40 | <tag k='bot' v='yes'/> | ||
| 41 | </changeset> | ||
| 42 | </osm> | ||
| 43 | EOF | ||
| 44 | } | ||
| 45 | |||
| 46 | # Return http code after uploading a file | ||
| 47 | uploade_file_to_changeset() { | ||
| 48 | curl -X POST $OSM_API/changeset/${changeset_id}/upload \ | ||
| 49 | --user ${OSM_USER_PASSWD} -i \ | ||
| 50 | --upload-file - \ | ||
| 51 | --silent -o /dev/null -w "%{http_code}" | ||
| 52 | } | ||
| 53 | |||
| 54 | close_changeset() { | ||
| 55 | curl -X PUT ${OSM_API}/changeset/${changeset_id}/close \ | ||
| 56 | --user ${OSM_USER_PASSWD} -i \ | ||
| 57 | --silent -o /dev/null -w "%{http_code}" | ||
| 58 | } | ||
| 59 | |||
| 60 | # Create changeset with given information | ||
| 61 | changeset_id=$(create_changeset) | ||
| 62 | |||
| 63 | # Print created changeset id | ||
| 64 | echo "Changeset created, check ${SERVER}/changeset/${changeset_id}" >/dev/tty | ||
| 65 | echo ${changeset_id} | ||
| 66 | |||
| 67 | # Upload OSC file to Changeset | ||
| 68 | sed -Ee "/<(node|way|relation)/ s/>/ changeset=\"${changeset_id}\">/" $FILE |\ | ||
| 69 | uploade_file_to_changeset | if [[ $(cat) == '200' ]]; then | ||
| 70 | echo Upload file $FILE to changeset ${changeset_id} >/dev/tty | ||
| 71 | else | ||
| 72 | exit 1 | ||
| 73 | fi | ||
| 74 | |||
| 75 | # Close Changeset | ||
| 76 | close_changeset | if [[ $(cat) == '200' ]]; then | ||
| 77 | echo Changeset ${changeset_id} closed >/dev/tty | ||
| 78 | else | ||
| 79 | exit 1 | ||
| 80 | fi | ||
diff --git a/bin/osm/osm.api.changeset.create b/bin/osm/osm.api.changeset.create new file mode 100755 index 0000000..ab9fe85 --- /dev/null +++ b/bin/osm/osm.api.changeset.create | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | set -e | ||
| 4 | |||
| 5 | read -e -p 'Type comment: ' -r comment </dev/tty | ||
| 6 | if [ -z $OSM_USER_PASSWD ]; then | ||
| 7 | read -e -p 'Type USER:PASSWD: ' -r OSM_USER_PASSWD </dev/tty | ||
| 8 | fi | ||
| 9 | |||
| 10 | info="<osm> | ||
| 11 | <changeset> | ||
| 12 | <tag k='comment' v='$comment'/> | ||
| 13 | <tag k='created_by' v='bash script'/> | ||
| 14 | <tag k='bot' v='yes'/> | ||
| 15 | </changeset> | ||
| 16 | </osm> | ||
| 17 | " | ||
| 18 | |||
| 19 | changeset_id=$(echo $info |\ | ||
| 20 | curl -u $OSM_USER_PASSWD --upload-file - $OSM_API/changeset/create |\ | ||
| 21 | tail -1) | ||
| 22 | |||
| 23 | echo >/dev/tty | ||
| 24 | echo "changeset created, check $OSM_SERVER/changeset/$changeset_id" >/dev/tty | ||
| 25 | echo $changeset_id | tee /tmp/changeset && echo copied to /tmp/changeset | ||
diff --git a/bin/osm/osm.api.changeset.update b/bin/osm/osm.api.changeset.update new file mode 100755 index 0000000..7267449 --- /dev/null +++ b/bin/osm/osm.api.changeset.update | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | ID=${1:-$(cat /tmp/changeset 2>/dev/null)} | ||
| 4 | [ -z $ID ] && echo Please specify Changeset ID && exit 1 | ||
| 5 | |||
| 6 | read -e -p 'Type comment: ' -r comment </dev/tty | ||
| 7 | |||
| 8 | echo "<osm><changeset><tag k=\"comment\" v=\"$comment\"/></changeset></osm>" | \ | ||
| 9 | curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1 | ||
diff --git a/bin/osm/osm.api.changeset.upload b/bin/osm/osm.api.changeset.upload new file mode 100755 index 0000000..96bdff4 --- /dev/null +++ b/bin/osm/osm.api.changeset.upload | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | cat $2 |\ | ||
| 4 | sed -r "/<(node|way|relation)/ s/>/ changeset=\"$1\">/" |\ | ||
| 5 | tee /dev/tty |\ | ||
| 6 | curl -X POST -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1/upload | ||
diff --git a/bin/osm/osm.api.fetch b/bin/osm/osm.api.fetch new file mode 100755 index 0000000..8460c5f --- /dev/null +++ b/bin/osm/osm.api.fetch | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | # get .osm format data | ||
| 4 | curl -X GET $OSM_API/$1/$2 |\ | ||
| 5 | tee /tmp/osm &&\ | ||
| 6 | echo content of $1 $2 is copied into /tmp/osm > /dev/tty | ||
diff --git a/bin/osm/osm.api.fetch.full b/bin/osm/osm.api.fetch.full new file mode 100755 index 0000000..5ee78b8 --- /dev/null +++ b/bin/osm/osm.api.fetch.full | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | curl -X GET $OSM_API/$1/$2/full |\ | ||
| 4 | tee /tmp/osm &&\ | ||
| 5 | echo "\n" content of $1 $2 and its members are copied into /tmp/osm > /dev/tty | ||
diff --git a/bin/osm/osm.api.fetch.history b/bin/osm/osm.api.fetch.history new file mode 100755 index 0000000..bc837bd --- /dev/null +++ b/bin/osm/osm.api.fetch.history | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | curl -X GET $OSM_API/$1/$2/history |\ | ||
| 4 | tee /tmp/osm &&\ | ||
| 5 | echo &&\ | ||
| 6 | echo "\n" history of $1 $2 are copied into /tmp/osm > /dev/tty | ||
diff --git a/bin/osm/osm.api.member.relation b/bin/osm/osm.api.member.relation new file mode 100755 index 0000000..5addef7 --- /dev/null +++ b/bin/osm/osm.api.member.relation | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | osm.api.referenced.relation $1 $2 |\ | ||
| 4 | osm.member.relation $1 $2 $3 | ||
diff --git a/bin/osm/osm.api.referenced.relation b/bin/osm/osm.api.referenced.relation new file mode 100755 index 0000000..2ce0631 --- /dev/null +++ b/bin/osm/osm.api.referenced.relation | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | curl -X GET $OSM_API/$1/$2/relations |\ | ||
| 4 | tee /tmp/osm &&\ | ||
| 5 | echo &&\ | ||
| 6 | echo "\n" relations contain $1 $2 are copied into /tmp/osm > /dev/tty | ||
diff --git a/bin/osm/osm.api.referenced.way b/bin/osm/osm.api.referenced.way new file mode 100755 index 0000000..1d84238 --- /dev/null +++ b/bin/osm/osm.api.referenced.way | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | curl -X GET $OSM_API/node/$1/ways |\ | ||
| 4 | tee /tmp/osm &&\ | ||
| 5 | echo ways contain node $1 are copied into /tmp/osm > /dev/tty | ||
diff --git a/bin/osm/osm.api.upload.to b/bin/osm/osm.api.upload.to new file mode 100755 index 0000000..3979220 --- /dev/null +++ b/bin/osm/osm.api.upload.to | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | # allows multiple elements in osm body | ||
| 4 | tee /tmp/osm |\ | ||
| 5 | osm.list.ids |\ | ||
| 6 | sed 's#.*#osm.extract \0 < /tmp/osm#g' |\ | ||
| 7 | sed "s/.*/\0 \| osm.api.changeset.add $1/g" |\ | ||
| 8 | while read -r command | ||
| 9 | do | ||
| 10 | echo $command | ||
| 11 | source<(echo "($command &)") | ||
| 12 | done | ||
diff --git a/bin/osm/osm.file.get b/bin/osm/osm.file.get new file mode 100755 index 0000000..e2b67b4 --- /dev/null +++ b/bin/osm/osm.file.get | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | file=$1; shift | ||
| 4 | osmium getid $file $@ --output-format=osm | ||
diff --git a/bin/osm/osm.file.get.full b/bin/osm/osm.file.get.full new file mode 100755 index 0000000..50ee2f1 --- /dev/null +++ b/bin/osm/osm.file.get.full | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | file=$1; shift | ||
| 4 | osmium getid $file $@ --output-format=osm --add-referenced | ||
diff --git a/bin/osm/osm.file.query b/bin/osm/osm.file.query new file mode 100755 index 0000000..4a01b5b --- /dev/null +++ b/bin/osm/osm.file.query | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | file=$1; shift | ||
| 4 | osmium tags-filter $file $@ --output-format=osm --omit-referenced | ||
diff --git a/bin/osm/osm.goto b/bin/osm/osm.goto new file mode 100755 index 0000000..25bceaa --- /dev/null +++ b/bin/osm/osm.goto | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | xdg-open https://www.openstreetmap.org/$1/$2 | ||
diff --git a/bin/osm/osm.help b/bin/osm/osm.help new file mode 100755 index 0000000..f4c20d2 --- /dev/null +++ b/bin/osm/osm.help | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | cat <<EOF | ||
| 4 | COMMANDS: | ||
| 5 | osm.help | ||
| 6 | util.osm.edit | ||
| 7 | osm.api.changeset.create create a new changeset and return its ID | ||
| 8 | osm.api.changeset.update <changeset ID> update changeset with new comment | ||
| 9 | osm.api.changeset.upload <changeset ID> <osc file> upload osc file to the given changeset | ||
| 10 | osm.api.changeset.add <changeset ID> STDIN=osm element, add it into changeset | ||
| 11 | osm.api.changeset.close <changeset ID> close the given changeset | ||
| 12 | osm.api.fetch <element type> <element ID> | ||
| 13 | osm.api.fetch.full like .fetch, but return with all referenced elements | ||
| 14 | osm.api.fetch.history <element type> <element ID> | ||
| 15 | osm.api.member.relation <element type> <element ID> <member type> get first relations reference it as member type | ||
| 16 | osm.api.referenced.relation <element type> <element ID> get all relations reference this element | ||
| 17 | osm.api.referenced.way <element type> <element ID> get all ways reference this element | ||
| 18 | osm.api.upload.to | ||
| 19 | |||
| 20 | osm.file.cat | ||
| 21 | osm.file.get | ||
| 22 | osm.file.get.full | ||
| 23 | osm.file.query | ||
| 24 | osm.list.ids | ||
| 25 | osm.list.tag | ||
| 26 | osm.list.tags | ||
| 27 | |||
| 28 | osm.get.id | ||
| 29 | osm.query | ||
| 30 | osm.member.relation | ||
| 31 | osm.pbf.update | ||
| 32 | osm.osm.update | ||
| 33 | EOF | ||
diff --git a/bin/osm/osm.list.ids b/bin/osm/osm.list.ids new file mode 100755 index 0000000..8e5d870 --- /dev/null +++ b/bin/osm/osm.list.ids | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | sed -nr 's/.*<(node|way|relation) id=\"([^"]+)\".*/\1 \2/p' | ||
diff --git a/bin/osm/osm.list.tag b/bin/osm/osm.list.tag new file mode 100755 index 0000000..76649a0 --- /dev/null +++ b/bin/osm/osm.list.tag | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | ele_pattern="(node|way|relation)" | ||
| 4 | sed -nr "/<$ele_pattern/,/<\/$ele_pattern/ { | ||
| 5 | /<tag k=\"$1\"/ { | ||
| 6 | s/.*v=\"([^\"]+)\".*/\1/ | ||
| 7 | h | ||
| 8 | } | ||
| 9 | /<\/$ele_pattern/ {x;p;s/.*//;x} | ||
| 10 | }" | ||
diff --git a/bin/osm/osm.list.tags b/bin/osm/osm.list.tags new file mode 100755 index 0000000..767d32a --- /dev/null +++ b/bin/osm/osm.list.tags | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | content=$(cat) | ||
| 4 | echo $content | osm.list.ids | tr ' ' ',' > /tmp/osm | ||
| 5 | |||
| 6 | for tag in $@ | ||
| 7 | do | ||
| 8 | echo $content |\ | ||
| 9 | osm.list.tag $tag |\ | ||
| 10 | paste -d',' /tmp/osm - > /tmp/osm.new &&\ | ||
| 11 | mv /tmp/osm.new /tmp/osm | ||
| 12 | done | ||
| 13 | |||
| 14 | cat /tmp/osm | ||
| 15 | echo "\ntag list is also copied into /tmp/osm" > /dev/tty | ||
diff --git a/bin/osm/osm.member.relation b/bin/osm/osm.member.relation new file mode 100755 index 0000000..f8d4598 --- /dev/null +++ b/bin/osm/osm.member.relation | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | sed -nr "/<relation/,/<\/relation/ { | ||
| 4 | /<relation/ {s/.* id=\"([^\"]+)\".*/\1/;h} | ||
| 5 | /<member type=\"$1\" ref=\"$2\" role=\"$3\"/ {g;p;q} | ||
| 6 | } | ||
| 7 | $ {s/.*//;P} | ||
| 8 | " | head -1 | ||
diff --git a/bin/osm/osm.osc.by_member b/bin/osm/osm.osc.by_member new file mode 100755 index 0000000..5f9d9be --- /dev/null +++ b/bin/osm/osm.osc.by_member | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | while read -r line | ||
| 4 | do | ||
| 5 | |||
| 6 | TYPE=$(echo $line | cut -d ' ' -f1) # field1 is type | ||
| 7 | ID=$(echo $line | cut -d ' ' -f2) # field2 is ID | ||
| 8 | |||
| 9 | NEW_MEMBERS=$(echo $line |\ | ||
| 10 | cut -d' ' -f3- |\ | ||
| 11 | sed -r 's/([0-9]+)/<member type=\"relation\" ref=\"\1\" role=\"subarea\"\/>/g') | ||
| 12 | |||
| 13 | echo $NEW_MEMBERS | ||
| 14 | |||
| 15 | # print matched element with new tags to .osc file | ||
| 16 | cat $1 |\ | ||
| 17 | sed -nr "/<$TYPE id=\"$ID\"/,/<\/$TYPE/ { | ||
| 18 | /<$TYPE id=\"$ID\"/ { | ||
| 19 | s/(version=\"[0-9]+\")(.*)/\1>/ | ||
| 20 | a \ \ \ \ $NEW_MEMBERS | ||
| 21 | } | ||
| 22 | p | ||
| 23 | /<\/$TYPE/ q | ||
| 24 | }" >> $1.osc | ||
| 25 | done | ||
| 26 | |||
| 27 | # Add .osc structure for output | ||
| 28 | sed -ir '1 i <osmChange version="0.6" generator="bash script"> | ||
| 29 | 1 i <modify> | ||
| 30 | $ a </modify> | ||
| 31 | $ a </osmChange>' $1.osc | ||
diff --git a/bin/osm/osm.osc.by_tag b/bin/osm/osm.osc.by_tag new file mode 100755 index 0000000..cc92c1d --- /dev/null +++ b/bin/osm/osm.osc.by_tag | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # create new tags from input line, for example: | ||
| 4 | # field1 field2 field3 field4 field5 field6 field7 field8... | ||
| 5 | # [element type] [element ID] key1_added "value1" key2_added "value2" key3_removed key4_removed... | ||
| 6 | |||
| 7 | # key should not quoted, value must be quoted | ||
| 8 | # And keys which need to be removed must be placed at the end | ||
| 9 | while read -r line | ||
| 10 | do | ||
| 11 | TYPE=$(echo $line | cut -d ' ' -f1) # field1 is type | ||
| 12 | ID=$(echo $line | cut -d ' ' -f2) # field2 is ID | ||
| 13 | |||
| 14 | # transform key-value pair into tag format: | ||
| 15 | # <tag k="[key]" v="[value]"/> | ||
| 16 | # keys without values are omitted | ||
| 17 | NEW_TAGS=$(echo $line |\ | ||
| 18 | cut -d' ' -f3- |\ | ||
| 19 | sed -r 's/([^ "]+) (\"[^"]+\")/<tag k=\"\1\" v=\2\/>/g; s/>[^"]*$/>/') | ||
| 20 | |||
| 21 | # get regex pattern need to removed from original osm element: | ||
| 22 | # key1|key2|key3|key4 | ||
| 23 | TAG_PATTERN=$(echo $line |\ | ||
| 24 | cut -d' ' -f3- | xargs -n2 echo |\ | ||
| 25 | cut -d' ' -f1 | paste -s -d'|') | ||
| 26 | |||
| 27 | echo $NEW_TAGS > /dev/tty | ||
| 28 | |||
| 29 | # print matched element with new tags to .osc file | ||
| 30 | cat $1 |\ | ||
| 31 | sed -nr "/<$TYPE id=\"$ID\"/,/<\/$TYPE/ { | ||
| 32 | /<$TYPE id=\"$ID\"/ { | ||
| 33 | s/(version=\"[0-9]+\")(.*)/\1>/ | ||
| 34 | a \ \ \ \ $NEW_TAGS | ||
| 35 | } | ||
| 36 | /<tag k=\"($TAG_PATTERN)\"/ !p | ||
| 37 | /<\/$TYPE/ q | ||
| 38 | }" >> $1.osc | ||
| 39 | done | ||
| 40 | |||
| 41 | # Add .osc structure for output | ||
| 42 | sed -ir '1 i <osmChange version="0.6" generator="bash script"> | ||
| 43 | 1 i <modify> | ||
| 44 | $ a </modify> | ||
| 45 | $ a </osmChange>' $1.osc | ||
diff --git a/bin/osm/osm.osm.remove b/bin/osm/osm.osm.remove new file mode 100755 index 0000000..83d6ea8 --- /dev/null +++ b/bin/osm/osm.osm.remove | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | while read -r line | ||
| 4 | do | ||
| 5 | # put element type and element ID into array | ||
| 6 | array=( $(echo $line) ) | ||
| 7 | cat $1 |\ | ||
| 8 | sed -i "/<$array[1] id=\"$array[2]\"/,/<\/$array[1]>/ d" | ||
| 9 | done | ||
diff --git a/bin/osm/osm.pbf.update b/bin/osm/osm.pbf.update new file mode 100755 index 0000000..6c6f445 --- /dev/null +++ b/bin/osm/osm.pbf.update | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | set -e | ||
| 4 | |||
| 5 | GEOFABRIK_SERVER=http://download.geofabrik.de/asia/taiwan-updates | ||
| 6 | PBF_FILE=$1 | ||
| 7 | |||
| 8 | # get latest sequence number | ||
| 9 | echo Fetching the latest sequence number | ||
| 10 | LATEST_SEQ=$(curl --silent http://download.geofabrik.de/asia/taiwan-updates/state.txt |\ | ||
| 11 | tail -1 | cut -d'=' -f2) | ||
| 12 | echo Latest sequence number is $LATEST_SEQ | ||
| 13 | |||
| 14 | # get current sequence number | ||
| 15 | SEQ=$(osmium fileinfo $PBF_FILE |\ | ||
| 16 | grep osmosis_replication_sequence_number |\ | ||
| 17 | cut -d'=' -f2) | ||
| 18 | echo File sequence number is $SEQ | ||
| 19 | |||
| 20 | # while server has osc file with given sequence number, | ||
| 21 | # get it and do file update | ||
| 22 | while | ||
| 23 | (( SEQ++ )) | ||
| 24 | [ $SEQ -le $LATEST_SEQ ] | ||
| 25 | do | ||
| 26 | mkdir -p changes | ||
| 27 | SEQ_PATH=$(echo $SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/') | ||
| 28 | CHANGE_URL=$GEOFABRIK_SERVER/000/$SEQ_PATH.osc.gz | ||
| 29 | echo $CHANGE_URL | ||
| 30 | curl -o changes/$SEQ.osc.gz $CHANGE_URL && \ | ||
| 31 | osmium apply-changes $PBF_FILE changes/$SEQ.osc.gz \ | ||
| 32 | --output-header=osmosis_replication_sequence_number=$SEQ \ | ||
| 33 | --overwrite \ | ||
| 34 | --output $SEQ.osm.pbf | ||
| 35 | |||
| 36 | mv $PBF_FILE $((SEQ-1)).osm.pbf | ||
| 37 | mv $SEQ.osm.pbf $PBF_FILE | ||
| 38 | done | ||
| 39 | |||
| 40 | echo | ||
| 41 | echo File sequence number is $((SEQ-1)), already the latest one on Geofrbrik | ||
diff --git a/bin/osm/osm.query b/bin/osm/osm.query new file mode 100755 index 0000000..8d0b9f7 --- /dev/null +++ b/bin/osm/osm.query | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced | ||
diff --git a/bin/osm/sequence_number.sh b/bin/osm/sequence_number.sh new file mode 100755 index 0000000..eb365d0 --- /dev/null +++ b/bin/osm/sequence_number.sh | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # $1 as --hour or --minute, $2 as timestamp | ||
| 4 | # return the latest sequence number | ||
| 5 | |||
| 6 | case $1 in | ||
| 7 | # hour difference with Tue Jun 4 03:00:00 UTC 2019 | ||
| 8 | # sequence number=58940 | ||
| 9 | --hour) | ||
| 10 | echo $[($2 - 1559617200)/3600 + 58940] | ||
| 11 | ;; | ||
| 12 | |||
| 13 | # minute difference with latest planet state file | ||
| 14 | --minute) | ||
| 15 | benchmark=benchmark | ||
| 16 | curl https://planet.openstreetmap.org/replication/minute/state > $benchmark | ||
| 17 | timeString=$(tail -1 $benchmark | tr -d 'timestamp=\\') | ||
| 18 | timestamp=$(date -d "$timeString" +%s) | ||
| 19 | seq=$(sed -n 2p $benchmark | tr -d "sequenceNumber=") | ||
| 20 | rm $benchmark | ||
| 21 | echo $[$seq - ($timestamp - $2)/60 - 1 ] | ||
| 22 | esac | ||