From 1d3508f66fddf3f8c6416e59ab9fbde405dbcf25 Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 17 Nov 2019 09:18:07 +0800 Subject: update --- utils/osm | 270 +------------------------------------ utils/osm.village.nat_ref.makefile | 59 -------- 2 files changed, 4 insertions(+), 325 deletions(-) delete mode 100644 utils/osm.village.nat_ref.makefile (limited to 'utils') diff --git a/utils/osm b/utils/osm index cdc4eb7..2d32575 100644 --- a/utils/osm +++ b/utils/osm @@ -1,275 +1,13 @@ #! /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.upload [changeset ID] [osc file] upload osc file to the given changeset - 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 [element type] [element ID] get all relations reference this element - osm.api.referenced.way [element type] [element ID] get all ways reference this element - 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.query - osm.member.relation - osm.pbf.update - osm.osm.update - ' -} -util.osm.edit() { - vim $FILENAME && source $FILENAME -} - -osm.goto() { - xdg-open https://www.openstreetmap.org/$1/$2 -} - #OSM_SERVER=https://master.apis.dev.openstreetmap.org OSM_SERVER=https://api.openstreetmap.org + OSM_API=$OSM_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 "\n" 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 &&\ - echo "\n" 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 &&\ - echo "\n" 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 "\ntag 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 -} -# $1 as osm file -osm.osc.create() { - while read -r line - do - array=( $(echo $line) ) - - cat $1 |\ - sed -nr " - /<$array[1] id=$array[2]/,/<\/$array[1]/ { - /<$array[1] id=$array[2]/ a \ \ \ \ - /> $1.osc - done - - sed -ir '1 i - 1 i - $ a - $ a ' $1.osc -} -# 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 $OSM_SERVER/changeset/$changeset_id" - echo $changeset_id | xsel -ib -} -# 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 -} -osm.api.changeset.upload() { - cat $2 |\ - sed -r "/<(node|way|relation)/ s/>/ changeset=\"$1\">/" |\ - curl -X POST -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1/upload -} -# 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 - GEOFABRICK_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=$GEOFABRICK_SERVER/000/$SEQ_PATH.state.txt - echo $STATE_URL - [ $(curl.code $STATE_URL) != "404" ] - do - mkdir -p changes - CHANGE_URL=$GEOFABRICK_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 +FILENAME=$0 - PBF_FILE=$NEW_SEQ.osm.pbf - ((NEW_SEQ++)) - done -} -osm.osm.remove() { - while read -r line - do - # put element type and element ID into array - array=( $(echo $line) ) - cat $1 |\ - sed -i "/<$array[1] id=\"$array[2]\"/,/<\/$array[1]>/ d" - done +util.osm.edit() { + vim $FILENAME && source $FILENAME } diff --git a/utils/osm.village.nat_ref.makefile b/utils/osm.village.nat_ref.makefile deleted file mode 100644 index 3937144..0000000 --- a/utils/osm.village.nat_ref.makefile +++ /dev/null @@ -1,59 +0,0 @@ -data/taiwan-latest.osm.pbf: - mkdir -p data - curl -o $@ http://download.geofabrik.de/asia/taiwan-latest.osm.pbf - -data/village.zip: - mkdir -p data - curl -o $@ -L http://data.moi.gov.tw/MoiOD/System/DownloadFile.aspx\?DATA\=B8AF344F-B5C6-4642-AF46-1832054399CE - -data/VILLAGE_MOI_1081007.shp: data/village.zip - @if [ ! -e $@ ]; then unzip $< -d data && rm data/*xml data/*xlsx; fi - -village.csv: data/taiwan-latest.osm.pbf - ogr2ogr $@ $< \ - -lco GEOMETRY=AS_XY \ - -dialect sqlite \ - -sql "SELECT osm_id, name, other_tags, ST_PointOnSurface(geometry) \ - FROM multipolygons \ - WHERE admin_level = '9'" - -village.no_nat_ref.csv: village.csv - grep -v nat_ref $< > $@ - -village.with_nat_ref.csv: village.csv - (head -1 $<; grep nat_ref $<) |\ - sed -r "s/\"\"\".*nat_ref\"\"=>\"\"([^\"]+).*\"\"\"/\1/g" |\ - sed '1s/other_tags/nat_ref/'> $@ - -matched.csv: data/VILLAGE_MOI_1081007.shp village.no_nat_ref.csv - ogr2ogr $@ $(word 2,$^) \ - -oo X_POSSIBLE_NAMES=X -oo Y_POSSIBLE_NAMES=Y \ - -dialect sqlite \ - -sql "SELECT osm.osm_id, gov.* \ - FROM 'village.no_nat_ref' osm, '$<'.VILLAGE_MOI_1081007 gov \ - WHERE osm.name = gov.VILLNAME AND Intersects(gov.geometry, osm.geometry)" - -matched.by_ref.csv: data/VILLAGE_MOI_1081007.shp village.with_nat_ref.csv - ogr2ogr $@ $(word 2,$^) \ - -oo X_POSSIBLE_NAMES=X -oo Y_POSSIBLE_NAMES=Y \ - -dialect sqlite \ - -sql "SELECT osm.osm_id, gov.* \ - FROM 'village.with_nat_ref' osm, '$<'.VILLAGE_MOI_1081007 gov \ - WHERE osm.nat_ref = gov.VILLCODE" - -confilct.list: matched.csv - cat $< | cut -d',' -f2 | sort | uniq -d | xargs -I {} grep {} $< - -origin.osm: matched.csv data/taiwan-latest.osm.pbf - cat $< |\ - sed 1d |\ - cut -d'"' -f2 |\ - sed -nr 's/.*/r\0/p' |\ - osmium getid $(word 2,$^) --id-file - --output-format=osm > $@ - -change.list: matched.csv - cat $< |\ - sed 1d |\ - awk -F',' '{print "relation", $$1, "nat_ref", $$2}' > $@ - -# sed -i -r 's/([0-9]+ +)(.+)$/\1"name:en" "\2"/' diff.eng -- cgit v1.2.3-70-g09d2