[gdal-dev] CopyLayer oddity using GML driver
Frank Broniewski
brfr at metrico.lu
Tue Nov 22 02:57:09 EST 2011
Am 21.11.2011 19:57, schrieb Even Rouault:
> 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
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
Hi,
another thing I noticed while using CopyDataSource, which might be
related to CopyLayer, that's why I am posting this here: the driver is
lost, don't know if this is intentional ... see the short code example below
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import ogr
>>> in_ds = ogr.Open('points.shp')
>>> in_ds.GetDriver()
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at
0x7f919c6f7fc0> >
>>> in_drv = in_ds.GetDriver()
>>> out_ds = in_drv.CopyDataSource(in_ds, 'cp.shp')
>>> out_ds.GetDriver()
>>> out_drv = out_ds.GetDriver()
>>> out_drv is None
True
>>>
Greetings
Frank
--
Frank BRONIEWSKI
METRICO s.à r.l.
géomètres
technologies d'information géographique
rue des Romains 36
L-5433 NIEDERDONVEN
tél.: +352 26 74 94 - 28
fax.: +352 26 74 94 99
http://www.metrico.lu
More information about the gdal-dev
mailing list