diff options
author | typebrook <typebrook@gmail.com> | 2019-11-17 09:18:07 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-11-17 09:18:07 +0800 |
commit | 1d3508f66fddf3f8c6416e59ab9fbde405dbcf25 (patch) | |
tree | 4f703dfa01596c133a4898c88fe837b8971e47e1 /utils/osm | |
parent | e0673805fc648d47eadf5c76b822ccef5780d241 (diff) |
update
Diffstat (limited to 'utils/osm')
-rw-r--r-- | utils/osm | 270 |
1 files changed, 4 insertions, 266 deletions
@@ -1,275 +1,13 @@ | |||
1 | #! /bin/sh | 1 | #! /bin/sh |
2 | 2 | ||
3 | FILENAME=$0 | ||
4 | |||
5 | osm.help() { | ||
6 | echo ' | ||
7 | COMMANDS: | ||
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 | } | ||
39 | util.osm.edit() { | ||
40 | vim $FILENAME && source $FILENAME | ||
41 | } | ||
42 | |||
43 | osm.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 |
48 | OSM_SERVER=https://api.openstreetmap.org | 4 | OSM_SERVER=https://api.openstreetmap.org |
5 | |||
49 | OSM_API=$OSM_SERVER/api/0.6 | 6 | OSM_API=$OSM_SERVER/api/0.6 |
50 | OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm) | 7 | OSM_USER_PASSWD=$(cat $SETTING_DIR/tokens/osm) |
51 | 8 | ||
52 | # get .osm format data | 9 | FILENAME=$0 |
53 | osm.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 | } | ||
58 | osm.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 | } | ||
63 | osm.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 | } | ||
69 | osm.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 | } | ||
75 | osm.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 | } | ||
83 | osm.file.cat() { | ||
84 | osmium cat $1 --output-format=osm -t $2 | ||
85 | } | ||
86 | osm.api.member.relation() { | ||
87 | osm.api.referenced.relation $1 $2 |\ | ||
88 | osm.member.relation $1 $2 $3 | ||
89 | } | ||
90 | osm.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 | } | ||
95 | osm.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 | } | ||
105 | osm.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 | ||
122 | osm.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 | ||
128 | osm.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 | ||
133 | osm.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 | ||
146 | osm.query() { | ||
147 | osmium tags-filter - $@ --input-format=osm --output-format=osm --omit-referenced | ||
148 | } | ||
149 | # query osm-related file with .osm format output | ||
150 | osm.file.query() { | ||
151 | file=$1; shift | ||
152 | osmium tags-filter $file $@ --output-format=osm --omit-referenced | ||
153 | } | ||
154 | # extract an element from osm file | ||
155 | osm.file.get() { | ||
156 | file=$1; shift | ||
157 | osmium getid $file $@ --output-format=osm | ||
158 | } | ||
159 | osm.file.get.full() { | ||
160 | file=$1; shift | ||
161 | osmium getid $file $@ --output-format=osm --add-referenced | ||
162 | } | ||
163 | # $1 as osm file | ||
164 | osm.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 | ||
184 | osm.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 | ||
207 | osm.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 | } | ||
217 | osm.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 | ||
223 | osm.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 | ||
231 | osm.api.changeset.close() { | ||
232 | curl -X PUT -u $OSM_USER_PASSWD -i $OSM_API/changeset/$1/close | ||
233 | } | ||
234 | # update an .osm.pbf file | ||
235 | osm.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 | 11 | util.osm.edit() { |
264 | ((NEW_SEQ++)) | 12 | vim $FILENAME && source $FILENAME |
265 | done | ||
266 | } | ||
267 | osm.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 | } |