aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/osm/osm.osm.by_tag
blob: cc92c1d33045e63d19e5fb0c614aff258dbf09c6 (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
#!/bin/bash

# create new tags from input line, for example: 
# field1         field2       field3     field4   field5     field6   field7       field8...
# [element type] [element ID] key1_added "value1" key2_added "value2" key3_removed key4_removed...

# key should not quoted, value must be quoted
# And keys which need to be removed must be placed at the end
while read -r line
do
    TYPE=$(echo $line | cut -d ' ' -f1) # field1 is type
    ID=$(echo $line | cut -d ' ' -f2)   # field2 is ID

    # transform key-value pair into tag format:
    # <tag k="[key]" v="[value]"/>
    # keys without values are omitted
    NEW_TAGS=$(echo $line |\
               cut -d' ' -f3- |\
               sed -r 's/([^ "]+) (\"[^"]+\")/<tag k=\"\1\" v=\2\/>/g; s/>[^"]*$/>/')

    # get regex pattern need to removed from original osm element:
    # key1|key2|key3|key4
    TAG_PATTERN=$(echo $line |\
                  cut -d' ' -f3- | xargs -n2 echo |\
                  cut -d' ' -f1 | paste -s -d'|')

    echo $NEW_TAGS > /dev/tty

    # print matched element with new tags to .osc file
    cat $1 |\
    sed -nr "/<$TYPE id=\"$ID\"/,/<\/$TYPE/ {
                 /<$TYPE id=\"$ID\"/ {
                     s/(version=\"[0-9]+\")(.*)/\1>/
                     a \ \ \ \ $NEW_TAGS
                 }
                 /<tag k=\"($TAG_PATTERN)\"/ !p
                 /<\/$TYPE/ q
            }" >> $1.osc
done

# Add .osc structure for output
sed -ir '1 i <osmChange version="0.6" generator="bash script">
         1 i <modify>
         $ a </modify>
         $ a </osmChange>' $1.osc