Skip to content

Instantly share code, notes, and snippets.

@ggl
Last active September 10, 2016 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggl/8813824 to your computer and use it in GitHub Desktop.
Save ggl/8813824 to your computer and use it in GitHub Desktop.
Create a POI SQLite database from a Garmin CSV
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
use Geohash;
use Text::CSV;
my $dbh = DBI->connect("dbi:SQLite:dbname=test.db","","");
my $query = qq~CREATE TABLE IF NOT EXISTS poi (
id INTEGER PRIMARY KEY,
gh TEXT,
lat REAL,
lon REAL,
desc TEXT,
UNIQUE(gh) ON CONFLICT REPLACE
)~;
$dbh->do($query) or die $dbh->errstr;
my $csv = Text::CSV->new({
binary => 1,
allow_whitespace => 1,
});
open(my $fh, "<:encoding(utf8)", "poi.csv")
or die "Cannot open file: $!";
my $gh = Geohash->new();
$dbh->begin_work or die $dbh->errstr;
while (my $row = $csv->getline($fh)) {
my %poi = (
lon => shift(@$row),
lat => shift(@$row),
desc => join(", ", @$row),
);
$poi{gh} = $gh->encode($poi{lat}, $poi{lon});
$query = qq~INSERT INTO poi (gh, lat, lon, desc) VALUES (?,?,?,?)~;
my $sth = $dbh->prepare($query) or die $dbh->errstr;
my @binds = ($poi{gh}, $poi{lat}, $poi{lon}, $poi{desc});
$sth->execute(@binds) or die $dbh->errstr;
}
$dbh->commit or die $dbh->errstr;
close $fh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment