0

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")


ogr.UseExceptions()

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)
outlyr.CreateFields(inLayer.schema)

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


for in_feat in inLayer:

    geom=in_feat.geometry()
    out_feat.SetGeometry(geom)

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

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

    outlyr.CreateFeature(out_feat)

del inds
del outds

1 Answer 1

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.