Geo::GDAL  2.4
Reading and writing a vector layer

The code here does not have any warranty. It is recommended that before using any of this code, you look into it and try to understand what it does, what input it needs, etc. Do not blindly execute anything!

The program below reads a vector layer from PostGIS database and writes it back with another name. Some schema manipulation is done for illustration.

use strict;
use warnings;
use Geo::GDAL;
my $db = Geo::OGR::Open('PG:dbname=test');
my $input = $db->Layer('input');
my $schema = {map {$_->{Name} => $_} @{$input->Schema->{Fields}}};
my %col;
my %geom;
$input->ResetReading();
while (my $feature = $input->GetNextFeature()) {
my $fid = $feature->FID;
$col{$fid} = $feature->Field('col');
my $points = $feature->Geometry->Points;
$geom{$fid} = $points;
}
my $output = $db->CreateLayer(
Name => 'output',
Fields => [
{ Name => 'col',
Type => $schema->{col}{Type}
},
{ Name => 'geom',
Type => $schema->{geom}{Type},
SpatialReference => $schema->{geom}{SpatialReference}
}
],
Options => {
OVERWRITE => 'YES'
}
);
for my $fid (sort {$a <=> $b} keys %col) {
$output->InsertFeature({
col => $col{$fid},
geom => {Points => $geom{$fid}}
});
}