Skip to content

Commit c1b54f3

Browse files
committed
Unpack all shx index offsets at once
Implemented basic version from #52 , but without numpy and memoryview to keep it simple. Not sure if any practical/noticable speedup, since reading the offsets is only a one-time issue and very little data involved. More out of principle.
1 parent 9dee68f commit c1b54f3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

shapefile.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,12 @@ def __shapeIndex(self, i=None):
390390
numRecords = shxRecordLength // 8
391391
# Jump to the first record.
392392
shx.seek(100)
393-
for r in range(numRecords):
394-
# Offsets are 16-bit words just like the file length
395-
self._offsets.append(unpack(">i", shx.read(4))[0] * 2)
396-
shx.seek(shx.tell() + 4)
393+
shxRecords = _Array('i')
394+
# Each offset consists of two nrs, only the first one matters
395+
shxRecords.fromfile(shx, 2 * numRecords)
396+
if sys.byteorder != 'big':
397+
shxRecords.byteswap()
398+
self._offsets = [2 * el for el in shxRecords[::2]]
397399
if not i == None:
398400
return self._offsets[i]
399401

shapefile.pyc

40.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)