From 9d31036a194b490517c75ddfc7c15a3fda82056f Mon Sep 17 00:00:00 2001 From: lgolston <30876419+lgolston@users.noreply.github.com> Date: Thu, 24 Aug 2023 11:20:29 -0500 Subject: [PATCH 1/2] bbox filtering for single points --- README.md | 2 +- shapefile.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dce792cc..aac57ed7 100644 --- a/README.md +++ b/README.md @@ -1224,7 +1224,7 @@ Selectively reading only the necessary data in this way is particularly useful f ### Spatial filtering -Another common use-case is that we only want to read those records that are located in some region of interest. Because the shapefile stores the bounding box of each shape separately from the geometry data, it's possible to quickly retrieve all shapes that might overlap a given bounding box region without having to load the full shape geometry data for every shape. This can be done by specifying the `bbox` argument to any of the record or shape methods: +Another common use-case is that we only want to read those records that are located in some region of interest. Because the shapefile stores the bounding box of each shape separately from the geometry data, it's possible to quickly retrieve all shapes that might overlap a given bounding box region without having to load the full shape geometry data for every shape. This can be done by specifying the `bbox` argument to the shapes, iterShapes, or iterShapeRecords methods: >>> bbox = [36.423, 12.360, 43.123, 18.004] # ca bbox of Eritrea diff --git a/shapefile.py b/shapefile.py index 04fb5ec0..0bf8c5f6 100644 --- a/shapefile.py +++ b/shapefile.py @@ -1360,6 +1360,12 @@ def __shape(self, oid=None, bbox=None): # Read a single point if shapeType in (1,11,21): record.points = [_Array('d', unpack("<2d", f.read(16)))] + # create bounding box for Point by duplicating coordinates + point_bbox = list(record.points[0] + record.points[0]) + # skip shape if no overlap with bounding box + if not bbox_overlap(bbox, point_bbox): + f.seek(next) + return None # Read a single Z value if shapeType == 11: record.z = list(unpack(" Date: Thu, 24 Aug 2023 15:47:57 -0500 Subject: [PATCH 2/2] add bbox None check --- shapefile.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/shapefile.py b/shapefile.py index 0bf8c5f6..325f284c 100644 --- a/shapefile.py +++ b/shapefile.py @@ -1360,12 +1360,13 @@ def __shape(self, oid=None, bbox=None): # Read a single point if shapeType in (1,11,21): record.points = [_Array('d', unpack("<2d", f.read(16)))] - # create bounding box for Point by duplicating coordinates - point_bbox = list(record.points[0] + record.points[0]) - # skip shape if no overlap with bounding box - if not bbox_overlap(bbox, point_bbox): - f.seek(next) - return None + if bbox is not None: + # create bounding box for Point by duplicating coordinates + point_bbox = list(record.points[0] + record.points[0]) + # skip shape if no overlap with bounding box + if not bbox_overlap(bbox, point_bbox): + f.seek(next) + return None # Read a single Z value if shapeType == 11: record.z = list(unpack("