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
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 15, 2022 at 15:47

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:

MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
answered Nov 16, 2022 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.