@@ -77,20 +77,29 @@ def __call__(self, transform_xy, x1, y1, x2, y2):
77
77
extremal coordinates; then adding some padding to take into account the
78
78
finite sampling.
79
79
80
- As each sampling step covers a relative range of * 1/nx* or * 1/ny* ,
80
+ As each sampling step covers a relative range of `` 1/nx`` or `` 1/ny`` ,
81
81
the padding is computed by expanding the span covered by the extremal
82
82
coordinates by these fractions.
83
83
"""
84
- x , y = np .meshgrid (
85
- np .linspace (x1 , x2 , self .nx ), np .linspace (y1 , y2 , self .ny ))
86
- xt , yt = transform_xy (np .ravel (x ), np .ravel (y ))
87
- return self ._add_pad (xt .min (), xt .max (), yt .min (), yt .max ())
84
+ tbbox = self ._find_transformed_bbox (
85
+ _User2DTransform (transform_xy , None ), Bbox .from_extents (x1 , y1 , x2 , y2 ))
86
+ return tbbox .x0 , tbbox .x1 , tbbox .y0 , tbbox .y1
88
87
89
- def _add_pad (self , x_min , x_max , y_min , y_max ):
90
- """Perform the padding mentioned in `__call__`."""
91
- dx = (x_max - x_min ) / self .nx
92
- dy = (y_max - y_min ) / self .ny
93
- return x_min - dx , x_max + dx , y_min - dy , y_max + dy
88
+ def _find_transformed_bbox (self , trans , bbox ):
89
+ """
90
+ Compute an approximation of the bounding box obtained by applying
91
+ *trans* to *bbox*.
92
+
93
+ See ``__call__`` for details; this method performs similar
94
+ calculations, but using a different representation of the arguments and
95
+ return value.
96
+ """
97
+ grid = np .reshape (np .meshgrid (np .linspace (bbox .x0 , bbox .x1 , self .nx ),
98
+ np .linspace (bbox .y0 , bbox .y1 , self .ny )),
99
+ (2 , - 1 )).T
100
+ tbbox = Bbox .null ()
101
+ tbbox .update_from_data_xy (trans .transform (grid ))
102
+ return tbbox .expanded (1 + 2 / self .nx , 1 + 2 / self .ny )
94
103
95
104
96
105
class _User2DTransform (Transform ):
@@ -170,42 +179,31 @@ def get_grid_info(self, x1, y1, x2, y2):
170
179
rough number of grids in each direction.
171
180
"""
172
181
173
- extremes = self .extreme_finder (self .inv_transform_xy , x1 , y1 , x2 , y2 )
174
-
175
- # min & max rage of lat (or lon) for each grid line will be drawn.
176
- # i.e., gridline of lon=0 will be drawn from lat_min to lat_max.
182
+ bbox = Bbox .from_extents (x1 , y1 , x2 , y2 )
183
+ tbbox = self .extreme_finder ._find_transformed_bbox (
184
+ self .get_transform ().inverted (), bbox )
177
185
178
- lon_min , lon_max , lat_min , lat_max = extremes
179
- lon_levs , lon_n , lon_factor = self .grid_locator1 (lon_min , lon_max )
180
- lon_levs = np .asarray (lon_levs )
181
- lat_levs , lat_n , lat_factor = self .grid_locator2 (lat_min , lat_max )
182
- lat_levs = np .asarray (lat_levs )
186
+ lon_levs , lon_n , lon_factor = self .grid_locator1 (* tbbox .intervalx )
187
+ lat_levs , lat_n , lat_factor = self .grid_locator2 (* tbbox .intervaly )
183
188
184
- lon_values = lon_levs [:lon_n ] / lon_factor
185
- lat_values = lat_levs [:lat_n ] / lat_factor
189
+ lon_values = np . asarray ( lon_levs [:lon_n ]) / lon_factor
190
+ lat_values = np . asarray ( lat_levs [:lat_n ]) / lat_factor
186
191
187
- lon_lines , lat_lines = self ._get_raw_grid_lines (lon_values ,
188
- lat_values ,
189
- lon_min , lon_max ,
190
- lat_min , lat_max )
192
+ lon_lines , lat_lines = self ._get_raw_grid_lines (lon_values , lat_values , tbbox )
191
193
192
- bb = Bbox .from_extents (x1 , y1 , x2 , y2 ).expanded (1 + 2e-10 , 1 + 2e-10 )
193
-
194
- grid_info = {
195
- "extremes" : extremes ,
196
- # "lon", "lat", filled below.
197
- }
194
+ bbox_expanded = bbox .expanded (1 + 2e-10 , 1 + 2e-10 )
195
+ grid_info = {"extremes" : tbbox } # "lon", "lat" keys filled below.
198
196
199
197
for idx , lon_or_lat , levs , factor , values , lines in [
200
198
(1 , "lon" , lon_levs , lon_factor , lon_values , lon_lines ),
201
199
(2 , "lat" , lat_levs , lat_factor , lat_values , lat_lines ),
202
200
]:
203
201
grid_info [lon_or_lat ] = gi = {
204
- "lines" : [[ l ] for l in lines ] ,
202
+ "lines" : lines ,
205
203
"ticks" : {"left" : [], "right" : [], "bottom" : [], "top" : []},
206
204
}
207
- for ( lx , ly ) , v , level in zip (lines , values , levs ):
208
- all_crossings = _find_line_box_crossings (np . column_stack ([ lx , ly ]), bb )
205
+ for xys , v , level in zip (lines , values , levs ):
206
+ all_crossings = _find_line_box_crossings (xys , bbox_expanded )
209
207
for side , crossings in zip (
210
208
["left" , "right" , "bottom" , "top" ], all_crossings ):
211
209
for crossing in crossings :
@@ -218,18 +216,14 @@ def get_grid_info(self, x1, y1, x2, y2):
218
216
219
217
return grid_info
220
218
221
- def _get_raw_grid_lines (self ,
222
- lon_values , lat_values ,
223
- lon_min , lon_max , lat_min , lat_max ):
224
-
225
- lons_i = np .linspace (lon_min , lon_max , 100 ) # for interpolation
226
- lats_i = np .linspace (lat_min , lat_max , 100 )
227
-
228
- lon_lines = [self .transform_xy (np .full_like (lats_i , lon ), lats_i )
219
+ def _get_raw_grid_lines (self , lon_values , lat_values , bbox ):
220
+ trans = self .get_transform ()
221
+ lons = np .linspace (bbox .x0 , bbox .x1 , 100 ) # for interpolation
222
+ lats = np .linspace (bbox .y0 , bbox .y1 , 100 )
223
+ lon_lines = [trans .transform (np .column_stack ([np .full_like (lats , lon ), lats ]))
229
224
for lon in lon_values ]
230
- lat_lines = [self . transform_xy ( lons_i , np .full_like (lons_i , lat ))
225
+ lat_lines = [trans . transform ( np . column_stack ([ lons , np .full_like (lons , lat )] ))
231
226
for lat in lat_values ]
232
-
233
227
return lon_lines , lat_lines
234
228
235
229
def set_transform (self , aux_trans ):
@@ -246,9 +240,11 @@ def get_transform(self):
246
240
247
241
update_transform = set_transform # backcompat alias.
248
242
243
+ @_api .deprecated ("3.11" , alternative = "grid_finder.get_transform()" )
249
244
def transform_xy (self , x , y ):
250
245
return self ._aux_transform .transform (np .column_stack ([x , y ])).T
251
246
247
+ @_api .deprecated ("3.11" , alternative = "grid_finder.get_transform().inverted()" )
252
248
def inv_transform_xy (self , x , y ):
253
249
return self ._aux_transform .inverted ().transform (
254
250
np .column_stack ([x , y ])).T
0 commit comments