<div dir="ltr"><div>Hi,<br></div><div><br></div><div>Out of curiosity, if you isolate the shp, dbf and shx (make a copy) in a separate folder is the data still corrupt?</div><div><br></div><div>Rob</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 15, 2023 at 6:35 AM Andrea Giudiceandrea via gdal-dev <<a href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi devs,<br>
in a reent QGIS issue report at <br>
<a href="https://github.com/qgis/QGIS/issues/53058" rel="noreferrer" target="_blank">https://github.com/qgis/QGIS/issues/53058</a> , an user complains about an <br>
ESRI Shapefile layer that was corrupted after an attribute value was <br>
changed and the edit was saved. The corrupted layer is opened by QGIS <br>
without errors or warning being reported, anyway it shows only a subset <br>
of the original feature geometry: a lot of records have now a null <br>
geometry associated, so they cannot be displayed.<br>
<br>
After some investigations, although I don't know why and how the layer <br>
was corrupted, it seems to me that the issue is mostly due to a <br>
corruption of the .idx file: in fact it contains, for various records, <br>
incorrect value of index and length of the record. This generates the <br>
incorrect reading of such record and the following ones, until the the <br>
index in the .idx file and the data in the .shp file line up again.<br>
<br>
Running the QGIS "Repair Shapefile" processing algorithm against such <br>
layer, the algorithm fails while the .idx file is actually updated but <br>
the layer becomes totally invalid and it is not possible to load it in <br>
QGIS. The same happens directly using ogrinfo after the .idx file was <br>
deleted and the SHAPE_RESTORE_SHX variable was set to YES: the .idx file <br>
was recreated but the layer becomes unreadable by both QGIS and ogrinfo.<br>
<br>
Inspecting the .idx file created by ogrinfo with SHAPE_RESTORE_SHX=YES <br>
(which is the same as the one created by the QGIS tool "Repair <br>
Shapefile"), it seems to me ogr fails to properly create the .idx file: <br>
it incorrectly stores, in the index file header, the total length in <br>
16-bit words of the .shp file instead of the total length in 16-bit <br>
words of the .idx file itself.<br>
In this particular case,<br>
it stores the incorrect value 00 29 2A C2 = 2697922 16-bit words = <br>
5395844 bytes<br>
instead of the correct value 00 02 1D 26 = 138534 16-bit words = 277068 <br>
bytes<br>
<br>
Changing such incorrect value to the correct one in the repaired .idx <br>
file, makes the layer valid again and showing again the previously <br>
missing feature geometries (with only some glitches and a missing record).<br>
<br>
This behaviour seems weird to me, as I remember that the Repair <br>
Shapefile tool or the SHAPE_RESTORE_SHX=YES setting worked well to <br>
repair Shapefiles with corrupted index in the past.<br>
<br>
Maybe the issue in this particular Shapefile prevent ogr to correctly <br>
repair the index?<br>
For comparison, the old "Shape Checker utility" succeeds to repair the <br>
.idx file: it creates the same .idx file as the one created by ogr, <br>
apart from the total file length value which is correct.<br>
<br>
Any clue as to what may have gone wrong during the layer editing in QGIS <br>
that eventually corrupted the layer?<br>
<br>
<br>
Best regards.<br>
<br>
Andrea Giudiceandrea<br>
_______________________________________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
</blockquote></div>