From 37156a874b323be70719e3f7187dee487f1a698e Mon Sep 17 00:00:00 2001 From: Shawn Laffan Date: Tue, 21 May 2019 08:01:21 +1000 Subject: [PATCH 1/3] Add Dataset::GetLayerCount method Also raise exceptions when invalid layers are accessed via Dataset::GetLayer --- lib/Geo/GDAL/FFI/Dataset.pm | 16 +++++++++++++-- t/dataset.t | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 t/dataset.t diff --git a/lib/Geo/GDAL/FFI/Dataset.pm b/lib/Geo/GDAL/FFI/Dataset.pm index 9d6caef..aa463f4 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'; } 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(); From 0186f2f2db95e295f3e0c1fbb1bde281730b6a13 Mon Sep 17 00:00:00 2001 From: Shawn Laffan Date: Tue, 21 May 2019 08:02:20 +1000 Subject: [PATCH 2/3] Add Test::Exception to TEST_REQUIRES --- Makefile.PL | 1 + 1 file changed, 1 insertion(+) 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 }, From c36ee9960642ee21e285436ea7f201f19aad335a Mon Sep 17 00:00:00 2001 From: Shawn Laffan Date: Tue, 21 May 2019 08:05:33 +1000 Subject: [PATCH 3/3] Add POD for GetLayerCount --- lib/Geo/GDAL/FFI/Dataset.pm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Geo/GDAL/FFI/Dataset.pm b/lib/Geo/GDAL/FFI/Dataset.pm index aa463f4..03e908b 100644 --- a/lib/Geo/GDAL/FFI/Dataset.pm +++ b/lib/Geo/GDAL/FFI/Dataset.pm @@ -516,6 +516,10 @@ SpatialReference are ignored. =back +=head2 GetLayerCount + my $count = $dataset->GetLayerCount(); + + =head2 GetLayer my $layer = $dataset->GetLayer($name);