[Gdal-dev] Transforming an ogr layer

Ari Jolma ari.jolma at tkk.fi
Wed Sep 7 08:34:40 EDT 2005


Through some experimentation I managed to transform a layer (sorry, Perl 
code, but should not be much different from Python)

# the layer I'm going to tranform does not have a spatial reference
$osr_from = new osr::SpatialReference;
$osr_from->SetWellKnownGeogCS('WGS84');
$osr_to = new osr::SpatialReference;
$osr_to->ImportFromEPSG(3035);
$ct = new osr::CoordinateTransformation($osr_from,$osr_to);

# working in this directory
$datasource = ogr::Open('/home/ajolma/proj/clime/lakes/',1);

if ($datasource) {

    # existing layer, a shapefile of polygons, one datafield
    $layer = $datasource->GetLayerByName('lakes');

    # a new layer, similar but new spatial reference
    $new_layer = 
$datasource->CreateLayer('lakes_t',$osr_to,$ogr::wkbPolygon);

    for $i (0..$layer->GetFeatureCount()-1) {

       # this sequence was a bit hard to get right...
        $feature = $layer->GetFeature($i);
        $geom = $feature->GetGeometryRef();
        $geom->Transform($ct);
        $new_layer->CreateFeature($feature);

    }
   
    # save, this is needed
    $new_layer->SyncToDisk;
}

Works fine for the vertices but the "name" field is lost from the 
feature and instead there is a new one FID of type Real with seemingly 
integer value. How do I make the feature not loose its existing field?

I'm sending this to the list also in the hope of generating some 
documentation :)

Ari
 

-- 
Prof. Ari Jolma
Kartografia ja Geoinformatiikka / Cartography and Geoinformatics
Teknillinen Korkeakoulu / Helsinki University of Technology
POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma




More information about the Gdal-dev mailing list