10

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?

3
  • 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
    Commented Dec 17, 2018 at 13:13
  • @Vince as far as I can read from the Shapefile specification, a shp is always gapless, and its record are always prepended by a header specifying its length. The two rules guarantee correct seeking and therefore perfect reconstruction of the shx. Are there any extensions that break them? Commented Feb 2, 2021 at 12:37
  • ArcGIS always rewrites the shapefile contents after edit to remove gaps caused by editing, but it is not required.
    – Vince
    Commented Feb 2, 2021 at 12:49

2 Answers 2

19

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

18

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.

2

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