Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15851

Error converting DMS coordinates to decimal format

$
0
0
class GeoCoordinates:    def __init__(self, lat: Optional[float] = None, long: Optional[float] = None, dms: Optional[str] = None):        if (lat is not None and long is not None) and dms:            self.lat = lat            self.long = long            self.dms = self.decimal_to_dms(lat, long)        elif dms:            self.dms = dms            self.lat, self.long = self.dms_to_decimal(dms)        else:            raise ValueError("Either latitude and longitude or DMS format should be provided.")    def __str__(self) -> str:        return f"DMS: {self.dms}, Latitude: {self.lat}, Longitude: {self.long}"    def distance(self, other: GeoCoordinates) -> float:        earth_radius = 6373.0        lat1 = radians(self.lat)        lon1 = radians(self.long)        lat2 = radians(other.lat)        lon2 = radians(other.long)        dlon = lon2 - lon1        dlat = lat2 - lat1        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2        c = 2 * atan2(sqrt(a), sqrt(1 - a))        distance = earth_radius * c        return distance

I'm working on implementing a GeoCoordinates class in Python that stores coordinates in both Degree/Minute/Second (DMS) format and plain latitude/longitude format. However, I'm encountering a ValueError when trying to initialize the class with DMS coordinates. Here's the relevant code snippet:

gc_la = GeoCoordinates(dms='42°09′N 120°12′W')

and the error message:

ValueError: could not convert string to float: '42°09'

The GeoCoordinates class is designed to accept either DMS coordinates or latitude/longitude in decimal format. I've provided functions to convert between these formats, but it seems there's an issue with the conversion from DMS to decimal.


Viewing all articles
Browse latest Browse all 15851

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>