Skip to content

Commit b640432

Browse files
committed
Add separate shapely tests
1 parent 478fc5f commit b640432

3 files changed

Lines changed: 105 additions & 4 deletions

File tree

pyresample/future/spherical/extent.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import numpy as np
2222
from shapely.geometry import MultiPolygon, Polygon
23+
from shapely.ops import unary_union
2324

2425

2526
def bounds_from_extent(extent):
@@ -204,9 +205,6 @@ def _repr_svg_(self):
204205
@property
205206
def is_global(self):
206207
"""Check if the extent is global."""
207-
from shapely.geometry import Polygon
208-
from shapely.ops import unary_union
209-
210208
# Try union the polygons
211209
unioned_polygon = unary_union(self.polygons)
212210
# If still a MultiPolygon, not a global extent

pyresample/test/test_sgeom/test_extent.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,40 @@ def test_repr(self):
195195
sext = SExtent(extent)
196196
self.assertEqual(repr(sext), '[[0, 20, 10, 30]]')
197197

198+
def test_repr_svg(self):
199+
"""Check the sketch Ipython representation."""
200+
extent = [-180, 180, -90, 90]
201+
sext = SExtent(extent)
202+
repr_svg = '<svg xmlns="http://www.w3.org/2000/svg" ' + \
203+
'xmlns:xlink="http://www.w3.org/1999/xlink" ' + \
204+
'width="300" height="208.8" ' + \
205+
'viewBox="-194.4 -104.4 388.8 208.8" ' + \
206+
'preserveAspectRatio="xMinYMin meet">' + \
207+
'<g transform="matrix(1,0,0,-1,0,0.0)"><g>' + \
208+
'<path fill-rule="evenodd" fill="#66cc99" stroke="#555555" ' + \
209+
'stroke-width="2.592" opacity="0.6" ' + \
210+
'd="M -180.0,-90.0 L -180.0,90.0 L 180.0,90.0 L 180.0,-90.0 L -180.0,-90.0 z" ' + \
211+
'/></g></g></svg>'
212+
self.assertEqual(sext._repr_svg_(), repr_svg)
213+
198214
def test_is_global(self):
199215
"""Check is_global property."""
200216
# Is global
201217
extent = [-180, 180, -90, 90]
202218
sext = SExtent(extent)
203219
assert sext.is_global
204220

205-
# Is clearly not global
221+
# Is clearly not global (single extent)
206222
extent = [-175, 180, -90, 90]
207223
sext = SExtent(extent)
208224
assert not sext.is_global
209225

226+
# Is clearly not global (multiple extent)
227+
extent1 = [-170, -160, -90, 90]
228+
extent2 = [0, 10, -90, 90]
229+
sext = SExtent(extent1, extent2)
230+
assert not sext.is_global
231+
210232
# Is global, but with multiple extents
211233
extent1 = [-180, 0, -90, 90]
212234
extent2 = [0, 180, -90, 90]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)