Skip to content

Commit

Permalink
Refactory: _lat2str, _lon2str
Browse files Browse the repository at this point in the history
  • Loading branch information
g-battaglia committed May 27, 2024
1 parent 114d6fa commit 4e271a0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
51 changes: 50 additions & 1 deletion kerykeion/charts/charts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,53 @@ def draw_zodiac_slice(
dropin = 18 + c1
sign = f'<g transform="translate(-16,-16)"><use x="{str(dropin + sliceToX(num, r - dropin, offset))}" y="{str(dropin + sliceToY(num, r - dropin, offset))}" xlink:href="#{type}" /></g>'

return slice + "" + sign
return slice + "" + sign

def convert_latitude_coordinate_to_string(coord: Union[int, float], north_label: str, south_label: str) -> str:
"""
Converts a floating point latitude to string with
degree, minutes and seconds and the appropriate sign
(north or south). Eg. 52.1234567 -> 52°7'25" N
Args:
- coord (float | int): latitude in floating or integer format
- north_label (str): String label for north
- south_label (str): String label for south
Returns:
- str: latitude in string format with degree, minutes,
seconds and sign (N/S)
"""

sign = north_label
if coord < 0.0:
sign = south_label
coord = abs(coord)
deg = int(coord)
min = int((float(coord) - deg) * 60)
sec = int(round(float(((float(coord) - deg) * 60) - min) * 60.0))
return f"{deg}°{min}'{sec}\" {sign}"


def convert_longitude_coordinate_to_string(coord: Union[int, float], east_label: str, west_label: str) -> str:
"""
Converts a floating point longitude to string with
degree, minutes and seconds and the appropriate sign
(east or west). Eg. 52.1234567 -> 52°7'25" E
Args:
- coord (float|int): longitude in floating point format
- east_label (str): String label for east
- west_label (str): String label for west
Returns:
str: longitude in string format with degree, minutes,
seconds and sign (E/W)
"""

sign = east_label
if coord < 0.0:
sign = west_label
coord = abs(coord)
deg = int(coord)
min = int((float(coord) - deg) * 60)
sec = int(round(float(((float(coord) - deg) * 60) - min) * 60.0))
return f"{deg}°{min}'{sec}\" {sign}"
68 changes: 23 additions & 45 deletions kerykeion/charts/kerykeion_chart_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
from kerykeion.astrological_subject import AstrologicalSubject
from kerykeion.kr_types import KerykeionException, ChartType
from kerykeion.kr_types import ChartTemplateDictionary
from kerykeion.charts.charts_utils import decHourJoin, degreeDiff, offsetToTz, sliceToX, sliceToY, draw_zodiac_slice
from kerykeion.charts.charts_utils import (
decHourJoin,
degreeDiff,
offsetToTz,
sliceToX,
sliceToY,
draw_zodiac_slice,
convert_latitude_coordinate_to_string,
convert_longitude_coordinate_to_string
)
from pathlib import Path
from string import Template
from typing import Union
Expand Down Expand Up @@ -294,48 +303,6 @@ def _degreeTransitRing(self, r):

return out

def _lat2str(self, coord):
"""Converts a floating point latitude to string with
degree, minutes and seconds and the appropriate sign
(north or south). Eg. 52.1234567 -> 52°7'25" N
Args:
coord (float): latitude in floating point format
Returns:
str: latitude in string format with degree, minutes,
seconds and sign (N/S)
"""

sign = self.language_settings["north"]
if coord < 0.0:
sign = self.language_settings["south"]
coord = abs(coord)
deg = int(coord)
min = int((float(coord) - deg) * 60)
sec = int(round(float(((float(coord) - deg) * 60) - min) * 60.0))
return f"{deg}°{min}'{sec}\" {sign}"

def _lon2str(self, coord):
"""Converts a floating point longitude to string with
degree, minutes and seconds and the appropriate sign
(east or west). Eg. 52.1234567 -> 52°7'25" E
Args:
coord (float): longitude in floating point format
Returns:
str: longitude in string format with degree, minutes,
seconds and sign (E/W)
"""

sign = self.language_settings["east"]
if coord < 0.0:
sign = self.language_settings["west"]
coord = abs(coord)
deg = int(coord)
min = int((float(coord) - deg) * 60)
sec = int(round(float(((float(coord) - deg) * 60) - min) * 60.0))
return f"{deg}°{min}'{sec}\" {sign}"

def _dec2deg(self, dec, type="3"):
"""Coverts decimal float to degrees in format
a°b'c".
Expand Down Expand Up @@ -1417,8 +1384,19 @@ def _createTemplateDictionary(self) -> ChartTemplateDictionary:
td["stringPosition"] = f"{self.t_user.year}-{self.t_user.month}-{self.t_user.day} {self.t_user.hour:02d}:{self.t_user.minute:02d}"

else:
td["stringLat"] = f"{self.language_settings['latitude']}: {self._lat2str(self.geolat)}"
td["stringLon"] = f"{self.language_settings['longitude']}: {self._lon2str(self.geolon)}"
latitude_string = convert_latitude_coordinate_to_string(
self.geolat,
self.language_settings['north'],
self.language_settings['south']
)
longitude_string = convert_longitude_coordinate_to_string(
self.geolon,
self.language_settings['east'],
self.language_settings['west']
)

td["stringLat"] = f"{self.language_settings['latitude']}: {latitude_string}"
td["stringLon"] = f"{self.language_settings['longitude']}: {longitude_string}"
td["stringPosition"] = f"{self.language_settings['type']}: {self.charttype}"

# paper_color_X
Expand Down

0 comments on commit 4e271a0

Please sign in to comment.