Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Layer::GetFeatureCount method #60

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading