Skip to content

Commit 2e112a7

Browse files
authored
Merge pull request #404 from JamesParrott/Get_rid_of_Array
Make ShxReader._shxRecords an array.array[int] instead of _Array[int]
2 parents d9c9143 + de067aa commit 2e112a7

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

src/shapefile.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ class GeoJSONFeatureCollectionWithBBox(GeoJSONFeatureCollection):
394394
ARR_TYPE = TypeVar("ARR_TYPE", int, float)
395395

396396

397+
# Needed for backwards compatibility, and to keep a
398+
# simpler list like repr, e.g. in docstrings
397399
class _Array(array.array, Generic[ARR_TYPE]): # type: ignore[type-arg]
398400
"""Converts python tuples to lists of the appropriate type.
399401
Used to unpack different shapefile header parts."""
@@ -2873,7 +2875,7 @@ def __init__(
28732875
super().__init__(file=shx)
28742876
self.numShapes: int
28752877
self._shxHeader()
2876-
self._shxRecords_16bw: _Array[int] | None = None
2878+
self._shxRecords_16bw: array.array[int] | None = None
28772879

28782880
def _shxHeader(self) -> None:
28792881
"""Reads the header information from a .shx file."""
@@ -2888,7 +2890,7 @@ def _read_shxRecords(self) -> None:
28882890
# Jump to the first record.
28892891
self.file.seek(100)
28902892
# Each index record consists of two nums. We only want the first one
2891-
self._shxRecords_16bw = _Array[int]("i", self.file.read(2 * self.numShapes * 4))
2893+
self._shxRecords_16bw = array.array("i", self.file.read(2 * self.numShapes * 4))
28922894
if sys.byteorder != "big":
28932895
self._shxRecords_16bw.byteswap()
28942896

@@ -2897,14 +2899,14 @@ def _read_shxRecords(self) -> None:
28972899
def offsets(self) -> list[int]:
28982900
self._read_shxRecords()
28992901
# Convert from offsets in 16b Words to Bytes (8b).
2900-
offsets_ = [2 * el for el in cast(_Array[int], self._shxRecords_16bw)[::2]]
2902+
offsets_ = [2 * el for el in cast(Sequence[int], self._shxRecords_16bw)[::2]]
29012903
assert len(offsets_) == self.numShapes, f"{self.numShapes=}, {len(offsets_)=}"
29022904
return offsets_
29032905

29042906
@functools.cached_property
29052907
def shape_lengths_B(self) -> list[int]:
29062908
self._read_shxRecords()
2907-
return [2 * x for x in cast(_Array[int], self._shxRecords_16bw)[1::2]]
2909+
return [2 * x for x in cast(Sequence[int], self._shxRecords_16bw)[1::2]]
29082910

29092911

29102912
ShapeHeaderInfoT = tuple[int, int, int]
@@ -2943,15 +2945,9 @@ def _shpHeader(self) -> None:
29432945
self.file.seek(32)
29442946
self.shapeType = unpack("<i", self.file.read(4))[0]
29452947
# The shapefile's bounding box (lower left, upper right)
2946-
# self.bbox: BBox = tuple(_Array("d", unpack("<4d", shp.read(32))))
29472948
self.bbox = BBox(*unpack("<4d", self.file.read(32)))
2948-
# xmin, ymin, xmax, ymax = unpack("<4d", shp.read(32))
2949-
# self.bbox = BBox(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax)
29502949
# Elevation
2951-
# self.zbox: ZBox = tuple(_Array("d", unpack("<2d", shp.read(16))))
29522950
self.zbox = ZBox(*unpack("<2d", self.file.read(16)))
2953-
# zmin, zmax = unpack("<2d", shp.read(16))
2954-
# self.zbox = ZBox(zmin=zmin, zmax=zmax)
29552951
# Measure
29562952
# Measure values less than -1e38 are nodata values according to the spec
29572953
self.mbox = MBox(

0 commit comments

Comments
 (0)