|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2013-2022 Pyresample Developers |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +"""Test cases for shapely.""" |
| 19 | +import unittest |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +from shapely.geometry import MultiPolygon, Polygon |
| 23 | +from shapely.ops import unary_union |
| 24 | + |
| 25 | + |
| 26 | +class TestShapely(unittest.TestCase): |
| 27 | + """Test Shapely methods.""" |
| 28 | + |
| 29 | + def test_polygon_unary_union(self): |
| 30 | + """Test MultiPolygon unary union.""" |
| 31 | + bounds1 = (-180.0, -90.0, 0.0, 90.0) # extent1 = [-180, 0, -90, 90] |
| 32 | + bounds2 = (0.0, 0.0, 180.0, 90.0) # extent2 = [0, 180, -90, 90] |
| 33 | + polygon1 = Polygon.from_bounds(*bounds1) |
| 34 | + polygon2 = Polygon.from_bounds(*bounds2) |
| 35 | + multipolygon = MultiPolygon([polygon1, polygon2]) |
| 36 | + unified_polygon = unary_union(multipolygon) |
| 37 | + assert isinstance(unified_polygon, Polygon) |
| 38 | + vertices = np.array(unified_polygon.exterior.coords) |
| 39 | + expected_vertices = np.array([[-180., 90.], |
| 40 | + [0., 90.], |
| 41 | + [180., 90.], |
| 42 | + [180., 0.], |
| 43 | + [0., 0.], |
| 44 | + [0., -90.], |
| 45 | + [-180., -90.], |
| 46 | + [-180., 90.]]) |
| 47 | + assert np.allclose(expected_vertices, vertices) |
| 48 | + |
| 49 | + def test_polygon_from_bounds(self): |
| 50 | + """Test Polygon definition from bounds.""" |
| 51 | + global_polygon = Polygon.from_bounds(-180, -90, 180, 90) |
| 52 | + assert global_polygon.bounds == (-180.0, -90.0, 180.0, 90.0) |
| 53 | + |
| 54 | + def test_polygon_equals(self): |
| 55 | + # First polygon goes from -180 to 0 longitude |
| 56 | + vertices1 = np.array([[-180., -90.], |
| 57 | + [-180., 90.], |
| 58 | + [0., 90.], |
| 59 | + [0., -90.], |
| 60 | + [-180., -90.]]) |
| 61 | + polygon1 = Polygon(vertices1) |
| 62 | + # Second polygon goes from 0 to 180 longitude |
| 63 | + vertices2 = np.array([[0., -90.], |
| 64 | + [0., 90.], |
| 65 | + [180., 90.], |
| 66 | + [180., -90.], |
| 67 | + [0., -90.]]) |
| 68 | + polygon2 = Polygon(vertices2) |
| 69 | + |
| 70 | + # Global separate polygon from -180 to 180 |
| 71 | + global_separate = MultiPolygon([polygon1, polygon2]) |
| 72 | + |
| 73 | + # Global single polygon from -180 to 180 |
| 74 | + global_polygon = Polygon.from_bounds(-180, -90, 180, 90) |
| 75 | + |
| 76 | + # Unioned polygon |
| 77 | + global_union = unary_union(global_separate) |
| 78 | + |
| 79 | + # Checks topological equality |
| 80 | + assert global_polygon.equals(global_union) |
| 81 | + assert global_polygon.equals(global_separate) |
0 commit comments