The Bounding Box argument of View GeoJSON works well for points. But it only works with the centroid of the geometry for higher dimension geometries (polylines and polygons). This is problematic if you are working with, for example, a long road that crosses the view-port but with a centroid outside the box because the road may not show up! Similarly, a polygon that is partially within the view-port will not show until it's centroid is within the bbox even though part of its boundary should appear.
Bounding boxes are aligned with the axes of the coordinate system and this greatly simplifies the problem of determining whether a geometry is in the bbox. To check whether two aligned bounding boxes overlap, check the following conditions:
- if one rectangle is completely to one side of the other, they cannot overlap.
- if one rectangle is completely above or below the other, they cannot overlap.
If neither of those conditions are true, then the rectangles overlap. With the following representing numeric coordinates for bbox edges:
- bbl: bbox left
- bbb: bbox bottom
- bbr: bbox right
- bbt: bbox top
and feature bounding box edges represented by fields l, b, t, r, the above conditions can be reworked through Boolean algebra to show that features whose bounding boxes meet the following condition overlap with the view bounding box edges:
(l < bbr && bbl < r) && (b < bbt && bbb < t)
This condition identifies the features to be included in the view and is straightforward to transform into a WHERE clause, replacing the field letters above by references to the correct columns in the Geofield tables and substituting the actual bbox edge coordinates to prepare the query.
Note: Issue
#1516564: Bounding Box arg handling for polygons β
, open for D7, for which I have submitted patches describes the same problem. Since doing that work, I've realized that the approach discussed there and the patches I submitted, although an improvement, still miss some features that should be included. The logic above also results in a less complex query.