[gdal-dev] copy (duplicate) feature with python ogr?

Chao YUE chaoyuejoy at gmail.com
Sat Mar 30 07:01:15 PDT 2013


Hi Even,

this solves my probelm, the example in the script is quite useful. Thanks.

chao


On Sat, Mar 30, 2013 at 12:41 PM, Even Rouault <even.rouault at mines-paris.org
> wrote:

> Le samedi 30 mars 2013 12:22:25, Chao YUE a écrit :
> > Dear all,
> >
> > I am trying to create a new polygon shapefile and copy features from
> > existing shapefile into it and I have some problems doing this.
> > the script is below:
> >
> > num_feature_multipolygon is a dictionary of (fdi,feature) pairs (see at
> the
> > end of mail)
> >
> > dsnew = driver.CreateDataSource('Multi_polygon.shp')
> > layernew =
> dsnew.CreateLayer('multi_polygon',geom_type=ogr.wkbMultiPolygon)
>
> --> You need to setup the layernew field definition here, i.e. use
> layernew.CreateField()
>
> > for fid, feature_temp in num_feature_multipolygon.items():
> >     feature_new = feature_temp.Clone()
>
> --> you need to create a new feature from the layernew field definition
> and copy
> the feature_temp into it with feature_new.SetFrom(feature_temp)
>
> >     layernew.CreateFeature(feature_new)
> >     #feature_new.Destroy()
> > ds.Destroy()
> >
> >
> > I tested the resulting feature from feature.Clone() has the same
> attribute
> > values and geometry with the original one.
> > I guess somewhere I am wrong in the script?
>
> You can find something very close to what you are looking for in
> http://svn.osgeo.org/gdal/trunk/autotest/ogr/ogr_shape.py in the
> ogr_shape_2()
> test.
>



-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130330/ea85a840/attachment-0001.html>


More information about the gdal-dev mailing list