diff --git a/Makefile.PL b/Makefile.PL index ea585ca..a1bae11 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,7 @@ WriteMakefile( }, TEST_REQUIRES => { 'Test::More' => 0, + 'Test::Exception' => 0, 'JSON' => 0, 'Data::Dumper' => 0 }, diff --git a/lib/Geo/GDAL/FFI/Dataset.pm b/lib/Geo/GDAL/FFI/Dataset.pm index 9d6caef..03e908b 100644 --- a/lib/Geo/GDAL/FFI/Dataset.pm +++ b/lib/Geo/GDAL/FFI/Dataset.pm @@ -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'; } @@ -504,6 +516,10 @@ SpatialReference are ignored. =back +=head2 GetLayerCount + my $count = $dataset->GetLayerCount(); + + =head2 GetLayer my $layer = $dataset->GetLayer($name); diff --git a/t/dataset.t b/t/dataset.t new file mode 100644 index 0000000..b0cda12 --- /dev/null +++ b/t/dataset.t @@ -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();