Skip to content

Commit

Permalink
Merge pull request #60 from ajolma/issue_59_GetFeatureCount
Browse files Browse the repository at this point in the history
Add Layer::GetFeatureCount method
  • Loading branch information
shawnlaffan committed Oct 29, 2023
2 parents 0a20a58 + 02c2ba2 commit 18bfef5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/Geo/GDAL/FFI/Layer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ sub GetFeature {
return bless \$f, 'Geo::GDAL::FFI::Feature';
}

sub GetFeatureCount {
my ($self, $force) = @_;
Geo::GDAL::FFI::OGR_L_GetFeatureCount($$self, !!$force);
}

sub SetFeature {
my ($self, $f) = @_;
Geo::GDAL::FFI::OGR_L_SetFeature($$self, $$f);
Expand Down Expand Up @@ -175,6 +180,13 @@ A set of (vector) features having a same schema (the same Defn
object). Obtain a layer object by the CreateLayer or GetLayer method
of a vector dataset object.
Note that the system stores a reference to the parent dataset for
each layer object to ensure layer objects remain viable.
If you are relying on a dataset object's destruction to
flush its dataset cache and then close it then you need to ensure
all associated child layers are also destroyed. Failure to do so could
lead to corrupt data when reading in newly written files.
=head1 METHODS
=head2 GetDefn
Expand Down Expand Up @@ -206,6 +218,10 @@ Returns the FeatureDefn object for this layer.
=head2 DeleteFeature
$layer->DeleteFeature($fid);
=head2 GetFeatureCount
my $count = $layer->GetFeatureCount();
=head2 GetExtent
$layer->GetExtent();
Expand Down
1 change: 1 addition & 0 deletions t/00.t
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ if(1){
my $d = $l->GetDefn();
my $f = Geo::GDAL::FFI::Feature->new($d);
$l->CreateFeature($f);
undef $l; # otherwise $ds is not flushed due to parent ref
$ds = Open('test.shp');
$l = $ds->GetLayer;
$d = $l->GetDefn();
Expand Down
9 changes: 8 additions & 1 deletion t/layer.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ $f->SetGeomField([WKT => 'POLYGON ((2 1, 2 2, 4 2, 4 1, 2 1))']);

$method->CreateFeature($f);

{
my $feature_count = $layer->GetFeatureCount;
is $feature_count, 1, 'Got correct feature count';
$feature_count = $layer->GetFeatureCount (1);
is $feature_count, 1, 'Got correct feature count with force arg=true';
}

my $progress;

my $result;
Expand Down Expand Up @@ -136,7 +143,7 @@ is_deeply $layer->GetExtent(1), $exp_extent, 'Got correct layer extent when forc
{
#local $TODO = 'sql DISTINCT not yet working, despite following GDAL doc example';
is_deeply (\@items, ['one','ten'], 'got correct distinct items');
}
}

my $result = eval {
$ds->ExecuteSQL (qq{CREATE SPATIAL INDEX ON "$layer_name"});
Expand Down

0 comments on commit 18bfef5

Please sign in to comment.