summaryrefslogtreecommitdiff
path: root/kmlstitch.sh
blob: 5a254a352e5ff2788fd4d265d496b348a2438bae (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
# Shell script to stitch kml files together.
# SJP August 2015

# name the data directory
datadir='../data/'
echo $datadir

outfile='alltrip.kml'
endheader='<name>'

# Functions ##########################

unzipKmz()
{
 echo file is $file   
 if [ ${file: -4} == ".kmz" ]
 then
    unzip $file
 fi
}


extractTrack()
{
    if [ -f doc.kml ]
    then    #
        placelines=$( lineNums placemark doc.kml)
        startline=$(echo $placelines | cut -d ' ' -f 1 )
        endline=$(echo $placelines | rev | cut -d ' ' -f 1 | rev)
        echo $FUNCNAME
        sed -n "${startline},${endline}p" doc.kml >> ${outfile}
        echo $FUNCNAME: placelines: $placelines
        echo startline: $startline
        echo endline $endline
        rm doc.kml
    fi
}
#

writeHeader()
{
    if [ -f $outfile ]
    then
        mv $outfile $outfile.old
    fi
    
    #stopline=$(grep -n $endheader doc.kml | cut -d : -f 1 | head -1)
    stopline=$(lineNums $endheader doc.kml | head -1)
echo $FUNCNAME
    sed -e ${stopline}q doc.kml > $outfile
    echo stopline = $stopline
}

# return a list of line numbers that match arg1
lineNums()
{
    match=$1
    file=$2
    grep -ni ${match} ${file} | cut -d : -f 1 
}

# Add ending tags to kml file

endKml()
{
    echo "</Document>" >> ${outfile}
    echo "</kml>" >> ${outfile}
}

######################################

# loop through the kml directory

count=0     # loop counter

for file in "$datadir"*.km?
do
    count=$((count+1))
    echo $count ":"
    unzipKmz
    if [ $count == 1 ]
    then
        writeHeader
    fi
    extractTrack
done

endKml
zip ${outfile}.kmz ${outfile}
rm ${outfile}

echo "finished"	

#ls -f $datadir | while read -r file; do wc "$file"; done