Skip to content

Commit 952cec6

Browse files
authored
Merge pull request #465 from ghiggi/feature-spoint
2 parents 1268d90 + a48ac65 commit 952cec6

File tree

6 files changed

+588
-40
lines changed

6 files changed

+588
-40
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021 Pyresample developers
5+
#
6+
# This program is free software: you can redistribute it and/or modify it under
7+
# the terms of the GNU Lesser General Public License as published by the Free
8+
# Software Foundation, either version 3 of the License, or (at your option) any
9+
# later version.
10+
#
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14+
# details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License along
17+
# with this program. If not, see <http://www.gnu.org/licenses/>.
18+
"""Future features that are backwards incompatible with current functionality."""
19+
20+
from .point import SMultiPoint, SPoint # noqa

pyresample/future/spherical/point.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 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+
"""Define single and multiple points on the sphere through SPoint and SMultiPoint classes."""
19+
import numpy as np
20+
21+
from pyresample.spherical import SCoordinate
22+
23+
24+
class SPoint(SCoordinate):
25+
"""Object representing a single point on a sphere.
26+
27+
The ``lon`` and ``lat`` coordinates must be provided in radians.
28+
"""
29+
30+
def __init__(self, lon, lat):
31+
lon = np.asarray(lon)
32+
lat = np.asarray(lat)
33+
if lon.size > 1 or lat.size > 1:
34+
raise ValueError("Use SMultiPoint to define multiple points.")
35+
super().__init__(lon, lat)
36+
37+
@classmethod
38+
def from_degrees(cls, lon, lat):
39+
"""Create SPoint from lon/lat coordinates in degrees."""
40+
return cls(np.deg2rad(lon), np.deg2rad(lat))
41+
42+
def __str__(self):
43+
"""Get simplified representation of lon/lat arrays in radians."""
44+
return str((float(self.lon), float(self.lat)))
45+
46+
def __repr__(self):
47+
"""Get simplified representation of lon/lat arrays in radians."""
48+
return str((float(self.lon), float(self.lat)))
49+
50+
def to_shapely(self):
51+
"""Convert the SPoint to a shapely Point (in lon/lat degrees)."""
52+
from shapely.geometry import Point
53+
point = Point(*self.vertices_in_degrees[0])
54+
return point
55+
56+
57+
class SMultiPoint(SCoordinate):
58+
"""Object representing multiple points on a sphere."""
59+
60+
def __init__(self, lon, lat):
61+
lon = np.asarray(lon)
62+
lat = np.asarray(lat)
63+
if lon.ndim == 0 or lat.ndim == 0:
64+
raise ValueError("Use SPoint to define single points.")
65+
super().__init__(lon, lat)
66+
67+
@classmethod
68+
def from_degrees(cls, lon, lat):
69+
"""Create SMultiPoint from lon/lat coordinates in degrees."""
70+
return cls(np.deg2rad(lon), np.deg2rad(lat))
71+
72+
def __eq__(self, other):
73+
"""Check equality."""
74+
return np.allclose(self.lon, other.lon) and np.allclose(self.lat, other.lat)
75+
76+
def __str__(self):
77+
"""Get simplified representation of lon/lat arrays in radians."""
78+
return str(self.vertices)
79+
80+
def __repr__(self):
81+
"""Get simplified representation of lon/lat arrays in radians."""
82+
return str(self.vertices)
83+
84+
def to_shapely(self):
85+
"""Convert the SMultiPoint to a shapely MultiPoint (in lon/lat degrees)."""
86+
from shapely.geometry import MultiPoint
87+
point = MultiPoint(self.vertices_in_degrees)
88+
return point

0 commit comments

Comments
 (0)