Skip to content

Commit 0117b06

Browse files
Merge pull request #259 from lgolston/bbox-test
Add spatial filtering for single points
2 parents ce1ff22 + 68a7a47 commit 0117b06

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ Selectively reading only the necessary data in this way is particularly useful f
12261226

12271227
### Spatial filtering
12281228

1229-
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:
1229+
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:
12301230

12311231

12321232
>>> bbox = [36.423, 12.360, 43.123, 18.004] # ca bbox of Eritrea

shapefile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,13 @@ def __shape(self, oid=None, bbox=None):
13601360
# Read a single point
13611361
if shapeType in (1,11,21):
13621362
record.points = [_Array('d', unpack("<2d", f.read(16)))]
1363+
if bbox is not None:
1364+
# create bounding box for Point by duplicating coordinates
1365+
point_bbox = list(record.points[0] + record.points[0])
1366+
# skip shape if no overlap with bounding box
1367+
if not bbox_overlap(bbox, point_bbox):
1368+
f.seek(next)
1369+
return None
13631370
# Read a single Z value
13641371
if shapeType == 11:
13651372
record.z = list(unpack("<d", f.read(8)))

0 commit comments

Comments
 (0)