0

I am using OGR to open a layer, add a filter and then save the results as a separate MapInfo TAB file. Upon trying to save the layer, I get the following error message:

ERROR 5: CreateFeature() failed: invalid feature id 0

Using the same procedure to create a CSV or ESRI Shp works as expected.

My code is as follows:

result = dataSource.ExecuteSQL("select * from '"+ self.inputname +"' WHERE not ST_IsValid(geometry)", dialect = "SQLITE")

if ".shp" in self.outputpath:
    outdrv = ogr.GetDriverByName("ESRI Shapefile")
elif ".csv" in self.outputpath:
    outdrv = ogr.GetDriverByName("CSV")
elif ".tab" in self.outputpath:
    outdrv = ogr.GetDriverByName("MapInfo File")

filtered = outdrv.CreateDataSource (self.outputpath)
filter_layer = filtered.CopyLayer(result, 'filtered')
QMessageBox.information(self, "Process complete", "Process complete! " + str(resultpolygons) + " error(s) detected.")

Whilst attempting to save a layer from an MSSQL server to MapInfo File, I encountered the same issue. It seems as though the OGR python bindings have issues writing the new MapInfo Files.

  • Perhaps MapInfo driver does not like invalid geometries. Test by using the opposite WHERE ST_IsValid(geometry). – user30184 Mar 10 '17 at 13:38
  • Unfortunately I get the same error. I can also convert exported SHP layers containing invalid geometry to MapInfo using ogr2ogr. – JamieTasker Mar 10 '17 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.