Skip to content

Commit

Permalink
Merge pull request #35 from shawnlaffan/dataset_getlayer_method
Browse files Browse the repository at this point in the history
add a Dataset::GetLayerCount method
  • Loading branch information
ajolma authored Oct 16, 2019
2 parents c3a69e2 + c36ee99 commit 9b1a6a5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ WriteMakefile(
},
TEST_REQUIRES => {
'Test::More' => 0,
'Test::Exception' => 0,
'JSON' => 0,
'Data::Dumper' => 0
},
Expand Down
20 changes: 18 additions & 2 deletions lib/Geo/GDAL/FFI/Dataset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,23 @@ sub GetBands {
return @bands;
}

sub GetLayerCount {
my ($self) = @_;
return Geo::GDAL::FFI::GDALDatasetGetLayerCount($$self);
}


sub GetLayer {
my ($self, $i) = @_;
$i //= 0;
my $l = Geo::GDAL::FFI::isint($i) ? Geo::GDAL::FFI::GDALDatasetGetLayer($$self, $i) :
Geo::GDAL::FFI::GDALDatasetGetLayerByName($$self, $i);
my $l = Geo::GDAL::FFI::isint($i)
? Geo::GDAL::FFI::GDALDatasetGetLayer($$self, $i)
: Geo::GDAL::FFI::GDALDatasetGetLayerByName($$self, $i);
unless ($l) {
my $msg = Geo::GDAL::FFI::error_msg()
// "Could not access layer $i in data set.";
confess $msg if $msg;
}
Geo::GDAL::FFI::_register_parent_ref ($l, $self);
return bless \$l, 'Geo::GDAL::FFI::Layer';
}
Expand Down Expand Up @@ -504,6 +516,10 @@ SpatialReference are ignored.
=back
=head2 GetLayerCount
my $count = $dataset->GetLayerCount();
=head2 GetLayer
my $layer = $dataset->GetLayer($name);
Expand Down
40 changes: 40 additions & 0 deletions t/dataset.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use v5.10;
use strict;
use warnings;
use Carp;
use Geo::GDAL::FFI qw/GetDriver HaveGEOS/;
use Test::More;
use Test::Exception;
use Data::Dumper;

my $ds = GetDriver('GPKG')->Create('test.gpkg');
my $sr = Geo::GDAL::FFI::SpatialReference->new(EPSG => 3067);
foreach my $i (1..3) {
my $l = $ds->CreateLayer({
Name => "test$i",
SpatialReference => $sr,
GeometryType => 'Point',
});
my $d = $l->GetDefn();
my $f = Geo::GDAL::FFI::Feature->new($d);
$l->CreateFeature($f);
}


is ($ds->GetLayerCount, 3, 'Got expected number of layers');

dies_ok (
sub {$ds->GetLayer ('not_exists')},
'GetLayer exception for non-existent layer name',
);
dies_ok (
sub {$ds->GetLayer (23)},
'GetLayer exception for too large index',
);
dies_ok (
sub {$ds->GetLayer (-1)},
'GetLayer exception for negative index',
);


done_testing();

0 comments on commit 9b1a6a5

Please sign in to comment.