From bd6fa575932cb5fac285394469ad11cf6430c599 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Sat, 6 Jul 2024 20:21:40 +1000 Subject: [PATCH] DFReader: add a get_latlon method to DFMessage --- DFReader.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/DFReader.py b/DFReader.py index 4f8f50711..0a0487d11 100644 --- a/DFReader.py +++ b/DFReader.py @@ -172,10 +172,11 @@ def set_mult_ids(self, mult_ids, mult_lookup): if mult_ids[i] in mult_lookup: unitmult = mult_lookup[mult_ids[i]] # Combine the multipler and unit to derive the real unit - if unitmult in MULT_TO_PREFIX: - self.units[i] = MULT_TO_PREFIX[unitmult]+self.units[i] - else: - self.units[i] = "%.4g %s" % (unitmult, self.units[i]) + self.msg_mults[i] = unitmult +# if unitmult in MULT_TO_PREFIX: +# self.units[i] = MULT_TO_PREFIX[unitmult]+self.units[i] +# else: +# self.units[i] = "%.4g %s" % (unitmult, self.units[i]) def get_unit(self, col): '''Return the unit for the specified field''' @@ -255,7 +256,7 @@ def __getattr__(self, field): if self.fmt.msg_mults[i] > 0.0 and self.fmt.msg_mults[i] < 1.0: divisor = 1/self.fmt.msg_mults[i] v /= divisor - else: + elif self.fmt.msg_mults[i] > 1: v *= self.fmt.msg_mults[i] return v @@ -293,6 +294,46 @@ def __str__(self): ret = ret[:-2] return ret + '}' + def get_multiplied_field_value(self, field): + v = getattr(self, field) + if getattr(self, "_apply_multiplier", False) is True: + return v + # not applied already... + i = self.fmt.colhash[field] + mult = self.fmt.msg_mults[i] + if not mult: + return v + # For reasons relating to floating point accuracy, you get a more + # accurate result by dividing by 1e2 or 1e7 than multiplying by + # 1e-2 or 1e-7 + if mult > 0.0 and mult < 1.0: + divisor = 1/mult + return v / divisor + + return v * mult + + def get_latitude(self): + for i in 'Lat', 'lat': + try: + return self.get_multiplied_field_value(i) + except AttributeError: + continue + raise AttributeError("No latitude found") + + def get_longitude(self): + for i in 'Lon', 'Lng', 'lon', 'lng': + try: + return self.get_multiplied_field_value(i) + except AttributeError: + continue + raise AttributeError("No longitude found") + + def get_latlon(self): + '''return a mavutil.location object for the first location present in + the object''' + latlon = (self.get_latitude(), self.get_longitude()) + return latlon + def dump_verbose_bitmask(self, f, c, val, field_metadata): try: try: