"Number of shapes does not match number of table records"? Look familiar?

As long as your .shp is in order, this program will re-generate your .shx which is the shapefile index (not the spatial index). Also, if you have too many or too few records in the dbf, it will add or delete records to match the shapefile. Thus, you can completely fix this problem automatically..

If your dbf file is completely missing, then this will generate a new one with unique ids so that at least you can keep your spatial data.

At the moment, it will also recover shapes up to the point of premature file end. It also now handles premature end (before data begins) in the dBase file as well giving the option to delete and recreate.

It will also detect 'garbage' information in the .shp and let you know how many bytes you could save if AV regenerated it. On some shapefiles, this can be a siginficant saving. (6k on globe.shp in the 3D Analyst examples)

Run this on a copy of your damaged .shp/dbf. That way, you've still got the original broken one if this can't fix it. Which is nice...

Update: 21/6/99. Right. Now I've tried this on demo polylineZ, pointZ, polygonZ and multipatch files. They check out OK, though the globe (polygonz) included in 3D analyst is odd. It has a content length of 118 for the first polygon, but if you do the maths, it should be 116. Four bytes at the end of the each record are useless, but because content-length steers away from them they are no problem. I'd still be VERY interested to know why shapefiles exist like this though. Anyway, the checker handles this now. Please give this a go (in /debug mode) on backups of your Z/M/Multipatch files and let me know what happens. Thanks. Also, why not take a look at a very strange shapefile (3kb zipped) and see just how screwed up, but legal these things can be.

Update: 12/7/99. Broke a bit in multipoints with counting content length. Fixed it.

Delphi program in a ZIP file. The older non-3D/M version is here. Also, if you want the source code, then read on...


OK. Now. Before you download this, please read the following very carefully...

First of all, I don't claim to be any great programmer. Reading this source code will prove that. Also, I know it does a lot now, but when I started it was just as an exercise to try and teach myself how to read binary files of no fixed structure.

Then, it dawned on me that the program might be useful - so I kept on adding. Then there were bugs, so I fixed those. Then people started using it big style so I got requests for enhancements. So I added those. And I broke the additions. So I fixed them.

Then, when I was happy with it, ESRI added measure and 3D shape types so I had to add those. But I added them wrong - partly on the strength of only having the whitepaper (which could have done with a bit of clarification) and no example files. Then I got some example files and thought there was something odd going on. There was. Shapefiles can be mighty strange, but still legal. But I fixed my way round it.

In other words, I've just kept kludging and kludging until things worked. Look at the 'automatic' bit if you don't believe me. Though I did think that using a timer to automatically push the buttons was quite a good idea, it will probably horrify you.

Also, I'm not a great one for commenting my source code. I know what it does. You will have to guess.

Done that. Good. Here is the source code then. Delphi 3. Feel free to email me if you ever write a more competent/clean version or just want to tell me how crap I am.

Andrew