aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xscripts/csv/csv.move_column (renamed from scripts/csv.move_column)0
-rwxr-xr-xscripts/csv/csv.reorder (renamed from scripts/csv.reorder)4
-rwxr-xr-xscripts/csv/csv2geojson (renamed from scripts/csv2geojson)0
-rw-r--r--scripts/osm/osm.api.change.create21
-rw-r--r--scripts/osm/osm.api.changeset.add10
-rw-r--r--scripts/osm/osm.api.changeset.close3
-rw-r--r--scripts/osm/osm.api.changeset.update7
-rw-r--r--scripts/osm/osm.api.changeset.upload5
-rw-r--r--scripts/osm/osm.api.fetch6
-rw-r--r--scripts/osm/osm.api.fetch.full5
-rw-r--r--scripts/osm/osm.api.fetch.history6
-rw-r--r--scripts/osm/osm.api.member.relation4
-rw-r--r--scripts/osm/osm.api.referenced.relation6
-rw-r--r--scripts/osm/osm.api.referenced.way3
-rw-r--r--scripts/osm/osm.api.upload.to12
-rw-r--r--scripts/osm/osm.file.get4
-rw-r--r--scripts/osm/osm.file.get.full4
-rw-r--r--scripts/osm/osm.file.query4
-rw-r--r--scripts/osm/osm.get.id6
-rw-r--r--scripts/osm/osm.goto1
-rw-r--r--scripts/osm/osm.help34
-rw-r--r--scripts/osm/osm.list.ids3
-rw-r--r--scripts/osm/osm.list.tag8
-rw-r--r--scripts/osm/osm.list.tags13
-rw-r--r--scripts/osm/osm.member.relation8
-rw-r--r--scripts/osm/osm.osc.create17
-rw-r--r--scripts/osm/osm.osm.remove7
-rw-r--r--scripts/osm/osm.pbf.update30
-rw-r--r--scripts/osm/osm.query1
-rw-r--r--scripts/osm/osm.village.nat_ref.makefile (renamed from utils/osm.village.nat_ref.makefile)0
-rw-r--r--utils/osm270
31 files changed, 235 insertions, 267 deletions
diff --git a/scripts/csv.move_column b/scripts/csv/csv.move_column
index a62701f..a62701f 100755
--- a/scripts/csv.move_column
+++ b/scripts/csv/csv.move_column
diff --git a/scripts/csv.reorder b/scripts/csv/csv.reorder
index ef9766b..df928f1 100755
--- a/scripts/csv.reorder
+++ b/scripts/csv/csv.reorder
@@ -12,6 +12,8 @@ read -p "type column numbers by new order, like 3 2 1: " order
12arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',') 12arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',')
13 13
14cat $1 |\ 14cat $1 |\
15awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" 15awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\
16tee /tmp/csv |\
17echo "Also copied to /tmp/csv" > /dev/tty
16 18
17 19
diff --git a/scripts/csv2geojson b/scripts/csv/csv2geojson
index 085e12e..085e12e 100755
--- a/scripts/csv2geojson
+++ b/scripts/csv/csv2geojson
diff --git a/scripts/osm/osm.api.change.create b/scripts/osm/osm.api.change.create
new file mode 100644
index 0000000..1f17334
--- /dev/null
+++ b/scripts/osm/osm.api.change.create
@@ -0,0 +1,21 @@
1#!/bin/sh
2
3echo -n "type comment: "
4read comment
5
6info="<osm>
7 <changeset>
8 <tag k='comment' v='$comment'/>
9 <tag k='bot' v='yes'/>
10 </changeset>
11 </osm>
12 "
13
14echo $info |\
15curl -u $OSM_USER_PASSWD --upload-file - $OSM_API/changeset/create |\
16tee /dev/tty |\
17tail -1 | read changeset_id
18
19echo " copied into clipboard"
20echo "changeset created, check $OSM_SERVER/changeset/$changeset_id"
21echo $changeset_id | xsel -ib
diff --git a/scripts/osm/osm.api.changeset.add b/scripts/osm/osm.api.changeset.add
new file mode 100644
index 0000000..3e4878e
--- /dev/null
+++ b/scripts/osm/osm.api.changeset.add
@@ -0,0 +1,10 @@
1#!/bin/sh
2
3element=$(cat)
4header=$(echo $element | grep -E "<(node|way|relation)\s")
5ele_type=$(echo $header | sed -r 's/.*<(node|way|relation).*$/\1/')
6id=$(echo $header | sed -r 's/.* id=\"([^"]+)\".*$/\1/')
7
8echo $element | \
9sed -r "s/^( *<(node|way|relation).*version[^ ]+ )(.*)$/\1changeset=\"$1\">/" | \
10curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/$ele_type/$id
diff --git a/scripts/osm/osm.api.changeset.close b/scripts/osm/osm.api.changeset.close
new file mode 100644
index 0000000..3b016c3
--- /dev/null
+++ b/scripts/osm/osm.api.changeset.close
@@ -0,0 +1,3 @@
1#!/bin/sh
2
3curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close
diff --git a/scripts/osm/osm.api.changeset.update b/scripts/osm/osm.api.changeset.update
new file mode 100644
index 0000000..7b32741
--- /dev/null
+++ b/scripts/osm/osm.api.changeset.update
@@ -0,0 +1,7 @@
1#!/bin/sh
2
3echo -n 'type comment: '
4read -r comment
5
6echo "<osm><changeset><tag k=\"comment\" v=\"$comment\"/></changeset></osm>" | \
7curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1
diff --git a/scripts/osm/osm.api.changeset.upload b/scripts/osm/osm.api.changeset.upload
new file mode 100644
index 0000000..918a755
--- /dev/null
+++ b/scripts/osm/osm.api.changeset.upload
@@ -0,0 +1,5 @@
1#!/bin/sh
2
3cat $2 |\
4sed -r "/<(node|way|relation)/ s/>/ changeset=\"$1\">/" |\
5curl -X POST -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1/upload
diff --git a/scripts/osm/osm.api.fetch b/scripts/osm/osm.api.fetch
new file mode 100644
index 0000000..8460c5f
--- /dev/null
+++ b/scripts/osm/osm.api.fetch
@@ -0,0 +1,6 @@
1#! /bin/sh
2
3# get .osm format data
4curl -X GET $OSM_API/$1/$2 |\
5tee /tmp/osm &&\
6echo content of $1 $2 is copied into /tmp/osm > /dev/tty
diff --git a/scripts/osm/osm.api.fetch.full b/scripts/osm/osm.api.fetch.full
new file mode 100644
index 0000000..5ee78b8
--- /dev/null
+++ b/scripts/osm/osm.api.fetch.full
@@ -0,0 +1,5 @@
1#! /bin/sh
2
3curl -X GET $OSM_API/$1/$2/full |\
4tee /tmp/osm &&\
5echo "\n" content of $1 $2 and its members are copied into /tmp/osm > /dev/tty
diff --git a/scripts/osm/osm.api.fetch.history b/scripts/osm/osm.api.fetch.history
new file mode 100644
index 0000000..bc837bd
--- /dev/null
+++ b/scripts/osm/osm.api.fetch.history
@@ -0,0 +1,6 @@
1#! /bin/sh
2
3curl -X GET $OSM_API/$1/$2/history |\
4tee /tmp/osm &&\
5echo &&\
6echo "\n" history of $1 $2 are copied into /tmp/osm > /dev/tty
diff --git a/scripts/osm/osm.api.member.relation b/scripts/osm/osm.api.member.relation
new file mode 100644
index 0000000..5addef7
--- /dev/null
+++ b/scripts/osm/osm.api.member.relation
@@ -0,0 +1,4 @@
1#! /bin/sh
2
3osm.api.referenced.relation $1 $2 |\
4osm.member.relation $1 $2 $3
diff --git a/scripts/osm/osm.api.referenced.relation b/scripts/osm/osm.api.referenced.relation
new file mode 100644
index 0000000..2ce0631
--- /dev/null
+++ b/scripts/osm/osm.api.referenced.relation
@@ -0,0 +1,6 @@
1#! /bin/sh
2
3curl -X GET $OSM_API/$1/$2/relations |\
4tee /tmp/osm &&\
5echo &&\
6echo "\n" relations contain $1 $2 are copied into /tmp/osm > /dev/tty
diff --git a/scripts/osm/osm.api.referenced.way b/scripts/osm/osm.api.referenced.way
new file mode 100644
index 0000000..b62ac58
--- /dev/null
+++ b/scripts/osm/osm.api.referenced.way
@@ -0,0 +1,3 @@
1 curl -X GET $OSM_API/node/$1/ways |\
2 tee /tmp/osm &&\
3 echo ways contain node $1 are copied into /tmp/osm > /dev/tty
diff --git a/scripts/osm/osm.api.upload.to b/scripts/osm/osm.api.upload.to
new file mode 100644
index 0000000..3979220
--- /dev/null
+++ b/scripts/osm/osm.api.upload.to
@@ -0,0 +1,12 @@
1#! /bin/sh
2
3# allows multiple elements in osm body
4tee /tmp/osm |\
5osm.list.ids |\
6sed 's#.*#osm.extract \0 < /tmp/osm#g' |\
7sed "s/.*/\0 \| osm.api.changeset.add $1/g" |\
8while read -r command
9do
10 echo $command
11 source<(echo "($command &)")
12done
diff --git a/scripts/osm/osm.file.get b/scripts/osm/osm.file.get
new file mode 100644
index 0000000..e2b67b4
--- /dev/null
+++ b/scripts/osm/osm.file.get
@@ -0,0 +1,4 @@
1#! /bin/sh
2
3file=$1; shift
4osmium getid $file $@ --output-format=osm
diff --git a/scripts/osm/osm.file.get.full b/scripts/osm/osm.file.get.full
new file mode 100644
index 0000000..50ee2f1
--- /dev/null
+++ b/scripts/osm/osm.file.get.full
@@ -0,0 +1,4 @@
1#! /bin/sh
2
3file=$1; shift
4osmium getid $file $@ --output-format=osm --add-referenced
diff --git a/scripts/osm/osm.file.query b/scripts/osm/osm.file.query
new file mode 100644
index 0000000..4a01b5b
--- /dev/null
+++ b/scripts/osm/osm.file.query
@@ -0,0 +1,4 @@
1#! /bin/sh
2
3file=$1; shift
4osmium tags-filter $file $@ --output-format=osm --omit-referenced
diff --git a/scripts/osm/osm.get.id b/scripts/osm/osm.get.id
new file mode 100644
index 0000000..93593b5
--- /dev/null
+++ b/scripts/osm/osm.get.id
@@ -0,0 +1,6 @@
1#! /bin/sh
2
3# $1 as [node|way|relation], $2 as id
4echo "<osm version=\"0.6\">"
5sed -nr "/^ *<$1 id=\"$2\".*/,/^ *<\/$1>/p"
6echo "</osm>"
diff --git a/scripts/osm/osm.goto b/scripts/osm/osm.goto
new file mode 100644
index 0000000..7da3394
--- /dev/null
+++ b/scripts/osm/osm.goto
@@ -0,0 +1 @@
xdg-open https://www.openstreetmap.org/$1/$2
diff --git a/scripts/osm/osm.help b/scripts/osm/osm.help
new file mode 100644
index 0000000..aa3b36b
--- /dev/null
+++ b/scripts/osm/osm.help
@@ -0,0 +1,34 @@
1#! /bin/sh
2
3 echo '
4COMMANDS:
5 osm.help
6 util.osm.edit
7
8 osm.api.changeset.create create a new changeset and return its ID
9 osm.api.changeset.update [changeset ID] update changeset with new comment
10 osm.api.changeset.upload [changeset ID] [osc file] upload osc file to the given changeset
11 osm.api.changeset.add [changeset ID] STDIN=osm element, add it into changeset
12 osm.api.changeset.close [changeset ID] close the given changeset
13 osm.api.fetch [element type] [element ID]
14 osm.api.fetch.full like .fetch, but return with all referenced elements
15 osm.api.fetch.history [element type] [element ID]
16 osm.api.member.relation [element type] [element ID] [member type] get first relations reference it as member type
17 osm.api.referenced.relation [element type] [element ID] get all relations reference this element
18 osm.api.referenced.way [element type] [element ID] get all ways reference this element
19 osm.api.upload.to
20
21 osm.file.cat
22 osm.file.get
23 osm.file.get.full
24 osm.file.query
25 osm.list.ids
26 osm.list.tag
27 osm.list.tags
28
29 osm.get.id
30 osm.query
31 osm.member.relation
32 osm.pbf.update
33 osm.osm.update
34 '
diff --git a/scripts/osm/osm.list.ids b/scripts/osm/osm.list.ids
new file mode 100644
index 0000000..8e5d870
--- /dev/null
+++ b/scripts/osm/osm.list.ids
@@ -0,0 +1,3 @@
1#! /bin/sh
2
3sed -nr 's/.*<(node|way|relation) id=\"([^"]+)\".*/\1 \2/p'
diff --git a/scripts/osm/osm.list.tag b/scripts/osm/osm.list.tag
new file mode 100644
index 0000000..3d5157d
--- /dev/null
+++ b/scripts/osm/osm.list.tag
@@ -0,0 +1,8 @@
1 ele_pattern="(node|way|relation)"
2 sed -nr "/<$ele_pattern/,/<\/$ele_pattern/ {
3 /<tag k=\"$1\"/ {
4 s/.*v=\"([^\"]+)\".*/\1/
5 h
6 }
7 /<\/$ele_pattern/ {x;p;s/.*//;x}
8 }"
diff --git a/scripts/osm/osm.list.tags b/scripts/osm/osm.list.tags
new file mode 100644
index 0000000..6fe4da4
--- /dev/null
+++ b/scripts/osm/osm.list.tags
@@ -0,0 +1,13 @@
1 content=$(cat)
2 echo $content | osm.list.ids | tr ' ' ',' > /tmp/osm
3
4 for tag in $@
5 do
6 echo $content |\
7 osm.list.tag $tag |\
8 paste -d',' /tmp/osm - > /tmp/osm.new &&\
9 mv /tmp/osm.new /tmp/osm
10 done
11
12 cat /tmp/osm
13 echo "\ntag list is also copied into /tmp/osm" > /dev/tty
diff --git a/scripts/osm/osm.member.relation b/scripts/osm/osm.member.relation
new file mode 100644
index 0000000..f8d4598
--- /dev/null
+++ b/scripts/osm/osm.member.relation
@@ -0,0 +1,8 @@
1#! /bin/sh
2
3sed -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/scripts/osm/osm.osc.create b/scripts/osm/osm.osc.create
new file mode 100644
index 0000000..61a4dfb
--- /dev/null
+++ b/scripts/osm/osm.osc.create
@@ -0,0 +1,17 @@
1 while read -r line
2 do
3 array=( $(echo $line) )
4
5 cat $1 |\
6 sed -nr "
7 /<$array[1] id=$array[2]/,/<\/$array[1]/ {
8 /<$array[1] id=$array[2]/ a \ \ \ \ <tag k=\"$array[3]\" v=$array[4]\/>
9 /<tag k=$array[3]/ !p
10 /<\/$array[1]/ q
11 }" >> $1.osc
12 done
13
14 sed -ir '1 i <osmChange version="0.6" generator="bash script">
15 1 i <modify>
16 $ a </modify>
17 $ a </osmChange>' $1.osc
diff --git a/scripts/osm/osm.osm.remove b/scripts/osm/osm.osm.remove
new file mode 100644
index 0000000..fbbaf0c
--- /dev/null
+++ b/scripts/osm/osm.osm.remove
@@ -0,0 +1,7 @@
1 while read -r line
2 do
3 # put element type and element ID into array
4 array=( $(echo $line) )
5 cat $1 |\
6 sed -i "/<$array[1] id=\"$array[2]\"/,/<\/$array[1]>/ d"
7 done
diff --git a/scripts/osm/osm.pbf.update b/scripts/osm/osm.pbf.update
new file mode 100644
index 0000000..694ae4d
--- /dev/null
+++ b/scripts/osm/osm.pbf.update
@@ -0,0 +1,30 @@
1 PBF_FILE=$1
2 GEOFABRICK_SERVER=http://download.geofabrik.de/asia/taiwan-updates
3
4 # get next sequence number and store it into NEW_SEQ
5 osmium fileinfo $PBF_FILE | \
6 grep osmosis_replication_sequence_number | \
7 cut -d'=' -f2 | \
8 sed 's/$/+1/' | bc | \
9 read NEW_SEQ
10
11 # while server has osc file with given sequence number,
12 # get it and do file update
13 while
14 SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/')
15 STATE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.state.txt
16 echo $STATE_URL
17 [ $(curl.code $STATE_URL) != "404" ]
18 do
19 mkdir -p changes
20 CHANGE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.osc.gz
21 echo $CHANGE_URL
22 curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \
23 osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \
24 --output-header=osmosis_replication_sequence_number=$NEW_SEQ \
25 --overwrite \
26 --output $NEW_SEQ.osm.pbf
27
28 PBF_FILE=$NEW_SEQ.osm.pbf
29 ((NEW_SEQ++))
30 done
diff --git a/scripts/osm/osm.query b/scripts/osm/osm.query
new file mode 100644
index 0000000..fd48fd2
--- /dev/null
+++ b/scripts/osm/osm.query
@@ -0,0 +1 @@
osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced
diff --git a/utils/osm.village.nat_ref.makefile b/scripts/osm/osm.village.nat_ref.makefile
index 3937144..3937144 100644
--- a/utils/osm.village.nat_ref.makefile
+++ b/scripts/osm/osm.village.nat_ref.makefile
diff --git a/utils/osm b/utils/osm
index cdc4eb7..2d32575 100644
--- a/utils/osm
+++ b/utils/osm
@@ -1,275 +1,13 @@
1#! /bin/sh 1#! /bin/sh
2 2
3FILENAME=$0
4
5osm.help() {
6 echo '
7COMMANDS:
8 osm.help
9 util.osm.edit
10
11 osm.api.changeset.create create a new changeset and return its ID
12 osm.api.changeset.update [changeset ID] update changeset with new comment
13 osm.api.changeset.upload [changeset ID] [osc file] upload osc file to the given changeset
14 osm.api.changeset.add [changeset ID] STDIN=osm element, add it into changeset
15 osm.api.changeset.close [changeset ID] close the given changeset
16 osm.api.fetch [element type] [element ID]
17 osm.api.fetch.full like .fetch, but return with all referenced elements
18 osm.api.fetch.history [element type] [element ID]
19 osm.api.member.relation [element type] [element ID] [member type] get first relations reference it as member type
20 osm.api.referenced.relation [element type] [element ID] get all relations reference this element
21 osm.api.referenced.way [element type] [element ID] get all ways reference this element
22 osm.api.upload.to
23
24 osm.file.cat
25 osm.file.get
26 osm.file.get.full
27 osm.file.query
28 osm.list.ids
29 osm.list.tag
30 osm.list.tags
31
32 osm.get.id
33 osm.query
34 osm.member.relation
35 osm.pbf.update
36 osm.osm.update
37 '
38}
39util.osm.edit() {
40 vim $FILENAME && source $FILENAME
41}
42
43osm.goto() {
44 xdg-open https://www.openstreetmap.org/$1/$2
45}
46
47#OSM_SERVER=https://master.apis.dev.openstreetmap.org 3#OSM_SERVER=https://master.apis.dev.openstreetmap.org
48OSM_SERVER=https://api.openstreetmap.org 4OSM_SERVER=https://api.openstreetmap.org
5
49OSM_API=$OSM_SERVER/api/0.6 6OSM_API=$OSM_SERVER/api/0.6
50OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm) 7OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm)
51 8
52# get .osm format data 9FILENAME=$0
53osm.api.fetch() {
54 curl -X GET $OSM_API/$1/$2 |\
55 tee /tmp/osm &&\
56 echo content of $1 $2 is copied into /tmp/osm > /dev/tty
57}
58osm.api.fetch.full() {
59 curl -X GET $OSM_API/$1/$2/full |\
60 tee /tmp/osm &&\
61 echo "\n" content of $1 $2 and its members are copied into /tmp/osm > /dev/tty
62}
63osm.api.fetch.history() {
64 curl -X GET $OSM_API/$1/$2/history |\
65 tee /tmp/osm &&\
66 echo &&\
67 echo "\n" history of $1 $2 are copied into /tmp/osm > /dev/tty
68}
69osm.api.referenced.relation() {
70 curl -X GET $OSM_API/$1/$2/relations |\
71 tee /tmp/osm &&\
72 echo &&\
73 echo "\n" relations contain $1 $2 are copied into /tmp/osm > /dev/tty
74}
75osm.member.relation() {
76 sed -nr "/<relation/,/<\/relation/ {
77 /<relation/ {s/.* id=\"([^\"]+)\".*/\1/;h}
78 /<member type=\"$1\" ref=\"$2\" role=\"$3\"/ {g;p;q}
79 }
80 $ {s/.*//;P}
81 " | head -1
82}
83osm.file.cat() {
84 osmium cat $1 --output-format=osm -t $2
85}
86osm.api.member.relation() {
87 osm.api.referenced.relation $1 $2 |\
88 osm.member.relation $1 $2 $3
89}
90osm.api.referenced.way() {
91 curl -X GET $OSM_API/node/$1/ways |\
92 tee /tmp/osm &&\
93 echo ways contain node $1 are copied into /tmp/osm > /dev/tty
94}
95osm.list.tag() {
96 ele_pattern="(node|way|relation)"
97 sed -nr "/<$ele_pattern/,/<\/$ele_pattern/ {
98 /<tag k=\"$1\"/ {
99 s/.*v=\"([^\"]+)\".*/\1/
100 h
101 }
102 /<\/$ele_pattern/ {x;p;s/.*//;x}
103 }"
104}
105osm.list.tags() {
106 content=$(cat)
107 echo $content | osm.list.ids | tr ' ' ',' > /tmp/osm
108
109 for tag in $@
110 do
111 echo $content |\
112 osm.list.tag $tag |\
113 paste -d',' /tmp/osm - > /tmp/osm.new &&\
114 mv /tmp/osm.new /tmp/osm
115 done
116
117 cat /tmp/osm
118 echo "\ntag list is also copied into /tmp/osm" > /dev/tty
119}
120# extract an element from .osm format STDIN
121# $1 as [node|way|relation], $2 as id
122osm.get.id() {
123 echo "<osm version=\"0.6\">"
124 sed -nr "/^ *<$1 id=\"$2\".*/,/^ *<\/$1>/p"
125 echo "</osm>"
126}
127# get ids from .osm format STDIN
128osm.list.ids() {
129 sed -nr 's/.*<(node|way|relation) id=\"([^"]+)\".*/\1 \2/p'
130}
131# upload .osm format STDIN to a given changeset
132# allows multiple elements in osm body
133osm.api.upload.to() {
134
135 tee /tmp/osm |\
136 osm.list.ids |\
137 sed 's#.*#osm.extract \0 < /tmp/osm#g' |\
138 sed "s/.*/\0 \| osm.api.changeset.add $1/g" |\
139 while read -r command
140 do
141 echo $command
142 source<(echo "($command &)")
143 done
144}
145# query .osm format STDIN
146osm.query() {
147 osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced
148}
149# query osm-related file with .osm format output
150osm.file.query() {
151 file=$1; shift
152 osmium tags-filter $file $@ --output-format=osm --omit-referenced
153}
154# extract an element from osm file
155osm.file.get() {
156 file=$1; shift
157 osmium getid $file $@ --output-format=osm
158}
159osm.file.get.full() {
160 file=$1; shift
161 osmium getid $file $@ --output-format=osm --add-referenced
162}
163# $1 as osm file
164osm.osc.create() {
165 while read -r line
166 do
167 array=( $(echo $line) )
168
169 cat $1 |\
170 sed -nr "
171 /<$array[1] id=$array[2]/,/<\/$array[1]/ {
172 /<$array[1] id=$array[2]/ a \ \ \ \ <tag k=\"$array[3]\" v=$array[4]\/>
173 /<tag k=$array[3]/ !p
174 /<\/$array[1]/ q
175 }" >> $1.osc
176 done
177
178 sed -ir '1 i <osmChange version="0.6" generator="bash script">
179 1 i <modify>
180 $ a </modify>
181 $ a </osmChange>' $1.osc
182}
183# create a new changeset
184osm.api.changeset.create() {
185
186 echo -n "type comment: "
187 read comment
188
189 info="<osm>
190 <changeset>
191 <tag k='comment' v='$comment'/>
192 <tag k='bot' v='yes'/>
193 </changeset>
194 </osm>
195 "
196
197 echo $info |\
198 curl -u $OSM_USER_PASSWD --upload-file - $OSM_API/changeset/create |\
199 tee /dev/tty |\
200 tail -1 | read changeset_id
201
202 echo " copied into clipboard"
203 echo "changeset created, check $OSM_SERVER/changeset/$changeset_id"
204 echo $changeset_id | xsel -ib
205}
206# add a new element into changeset
207osm.api.changeset.add() {
208 element=$(cat)
209 header=$(echo $element | grep -E "<(node|way|relation)\s")
210 ele_type=$(echo $header | sed -r 's/.*<(node|way|relation).*$/\1/')
211 id=$(echo $header | sed -r 's/.* id=\"([^"]+)\".*$/\1/')
212
213 echo $element | \
214 sed -r "s/^( *<(node|way|relation).*version[^ ]+ )(.*)$/\1changeset=\"$1\">/" | \
215 curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/$ele_type/$id
216}
217osm.api.changeset.upload() {
218 cat $2 |\
219 sed -r "/<(node|way|relation)/ s/>/ changeset=\"$1\">/" |\
220 curl -X POST -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1/upload
221}
222# update changeset with a new comment
223osm.api.changeset.update() {
224 echo -n 'type comment: '
225 read -r comment
226
227 echo "<osm><changeset><tag k=\"comment\" v=\"$comment\"/></changeset></osm>" | \
228 curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1
229}
230# close a changeset
231osm.api.changeset.close() {
232 curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close
233}
234# update an .osm.pbf file
235osm.pbf.update() {
236 PBF_FILE=$1
237 GEOFABRICK_SERVER=http://download.geofabrik.de/asia/taiwan-updates
238
239 # get next sequence number and store it into NEW_SEQ
240 osmium fileinfo $PBF_FILE | \
241 grep osmosis_replication_sequence_number | \
242 cut -d'=' -f2 | \
243 sed 's/$/+1/' | bc | \
244 read NEW_SEQ
245
246 # while server has osc file with given sequence number,
247 # get it and do file update
248 while
249 SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/')
250 STATE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.state.txt
251 echo $STATE_URL
252 [ $(curl.code $STATE_URL) != "404" ]
253 do
254 mkdir -p changes
255 CHANGE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.osc.gz
256 echo $CHANGE_URL
257 curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \
258 osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \
259 --output-header=osmosis_replication_sequence_number=$NEW_SEQ \
260 --overwrite \
261 --output $NEW_SEQ.osm.pbf
262 10
263 PBF_FILE=$NEW_SEQ.osm.pbf 11util.osm.edit() {
264 ((NEW_SEQ++)) 12 vim $FILENAME && source $FILENAME
265 done
266}
267osm.osm.remove() {
268 while read -r line
269 do
270 # put element type and element ID into array
271 array=( $(echo $line) )
272 cat $1 |\
273 sed -i "/<$array[1] id=\"$array[2]\"/,/<\/$array[1]>/ d"
274 done
275} 13}