Geo::GDAL  2.4
Transforming a dataset

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!

This example will transform a vector dataset from spatial reference system EPSG 2392 to EPSG 2393. The geometries are translated by (dx, dy) = (2500000, 6600000) before applying the transformation.

$dsname = shift @ARGV;
$lname = shift @ARGV;
$datasource = Geo::OGR::Open($dsname) or die;
$layer1 = $datasource->Layer($lname) or die;
$sr1 = Geo::OSR::SpatialReference->new(EPSG => 2392);
$dsname = shift @ARGV;
$lname = shift @ARGV;
$datasource = Geo::OGR::Driver('ESRI Shapefile')->Create($dsname) or die;
$sr2 = Geo::OSR::SpatialReference->new(EPSG => 2393);
$layer2 = $datasource->CreateLayer
({ Name => $lname, SRS => $sr2, GeometryType => $layer1->GetLayerDefn->GeometryType});
$layer2->Schema( $layer1->Schema );
$tr = Geo::OSR::CoordinateTransformation->new($sr1, $sr2) or die;
$layer1->ResetReading();
while ($feature = $layer1->GetNextFeature()) {
my $geom = $feature->GetGeometry();
$geom->Move(2500000, 6600000);
$geom->Transform($tr);
$layer2->InsertFeature($feature);
}
Geo::OGR::Open
public Geo::GDAL::Dataset Open(scalar name, scalar update=0)
Geo::OGR::Driver
public Geo::GDAL::Driver Driver(scalar name)
Geo::OSR::CoordinateTransformation::new
public Geo::OSR::CoordinateTransformation new(scalar src, scalar dst)
Geo::OSR::SpatialReference
A spatial reference system.
Definition: all.pm:16221
Geo::GDAL
GDAL utility functions and a root class for raster classes.
Definition: all.pm:14
Geo::OSR::CoordinateTransformation
An object for transforming from one projection to another.
Definition: all.pm:16059
Geo::GDAL::Driver::Create
public Geo::GDAL::Dataset Create(hash params)
Geo::OSR::SpatialReference::new
public Geo::OSR::SpatialReference new(hash params)