I'm trying to save/copy a shapefile to a .tab format with Python OGR. Every attempts give me the following error:

RuntimeError: CreateFeature() failed: cannot re-write already existing feature 1

When I try to export the shapefile to another shapefile it's working perfectly.

Here is my code:

import os, sys
from osgeo import ogr

path_script = os.path.dirname(sys.argv[0])
shp_path = os.path.join(os.path.dirname(path_script), "shapefiles", "arrondissement_ExportFeature.shp")


driver = ogr.GetDriverByName('ESRI Shapefile')

inds = driver.Open(shp_path, 0)
if inds is None:
    sys.Exit("impossible douvrir le fichier")

inLayer = inds.GetLayer(0)

driver_out = ogr.GetDriverByName("MapInfo File")
outds = driver_out.CreateDataSource("outLast.tab")

outlyr = outds.CreateLayer("outLast", inLayer.GetSpatialRef(), ogr.wkbPolygon)

out_defn = outlyr.GetLayerDefn()
out_feat = ogr.Feature(out_defn)

for in_feat in inLayer:


    for i in range(in_feat.GetFieldCount()):

        value = in_feat.GetField(i)
        out_feat.SetField(i, value)


del inds
del outds

1 Answer 1


I found the solution. out_feat = ogr.Feature(out_defn) has to be inside the loop for in_feat in inLayer:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.