Creating missing .shx file?

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
    w.save("myshape")

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.

Update 2020_03: Since QGIS 3.12 there is a native algorithm in QGIS to repair or create the *.shx file https://twitter.com/i/status/1234406983505235968