Skip to content

Commit d3ab52d

Browse files
authored
Add support for LatLon in boundingbox (#1043)
* Add support for 'LatLon' in 'boundingbox' * Update tests * Update tests
1 parent 1fa2950 commit d3ab52d

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

src/boundingboxes.jl

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,51 @@ boundingbox(m::Mesh) = _pboxes(vertices(m))
8585

8686
_bboxes(boxes) = _pboxes(point for box in boxes for point in extrema(box))
8787

88-
function _pboxes(points)
88+
_pboxes(points) = _pboxes(manifold(first(points)), points)
89+
90+
function _pboxes(::Type{𝔼{2}}, points)
91+
p = first(points)
92+
= lentype(p)
93+
xmin, ymin = typemax(ℒ), typemax(ℒ)
94+
xmax, ymax = typemin(ℒ), typemin(ℒ)
95+
for p in points
96+
c = convert(Cartesian, coords(p))
97+
xmin = min(c.x, xmin)
98+
ymin = min(c.y, ymin)
99+
xmax = max(c.x, xmax)
100+
ymax = max(c.y, ymax)
101+
end
102+
Box(withcrs(p, (xmin, ymin)), withcrs(p, (xmax, ymax)))
103+
end
104+
105+
function _pboxes(::Type{𝔼{3}}, points)
89106
p = first(points)
90107
= lentype(p)
91-
Dim = embeddim(p)
92-
xmin = MVector(ntuple(i -> typemax(ℒ), Dim))
93-
xmax = MVector(ntuple(i -> typemin(ℒ), Dim))
108+
xmin, ymin, zmin = typemax(ℒ), typemax(ℒ), typemax(ℒ)
109+
xmax, ymax, zmax = typemin(ℒ), typemin(ℒ), typemin(ℒ)
110+
for p in points
111+
c = convert(Cartesian, coords(p))
112+
xmin = min(c.x, xmin)
113+
ymin = min(c.y, ymin)
114+
zmin = min(c.z, zmin)
115+
xmax = max(c.x, xmax)
116+
ymax = max(c.y, ymax)
117+
zmax = max(c.z, zmax)
118+
end
119+
Box(withcrs(p, (xmin, ymin, zmin)), withcrs(p, (xmax, ymax, zmax)))
120+
end
121+
122+
function _pboxes(::Type{🌐}, points)
123+
p = first(points)
124+
T = numtype(lentype(p))
125+
lonmin, latmin = T(180) * u"°", T(90) * u"°"
126+
lonmax, latmax = T(-180) * u"°", T(-90) * u"°"
94127
for p in points
95-
x = to(p)
96-
@. xmin = min(x, xmin)
97-
@. xmax = max(x, xmax)
128+
c = convert(LatLon, coords(p))
129+
lonmin = min(c.lon, lonmin)
130+
latmin = min(c.lat, latmin)
131+
lonmax = max(c.lon, lonmax)
132+
latmax = max(c.lat, latmax)
98133
end
99-
Box(withcrs(p, xmin), withcrs(p, xmax))
134+
Box(withcrs(p, (latmin, lonmin), LatLon), withcrs(p, (latmax, lonmax), LatLon))
100135
end

src/utils/misc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ function cartesianrange(grid::Grid{🌐}, limits)
262262
nlon, nlat = vsize(grid)
263263
(llonmin, llonmax), (llatmin, llatmax) = limits
264264

265-
a = convert(Cartesian, coords(vertex(grid, (1, 1))))
266-
b = convert(Cartesian, coords(vertex(grid, (nlon, 1))))
267-
c = convert(Cartesian, coords(vertex(grid, (1, nlat))))
265+
a = convert(LatLon, coords(vertex(grid, (1, 1))))
266+
b = convert(LatLon, coords(vertex(grid, (nlon, 1))))
267+
c = convert(LatLon, coords(vertex(grid, (1, nlat))))
268268

269269
lonmin = max(llonmin, a.lon)
270270
latmin = max(llatmin, a.lat)

test/boundingboxes.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@
5656
d = GeometrySet([b, s])
5757
@test boundingbox(m) == Box(cart(-3, -2), cart(2, 2))
5858
@test boundingbox(d) == Box(cart(-3, -2), cart(2, 2))
59-
@test @allocated(boundingbox(m)) < 2500
60-
@test @allocated(boundingbox(d)) < 2500
59+
if Sys.iswindows() && VERSION < v"1.10"
60+
@test @allocated(boundingbox(m)) < 4100
61+
@test @allocated(boundingbox(d)) < 4100
62+
else
63+
@test @allocated(boundingbox(m)) < 2600
64+
@test @allocated(boundingbox(d)) < 2600
65+
end
6166

6267
b1 = Box(cart(0, 0), cart(1, 1))
6368
b2 = Box(cart(-1, -1), cart(0.5, 0.5))
@@ -119,6 +124,19 @@
119124
p = ParaboloidSurface(cart(1, 2, 3), T(5), T(4))
120125
@test boundingbox(p) Box(cart(-4, -3, 3), cart(6, 7, 73 / 16))
121126

127+
# latlon coordinates
128+
t = Triangle(latlon(0, 0), latlon(0, 2), latlon(1, 1))
129+
@test boundingbox(t) == Box(latlon(0, 0), latlon(1, 2))
130+
@test @allocated(boundingbox(t)) < 50
131+
132+
p = Pentagon(latlon(1, 6), latlon(10, 2), latlon(16, 10), latlon(10, 18), latlon(1, 14))
133+
@test boundingbox(p) == Box(latlon(1, 2), latlon(16, 18))
134+
@test @allocated(boundingbox(p)) < 50
135+
136+
d = PointSet(latlon(0, 0), latlon(2, 1), latlon(1, 2))
137+
@test boundingbox(d) == Box(latlon(0, 0), latlon(2, 2))
138+
@test @allocated(boundingbox(d)) < 50
139+
122140
# CRS propagation
123141
r = Ray(merc(-1, 1), vector(1, -1))
124142
@test crs(boundingbox(r)) === crs(r)

0 commit comments

Comments
 (0)