Geo::GDAL  2.4
Tips and tricks

This page is for tips and tricks when working with Geo::GDAL.

First few lines

You should probably use Perlbrew and the latest Perl.

#!/usr/bin/env perl
use strict;
use v5.10;
use Geo::GDAL;

GDAL style progress function in Perl

# call this somewhere early:
$|++; # flush print right away for one line progress
sub progress {
state $s = -1; # requires 'use v5.10;'
my $p = int($_[0]*100);
return 1 if $p == $s;
$s = 0 if $s < 0;
while ($s < $p) {
print $s%10 == 0 ? $s : ($s%2 == 0 ? '.' : '');
++$s;
}
return 1;
}

Note: this will not catch Ctrl-C (SIGINT). For that you could cook up something like the following.

$SIG{'INT'} = progress('Return 0 next time!');
# modify progress to understand the above

Get the raster coordinates in iteration

my $raster = Geo::GDAL::Open('bigraster.tiff');
my $transform = $raster->GeoTransform;
my ($xoff, $yoff, $w, $h) = (0, 0, 200, 200);
$|++;
while (1) {
progress($yoff/$H);
if ($xoff >= $W) {
$xoff = 0;
$yoff += $h;
last if $yoff >= $H;
}
my $data = $raster->Band(1)->ReadTile($xoff, $yoff, min($W-$xoff, $w), min($H-$yoff, $h));
for my $y (0..$#$data) {
for my $x (0..$#{$data->[$y]}) {
my ($px, $py) = $transform->Apply([$xoff+$x], [$yoff+$y]);
my $upper_left = Geo::OGR::Geometry->new(
GeometryType => 'Point',
Points => [$px->[0], $py->[0]])});
}
}
$xoff += $w;
}
sub min {
return $_[0] < $_[1] ? $_[0] : $_[1];
}