Skip to content

Add spatial filtering for single points #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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