Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/cogo/bearing2dd.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
2.9 KiB
Python
Raw Normal View History

2025-07-08 10:09:14 -07:00
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2025 Thomas Krijnen <thomas@aecgeeks.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell.util.geolocation
2025-07-09 13:47:32 +05:00
def bearing2dd(bearing: str) -> float:
2025-07-08 10:09:14 -07:00
"""
Converts a quadrant bearing string to decimal degrees
The format of the string is "N|S dd (mm (ss.s)) E|W"
where:
N|S is N or S for North or South
dd is degree (required)
mm is minute (optional, but required if second is provided)
ss.s is second (required)
E|W is E or W for East or West
:param str: the bearing string
:return: Angle in radian
"""
error_msg = "Invalid bearing string"
2025-07-09 13:47:32 +05:00
bearing = bearing.strip() # trim external white space
bearing = " ".join(bearing.split()) # make sure all parts separated by a single space
2025-07-08 10:09:14 -07:00
parts = bearing.split()
nParts = len(parts)
if nParts < 3 or 5 < nParts:
raise ValueError(error_msg)
2025-07-09 13:47:32 +05:00
2025-07-08 10:09:14 -07:00
cY = parts[0]
cY = cY.upper()
2025-07-09 13:47:32 +05:00
if cY != "N" and cY != "S":
2025-07-08 10:09:14 -07:00
raise ValueError(error_msg)
cX = parts[-1]
cX = cX.upper()
2025-07-09 13:47:32 +05:00
if cX != "E" and cX != "W":
2025-07-08 10:09:14 -07:00
raise ValueError(error_msg)
2025-07-09 13:47:32 +05:00
2025-07-08 10:09:14 -07:00
d = 0
m = 0
2025-07-09 13:47:32 +05:00
s = 0.0
2025-07-08 10:09:14 -07:00
ms = 0
if nParts == 3:
d = int(parts[1])
elif nParts == 4:
d = int(parts[1])
m = int(parts[2])
elif nParts == 5:
d = int(parts[1])
m = int(parts[2])
s = float(parts[3])
# s in a decimal number
# need to break it into whole seconds and milliseconds
2025-07-09 13:47:32 +05:00
ms = 100.0 * (s - int(s))
2025-07-08 10:09:14 -07:00
s = int(s)
if d < 0 or (m < 0 or 60 <= m) or (s < 0 or 60 <= s) or ms < 0:
raise ValueError(error_msg)
2025-07-09 13:47:32 +05:00
if cY == "N" and cX == "E":
angle = 90.0
sign = -1.0
elif cY == "N" and cX == "W":
angle = 90.0
sign = 1.0
elif cY == "S" and cX == "E":
angle = 270.0
sign = 1.0
elif cY == "S" and cX == "W":
angle = 270.0
sign = -1.0
2025-07-08 10:09:14 -07:00
try:
2025-07-09 13:47:32 +05:00
dms = ifcopenshell.util.geolocation.dms2dd(d, m, s, ms)
2025-07-08 10:09:14 -07:00
except ValueError:
raise ValueError(error_msg)
2025-07-09 13:47:32 +05:00
if dms < 0.0 or 90.0 < dms:
2025-07-08 10:09:14 -07:00
raise ValueError(error_msg)
2025-07-09 13:47:32 +05:00
angle += sign * dms
2025-07-08 10:09:14 -07:00
# S 90 E will evaluate to 360
2025-07-09 13:47:32 +05:00
if angle == 360.0:
angle = 0.0
2025-07-08 10:09:14 -07:00
return angle