Skip to content

Instantly share code, notes, and snippets.

Last active September 11, 2016 10:00
What would you like to do?
Extract tiles from *.mbtiles file made by TileMill
# VERSION: 1.0
# CREATED: 10.02.2013 23:41:17
use strict;
use warnings;
=head1 USAGE
./ package.mbtiles
Extract tiles from *.mbtiles file made by TileMill
=head2 OPTIONS
=head2 SEE ALSO
=head1 AUTHOR
Alexander Sapozhnikov L<>, L<< E<lt>shoorick@cpan.orgE<gt> >>
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');
while ( my $tile = $sth->fetchrow_hashref ) {
my $path = join('/', $dir, $tile->{'zoom_level'}, $tile->{'tile_column'});
make_path $path
unless -d $path;
$path . '/' . $tile->{'tile_row'} . '.png',
{ 'binmode' => ':raw' },
my $rc = $dbh->disconnect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment