[gdal-dev] CopyLayer oddity using GML driver

Even Rouault even.rouault at mines-paris.org
Mon Nov 21 13:57:23 EST 2011


Le lundi 21 novembre 2011 11:20:33, Ari Jolma a écrit :
> Hi,
> 
> I'm making a new GML layer from an existing layer. For some reason the
> CopyLayer method leaves adjusted fields out but CreateField,
> CreateFeature sequence does not. I guess they should be identical?
> 
> i.e.
> 
> my $datasource = Geo::OGR::Open('Pg:...');
> my $l = $datasource->ExecuteSQL('...');
> my $gml = Geo::OGR::Driver('GML')->Create('/vsistdout/');
> $gml->CopyLayer($l, 'a');
> 
> leaves fields like "a b" out, while

I guess it is because the GML driver transforms "a b" into "a_b" to make it a 
valid XML name. But the implementation of CopyLayer() uses SetFrom(), which 
cannot guess that "a b" values from source feature should be mapped to "a_b" 
of target feature.

> 
> my $datasource = Geo::OGR::Open('Pg:...');
> my $l = $datasource->ExecuteSQL('...');
> my $gml = Geo::OGR::Driver('GML')->Create('/vsistdout/');
> my $l2 = $gml->CreateLayer('test');
> 
> my $d = $l->GetLayerDefn;
> for (0..$d->GetFieldCount-1) {
>      my $f = $d->GetFieldDefn($_);
>      $l2->CreateField($f);
> }
> 
> $l->ResetReading;
> while (my $f = $l->GetNextFeature) {
>      $l2->CreateFeature($f);
> }
> 
> works ok (and prints out warnings about the field name adjustments)

Works ok perhaps, but it is potentially risky. You should not use $l2-
>CreateFeature($f) with $f being a feature that has the feature definition of 
$l and not $l2. That could crash in some situations... You're lucky here that 
it works for you because you have the same number of fields and same type.

> 
> I tried to look into what the CopyLayer method generic driver does
> differently but could not tell.

CopyLayer() should likely implement the more advanced tricks that ogr2ogr.cpp 
uses to do the proprer mapping of field names, even when renames occur.

> 
> Any ideas?
> 
> Ari
> 
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list