Skip to content

Commit 9773472

Browse files
authored
Merge pull request #650 from nilssonjacob/main
Added resolution to SwathDefinition in slice.py
2 parents 26f0ee4 + 3d2c4d9 commit 9773472

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

pyresample/slicer.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(self, area_to_crop, area_to_contain, work_crs=None):
103103
def get_polygon_to_contain(self):
104104
"""Get the shapely Polygon corresponding to *area_to_contain* in lon/lat coordinates."""
105105
from shapely.geometry import Polygon
106+
106107
x, y = self.area_to_contain.get_edge_bbox_in_projection_coordinates(10)
107108
poly = Polygon(zip(*self._source_transformer.transform(x, y)))
108109
return poly
@@ -129,7 +130,8 @@ def _assemble_slices(chunk_slices):
129130
def _get_chunk_polygons_for_swath_to_crop(self, swath_to_crop):
130131
"""Get the polygons for each chunk of the area_to_crop."""
131132
from shapely.geometry import Polygon
132-
for ((lons, lats), (line_slice, col_slice)) in _get_chunk_bboxes_for_swath_to_crop(swath_to_crop):
133+
134+
for (lons, lats), (line_slice, col_slice) in _get_chunk_bboxes_for_swath_to_crop(swath_to_crop):
133135
smaller_poly = Polygon(zip(*self._target_transformer.transform(lons, lats)))
134136
yield (smaller_poly, (line_slice, col_slice))
135137

@@ -166,6 +168,7 @@ def __init__(self, area_to_crop, area_to_contain):
166168
def get_polygon_to_contain(self):
167169
"""Get the shapely Polygon corresponding to *area_to_contain* in projection coordinates of *area_to_crop*."""
168170
from shapely.geometry import Polygon
171+
169172
try:
170173
x, y = self.area_to_contain.get_edge_bbox_in_projection_coordinates(frequency=10)
171174
except AttributeError:
@@ -177,7 +180,7 @@ def get_polygon_to_contain(self):
177180
poly = Polygon(zip(x, y))
178181
poly = poly.intersection(geos_poly)
179182
if poly.is_empty:
180-
raise IncompatibleAreas('No slice on area.')
183+
raise IncompatibleAreas("No slice on area.")
181184
x, y = zip(*poly.exterior.coords)
182185

183186
return Polygon(zip(*self._source_transformer.transform(x, y)))
@@ -189,14 +192,15 @@ def get_slices_from_polygon(self, poly_to_contain):
189192
try:
190193
# We take a little margin around the polygon to ensure all needed pixels will be included.
191194
if self.area_to_crop.crs.axis_info[0].unit_name == self.area_to_contain.crs.axis_info[0].unit_name:
192-
buffer_size = np.max(self.area_to_contain.resolution)
195+
buffer_size = np.max(getattr(self.area_to_contain, "resolution", (0, )))
193196
else:
194197
buffer_size = 0
195198
buffered_poly = poly_to_contain.buffer(buffer_size)
196199
bounds = buffered_poly.bounds
197200
except ValueError as err:
198201
raise InvalidArea("Invalid area") from err
199202
from shapely.geometry import Polygon
203+
200204
poly_to_crop = Polygon(zip(*self.area_to_crop.get_edge_bbox_in_projection_coordinates(frequency=10)))
201205
if not poly_to_crop.intersects(buffered_poly):
202206
raise IncompatibleAreas("Areas not overlapping.")
@@ -209,23 +213,22 @@ def _sanitize_polygon_bounds(self, bounds):
209213
try:
210214
(minx, miny, maxx, maxy) = bounds
211215
except ValueError as err:
212-
raise IncompatibleAreas('No slice on area.') from err
213-
x_bounds, y_bounds = self.area_to_crop.get_array_coordinates_from_projection_coordinates(np.array([minx, maxx]),
214-
np.array([miny, maxy]))
216+
raise IncompatibleAreas("No slice on area.") from err
217+
x_bounds, y_bounds = self.area_to_crop.get_array_coordinates_from_projection_coordinates(
218+
np.array([minx, maxx]), np.array([miny, maxy])
219+
)
215220
y_size, x_size = self.area_to_crop.shape
216221
if np.all(x_bounds < 0) or np.all(y_bounds < 0) or np.all(x_bounds >= x_size) or np.all(y_bounds >= y_size):
217-
raise IncompatibleAreas('No slice on area.')
222+
raise IncompatibleAreas("No slice on area.")
218223
return x_bounds, y_bounds
219224

220225
@staticmethod
221226
def _create_slices_from_bounds(bounds):
222227
"""Create slices from bounds."""
223228
x_bounds, y_bounds = bounds
224229
try:
225-
slice_x = slice(int(np.floor(max(np.min(x_bounds), 0))),
226-
int(np.ceil(np.max(x_bounds))))
227-
slice_y = slice(int(np.floor(max(np.min(y_bounds), 0))),
228-
int(np.ceil(np.max(y_bounds))))
230+
slice_x = slice(int(np.floor(max(np.min(x_bounds), 0))), int(np.ceil(np.max(x_bounds))))
231+
slice_y = slice(int(np.floor(max(np.min(y_bounds), 0))), int(np.ceil(np.max(y_bounds))))
229232
except OverflowError as err:
230233
raise IncompatibleAreas("Area not within finite bounds.") from err
231234
return expand_slice(slice_x), expand_slice(slice_y)

pyresample/test/test_slicer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ def test_slicing_works_with_extents_of_different_units(self):
193193
assert 60 <= slice_x.stop < 65
194194
assert 50 <= slice_y.stop < 55
195195

196+
def test_dest_area_is_a_swath(self):
197+
"""Test dest area is a swath."""
198+
src_area = AreaDefinition('dst', 'dst area', None,
199+
{'ellps': 'WGS84', 'proj': 'latlong'},
200+
100, 100,
201+
(-20, 0, 20, 90))
202+
dst_swath = swath_from_area(self.dst_area, 35)
203+
204+
slicer = create_slicer(src_area, dst_swath)
205+
x_slice, y_slice = slicer.get_slices()
206+
assert x_slice.start >= 0 and x_slice.stop <= 200
207+
assert y_slice.start >= 0 and y_slice.stop <= 200
196208

197209
class TestSwathSlicer(unittest.TestCase):
198210
"""Test the get_slice function when input is a swath."""

0 commit comments

Comments
 (0)