Skip to content
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

Refactor area boundary sides retrieval with _geographic_sides and _projection_sides methods #566

Merged
merged 9 commits into from
Dec 8, 2023
Merged
35 changes: 18 additions & 17 deletions pyresample/gradient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,33 @@ def _get_projection_coordinates(self, datachunks):
src_prj=src_crs, dst_prj=dst_crs)
self.prj = pyproj.Proj(self.target_geo_def.crs)

def _get_prj_poly(self, geo_def):
# - None if out of Earth Disk
# - False is SwathDefinition
if isinstance(geo_def, SwathDefinition):
return False
try:
poly = get_polygon(self.prj, geo_def)
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a more specific exception here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It can arise from

  • NotImplementedError in get_geo_bounding_box (totally out of Earth disk)
  • ValueError when not valid boundary coordinates (all nan or inf)
  • ValueError when not at least 4 valid vertices can be retrieved for boundary sides creation.

poly = None
return poly

def _get_src_poly(self, src_y_start, src_y_end, src_x_start, src_x_end):
"""Get bounding polygon for source chunk."""
geo_def = self.source_geo_def[src_y_start:src_y_end,
src_x_start:src_x_end]
try:
src_poly = get_polygon(self.prj, geo_def)
except AttributeError:
# Can't create polygons for SwathDefinition
src_poly = False

return src_poly
return self._get_prj_poly(geo_def)

def _get_dst_poly(self, idx, dst_x_start, dst_x_end,
def _get_dst_poly(self, idx,
dst_x_start, dst_x_end,
dst_y_start, dst_y_end):
"""Get target chunk polygon."""
dst_poly = self.dst_polys.get(idx, None)
if dst_poly is None:
geo_def = self.target_geo_def[dst_y_start:dst_y_end,
dst_x_start:dst_x_end]
try:
dst_poly = get_polygon(self.prj, geo_def)
except AttributeError:
# Can't create polygons for SwathDefinition
dst_poly = False
dst_poly = self._get_prj_poly(geo_def)
self.dst_polys[idx] = dst_poly

return dst_poly

def get_chunk_mappings(self):
Expand Down Expand Up @@ -294,19 +295,20 @@ def compute(self, data, fill_value=None, **kwargs):
res = res.squeeze()

res = xr.DataArray(res, dims=data_dims, coords=coords)

return res


def check_overlap(src_poly, dst_poly):
"""Check if the two polygons overlap."""
# swath definition case
if dst_poly is False or src_poly is False:
covers = True
# area / area case
elif dst_poly is not None and src_poly is not None:
covers = src_poly.intersects(dst_poly)
# out of earth disk case
else:
covers = False

return covers


Expand All @@ -328,7 +330,6 @@ def _gradient_resample_data(src_data, src_x, src_y,
src_gradient_yl, src_gradient_yp,
dst_x, dst_y,
method=method)

return image


Expand Down
18 changes: 12 additions & 6 deletions pyresample/test/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def setup_method(self):
102, 102,
(-2717181.7304994687, -5571048.14031214,
1378818.2695005313, -1475048.1403121399))
self.dst_swath = SwathDefinition(*self.dst_area.get_lonlats())

with warnings.catch_warnings():
warnings.filterwarnings("ignore", message=".*which is still EXPERIMENTAL.*", category=UserWarning)
self.resampler = StackingGradientSearchResampler(self.src_area, self.dst_area)
self.swath_resampler = StackingGradientSearchResampler(self.src_swath, self.dst_area)
self.area_to_swath_resampler = StackingGradientSearchResampler(self.src_area, self.dst_swath)

def test_get_projection_coordinates_area_to_area(self):
"""Check that the coordinates are initialized, for area -> area."""
Expand Down Expand Up @@ -130,12 +132,12 @@ def test_get_src_poly_swath(self):
chunks = (10, 10)
self.swath_resampler._get_projection_coordinates(chunks)
self.swath_resampler._get_gradients()
# Swath area defs can't be sliced, so False is returned
# SwathDefinition can't be sliced, so False is returned
poly = self.swath_resampler._get_src_poly(0, 40, 0, 40)
assert poly is False

@mock.patch('pyresample.gradient.get_polygon')
def test_get_dst_poly(self, get_polygon):
def test_get_dst_poly_area(self, get_polygon):
"""Test defining destination chunk polygon."""
chunks = (10, 10)
self.resampler._get_projection_coordinates(chunks)
Expand All @@ -148,10 +150,14 @@ def test_get_dst_poly(self, get_polygon):
self.resampler._get_dst_poly('idx1', 0, 10, 0, 10)
assert get_polygon.call_count == 1

# Swath defs raise AttributeError, and False is returned
get_polygon.side_effect = AttributeError
self.resampler._get_dst_poly('idx2', 0, 10, 0, 10)
assert self.resampler.dst_polys['idx2'] is False
def test_get_dst_poly_swath(self):
"""Test defining dst chunk polygon for SwathDefinition."""
chunks = (10, 10)
self.area_to_swath_resampler._get_projection_coordinates(chunks)
self.area_to_swath_resampler._get_gradients()
# SwathDefinition can't be sliced, so False is returned
self.area_to_swath_resampler._get_dst_poly('idx2', 0, 10, 0, 10)
assert self.area_to_swath_resampler.dst_polys['idx2'] is False

def test_filter_data(self):
"""Test filtering chunks that do not overlap."""
Expand Down
Loading