-
Notifications
You must be signed in to change notification settings - Fork 48
DRAFT: Construct a spatialpandas.spatialindex.HilbertRtree for fast bounding box queries
#1215
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
03f9b74
initial implementation of rtree
philipc2 0df6339
initial test file
philipc2 823ae2c
Added benchmarks
aaronzedwick 9ac5a16
Merge branch 'main' into bounds-rtree
philipc2 47f18bf
Merge branch 'main' into bounds-rtree
philipc2 bff281a
Merge branch 'main' into bounds-rtree
philipc2 e0d87ec
Merge branch 'main' into bounds-rtree
philipc2 5d44dae
Merge branch 'main' into bounds-rtree
aaronzedwick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import uxarray as ux | ||
|
|
||
| import numpy.testing as nt | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| current_path = Path(os.path.dirname(os.path.realpath(__file__))) | ||
|
|
||
| CSne30_data_path = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30_vortex.nc" | ||
| quad_hex_grid_path = current_path / "meshfiles" / "ugrid" / "quad-hexagon" / "grid.nc" | ||
|
|
||
|
|
||
| def test_quad_hex_face_centers(): | ||
| """Tests a face center query into the RTree, which expects the same index | ||
| to be returned.""" | ||
| uxgrid = ux.open_grid(quad_hex_grid_path) | ||
| rt = uxgrid.get_rtree() | ||
|
|
||
| for i in range(uxgrid.n_face): | ||
| x = uxgrid.face_x[i].values | ||
| y = uxgrid.face_y[i].values | ||
| z = uxgrid.face_z[i].values | ||
| res = rt.intersects((x, y, z, x, y, z)) | ||
| assert len(res) == 1 | ||
| assert res[0] == i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import numpy as np | ||
| from numba import njit, prange | ||
| from spatialpandas.spatialindex import HilbertRtree | ||
|
|
||
|
|
||
| def _construct_rtree(bounds, p=10, page_size=512): | ||
| lat_bounds = bounds.sel(lon_lat=0, min_max=[0, 1]).values | ||
| lon_bounds = bounds.sel(lon_lat=1, min_max=[0, 1]).values | ||
|
|
||
| boxes = face_aabb_xyz(lat_bounds, lon_bounds) | ||
|
|
||
| return HilbertRtree(boxes, p, page_size) | ||
|
|
||
|
|
||
| @njit(cache=True) | ||
| def face_aabb_xyz_kernel(lat0, lat1, lon0, lon1, eps=1e-12): | ||
| two_pi = 2 * np.pi | ||
|
|
||
| # if it crosses the antimeridian, unwrap lon1 | ||
| if lon1 < lon0: | ||
| lon1 += two_pi | ||
|
|
||
| # build list of theta samples: ends and any cardinal meridians inside | ||
| samples = [lon0, lon1] | ||
| for theta_c in (0.0, np.pi / 2, np.pi, 3 * np.pi / 2): | ||
| t = theta_c | ||
| if t < lon0: | ||
| t += two_pi | ||
| if lon0 <= t <= lon1: | ||
| samples.append(t) | ||
|
|
||
| # build list of phi samples: bounds and equator if spanned | ||
| phis = [lat0, lat1] | ||
| if lat0 <= 0.0 <= lat1: | ||
| phis.append(0.0) | ||
|
|
||
| # initialize extremes TODO | ||
| xmin = ymin = zmin = 1e20 | ||
| xmax = ymax = zmax = -1e20 | ||
|
|
||
| # sample all (phi, theta) | ||
| for phi in phis: | ||
| sin_phi = np.sin(phi) | ||
| cos_phi = np.cos(phi) | ||
| for theta in samples: | ||
| x = cos_phi * np.cos(theta) | ||
| y = cos_phi * np.sin(theta) | ||
| z = sin_phi | ||
|
|
||
| if x < xmin: | ||
| xmin = x | ||
| if x > xmax: | ||
| xmax = x | ||
| if y < ymin: | ||
| ymin = y | ||
| if y > ymax: | ||
| ymax = y | ||
| if z < zmin: | ||
| zmin = z | ||
| if z > zmax: | ||
| zmax = z | ||
|
|
||
| return (xmin - eps, ymin - eps, zmin - eps, xmax + eps, ymax + eps, zmax + eps) | ||
|
|
||
|
|
||
| @njit(cache=True, parallel=True) | ||
| def face_aabb_xyz(lat_bounds, lon_bounds, eps=1e-12): | ||
| n = lat_bounds.shape[0] | ||
| boxes = np.empty((n, 6), dtype=np.float64) | ||
| for i in prange(n): | ||
| boxes[i, :] = face_aabb_xyz_kernel( | ||
| lat_bounds[i, 0], lat_bounds[i, 1], lon_bounds[i, 0], lon_bounds[i, 1], eps | ||
| ) | ||
| return boxes |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we name this
get_r_treejust to be consistent with how we name our other tree functions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestions.