Skip to content

Commit

Permalink
Fix formatting warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Nov 27, 2023
1 parent 915e34f commit 61240eb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 122 deletions.
19 changes: 10 additions & 9 deletions pyresample/boundary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Utility to extract boundary mask and indices."""
import numpy as np
import numpy as np


def _find_boundary_mask(mask):
def _find_boundary_mask(mask):
"""Find boundary of a binary mask."""
mask = mask.astype(int)
# Pad with zeros (enable to detect Trues at mask boundaries)
Expand All @@ -32,28 +32,30 @@ def _find_boundary_mask(mask):
shift_right = np.roll(padded_mask, 1, axis=1)

# Find the boundary points
padded_boundary_mask = ((padded_mask != shift_up) | (padded_mask != shift_down) |
(padded_mask != shift_left) | (padded_mask != shift_right)) & padded_mask
padded_boundary_mask = ((padded_mask != shift_up) | (padded_mask != shift_down) |
(padded_mask != shift_left) | (padded_mask != shift_right)) & padded_mask

boundary_mask = padded_boundary_mask[1:-1,1:-1]
boundary_mask = padded_boundary_mask[1:-1, 1:-1]
return boundary_mask


def find_boundary_mask(lons, lats):
"""Find the boundary mask."""
valid_mask = np.isfinite(lons) & np.isfinite(lats)
return _find_boundary_mask(valid_mask)


def get_ordered_contour(contour_mask):
"""Return the ordered indices of a contour mask."""
# Count number of rows and columns
# Count number of rows and columns
rows, cols = contour_mask.shape

# Function to find the next contour point
def next_point(current, last, visited):
for dx, dy in [(-1, 0), (0, 1), (1, 0), (0, -1), (1, -1), (-1, -1), (1, 1), (-1, 1)]:
next_pt = (current[0] + dx, current[1] + dy)
if next_pt != last and next_pt not in visited and 0 <= next_pt[0] < rows and 0 <= next_pt[1] < cols and contour_mask[next_pt]:
if (next_pt != last and next_pt not in visited and
0 <= next_pt[0] < rows and 0 <= next_pt[1] < cols and contour_mask[next_pt]):
return next_pt
return None
# Initialize
Expand Down Expand Up @@ -81,4 +83,3 @@ def find_boundary_contour_indices(lons, lats):
boundary_mask = find_boundary_mask(lons, lats)
boundary_contour_idx = get_ordered_contour(boundary_mask)
return boundary_contour_idx

149 changes: 80 additions & 69 deletions pyresample/test/test_boundary/test_order.py
Original file line number Diff line number Diff line change
@@ -1,133 +1,144 @@
#!/usr/bin/env python3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 27 15:48:41 2023
# pyresample, Resampling of remote sensing image data in python
#
# Copyright (C) 2010-2022 Pyresample developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the boundary ordering checks."""

import pytest

@author: ghiggi
"""
import pytest
from pyresample.boundary.geographic_boundary import _is_clockwise_order


class Test_Is_Clockwise_Order:

"""Test clockwise check."""

def test_vertical_edges(self):
"""Test with vertical polygon edges."""
# North Hemisphere
first_point = (0, 10)
second_point = (0, 20)
point_inside = (1, 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
assert not _is_clockwise_order(second_point, first_point, point_inside)

# South Hemisphere
first_point = (0, -20)
first_point = (0, -20)
second_point = (0, -10)
point_inside = (1, - 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
@pytest.mark.parametrize("lon",[-180, -90, 0, 90, 180])
assert not _is_clockwise_order(second_point, first_point, point_inside)

@pytest.mark.parametrize("lon", [-180, -90, 0, 90, 180])
def test_horizontal_edges(self, lon):
"""Test with horizontal polygon edges."""
# Point in northern hemisphere
# Point in northern hemisphere
first_point = (lon, 0)
second_point = (lon-10, 0)
point_inside = (1, 15)
second_point = (lon - 10, 0)
point_inside = (1, 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
# Point in northern hemisphere
assert not _is_clockwise_order(second_point, first_point, point_inside)

# Point in northern hemisphere
first_point = (lon, 0)
second_point = (lon+10, 0)
point_inside = (1, -15)
second_point = (lon + 10, 0)
point_inside = (1, -15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
assert not _is_clockwise_order(second_point, first_point, point_inside)

def test_diagonal_edges(self):
"""Test with diagonal polygon edges."""
point_inside = (20, 15)
# Edge toward right (above point) --> clockwise
point_inside = (20, 15)

# Edge toward right (above point) --> clockwise
first_point = (0, 0)
second_point = (20, 20)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
assert not _is_clockwise_order(second_point, first_point, point_inside)

# Edge toward right (below point) --> not clockwise
first_point = (0, 0)
second_point = (20, 10)
assert not _is_clockwise_order(first_point, second_point, point_inside)

def test_polygon_edges_on_antimeridian(self):
"""Test polygon edges touching the antimeridian edges."""
## Right side of antimeridian
# North Hemisphere
# Right side of antimeridian
# - North Hemisphere
first_point = (-180, 10)
second_point = (-180, 20)
point_inside = (-179, 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
# South Hemisphere
first_point = (-180, -20)
assert not _is_clockwise_order(second_point, first_point, point_inside)

# - South Hemisphere
first_point = (-180, -20)
second_point = (-180, -10)
point_inside = (-179, - 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
## Left side of antimeridian
# North Hemisphere
assert not _is_clockwise_order(second_point, first_point, point_inside)

# Left side of antimeridian
# - North Hemisphere
first_point = (-180, 20)
second_point = (-180, 10)
point_inside = (179, 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
# South Hemisphere
first_point = (-180, -10)
second_point = (-180,-20)
assert not _is_clockwise_order(second_point, first_point, point_inside)

# - South Hemisphere
first_point = (-180, -10)
second_point = (-180, -20)
point_inside = (179, - 15)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
@pytest.mark.parametrize("lon",[179, 180, -180, -179])
assert not _is_clockwise_order(second_point, first_point, point_inside)

@pytest.mark.parametrize("lon", [179, 180, -180, -179])
def test_polygon_around_antimeridian(self, lon):
"""Test polygon edges crossing antimeridian."""
# North Hemisphere
first_point = (170, 10)
first_point = (170, 10)
second_point = (-170, 10)
point_inside = (lon, 5)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
assert not _is_clockwise_order(second_point, first_point, point_inside)

# South Hemisphere
first_point = (-170, -10)
first_point = (-170, -10)
second_point = (170, -10)
point_inside = (lon, - 5)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
@pytest.mark.parametrize("lon_pole",[-180, 90, 45, 0, 45, 90, 180])
@pytest.mark.parametrize("lat",[85, 0, -85])
assert not _is_clockwise_order(second_point, first_point, point_inside)

@pytest.mark.parametrize("lon_pole", [-180, 90, 45, 0, 45, 90, 180])
@pytest.mark.parametrize("lat", [85, 0, -85])
def test_polygon_around_north_pole(self, lon_pole, lat):
"""Test polygon edges around north pole (right to left)."""
point_inside = (lon_pole, 90)
first_point = (0, lat)
second_point = (-10, lat)
first_point = (0, lat)
second_point = (-10, lat)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)
@pytest.mark.parametrize("lon_pole",[-180, 90, 45, 0, 45, 90, 180])
@pytest.mark.parametrize("lat",[85, 0, -85])
assert not _is_clockwise_order(second_point, first_point, point_inside)

@pytest.mark.parametrize("lon_pole", [-180, 90, 45, 0, 45, 90, 180])
@pytest.mark.parametrize("lat", [85, 0, -85])
def test_polygon_around_south_pole(self, lon_pole, lat):
"""Test polygon edges around south pole (left to right)."""
point_inside = (lon_pole, -90)
first_point = (0, lat)
second_point = (10, lat)
first_point = (0, lat)
second_point = (10, lat)
assert _is_clockwise_order(first_point, second_point, point_inside)
assert not _is_clockwise_order(second_point, first_point , point_inside)





assert not _is_clockwise_order(second_point, first_point, point_inside)
Loading

0 comments on commit 61240eb

Please sign in to comment.