Adding Shapes Corrupts Shapefile

I am having an issue when adding or deleting shapes using the shape editor with the shapefiles being corrupted. It only happens the second time the shapefile is opened for editing using the StartEditingShapes function. I have created a very simple form showing the code I’m using.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sf As New Shapefile
    sf.Open("L:\Test\SUBDIV.shp")
    AxMap1.AddLayer(sf, True)
End Sub

Private Sub CmdStartEdit_Click(sender As Object, e As EventArgs) Handles CmdStartEdit.Click
    Dim sf As Shapefile = AxMap1.get_Shapefile(0)
    sf.StartEditingShapes(True)
    sf.InteractiveEditing = True
    AxMap1.CursorMode = tkCursorMode.cmEditShape
End Sub

Private Sub CmdSaveEdit_Click(sender As Object, e As EventArgs) Handles CmdSaveEdit.Click
    Dim sf As Shapefile = AxMap1.get_Shapefile(0)
    sf.StopEditingShapes(True, True)
    sf.InteractiveEditing = False
    sf.Save()
    AxMap1.CursorMode = tkCursorMode.cmSelection
End Sub

Private Sub CmdEditShapes_Click(sender As Object, e As EventArgs) Handles CmdEditShapes.Click
    AxMap1.CursorMode = tkCursorMode.cmEditShape
End Sub

Private Sub CmdAddShapes_Click(sender As Object, e As EventArgs) Handles CmdAddShapes.Click
    AxMap1.CursorMode = tkCursorMode.cmAddShape
End Sub

Private Sub AxMap1_ChooseLayer(sender As Object, e As _DMapEvents_ChooseLayerEvent) Handles AxMap1.ChooseLayer
    e.layerHandle = 0
End Sub

The After clicking the StartEdit button and adding 2 or more polygons and saving the changes using the SaveEdit button everything seems to be fine, however the second time you select the StartEdit button, add polygons and save the changes you can tell something is wrong because you can’t select the added shapes. At this point the shapefile will crash if you try to reload it. Am I missing a step in opening or saving the shapefile for editing. This is the same syntax we used with 4.9 and I didn’t notice the issue then.

I have tried this with different shapefiles with the same results.

Looking at this further, when the sf.StopEditingShapes(True, True) results in an error code 24, (Failed to delete a file) you can no longer select the added shapes and the shapefile will crash when loaded. I am guessing file that is not getting deleted is a index file but I’m not sure.

Hello @HollidayHT

I’m sorry for the delayed response; I’m currently buried in work-related issues, but I should be able to debug this this weekend.

Regards,
Jerry.

I appreciate the response, were able to get this error?

Ok, I just tried a few iterations. I copied your code exactly, creating buttons accordingly. Of course I am using a different shapefile, but I do not get any errors. I edited an existing polygon, added a few triangles, in 3 separate edits, opening and closing the application a couple times along the way. Unless it is a very specific order of operations, I am not able to reproduce the problem.

Do you maybe want to send your specific shapefile?

Regards,
Jerry.

P.S. I am running a fairly current 5.2 build (admittedly not exactly the same as the formal release).

This is the vb project and the shapefile I used to test this. Sometimes it take 3 or 4 saves before you get the Failed to delete a file error.

Thanks again for looking at this.

ToddH

MapTest.zip (2.93 MB)

I’m sorry, but I cannot get it to fail. Multiple Starts and Saves, various Adds and Edits. Multiple closing and re-opening the app. I’m not sure what to suggest. At some point I will compare my code with the standard release to see if I’ve made any significant changes that may affect edits. Kind Regards, Jerry.

@HollidayHT Do you still have this issue with the latest MapWinGIS (currently v5.3)?

And sf.StopEditingShapes(True, True) is already saving the changes. So no need to call sf.Save() which according to the documentation does

Saves edits of the in-memory shapefile to the disk without closing editing mode.

So either call sf.Save() and call sf.StopEditingShapes on a different button or just omit sf.Save().

And if you set GlobalSettings.ApplicationCallback and implement ICallback you get additional (error) messages.