@@ -13,6 +13,16 @@ and cell spacing `spacing`. The three arguments must have the same length.
1313A regular grid with dimensions `dims`, with lower left corner of element
1414`offset` at `origin` and cell spacing `spacing`.
1515
16+ RegularGrid(start, finish, dims=dims)
17+
18+ Alternatively, construct a regular grid from a `start` point to a `finish`
19+ with dimensions `dims`.
20+
21+ RegularGrid(start, finish, spacing)
22+
23+ Alternatively, construct a regular grid from a `start` point to a `finish`
24+ point using a given `spacing`.
25+
1626## Examples
1727
1828```
@@ -44,13 +54,9 @@ function RegularGrid(
4454 offset:: Dims{N} ,
4555 topology:: GridTopology{N}
4656) where {M<: Manifold ,C<: CRS ,N}
47- if M < : 🌐 && ! (C <: LatLon )
48- throw (ArgumentError (" regular spacing on `🌐` requires `LatLon` coordinates" ))
49- end
57+ _checkorigin (origin)
5058
51- T = CoordRefSystems. mactype (C)
5259 nc = CoordRefSystems. ncoords (C)
53- us = CoordRefSystems. units (C)
5460
5561 if N ≠ nc
5662 throw (ArgumentError ("""
@@ -59,9 +65,9 @@ function RegularGrid(
5965 """ ))
6066 end
6167
62- sp = ntuple (i -> numconvert (T, withunit ( spacing[i], us[i])), nc )
68+ spac = _spacing (origin, spacing)
6369
64- RegularGrid {M,C,N,typeof(sp )} (origin, sp , offset, topology)
70+ RegularGrid {M,C,N,typeof(spac )} (origin, spac , offset, topology)
6571end
6672
6773function RegularGrid (
@@ -76,6 +82,21 @@ function RegularGrid(
7682 RegularGrid (origin, spacing, offset, GridTopology (dims))
7783end
7884
85+ function RegularGrid (start:: Point , finish:: Point , spacing:: NTuple{N,Number} ) where {N}
86+ _checkorigin (start)
87+ svals, fvals = _startfinish (start, finish)
88+ spac = _spacing (start, spacing)
89+ dims = ceil .(Int, (fvals .- svals) ./ spac)
90+ RegularGrid (dims, start, spac)
91+ end
92+
93+ function RegularGrid (start:: Point , finish:: Point ; dims:: Dims = ntuple (i -> 100 , CoordRefSystems. ncoords (crs (start))))
94+ _checkorigin (start)
95+ svals, fvals = _startfinish (start, finish)
96+ spacing = (fvals .- svals) ./ dims
97+ RegularGrid (dims, start, spacing)
98+ end
99+
79100spacing (g:: RegularGrid ) = g. spacing
80101
81102offset (g:: RegularGrid ) = g. offset
@@ -132,3 +153,38 @@ function Base.show(io::IO, ::MIME"text/plain", g::RegularGrid)
132153 println (io, " ├─ maximum: " , maximum (g))
133154 print (io, " └─ spacing: " , spacing (g))
134155end
156+
157+ # -----------------
158+ # HELPER FUNCTIONS
159+ # -----------------
160+
161+ function _checkorigin (origin)
162+ if manifold (origin) < : 🌐 && ! (crs (origin) <: LatLon )
163+ throw (ArgumentError (" regular spacing on `🌐` requires `LatLon` coordinates" ))
164+ end
165+ end
166+
167+ function _spacing (origin, spacing)
168+ C = crs (origin)
169+ T = CoordRefSystems. mactype (C)
170+ nc = CoordRefSystems. ncoords (C)
171+ us = CoordRefSystems. units (C)
172+ ntuple (i -> numconvert (T, withunit (spacing[i], us[i])), nc)
173+ end
174+
175+ function _startfinish (start:: Point{<:𝔼} , finish:: Point{<:𝔼} )
176+ scoords = coords (start)
177+ fcoords = convert (crs (start), coords (finish))
178+ svals = CoordRefSystems. values (scoords)
179+ fvals = CoordRefSystems. values (fcoords)
180+ svals, fvals
181+ end
182+
183+ function _startfinish (start:: Point{<:🌐} , finish:: Point{<:🌐} )
184+ slatlon = convert (LatLon, coords (start))
185+ flatlon = convert (LatLon, coords (finish))
186+ slon = flatlon. lon < slatlon. lon ? slatlon. lon - 360 u " °" : slatlon. lon
187+ svals = (slatlon. lat, slon)
188+ fvals = (flatlon. lat, flatlon. lon)
189+ svals, fvals
190+ end
0 commit comments