I am using ArcGIS Desktop to view one shapefile.

Only a select number of features are being displayed. The same number are showing in the attribute table even though the database file (.dbf) has a complete list with many more features.

I then tried opening the shapefile in QGIS and it informs me that the file is invalid. Upon inspection of the shapefile, I found that the .shx file is missing.

Can I recreate the missing .shx file so that all the attributes/features can be loaded?

  • 1
    If your dBase file contains "many more features" than the .shp, then the shapefile is corrupt, and none of the data can be trusted. The .shx can usually be recostituted from the .shp, but it depends on whether there are any "gaps" in the file data stream. – Vince Dec 17 '18 at 13:13

ESRI provides a guide to repairing corrupted shapefiles which you can find on the ESRI website: https://support.esri.com/en/technical-article/000007161

I can't check on my machine but one of either the Shapefile Repairer Utility or the Shapefile Repair Tool (which are linked at the bottom of that ESRI help page) used to be able to reconstruct a .shx file.

You can also do it in Python. This link suggests this code to recreate a .shx file:

    # Build a new shx index file
    #Code by Joel Lawhead http://geospatialpython.com/2011/11/generating-shapefile-shx-files.html
    import shapefile
    # Explicitly name the shp and dbf file objects
    # so pyshp ignores the missing/corrupt shx
    myshp = open("myshape.shp", "rb")
    mydbf = open("myshape.dbf", "rb")
    r = shapefile.Reader(shp=myshp, shx=None, dbf=mydbf)
    w = shapefile.Writer(r.shapeType)
    # Copy everything from reader object to writer object
    w._shapes = r.shapes()
    w.records = r.records()
    w.fields = list(r.fields)
    # saving will generate the shx

Not that code requires the Python Shapefile Library (pyshp) to run.


You could open the Shapefile without the shx in OpenJump and save it as a new Shapefile. Then the shx-file will be generated.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.