summaryrefslogtreecommitdiffhomepage
path: root/scripts/osm
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2019-11-17 09:18:07 +0800
committertypebrook <typebrook@gmail.com>2019-11-17 09:18:07 +0800
commit1d3508f66fddf3f8c6416e59ab9fbde405dbcf25 (patch)
tree4f703dfa01596c133a4898c88fe837b8971e47e1 /scripts/osm
parente0673805fc648d47eadf5c76b822ccef5780d241 (diff)
update
Diffstat (limited to 'scripts/osm')
-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.makefile59
27 files changed, 287 insertions, 0 deletions
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/scripts/osm/osm.village.nat_ref.makefile b/scripts/osm/osm.village.nat_ref.makefile
new file mode 100644
index 0000000..3937144
--- /dev/null
+++ b/scripts/osm/osm.village.nat_ref.makefile
@@ -0,0 +1,59 @@
1data/taiwan-latest.osm.pbf:
2 mkdir -p data
3 curl -o $@ http://download.geofabrik.de/asia/taiwan-latest.osm.pbf
4
5data/village.zip:
6 mkdir -p data
7 curl -o $@ -L http://data.moi.gov.tw/MoiOD/System/DownloadFile.aspx\?DATA\=B8AF344F-B5C6-4642-AF46-1832054399CE
8
9data/VILLAGE_MOI_1081007.shp: data/village.zip
10 @if [ ! -e $@ ]; then unzip $< -d data && rm data/*xml data/*xlsx; fi
11
12village.csv: data/taiwan-latest.osm.pbf
13 ogr2ogr $@ $< \
14 -lco GEOMETRY=AS_XY \
15 -dialect sqlite \
16 -sql "SELECT osm_id, name, other_tags, ST_PointOnSurface(geometry) \
17 FROM multipolygons \
18 WHERE admin_level = '9'"
19
20village.no_nat_ref.csv: village.csv
21 grep -v nat_ref $< > $@
22
23village.with_nat_ref.csv: village.csv
24 (head -1 $<; grep nat_ref $<) |\
25 sed -r "s/\"\"\".*nat_ref\"\"=>\"\"([^\"]+).*\"\"\"/\1/g" |\
26 sed '1s/other_tags/nat_ref/'> $@
27
28matched.csv: data/VILLAGE_MOI_1081007.shp village.no_nat_ref.csv
29 ogr2ogr $@ $(word 2,$^) \
30 -oo X_POSSIBLE_NAMES=X -oo Y_POSSIBLE_NAMES=Y \
31 -dialect sqlite \
32 -sql "SELECT osm.osm_id, gov.* \
33 FROM 'village.no_nat_ref' osm, '$<'.VILLAGE_MOI_1081007 gov \
34 WHERE osm.name = gov.VILLNAME AND Intersects(gov.geometry, osm.geometry)"
35
36matched.by_ref.csv: data/VILLAGE_MOI_1081007.shp village.with_nat_ref.csv
37 ogr2ogr $@ $(word 2,$^) \
38 -oo X_POSSIBLE_NAMES=X -oo Y_POSSIBLE_NAMES=Y \
39 -dialect sqlite \
40 -sql "SELECT osm.osm_id, gov.* \
41 FROM 'village.with_nat_ref' osm, '$<'.VILLAGE_MOI_1081007 gov \
42 WHERE osm.nat_ref = gov.VILLCODE"
43
44confilct.list: matched.csv
45 cat $< | cut -d',' -f2 | sort | uniq -d | xargs -I {} grep {} $<
46
47origin.osm: matched.csv data/taiwan-latest.osm.pbf
48 cat $< |\
49 sed 1d |\
50 cut -d'"' -f2 |\
51 sed -nr 's/.*/r\0/p' |\
52 osmium getid $(word 2,$^) --id-file - --output-format=osm > $@
53
54change.list: matched.csv
55 cat $< |\
56 sed 1d |\
57 awk -F',' '{print "relation", $$1, "nat_ref", $$2}' > $@
58
59# sed -i -r 's/([0-9]+ +)(.+)$/\1"name:en" "\2"/' diff.eng