How do programmatically compress/compact a shapefile

171
0
04-05-2024 05:57 PM
Labels (1)
MarcHillman
Occasional Contributor

I have written a program in VB.net, using esriarcgisruntime 200, to add shapes to a shapefile. I have created the empty shapefile in QGIS. My program then adds a number of features. So far, so good - I can see all the added features in QGIS.

I then run the program a second time. The size of the shapefile doubles, and QGIS says the shapefile is corrrupt. I'm not sure what I've done wrong. I was hoping that compressing or compacting the shapefile may help. I will be running my program frequently, so shapefile size will be an ongoing problem - at the moment it just grows without limit. What is the correct procedure for deleting all features from a shapefile, and then adding new ones.

The code I am using the delete existing features is below. The process is:
line 3: Load the shapefile

Line 11: Query the shapefile to retrieve all features

Line 12: Delete all retrieved features. I do this in reverse order of FID in case it matters

Line 13: Close the shapefile in the hope it will automatically compact.

        Dim Shapefile = $"{Application.StartupPath}\DXCC.shp"
        Dim sft As New ShapefileFeatureTable(Shapefile)
        Dim fqr As FeatureQueryResult, qp As New QueryParameters
        Await sft.LoadAsync()
        ' Delete all existing features
        With qp
            .WhereClause = "1=1"            ' get all features
            .OrderByFields.Add(New OrderBy("FID", SortOrder.Descending))    ' sort by descending order of FID
            .ReturnGeometry = False
        End With
        fqr = Await sft.QueryFeaturesAsync(qp)
        Await sft.DeleteFeaturesAsync(fqr.ToList)
        sft.Close()

Following this, there is code to insert new features.

When run the first time, and there is no existing data to delete, the new features are added, and all is fine.

When run a subsequent time, I get a shapefile twice the size, and one that QGIS believes is corrupt.

Any ideas what I've done wrong?

0 Kudos
0 Replies