Skip to content

Commit 7299be2

Browse files
committed
Added geocaching style point formatting + test
1 parent f326167 commit 7299be2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pycaching/geo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import itertools
88
import geopy
99
import geopy.distance
10+
import geopy.format
1011
from statistics import mean
1112
from collections import namedtuple
1213
from pycaching.errors import ValueError as PycachingValueError, GeocodeError, BadBlockError, Error
@@ -137,6 +138,20 @@ def to_tile(self, geocaching, zoom):
137138
def __format__(self, format_spec):
138139
return "{:{}}".format(str(self), format_spec)
139140

141+
def format_gc(self):
142+
"""Return a location of this point in a typical Geocaching format.
143+
144+
:return str: Human-readable location.
145+
"""
146+
hemisphere_lat = self.latitude >= 0 and "N" or "S"
147+
hemisphere_lon = self.longitude >= 0 and "E" or "W"
148+
149+
fmt = "%(degrees)d%(deg)s %(minutes).3f"
150+
lat = geopy.format.format_degrees(abs(self.latitude), fmt, geopy.format.UNICODE_SYMBOLS)
151+
lon = geopy.format.format_degrees(abs(self.longitude), fmt, geopy.format.UNICODE_SYMBOLS)
152+
153+
return "{} {}, {} {}".format(hemisphere_lat, lat, hemisphere_lon, lon)
154+
140155

141156
class Area:
142157
"""Geometrical area."""

test/test_geo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def test_to_tile(self):
127127
with self.subTest("increase in longitude: increase in x value"):
128128
self.assertGreater(Point(49.75, 14.).to_tile(None, 14).x, t.x)
129129

130+
def test_format_gc(self):
131+
"""Test Geocaching point formatting."""
132+
self.assertEqual(Point(49.73012, 13.40102).format_gc(), "N 49° 43.807, E 13° 24.061")
133+
self.assertEqual(Point(-49.73012, -13.40102).format_gc(), "S 49° 43.807, W 13° 24.061")
134+
130135

131136
class TestPolygon(unittest.TestCase):
132137

0 commit comments

Comments
 (0)