@@ -13,6 +13,16 @@ and cell spacing `spacing`. The three arguments must have the same length.
13
13
A regular grid with dimensions `dims`, with lower left corner of element
14
14
`offset` at `origin` and cell spacing `spacing`.
15
15
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
+
16
26
## Examples
17
27
18
28
```
@@ -44,13 +54,9 @@ function RegularGrid(
44
54
offset:: Dims{N} ,
45
55
topology:: GridTopology{N}
46
56
) 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)
50
58
51
- T = CoordRefSystems. mactype (C)
52
59
nc = CoordRefSystems. ncoords (C)
53
- us = CoordRefSystems. units (C)
54
60
55
61
if N ≠ nc
56
62
throw (ArgumentError ("""
@@ -59,9 +65,9 @@ function RegularGrid(
59
65
""" ))
60
66
end
61
67
62
- sp = ntuple (i -> numconvert (T, withunit ( spacing[i], us[i])), nc )
68
+ spac = _spacing (origin, spacing)
63
69
64
- RegularGrid {M,C,N,typeof(sp )} (origin, sp , offset, topology)
70
+ RegularGrid {M,C,N,typeof(spac )} (origin, spac , offset, topology)
65
71
end
66
72
67
73
function RegularGrid (
@@ -76,6 +82,21 @@ function RegularGrid(
76
82
RegularGrid (origin, spacing, offset, GridTopology (dims))
77
83
end
78
84
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
+
79
100
spacing (g:: RegularGrid ) = g. spacing
80
101
81
102
offset (g:: RegularGrid ) = g. offset
@@ -132,3 +153,38 @@ function Base.show(io::IO, ::MIME"text/plain", g::RegularGrid)
132
153
println (io, " ├─ maximum: " , maximum (g))
133
154
print (io, " └─ spacing: " , spacing (g))
134
155
end
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