#! /bin/sh FILENAME=$0 osm.help() { echo ' COMMANDS: osm.help util.osm.edit osm.api.changeset.create create a new changeset and return its ID osm.api.changeset.update [changeset ID], update changeset with new comment osm.api.changeset.add [changeset ID], STDIN=osm element, add it into changeset osm.api.changeset.close [changeset ID], close the given changeset osm.api.fetch [element type] [element ID] osm.api.fetch.full like .fetch, but return with all referenced elements osm.api.fetch.history [element type] [element ID] osm.api.member.relation [element type] [element ID] [member type] get first relations reference it as member type osm.api.referenced.relation osm.api.referenced.way osm.api.upload.to osm.file.cat osm.file.get osm.file.get.full osm.file.query osm.list.ids osm.list.tag osm.list.tags osm.get.id osm.member.relation osm.pbf.update osm.query osm.update ' } util.osm.edit() { vim $FILENAME && source $FILENAME } osm.goto() { xdg-open https://www.openstreetmap.org/$1/$2 } SERVER=https://master.apis.dev.openstreetmap.org #SERVER=https://api.openstreetmap.org OSM_API=$SERVER/api/0.6 OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm) # get .osm format data osm.api.fetch() { curl -X GET $OSM_API/$1/$2 |\ tee /tmp/osm &&\ echo content of $1 $2 is copied into /tmp/osm > /dev/tty } osm.api.fetch.full() { curl -X GET $OSM_API/$1/$2/full |\ tee /tmp/osm &&\ echo content of $1 $2 and its members are copied into /tmp/osm > /dev/tty } osm.api.fetch.history() { curl -X GET $OSM_API/$1/$2/history |\ tee /tmp/osm &&\ echo history of $1 $2 are copied into /tmp/osm > /dev/tty } osm.api.referenced.relation() { curl -X GET $OSM_API/$1/$2/relations |\ tee /tmp/osm &&\ echo relations contain $1 $2 are copied into /tmp/osm > /dev/tty } osm.member.relation() { sed -nr "/ /dev/tty } osm.list.tag() { ele_pattern="(node|way|relation)" sed -nr "/<$ele_pattern/,/<\/$ele_pattern/ { / /tmp/osm for tag in $@ do echo $content |\ osm.list.tag $tag |\ paste -d',' /tmp/osm - > /tmp/osm.new &&\ mv /tmp/osm.new /tmp/osm done cat /tmp/osm echo tag list is also copied into /tmp/osm > /dev/tty } # extract an element from .osm format STDIN # $1 as [node|way|relation], $2 as id osm.get.id() { echo "" sed -nr "/^ *<$1 id=\"$2\".*/,/^ *<\/$1>/p" echo "" } # get ids from .osm format STDIN osm.list.ids() { sed -nr 's/.*<(node|way|relation) id=\"([^"]+)\".*/\1 \2/p' } # upload .osm format STDIN to a given changeset # allows multiple elements in osm body osm.api.upload.to() { tee /tmp/osm |\ osm.list.ids |\ sed 's#.*#osm.extract \0 < /tmp/osm#g' |\ sed "s/.*/\0 \| osm.api.changeset.add $1/g" |\ while read -r command do echo $command source<(echo "($command &)") done } # query .osm format STDIN osm.query() { osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced } # query osm-related file with .osm format output osm.file.query() { file=$1; shift osmium tags-filter $file $@ --output-format=osm --omit-referenced } # extract an element from osm file osm.file.get() { file=$1; shift osmium getid $file $@ --output-format=osm } osm.file.get.full() { file=$1; shift osmium getid $file $@ --output-format=osm --add-referenced } # update .osm format STDIN with key-value osm.update() { # remove original tag&value sed "/" else # just print it sed '' fi } # create a new changeset osm.api.changeset.create() { echo -n "type comment: " read comment info=" " echo $info |\ curl -u $OSM_USER_PASSWD --upload-file - $OSM_API/changeset/create |\ tee /dev/tty |\ tail -1 | read changeset_id echo " copied into clipboard" echo "changeset created, check $SERVER/changeset/$changeset_id" } # add a new element into changeset osm.api.changeset.add() { element=$(cat) header=$(echo $element | grep -E "<(node|way|relation)\s") ele_type=$(echo $header | sed -r 's/.*<(node|way|relation).*$/\1/') id=$(echo $header | sed -r 's/.* id=\"([^"]+)\".*$/\1/') echo $element | \ sed -r "s/^( *<(node|way|relation).*version[^ ]+ )(.*)$/\1changeset=\"$1\">/" | \ curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/$ele_type/$id } # update changeset with a new comment osm.api.changeset.update() { echo -n 'type comment: ' read -r comment echo "" | \ curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1 } # close a changeset osm.api.changeset.close() { curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close } # update an .osm.pbf file osm.pbf.update() { PBF_FILE=$1 SERVER=http://download.geofabrik.de/asia/taiwan-updates # get next sequence number and store it into NEW_SEQ osmium fileinfo $PBF_FILE | \ grep osmosis_replication_sequence_number | \ cut -d'=' -f2 | \ sed 's/$/+1/' | bc | \ read NEW_SEQ # while server has osc file with given sequence number, # get it and do file update while SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/') STATE_URL=$SERVER/000/$SEQ_PATH.state.txt [ $(curl.code $STATE_URL) != "404" ] do mkdir -p changes CHANGE_URL=$SERVER/000/$SEQ_PATH.osc.gz echo $CHANGE_URL curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \ osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \ --output-header=osmosis_replication_sequence_number=$NEW_SEQ \ --overwrite \ --output $NEW_SEQ.osm.pbf PBF_FILE=$NEW_SEQ.osm.pbf ((NEW_SEQ++)) done }