[gdal-dev] feature.clone() -> same featuredefn
Even Rouault
even.rouault at mines-paris.org
Mon Aug 4 02:24:01 PDT 2014
Le lundi 04 août 2014 11:02:41, Martin Landa a écrit :
> Hi Even,
>
> 2014-08-02 22:18 GMT+02:00 Even Rouault <even.rouault at mines-paris.org>:
>
> [...]
>
> >> 1) copy features from input layer to output layer
> >> 2) modify features in output layer (remove some geometry columns)
> >
> > You can use the SetFrom() method.
>
> thanks for hint, I found out that input and output layer somehow share
> FeatureDefn. Here is sample code:
>
> """
> # create new layer
> olayer = ods.CreateLayer(layerName, ilayer.GetSpatialRef(),
> geom_type, options)
>
> if not olayer:
> fatal("Unable to create layer '%'" % layerName)
>
> # create attributes
> feat_defn = ilayer.GetLayerDefn()
> for i in range(feat_defn.GetFieldCount()):
> olayer.CreateField(feat_defn.GetFieldDefn(i))
>
> # create also geometry attributes
> if not geom_name and \
> olayer.TestCapability(ogr.OLCCreateGeomField):
> for i in range(feat_defn.GetGeomFieldCount()):
> geom_defn = feat_defn.GetGeomFieldDefn(i)
> if geom_name and geom_defn.GetName() != geom_name:
> continue
> olayer.CreateGeomField(feat_defn.GetGeomFieldDefn(i))
>
> print
> print ilayer, ilayer.GetLayerDefn()
> print olayer, olayer.GetLayerDefn()
> """
>
> prints
>
> <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at
> 0x7fbf0d95f930> > <osgeo.ogr.FeatureDefn; proxy of <Swig Object of
> type 'OGRFeatureDefnShadow *' at 0x7fbf0d95fae0> >
> <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at
> 0x7fbf0d95fba0> > <osgeo.ogr.FeatureDefn; proxy of <Swig Object of
> type 'OGRFeatureDefnShadow *' at 0x7fbf0d95fae0> >
>
> I am not sure why... Thanks for clarification in advance, Martin
Martin,
You cannot deduce if the underlying C++ objects are identical from that. In
fact what you are seeing is the address of a SWIG "shadow" object, that is
something whose lifetime is related to the lifetime of the Python objects.
See the below example :
$ python
Python 2.6.6 (r266:84292, Oct 29 2012, 23:09:05)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import ogr
>>> ds = ogr.GetDriverByName('memory').CreateDataSource('')
>>> lyr1 = ds.CreateLayer('foo')
>>> lyr1.GetLayerDefn()
<osgeo.ogr.FeatureDefn; proxy of <Swig Object of type 'OGRFeatureDefnShadow *'
at 0x7fd85c0574e0> >
>>> lyr1.GetLayerDefn()
<osgeo.ogr.FeatureDefn; proxy of <Swig Object of type 'OGRFeatureDefnShadow *'
at 0x7fd847c493f0> >
>>> lyr1.GetLayerDefn()
<osgeo.ogr.FeatureDefn; proxy of <Swig Object of type 'OGRFeatureDefnShadow *'
at 0x7fd85c0574e0> >
>>> lyr1.GetLayerDefn()
<osgeo.ogr.FeatureDefn; proxy of <Swig Object of type 'OGRFeatureDefnShadow *'
at 0x7fd847c49450> >
>>>
The shadow object changes at every execution. In your case, the old address
for ilayer.GetLayerDefn() was likely "recycled" for olayer.GetLayerDefn()
You can try ilayer.GetLayerDefn().IsSame(olayer.GetLayerDefn()) and it should
return 0 if their content is different (note that it compares the "content" of
the layer and field definition, not the equality of pointers)
Best regards,
Even
--
Geospatial professional services
http://even.rouault.free.fr/services.html
More information about the gdal-dev
mailing list