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

Even Rouault even.rouault at mines-paris.org
Sat Mar 30 04:41:28 PDT 2013


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.


More information about the gdal-dev mailing list