Skip to content

Commit

Permalink
Merge pull request #259 from lgolston/bbox-test
Browse files Browse the repository at this point in the history
Add spatial filtering for single points
  • Loading branch information
GeospatialPython authored Jan 20, 2024
2 parents ce1ff22 + 68a7a47 commit 0117b06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,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
Expand Down
7 changes: 7 additions & 0 deletions shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +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)))]
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("<d", f.read(8)))
Expand Down

0 comments on commit 0117b06

Please sign in to comment.