Skip to content

Commit

Permalink
Add more slicer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed May 17, 2021
1 parent 78e7d3e commit ea3c1a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pyresample/resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import json
import logging
import os
from abc import ABC
from abc import ABC, abstractmethod
from functools import lru_cache, partial
from uuid import uuid4

Expand Down Expand Up @@ -407,6 +407,16 @@ def get_slices(self):
poly = self.get_polygon()
return self.get_slices_from_polygon(poly)

@abstractmethod
def get_polygon(self):
"""Get the shapely Polygon corresponding to *area_to_contain*."""
raise NotImplementedError

@abstractmethod
def get_slices_from_polygon(self, poly):
"""Get the slices based on the polygon."""
raise NotImplementedError


class SwathSlicer(Slicer):
"""A Slicer for cropping SwathDefinitions."""
Expand Down Expand Up @@ -507,9 +517,11 @@ def _sanitize_polygon_bounds(self, bounds):
def _create_slices_from_bounds(bounds):
"""Create slices from bounds."""
x_bounds, y_bounds = bounds
slice_x = slice(int(np.floor(max(np.min(x_bounds), 0))),
int(np.ceil(np.max(x_bounds))))
slice_y = slice(int(np.floor(max(np.min(y_bounds), 0))),
int(np.ceil(np.max(y_bounds))))

try:
slice_x = slice(int(np.floor(max(np.min(x_bounds), 0))),
int(np.ceil(np.max(x_bounds))))
slice_y = slice(int(np.floor(max(np.min(y_bounds), 0))),
int(np.ceil(np.max(y_bounds))))
except OverflowError:
raise IncompatibleAreas("Area not within finite bounds.")
return expand_slice(slice_x), expand_slice(slice_y)
27 changes: 27 additions & 0 deletions pyresample/test/test_dask_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,28 @@ def test_barely_touching_chunks_intersection(self):
assert x_slice.start > 0 and x_slice.stop < 100
assert y_slice.start > 0 and y_slice.stop >= 100

def test_slicing_an_area_with_infinite_bounds(self):
"""Test slicing an area with infinite bounds."""
src_area = AreaDefinition('dst', 'dst area', None,
{'ellps': 'WGS84', 'proj': 'merc'},
100, 100,
(-10000.0, -10000.0, 0.0, 0.0))

dst_area = AreaDefinition('moll', 'moll', None,
{
'ellps': 'WGS84',
'lon_0': '0',
'proj': 'moll',
'units': 'm'
},
102, 102,
(-100000.0, -4369712.0686,
18040096.0, 9020047.8481))

slicer = Slicer(src_area, dst_area)
with pytest.raises(IncompatibleAreas):
slicer.get_slices()


class TestSlicer(unittest.TestCase):
"""Test the get_slice function when input is a swath."""
Expand Down Expand Up @@ -286,6 +308,11 @@ def test_swath_get_polygon_returns_a_polygon(self):
poly = slicer.get_polygon()
assert isinstance(poly, Polygon)

def test_cannot_slice_a_string(self):
"""Test that we cannot slice a string."""
with pytest.raises(NotImplementedError):
Slicer("my_funky_area", self.dst_area)


class TestDaskResamplerFromSwath(unittest.TestCase):
"""Test case for the DaskResampler class swath to area."""
Expand Down

0 comments on commit ea3c1a8

Please sign in to comment.