#!/usr/bin/perl # make track db BTREE "trackdb" # companion to mk-photo-index5.pl # Andrew Daviel 1998-2006 advax@triumf.ca # reads Garmin GPS track data saved in text format # and creates location database use DB_File ; use Time::Local ; sub compare { my ($k1,$k2) = @_ ; $k1 <=> $k2 ; } $b = new DB_File::BTREEINFO ; $DB_BTREE->{'compare'} = \&compare ; $b->{'compare'} = \&compare ; $trackh = tie %track, "DB_File", "trackdb", O_RDWR|O_CREAT, 0666, $b or die "Cannot open file trackdb: $!\n" ; if ($dbg) { print STDERR "$b->{'flags'}\n" ; print STDERR "$b->{'cachesize'}\n" ; print STDERR "$b->{'maxkeypage'}\n" ; # not imp print STDERR "$b->{'minkeypage'}\n" ; # def 2 print STDERR "$b->{'psize'}\n" ; # between 512 and 64k, depends on OS print STDERR "$b->{'compare'}\n" ; print STDERR "$b->{'prefix'}\n" ; print STDERR "$b->{'lorder'}\n" ; } $brkt = shift ; while () { chomp ; if (/^Trackpoint/) { $ndg = $time = $time2 = '' ; ($type,$position,$time,$altitude,$depth,$length,$dtime,$speed,$course) = split(/\t/) ; if ($position =~ /([NS])([\d]+)[\s]([\d\.]+)[\s]([EW])([\d]+)[\s]([\d\.]+)/) { $nq = $1 ; $nd = $2 ; $nm = $3 ; $eq = $4 ; $ed = $5 ; $em = $6 ; $ndg = sprintf("%.6f",$nd + $nm/60) ; if ($nq eq 'S') { $ndg = -$ndg ; } $edg = sprintf("%.6f",$ed + $em/60) ; if ($eq eq 'W') { $edg = -$edg ; } } if ($time =~ /([\d]+)\/([\d]+)\/([\d]+) ([\d]+):([\d]+):([\d]+) ([\w]+)/) { $year = $3 ; $mon = $1 ; $mon-- ; $year -= 1900 ; if ($7 eq 'PM' and $4<12) { $h = $4 + 12 ; } else { $h = $4 ; } $time2 = timelocal($6,$5,$h,$2,$mon,$year) ; $time3 = localtime($time2) ; } elsif ($time =~ /([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/) { $year = $1 ; $mon = $2 ; $mon-- ; $year -= 1900 ; $time2 = timelocal($6,$5,$4,$3,$mon,$year) ; $time3 = localtime($time2) ; } if ($time2) { if ($time2 == $brkt) { &brk ; } print "$time $time2 $time3 $ndg,$edg\n" ; if ($ndg > 0 and $time2) { $track{$time2} = "$ndg,$edg" ; } } } elsif (/^Datum/) { ($type,$datum) = split(/\t/) ; } elsif (/^Track/) { ($type,$trackname,$start,$elapsed,$length,$avspeed,$link) = split(/\t/) ; } elsif (/^Header/) { # header } elsif (/^Grid/) { ($type,$grid) = split(/\t/) ; } else { print "Unknown: $_\n" ; } } if ($brkt) { print STDERR "$track{$brkt}\n" ; } untie (%track) or die ; $trackh = tie %track, "DB_File", "trackdb", O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open file trackdb: $!\n" ; #foreach (keys %track) { # $time = localtime($_) ; # print "$_ $time $track{$_}\n" ; #} if ($brkt) { print STDERR "$track{$brkt}\n" ; } untie (%track) or die ; #Track ACTIVE LOG 7/30/2004 7:26:56 PM 00:23:39 9.26 nm 23 kt #Trackpoint N49 15.338 W123 14.164 7/30/2004 7:26:56 PM 97.7 m #Trackpoint N49 15.258 W123 14.441 7/30/2004 7:27:37 PM 82.3 m 0.198 nm 00:00:41 17 kt 246° true #Trackpoint N49 15.137 W123 14.775 7/30/2004 7:28:11 PM 65.0 m 348 ft 00:00:09 23 kt 239 true sub brk { return ; }