Android用GPSナビゲーションソフトOruxMapsで取得したトラッキングデータ(SQLite形式でSDカード内に保存される)を、パソコン上で抽出・変換して、Google Maps上に展開するためのperlスクリプト。
■ 出来上がりサンプル
■ OruxMapsのSQLiteデータベース内のデータをCSVにエクスポートする
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use DBI;
use File::Basename;
my $str_dsn = 'DBI:SQLite:dbname=';
if(@ARGV != 1){
print("usage: ".basename($0)." [dbfile]\n");
exit();
}
my $str_dbfilename = shift;
if(!( -f $str_dbfilename)){
print("file not found\n");
exit();
}
$str_dsn .= $str_dbfilename;
my $dbh = undef;
eval{
$dbh = DBI->connect($str_dsn, '', '', {PrintError => 0, AutoCommit => 1}) or die(DBI::errstr);
my $str_query = "select trkpttime,trkptlat,trkptlon,trkptalt from trackpoints";
my $sth = $dbh->prepare($str_query) or die(DBI::errstr);
$sth->execute() or die(DBI::errstr);
while(my($time,$latitude,$longitude,$altitude) = $sth->fetchrow_array){
print(($time/1000).",".$latitude.",".$longitude.",".$altitude."\n");
}
$dbh->disconnect();
};
if($@){
if(defined($dbh)){ $dbh->disconnect(); }
print "error(".$@.")\n";
}
use strict;
use warnings;
use utf8;
use DBI;
use File::Basename;
my $str_dsn = 'DBI:SQLite:dbname=';
if(@ARGV != 1){
print("usage: ".basename($0)." [dbfile]\n");
exit();
}
my $str_dbfilename = shift;
if(!( -f $str_dbfilename)){
print("file not found\n");
exit();
}
$str_dsn .= $str_dbfilename;
my $dbh = undef;
eval{
$dbh = DBI->connect($str_dsn, '', '', {PrintError => 0, AutoCommit => 1}) or die(DBI::errstr);
my $str_query = "select trkpttime,trkptlat,trkptlon,trkptalt from trackpoints";
my $sth = $dbh->prepare($str_query) or die(DBI::errstr);
$sth->execute() or die(DBI::errstr);
while(my($time,$latitude,$longitude,$altitude) = $sth->fetchrow_array){
print(($time/1000).",".$latitude.",".$longitude.",".$altitude."\n");
}
$dbh->disconnect();
};
if($@){
if(defined($dbh)){ $dbh->disconnect(); }
print "error(".$@.")\n";
}
■ CSVファイルをGoogle Maps API形式HTMLに変換
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use File::Basename;
binmode(STDOUT, ":utf8");
my @arr_data;
if(@ARGV != 1){
print("usage: ".basename($0)." [track_csvfile]\n");
exit();
}
my $str_filename = shift;
if(!( -f $str_filename)){
print("file not found\n");
exit();
}
open(FH, "<".$str_filename) or die("file open error\n");
while(my $str_line = <FH>){
chomp($str_line);
# my($time,$latitude,$longitude,$altitude) = split(/\,/, $str_line);
my @arr = split(/\,/, $str_line);
if($#arr != 3){ next; }
push(@arr_data, [@arr]);
}
close(FH);
my $latitude_centre = 0;
my $longitude_centre = 0;
for(my $i=0; $i<$#arr_data; $i++){
$latitude_centre += $arr_data[$i][1];
$longitude_centre += $arr_data[$i][2];
}
$latitude_centre = $latitude_centre/$#arr_data;
$longitude_centre = $longitude_centre/$#arr_data;
printf("<html>\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n".
"<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>\n".
"<script type=\"text/javascript\">\n".
" window.onload = initialize;\n".
" function initialize() {\n".
" var myOptions = {\n".
" zoom: 10,\n".
" center: new google.maps.LatLng(%f,%f),\n".
" mapTypeId: google.maps.MapTypeId.ROADMAP\n".
" };\n".
" var myMap = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);\n",
$latitude_centre, $longitude_centre);
for(my $i=0; $i<$#arr_data; $i++){
my ($sec,$min,$hour,$mday,$month,$year,$wday,$stime) = localtime($arr_data[$i][0]);
my $str_date = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year+1900,$month+1,$mday,$hour,$min,$sec);
printf(" var marker%d = new google.maps.Marker({\n".
" map: myMap,\n".
" position: new google.maps.LatLng(%s,%s),\n".
" icon: \"http://labs.google.com/ridefinder/images/mm_20_white.png\",\n".
" title: \"time %s\",\n".
" });\n", $i, $arr_data[$i][1],$arr_data[$i][2], $str_date);
printf(" var infowindow%d = new google.maps.InfoWindow({\n".
" content: \"日時 %s<br/>緯度 %s<br/>経度 %s<br/>高度 %s m\",\n".
" });\n".
" google.maps.event.addListener(marker%d, 'click', function() {\n".
" infowindow%d.open(myMap, marker%d);\n".
" });\n",$i, $str_date, $arr_data[$i][1], $arr_data[$i][2], $arr_data[$i][3], $i, $i, $i);
}
print(" }\n".
"</script>\n".
"</head>\n".
"<body>\n".
"<div id=\"map_canvas\" style=\"float:left;width:800px; height:600px;\"></div>\n".
"</body>\n".
"</html>\n");
use strict;
use warnings;
use utf8;
use File::Basename;
binmode(STDOUT, ":utf8");
my @arr_data;
if(@ARGV != 1){
print("usage: ".basename($0)." [track_csvfile]\n");
exit();
}
my $str_filename = shift;
if(!( -f $str_filename)){
print("file not found\n");
exit();
}
open(FH, "<".$str_filename) or die("file open error\n");
while(my $str_line = <FH>){
chomp($str_line);
# my($time,$latitude,$longitude,$altitude) = split(/\,/, $str_line);
my @arr = split(/\,/, $str_line);
if($#arr != 3){ next; }
push(@arr_data, [@arr]);
}
close(FH);
my $latitude_centre = 0;
my $longitude_centre = 0;
for(my $i=0; $i<$#arr_data; $i++){
$latitude_centre += $arr_data[$i][1];
$longitude_centre += $arr_data[$i][2];
}
$latitude_centre = $latitude_centre/$#arr_data;
$longitude_centre = $longitude_centre/$#arr_data;
printf("<html>\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n".
"<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>\n".
"<script type=\"text/javascript\">\n".
" window.onload = initialize;\n".
" function initialize() {\n".
" var myOptions = {\n".
" zoom: 10,\n".
" center: new google.maps.LatLng(%f,%f),\n".
" mapTypeId: google.maps.MapTypeId.ROADMAP\n".
" };\n".
" var myMap = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);\n",
$latitude_centre, $longitude_centre);
for(my $i=0; $i<$#arr_data; $i++){
my ($sec,$min,$hour,$mday,$month,$year,$wday,$stime) = localtime($arr_data[$i][0]);
my $str_date = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year+1900,$month+1,$mday,$hour,$min,$sec);
printf(" var marker%d = new google.maps.Marker({\n".
" map: myMap,\n".
" position: new google.maps.LatLng(%s,%s),\n".
" icon: \"http://labs.google.com/ridefinder/images/mm_20_white.png\",\n".
" title: \"time %s\",\n".
" });\n", $i, $arr_data[$i][1],$arr_data[$i][2], $str_date);
printf(" var infowindow%d = new google.maps.InfoWindow({\n".
" content: \"日時 %s<br/>緯度 %s<br/>経度 %s<br/>高度 %s m\",\n".
" });\n".
" google.maps.event.addListener(marker%d, 'click', function() {\n".
" infowindow%d.open(myMap, marker%d);\n".
" });\n",$i, $str_date, $arr_data[$i][1], $arr_data[$i][2], $arr_data[$i][3], $i, $i, $i);
}
print(" }\n".
"</script>\n".
"</head>\n".
"<body>\n".
"<div id=\"map_canvas\" style=\"float:left;width:800px; height:600px;\"></div>\n".
"</body>\n".
"</html>\n");
単に、トラッキングルートを地図上に表示するだけなら、OruxMaps内でKML形式に変換して、そのファイルをインターネット上にアップロードして、そのURLをGoogleMapsの検索ボックスに入力すれば表示される。
■ 関連項目