aboutsummaryrefslogtreecommitdiffhomepage
path: root/utils/osm
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2019-11-10 00:39:14 +0800
committertypebrook <typebrook@gmail.com>2019-11-10 00:39:14 +0800
commitd933b8021b6838d418332635a537de6bdc2ade98 (patch)
tree756acee8d835afe0a424c5c6e52f0a57a18ef2e3 /utils/osm
parent6c135c1235c3ace511baee440ac6289a9495ff2d (diff)
update
Diffstat (limited to 'utils/osm')
-rw-r--r--utils/osm186
1 files changed, 186 insertions, 0 deletions
diff --git a/utils/osm b/utils/osm
new file mode 100644
index 0000000..e37c8ff
--- /dev/null
+++ b/utils/osm
@@ -0,0 +1,186 @@
1#! /bin/sh
2
3FILENAME=$0
4
5osm.help() {
6 echo "
7COMMANDS:
8 osm.utils.edit
9 osm.get
10 osm.get.full
11 osm.get.history
12 osm.in_relations
13 osm.in_ways
14 osm.extract
15 osm.extract.ids
16 osm.upload.to
17 osm.query
18 osm.file.query
19 osm.file.extract
20 osm.update
21 osm.changeset.create
22 osm.changeset.add
23 osm.changeset.update
24 osm.changeset.close
25 osm.pbf.update
26 "
27}
28osm.utils.edit() {
29 vim $FILENAME && source $FILENAME
30}
31
32#SERVER=https://master.apis.dev.openstreetmap.org
33SERVER=https://api.openstreetmap.org
34OSM_API=$SERVER/api/0.6
35OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm)
36
37# get .osm format data
38osm.fetch() {
39 curl -X GET $OSM_API/$1/$2 |\
40 tee /tmp/osm &&\
41 echo content of $1 $2 is copied into /tmp/osm > /dev/tty
42}
43osm.fetch.full() {
44 curl -X GET $OSM_API/$1/$2/full |\
45 tee /tmp/osm &&\
46 echo content of $1 $2 and its members are copied into /tmp/osm > /dev/tty
47}
48osm.fetch.history() {
49 curl -X GET $OSM_API/$1/$2/history |\
50 tee /tmp/osm &&\
51 echo history of $1 $2 are copied into /tmp/osm > /dev/tty
52}
53osm.in_relations() {
54 curl -X GET $OSM_API/$1/$2/relations |\
55 tee /tmp/osm &&\
56 echo relations contain $1 $2 are copied into /tmp/osm > /dev/tty
57}
58osm.in_ways() {
59 curl -X GET $OSM_API/node/$1/ways |\
60 tee /tmp/osm &&\
61 echo ways contain node $1 are copied into /tmp/osm > /dev/tty
62}
63# extract an element from .osm format STDIN
64osm.extract() {
65 echo "<osm version=\"0.6\">"
66 sed -nr "/^ *<$1 id=\"$2\".*/,/^ *<\/$1>/p"
67 echo "</osm>"
68}
69# get ids from .osm format STDIN
70osm.extract.ids() {
71 sed -nr 's/.*<(node|way|relation) id=\"([^"]+)\".*/\1 \2/p'
72}
73# upload .osm format STDIN to a given changeset
74# allows multiple elements in osm body
75osm.upload.to() {
76
77 tee /tmp/osm |\
78 osm.extract.ids |\
79 sed 's#.*#osm.extract \0 < /tmp/osm#g' |\
80 sed "s/.*/\0 \| osm.changeset.add $1/g" |\
81 while read -r command
82 do
83 echo $command
84 source<(echo "($command &)")
85 done
86}
87# query .osm format STDIN
88osm.query() {
89 osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced
90}
91# query osm-related file with .osm format output
92osm.file.query() {
93 file=$1; shift
94 osmium tags-filter $file $@ --output-format=osm --omit-referenced
95}
96# extract an element from osm file
97osm.file.extract() {
98 file=$1; shift
99 osmium getid $file $@ --output-format=osm
100}
101# update .osm format STDIN with key-value
102osm.update() {
103 # remove original tag&value
104 sed "/<tag k=\"$1\"/d" - | \
105 if [ "$2" != "" ]; then
106 # insert new tag&value
107 sed -r "/<(node|way|relation)/a \ \ \ \ <tag k=\"$1\" v=\"$2\"\/>"
108 else
109 # just print it
110 sed ''
111 fi
112}
113# create a new changeset
114osm.changeset.create() {
115
116 echo -n "type comment: "
117 read comment
118
119 info="<osm>
120 <changeset>
121 <tag k='comment' v='$comment'/>
122 </changeset>
123 </osm>
124 "
125
126 echo $info |\
127 curl -u $OSM_USER_PASSWD -i --upload-file - $OSM_API/changeset/create |\
128 tee /dev/tty |\
129 tail -1 |\
130 xsel -ib
131}
132# add a new element into changeset
133osm.changeset.add() {
134 element=$(cat)
135 header=$(echo $element | grep -E "<(node|way|relation)\s")
136 ele_type=$(echo $header | sed -r 's/.*<(node|way|relation).*$/\1/')
137 id=$(echo $header | sed -r 's/.* id=\"([^"]+)\".*$/\1/')
138
139 echo $element | \
140 sed -r "s/^( *<(node|way|relation).*version[^ ]+ )(.*)$/\1changeset=\"$1\">/" | \
141 curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/$ele_type/$id
142}
143# update changeset with a new comment
144osm.changeset.update() {
145 echo -n 'type comment: '
146 read -r comment
147
148 echo "<osm><changeset><tag k=\"comment\" v=\"$comment\"/></changeset></osm>" | \
149 curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1
150}
151# close a changeset
152osm.changeset.close() {
153 curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close
154}
155# update an .osm.pbf file
156osm.pbf.update() {
157 PBF_FILE=$1
158 SERVER=http://download.geofabrik.de/asia/taiwan-updates
159
160 # get next sequence number and store it into NEW_SEQ
161 osmium fileinfo $PBF_FILE | \
162 grep osmosis_replication_sequence_number | \
163 cut -d'=' -f2 | \
164 sed 's/$/+1/' | bc | \
165 read NEW_SEQ
166
167 # while server has osc file with given sequence number,
168 # get it and do file update
169 while
170 SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/')
171 STATE_URL=$SERVER/000/$SEQ_PATH.state.txt
172 [ $(curl.code $STATE_URL) != "404" ]
173 do
174 mkdir -p changes
175 CHANGE_URL=$SERVER/000/$SEQ_PATH.osc.gz
176 echo $CHANGE_URL
177 curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \
178 osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \
179 --output-header=osmosis_replication_sequence_number=$NEW_SEQ \
180 --overwrite \
181 --output $NEW_SEQ.osm.pbf
182
183 PBF_FILE=$NEW_SEQ.osm.pbf
184 ((NEW_SEQ++))
185 done
186}