aboutsummaryrefslogtreecommitdiffhomepage
path: root/utils/osm.utils.sh
blob: 56f31d0fffea31d546a5ff11527e6d6705128f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
FILENAME=$0

osm.help() {
    echo "
COMMANDS:
  osm.utils.edit
  osm.get
  osm.get.full
  osm.get.history
  osm.in_relations
  osm.in_ways
  osm.extract
  osm.extract.ids
  osm.upload.to
  osm.query
  osm.file.query
  osm.file.extract
  osm.update
  osm.changeset.create
  osm.changeset.add
  osm.changeset.update
  osm.changeset.close
  osm.pbf.update
    "
}
osm.utils.edit() {
    vim $FILENAME && source $FILENAME
}

#SERVER=https://master.apis.dev.openstreetmap.org
SERVER=https://api.openstreetmap.org
OSM_API=$SERVER/api/0.6
OSM_USER_PASSWD=$(cat $HOME/git/settings/tokens/osm)

# get .osm format data
osm.get() {
    curl -X GET $OSM_API/$1/$2 |\
    tee /tmp/osm &&\
    echo content of $1 $2 is copied into /tmp/osm > /dev/tty
}
osm.get.full() {
    curl -X GET $OSM_API/$1/$2/full |\
    tee /tmp/osm &&\
    echo content of $1 $2 and its members are copied into /tmp/osm > /dev/tty
}
osm.get.history() {
    curl -X GET $OSM_API/$1/$2/history |\
    tee /tmp/osm &&\
    echo history of $1 $2 are copied into /tmp/osm > /dev/tty
}
osm.in_relations() {
    curl -X GET $OSM_API/$1/$2/relations |\
    tee /tmp/osm &&\
    echo relations contain $1 $2 are copied into /tmp/osm > /dev/tty
}
osm.in_ways() {
    curl -X GET $OSM_API/node/$1/ways |\
    tee /tmp/osm &&\
    echo ways contain node $1 are copied into /tmp/osm > /dev/tty
}
# extract an element from .osm format STDIN
osm.extract() {
    echo "<osm version=\"0.6\">"
    sed -nr "/^ *<$1 id=\"$2\".*/,/^ *<\/$1>/p" -
    echo "</osm>"
}
# get ids from .osm format STDIN
osm.extract.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.upload.to() {
    cat - > /tmp/osm

    osm.get.ids < /tmp/osm |\
    sed 's#.*#osm.extract \0 < /tmp/osm#g' |\
    sed "s/.*/\0 \| osm.changeset.add $1/g" |\
    while read -r command
    do
        cat <(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.extract() {
    file=$1; shift
    osmium getid $file $@ --output-format=osm
}
# update .osm format STDIN with key-value
osm.update() {
    # remove original tag&value
    sed "/<tag k=\"$1\"/d" - | \
    if [ "$2" != "" ]; then
        # insert new tag&value
        sed -r "/<(node|way|relation)/a \ \ \ \ <tag k=\"$1\" v=\"$2\"\/>"
    else
        # just print it
        sed ''
    fi
}
# create a new changeset
osm.changeset.create() {

    echo -n "type comment: "
    read comment

    info="<osm>
            <changeset>
              <tag k='comment' v='$comment'/>
            </changeset>
          </osm>
         "

    echo $info |\
    curl -u $OSM_USER_PASSWD -i --upload-file - $OSM_API/changeset/create |\
    tee /dev/tty |\
    tail -1 |\
    xsel -ib
}
# add a new element into changeset
osm.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
}
# update changeset with a new comment
osm.changeset.update() {
    echo -n 'type comment: '
    read -r comment

    echo "<osm><changeset><tag k=\"comment\" v=\"$comment\"/></changeset></osm>" | \
    curl -X PUT -u $OSM_USER_PASSWD -i -T - $OSM_API/changeset/$1
}
# close a changeset
osm.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
    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=$SERVER/000/$SEQ_PATH.state.txt
        [ $(curl.code $STATE_URL) != "404" ]
    do
        mkdir -p changes
        CHANGE_URL=$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

        PBF_FILE=$NEW_SEQ.osm.pbf
        ((NEW_SEQ++))
    done
}