Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
implement LatLngRect.area()
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkreiss committed May 25, 2016
1 parent c56e7f1 commit 8f53f57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions s2sphere/sphere.py
Expand Up @@ -617,6 +617,16 @@ def get_vertex(self, k):
return LatLng.from_radians(self.lat().bound(k >> 1),
self.lng().bound((k >> 1) ^ (k & 1)))

def area(self):
"""Area in steradians."""
if self.is_empty():
return 0.0

return self.lng().get_length() * math.fabs(
math.sin(self.lat_hi().radians) -
math.sin(self.lat_lo().radians)
)

def is_empty(self):
return self.lat().is_empty()

Expand Down
6 changes: 6 additions & 0 deletions tests/sphere_test.py
Expand Up @@ -1607,6 +1607,12 @@ def testGetVertex(self):
r.get_vertex((k + 1) & 3).to_point())
)

def testArea(self):
self.assertEqual(s2sphere.LatLngRect.empty().area(), 0.0)
self.assertEqual(s2sphere.LatLngRect.full().area(), 4 * math.pi)
self.assertEqual(self.rect_from_degrees(0, 0, 90, 90).area(),
math.pi / 2)

def testContains(self):
eq_m180 = LatLng.from_radians(0, -math.pi)
north_pole = LatLng.from_radians(math.pi / 2.0, 0)
Expand Down

0 comments on commit 8f53f57

Please sign in to comment.