Skip to content

Instantly share code, notes, and snippets.

@satokjp
Last active August 29, 2015 14:00
Show Gist options
  • Save satokjp/11396563 to your computer and use it in GitHub Desktop.
Save satokjp/11396563 to your computer and use it in GitHub Desktop.
Android My Tracks App. kmz files combine script for MacOS X
#!/bin/bash
#
#######
# out.put files
f_kml="doc.kml"
f_kmz="out.kmz"
# tmp file
f_tmp="tmp.kml"
#######
if [ $# -ne 2 ]; then
echo
echo "Usage : $0 base_file add_file"
echo " output file : $f_kmz"
echo
exit 1
fi
#######
echo "---------------"
echo "Start"
# base file name input
#f1_kmz="f1.kmz"
f1_kmz="$1.kmz"
f1_kml="f1.kml"
# add file name input
#f2_kmz="f2.kmz"
f2_kmz="$2.kmz"
f2_kml="f2.kml"
echo "Base file : $f1_kmz"
echo "Add file : $f2_kmz"
echo "output file : $f_kmz"
echo
#######
# unzip kmz -> kml
rm $f_kml
rm $f_kmz
unzip $f1_kmz
mv $f_kml $f1_kml
unzip $f2_kmz
mv $f_kml $f2_kml
#######
# f1 file section
# </gx:Track> last line
f1_c1=`grep -n '</gx:Track>' $f1_kml | tail -n 1 | sed 's/:/ /' | awk '{print $1}'`
# line number
f1_c3=`cat $f1_kml | wc -l`
# add line
f1_c4=`expr $f1_c3 - $f1_c1`
# last coordinates value
f1_w=`grep coordinates $f1_kml | tail -n 1 | sed 's/\</ /g' | sed 's/\>/ /g' | awk '{ print $2 }'`
#echo $f1_c1 $f1_c2 $f1_c3 $f1_c4
#echo "'$f1_w'"
#######
# f2 file section
# <gx:Track> first line
f2_c1=`grep -n '<gx:Track>' $f2_kml | head -n 1 | sed 's/:/ /' | awk '{print $1}'`
# </gx:Track> last line
f2_c2=`grep -n '</gx:Track>' $f2_kml | tail -n 1 | sed 's/:/ /' | awk '{print $1}'`
# add area line
f2_c3=`expr $f2_c2 - $f2_c1 + 1`
# last coordinates value
f2_w=`grep coordinates $f2_kml | tail -n 1 | sed 's/\</ /g' | sed 's/\>/ /g' | awk '{ print $2 }'`
#echo $f2_c1 $f2_c2 $f2_c3
#echo "'$f2_w'"
#######
# header area of f1
head -n $f1_c1 $f1_kml > $f_tmp
# add area of f2
head -n $f2_c2 $f2_kml | tail -n $f2_c3 >> $f_tmp
# footer area of f1
tail -n $f1_c4 $f1_kml >> $f_tmp
# chenge end point
sed 's/'$f1_w'/'$f2_w'/g' $f_tmp > $f_kml
#######
# creae zip file for My Track
# zip kml -> kmz
zip $f_kmz $f_kml
echo "End"
echo "---------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment