Description
@fizzahussain 's recipe suggestion that zipped together shapes and records, made me realise Reader.iterShapeRecords should behave less astonishingly, if deletion flags happen to be set on dbf records for some reason.
We should probably double check the number of shapes and non-deleted records in each constituent file too. E.g.:
- warn if there are deleted records, but they must be skipped,
- warn if shapes will be skipped as the number of shapes and records (non deleted and a non-zero number of deleted ones) is the same,
- warn (or raise an Exception if strict=True) if there is no way to interpret the shapes and records consistently.
Possibly with a similar check too between the shp and shx, or allowing the "lazy deletion" via the shx to determine the true number of shapes for the above comparison with the number of dbf records.
Should .shx "lazy deletion" and .dbf deletion flags be required to match? Lazy deletion and deletion flags needn't be required to match by a .shp reader client. But other than creating a new file without those records and overwriting the original with it, the author of the .shp file doing so is the most unambiguous way to consistently (lazily) delete a shape and its record.
Do any GIS packages or any Shapefiles out in the wild use deletion flags or skip shapes in .shx files?
ex.py
import io
import shapefile as shp
shp_stream, dbf_stream = io.BytesIO(), io.BytesIO()
with shp.DbfWriter(dbf_stream) as w:
w.field("Text_field")
w._record(["0 0 0"], set_deletion_flag=True)
w._record(["1 1 1"], set_deletion_flag=False)
with shp.ShpWriter(shp_stream) as w:
w.shape(shp.PointZ(0,0,0))
w.shape(shp.PointZ(1,1,1))
with shp.Reader(shp=shp_stream, dbf=dbf_stream) as r:
for i, sr in enumerate(r.iterShapeRecords()):
print(f"{i}) Shape: {sr.shape.points_3D}, Rec: {sr.record})")>python ex.py
0) Shape: [(0.0, 0.0, 0.0)], Rec: Record #1: ['1 1 1'])
Activity
JamesParrott commentedon Nov 26, 2026
Note, neither shape.oid nor record.oid reflect any information in the shapefile. They are both artificial 0-indexed record numbers (coming from
enumeratein ShpReader.iterShapes and dbfReader.iterRecords respectively) so do not determine which shape corresponds to which record, they merely record how Reader determined that correspondence (by counting from the start).JamesParrott commentedon Nov 26, 2026
My current thinking is:
Replace the fewassertsfor file consistency with warnings (or raise an Exception if strict)Make ShxReader._read_shxRecords verify the differences between consecutive byte offsets, against each shape-length, and warn if lazy deletion is detected (delta bigger than needed) (possibly raise an exception if shape-record overlap is detected (delta too short))("The content length stored in the index record is the same as the value stored in the mainfile record header." (p24), "The content length for a record is the length of the record contents section measured in
16-bit words. Each record, therefore, contributes (4 + content length) 16-bit words
toward the total length of the file" (p5))
Implemented in PR Make record nums more consistent #475
_check_file_lengthto handle inconsistent declared file size (deduced from the declared number of .dbf records) and the actual file size. There's no need to eagerly check each record's del flag unless lazy deletion was detected in the .shx file, or a del flag has been encountered.The only acceptable scenarios are:
JamesParrott commentedon Nov 27, 2026
Re: distinguishing scenario 4, from scenario 5 (the two that both require ".shx file available, lazy deletion found (warned above). ").
There is no way of proving which continuous subsequence of skipped bytes in the .shp file "each lazily deleted shape" would've been in.
All we know by "lazy deletion found" is that some continuous ranged bytes are skipped in the records portion of the .shp file. We can deduce a maximum number of null shapes, and if such a range is large enough to store a null shape, but without further assumptions or e.g. inspecting the .shp file and still making further assumptions, we simply can never know for sure how many skipped shapes are in each range.
What we do know is, how many shapes are in each range of non-skipped bytes. We could also calculate the lengths of subsequences of consecutive dbf records that do not have their deletion flag set.
It seems reasonable to interpret these two sequences of integers being the same as a consistent shape file (but it's still so unusual, it's worth a logger.info at the very least).
JamesParrott commentedon Nov 29, 2026
Currently if the .shx file is given, and if it skips, oid is simply taken from
enumerate, and used to look up the .dbf record of that index.However, within each shapeRecord we have
_recNumbut don't pass it to users.