Create a gist now

Instantly share code, notes, and snippets.

Extract tiles from *.mbtiles file made by TileMill
#!/usr/bin/perl
# VERSION: 1.0
# CREATED: 10.02.2013 23:41:17
use strict;
use warnings;
=head1 USAGE
./extract-tiles.pl package.mbtiles
=head1 DESCRIPTION
Extract tiles from *.mbtiles file made by TileMill
=head2 OPTIONS
=head2 SEE ALSO
L<https://gist.github.com/shoorick/5077168>
=head1 AUTHOR
Alexander Sapozhnikov L<http://shoorick.ru/>, L<< E<lt>shoorick@cpan.orgE<gt> >>
=cut
use DBI;
use File::Path qw( make_path );
use File::Slurp;
my $dbfile = shift @ARGV
or die "Database not specified\n";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", '', '');
my $dir = "$dbfile-tile";
# DBI::selectall_arrayref try to get lot of memory when file is huge
# For example: tileset for area 600x400 km need more than 1 GB for zoom 9-16,
# allocation of all that memory need lot of time.
my $sth = $dbh->prepare('SELECT * FROM tiles');
$sth->execute;
while ( my $tile = $sth->fetchrow_hashref ) {
my $path = join('/', $dir, $tile->{'zoom_level'}, $tile->{'tile_column'});
make_path $path
unless -d $path;
write_file(
$path . '/' . $tile->{'tile_row'} . '.png',
{ 'binmode' => ':raw' },
$tile->{'tile_data'}
);
}
my $rc = $dbh->disconnect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment