Skip to content

Commit 811d329

Browse files
committed
Add method iterRecords_range
Using the method `iterRecords_range` should be somewhat faster than calling the method `record` within a loop, since we avoid the repeated calls to seek inside `record`.
1 parent 4efef9f commit 811d329

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

shapefile.py

+26
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,32 @@ def iterRecords(self, fields=None):
18271827
if r:
18281828
yield r
18291829

1830+
def iterRecords_range(self, start, stop, fields=None):
1831+
"""Returns a generator of records in a dbf file, for a range
1832+
of oid. Useful for large shapefiles or dbf files. To only
1833+
read some of the fields, specify the 'fields' arg as a list of
1834+
one or more fieldnames.
1835+
1836+
"""
1837+
if self.numRecords is None:
1838+
self.__dbfHeader()
1839+
f = self.__getFileObj(self.dbf)
1840+
start = self.__restrictIndex(start)
1841+
if abs(stop) > self.numRecords:
1842+
raise IndexError("Record index out of range.")
1843+
if stop < 0:
1844+
stop = range(self.numRecords)[stop]
1845+
recSize = self.__recordLength
1846+
f.seek(0)
1847+
f.seek(self.__dbfHdrLength + (start * recSize))
1848+
fieldTuples, recLookup, recStruct = self.__recordFields(fields)
1849+
for i in xrange(start, stop):
1850+
r = self.__record(
1851+
oid=i, fieldTuples=fieldTuples, recLookup=recLookup, recStruct=recStruct
1852+
)
1853+
if r:
1854+
yield r
1855+
18301856
def shapeRecord(self, i=0, fields=None, bbox=None):
18311857
"""Returns a combination geometry and attribute record for the
18321858
supplied record index.

0 commit comments

Comments
 (0)